Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\field\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
6 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
7 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests multilanguage fields logic.
|
Chris@0
|
11 *
|
Chris@0
|
12 * The following tests will check the multilanguage logic in field handling.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group field
|
Chris@0
|
15 */
|
Chris@0
|
16 class TranslationTest extends FieldKernelTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@17
|
21 * The node module is required because the tests alter the node entity type.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var array
|
Chris@0
|
24 */
|
Chris@0
|
25 public static $modules = ['language', 'node'];
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The name of the field to use in this test.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var string
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $fieldName;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The name of the entity type to use in this test.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $entityType = 'test_entity';
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * An array defining the field storage to use in this test.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var array
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $fieldStorageDefinition;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * An array defining the field to use in this test.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var array
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $fieldDefinition;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * The field storage to use in this test.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @var \Drupal\field\Entity\FieldStorageConfig
|
Chris@0
|
59 */
|
Chris@0
|
60 protected $fieldStorage;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * The field to use in this test.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @var \Drupal\field\Entity\FieldConfig
|
Chris@0
|
66 */
|
Chris@0
|
67 protected $field;
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 protected function setUp() {
|
Chris@0
|
73 parent::setUp();
|
Chris@0
|
74
|
Chris@0
|
75 $this->installConfig(['language']);
|
Chris@0
|
76
|
Chris@17
|
77 $this->fieldName = mb_strtolower($this->randomMachineName());
|
Chris@0
|
78
|
Chris@0
|
79 $this->entityType = 'entity_test';
|
Chris@0
|
80
|
Chris@0
|
81 $this->fieldStorageDefinition = [
|
Chris@0
|
82 'field_name' => $this->fieldName,
|
Chris@0
|
83 'entity_type' => $this->entityType,
|
Chris@0
|
84 'type' => 'test_field',
|
Chris@0
|
85 'cardinality' => 4,
|
Chris@0
|
86 ];
|
Chris@0
|
87 $this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition);
|
Chris@0
|
88 $this->fieldStorage->save();
|
Chris@0
|
89
|
Chris@0
|
90 $this->fieldDefinition = [
|
Chris@0
|
91 'field_storage' => $this->fieldStorage,
|
Chris@0
|
92 'bundle' => 'entity_test',
|
Chris@0
|
93 ];
|
Chris@0
|
94 $this->field = FieldConfig::create($this->fieldDefinition);
|
Chris@0
|
95 $this->field->save();
|
Chris@0
|
96
|
Chris@0
|
97 for ($i = 0; $i < 3; ++$i) {
|
Chris@0
|
98 ConfigurableLanguage::create([
|
Chris@0
|
99 'id' => 'l' . $i,
|
Chris@0
|
100 'label' => $this->randomString(),
|
Chris@0
|
101 ])->save();
|
Chris@0
|
102 }
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Test translatable fields storage/retrieval.
|
Chris@0
|
107 */
|
Chris@0
|
108 public function testTranslatableFieldSaveLoad() {
|
Chris@0
|
109 // Enable field translations for nodes.
|
Chris@0
|
110 field_test_entity_info_translatable('node', TRUE);
|
Chris@0
|
111 $entity_type = \Drupal::entityManager()->getDefinition('node');
|
Chris@0
|
112 $this->assertTrue($entity_type->isTranslatable(), 'Nodes are translatable.');
|
Chris@0
|
113
|
Chris@0
|
114 // Prepare the field translations.
|
Chris@0
|
115 $entity_type_id = 'entity_test';
|
Chris@0
|
116 field_test_entity_info_translatable($entity_type_id, TRUE);
|
Chris@0
|
117 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
118 ->getStorage($entity_type_id)
|
Chris@0
|
119 ->create(['type' => $this->field->getTargetBundle()]);
|
Chris@0
|
120 $field_translations = [];
|
Chris@0
|
121 $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
|
Chris@0
|
122 $entity->langcode->value = reset($available_langcodes);
|
Chris@0
|
123 foreach ($available_langcodes as $langcode) {
|
Chris@0
|
124 $field_translations[$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
|
Chris@0
|
125 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
126 $translation->{$this->fieldName}->setValue($field_translations[$langcode]);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 // Save and reload the field translations.
|
Chris@0
|
130 $entity = $this->entitySaveReload($entity);
|
Chris@0
|
131
|
Chris@0
|
132 // Check if the correct values were saved/loaded.
|
Chris@0
|
133 foreach ($field_translations as $langcode => $items) {
|
Chris@0
|
134 $result = TRUE;
|
Chris@0
|
135 foreach ($items as $delta => $item) {
|
Chris@0
|
136 $result = $result && $item['value'] == $entity->getTranslation($langcode)->{$this->fieldName}[$delta]->value;
|
Chris@0
|
137 }
|
Chris@0
|
138 $this->assertTrue($result, format_string('%language translation correctly handled.', ['%language' => $langcode]));
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 // Test default values.
|
Chris@17
|
142 $field_name_default = mb_strtolower($this->randomMachineName() . '_field_name');
|
Chris@0
|
143 $field_storage_definition = $this->fieldStorageDefinition;
|
Chris@0
|
144 $field_storage_definition['field_name'] = $field_name_default;
|
Chris@0
|
145 $field_storage = FieldStorageConfig::create($field_storage_definition);
|
Chris@0
|
146 $field_storage->save();
|
Chris@0
|
147
|
Chris@0
|
148 $field_definition = $this->fieldDefinition;
|
Chris@0
|
149 $field_definition['field_storage'] = $field_storage;
|
Chris@0
|
150 $field_definition['default_value'] = [['value' => rand(1, 127)]];
|
Chris@0
|
151 $field = FieldConfig::create($field_definition);
|
Chris@0
|
152 $field->save();
|
Chris@0
|
153
|
Chris@0
|
154 $translation_langcodes = array_slice($available_langcodes, 0, 2);
|
Chris@0
|
155 asort($translation_langcodes);
|
Chris@0
|
156 $translation_langcodes = array_values($translation_langcodes);
|
Chris@0
|
157
|
Chris@0
|
158 $values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]];
|
Chris@0
|
159 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
160 ->getStorage($entity_type_id)
|
Chris@0
|
161 ->create($values);
|
Chris@0
|
162 foreach ($translation_langcodes as $langcode) {
|
Chris@0
|
163 $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
|
Chris@0
|
164 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
165 $translation->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 $field_langcodes = array_keys($entity->getTranslationLanguages());
|
Chris@0
|
169 sort($field_langcodes);
|
Chris@0
|
170 $this->assertEqual($translation_langcodes, $field_langcodes, 'Missing translations did not get a default value.');
|
Chris@0
|
171
|
Chris@0
|
172 // @todo Test every translation once the Entity Translation API allows for
|
Chris@0
|
173 // multilingual defaults.
|
Chris@0
|
174 $langcode = $entity->language()->getId();
|
Chris@0
|
175 $this->assertEqual($entity->getTranslation($langcode)->{$field_name_default}->getValue(), $field->getDefaultValueLiteral(), format_string('Default value correctly populated for language %language.', ['%language' => $langcode]));
|
Chris@0
|
176
|
Chris@0
|
177 // Check that explicit empty values are not overridden with default values.
|
Chris@0
|
178 foreach ([NULL, []] as $empty_items) {
|
Chris@0
|
179 $values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]];
|
Chris@0
|
180 $entity = entity_create($entity_type_id, $values);
|
Chris@0
|
181 foreach ($translation_langcodes as $langcode) {
|
Chris@0
|
182 $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
|
Chris@0
|
183 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
184 $translation->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
|
Chris@0
|
185 $translation->{$field_name_default}->setValue($empty_items);
|
Chris@0
|
186 $values[$field_name_default][$langcode] = $empty_items;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 foreach ($entity->getTranslationLanguages() as $langcode => $language) {
|
Chris@0
|
190 $this->assertEquals([], $entity->getTranslation($langcode)->{$field_name_default}->getValue(), format_string('Empty value correctly populated for language %language.', ['%language' => $langcode]));
|
Chris@0
|
191 }
|
Chris@0
|
192 }
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Tests field access.
|
Chris@0
|
197 *
|
Chris@0
|
198 * Regression test to verify that fieldAccess() can be called while only
|
Chris@0
|
199 * passing the required parameters.
|
Chris@0
|
200 *
|
Chris@0
|
201 * @see https://www.drupal.org/node/2404739
|
Chris@0
|
202 */
|
Chris@0
|
203 public function testFieldAccess() {
|
Chris@0
|
204 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler($this->entityType);
|
Chris@0
|
205 $this->assertTrue($access_control_handler->fieldAccess('view', $this->field));
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 }
|