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