Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TaxonomyTranslationTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
9
10 /**
11  * Provides common testing base for translated taxonomy terms.
12  */
13 trait TaxonomyTranslationTestTrait {
14
15   use EntityReferenceTestTrait;
16
17   /**
18    * The vocabulary.
19    *
20    * @var \Drupal\taxonomy\Entity\Vocabulary;
21    */
22   protected $vocabulary;
23
24   /**
25    * The field name for our taxonomy term field.
26    *
27    * @var string
28    */
29   protected $termFieldName = 'field_tag';
30
31   /**
32    * The langcode of the source language.
33    *
34    * @var string
35    */
36   protected $baseLangcode = 'en';
37
38   /**
39    * Target langcode for translation.
40    *
41    * @var string
42    */
43   protected $translateToLangcode = 'hu';
44
45   /**
46    * The node to check the translated value on.
47    *
48    * @var \Drupal\node\Entity\Node
49    */
50   protected $node;
51
52   /**
53    * Adds additional languages.
54    */
55   protected function setupLanguages() {
56     ConfigurableLanguage::createFromLangcode($this->translateToLangcode)->save();
57     $this->rebuildContainer();
58   }
59
60   /**
61    * Enables translations where it needed.
62    */
63   protected function enableTranslation() {
64     // Enable translation for the current entity type and ensure the change is
65     // picked up.
66     \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
67     \Drupal::service('content_translation.manager')->setEnabled('taxonomy_term', $this->vocabulary->id(), TRUE);
68     drupal_static_reset();
69     \Drupal::entityManager()->clearCachedDefinitions();
70     \Drupal::service('router.builder')->rebuild();
71     \Drupal::service('entity.definition_update_manager')->applyUpdates();
72   }
73
74   /**
75    * Adds term reference field for the article content type.
76    *
77    * @param bool $translatable
78    *   (optional) If TRUE, create a translatable term reference field. Defaults
79    *   to FALSE.
80    */
81   protected function setUpTermReferenceField() {
82     $handler_settings = [
83       'target_bundles' => [
84         $this->vocabulary->id() => $this->vocabulary->id(),
85       ],
86       'auto_create' => TRUE,
87     ];
88     $this->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
89     $field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName);
90     $field_storage->setTranslatable(FALSE);
91     $field_storage->save();
92
93     entity_get_form_display('node', 'article', 'default')
94       ->setComponent($this->termFieldName, [
95         'type' => 'entity_reference_autocomplete_tags',
96       ])
97       ->save();
98     entity_get_display('node', 'article', 'default')
99       ->setComponent($this->termFieldName, [
100         'type' => 'entity_reference_label',
101       ])
102       ->save();
103   }
104
105 }