Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityLanguageTestBase.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Base class for language-aware entity tests.
11  */
12 abstract class EntityLanguageTestBase extends EntityKernelTestBase {
13
14   /**
15    * The language manager service.
16    *
17    * @var \Drupal\Core\Language\LanguageManagerInterface
18    */
19   protected $languageManager;
20
21   /**
22    * The available language codes.
23    *
24    * @var array
25    */
26   protected $langcodes;
27
28   /**
29    * The test field name.
30    *
31    * @var string
32    */
33   protected $fieldName;
34
35   /**
36    * The untranslatable test field name.
37    *
38    * @var string
39    */
40   protected $untranslatableFieldName;
41
42   public static $modules = ['language', 'entity_test'];
43
44   protected function setUp() {
45     parent::setUp();
46
47     $this->languageManager = $this->container->get('language_manager');
48
49     foreach (entity_test_entity_types() as $entity_type_id) {
50       // The entity_test schema is installed by the parent.
51       if ($entity_type_id != 'entity_test') {
52         $this->installEntitySchema($entity_type_id);
53       }
54     }
55
56     $this->installConfig(['language']);
57
58     // Create the test field.
59     module_load_install('entity_test');
60     entity_test_install();
61
62     // Enable translations for the test entity type.
63     $this->state->set('entity_test.translation', TRUE);
64
65     // Create a translatable test field.
66     $this->fieldName = mb_strtolower($this->randomMachineName() . '_field_name');
67
68     // Create an untranslatable test field.
69     $this->untranslatableFieldName = mb_strtolower($this->randomMachineName() . '_field_name');
70
71     // Create field fields in all entity variations.
72     foreach (entity_test_entity_types() as $entity_type) {
73       FieldStorageConfig::create([
74         'field_name' => $this->fieldName,
75         'entity_type' => $entity_type,
76         'type' => 'text',
77         'cardinality' => 4,
78       ])->save();
79       FieldConfig::create([
80         'field_name' => $this->fieldName,
81         'entity_type' => $entity_type,
82         'bundle' => $entity_type,
83         'translatable' => TRUE,
84       ])->save();
85
86       FieldStorageConfig::create([
87         'field_name' => $this->untranslatableFieldName,
88         'entity_type' => $entity_type,
89         'type' => 'text',
90         'cardinality' => 4,
91       ])->save();
92       FieldConfig::create([
93         'field_name' => $this->untranslatableFieldName,
94         'entity_type' => $entity_type,
95         'bundle' => $entity_type,
96         'translatable' => FALSE,
97       ])->save();
98     }
99
100     // Create the default languages.
101     $this->installConfig(['language']);
102
103     // Create test languages.
104     $this->langcodes = [];
105     for ($i = 0; $i < 3; ++$i) {
106       $language = ConfigurableLanguage::create([
107         'id' => 'l' . $i,
108         'label' => $this->randomString(),
109         'weight' => $i,
110       ]);
111       $this->langcodes[$i] = $language->getId();
112       $language->save();
113     }
114   }
115
116   /**
117    * Toggles field storage translatability.
118    *
119    * @param string $entity_type
120    *   The type of the entity fields are attached to.
121    */
122   protected function toggleFieldTranslatability($entity_type, $bundle) {
123     $fields = [$this->fieldName, $this->untranslatableFieldName];
124     foreach ($fields as $field_name) {
125       $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
126       $translatable = !$field->isTranslatable();
127       $field->set('translatable', $translatable);
128       $field->save();
129       $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
130       $this->assertEqual($field->isTranslatable(), $translatable, 'Field translatability changed.');
131     }
132     \Drupal::cache('entity')->deleteAll();
133   }
134
135 }