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@18
|
75 $this->installEntitySchema('node');
|
Chris@0
|
76 $this->installConfig(['language']);
|
Chris@0
|
77
|
Chris@17
|
78 $this->fieldName = mb_strtolower($this->randomMachineName());
|
Chris@0
|
79
|
Chris@0
|
80 $this->entityType = 'entity_test';
|
Chris@0
|
81
|
Chris@0
|
82 $this->fieldStorageDefinition = [
|
Chris@0
|
83 'field_name' => $this->fieldName,
|
Chris@0
|
84 'entity_type' => $this->entityType,
|
Chris@0
|
85 'type' => 'test_field',
|
Chris@0
|
86 'cardinality' => 4,
|
Chris@0
|
87 ];
|
Chris@0
|
88 $this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition);
|
Chris@0
|
89 $this->fieldStorage->save();
|
Chris@0
|
90
|
Chris@0
|
91 $this->fieldDefinition = [
|
Chris@0
|
92 'field_storage' => $this->fieldStorage,
|
Chris@0
|
93 'bundle' => 'entity_test',
|
Chris@0
|
94 ];
|
Chris@0
|
95 $this->field = FieldConfig::create($this->fieldDefinition);
|
Chris@0
|
96 $this->field->save();
|
Chris@0
|
97
|
Chris@0
|
98 for ($i = 0; $i < 3; ++$i) {
|
Chris@0
|
99 ConfigurableLanguage::create([
|
Chris@0
|
100 'id' => 'l' . $i,
|
Chris@0
|
101 'label' => $this->randomString(),
|
Chris@0
|
102 ])->save();
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Test translatable fields storage/retrieval.
|
Chris@0
|
108 */
|
Chris@0
|
109 public function testTranslatableFieldSaveLoad() {
|
Chris@0
|
110 // Enable field translations for nodes.
|
Chris@0
|
111 field_test_entity_info_translatable('node', TRUE);
|
Chris@0
|
112 $entity_type = \Drupal::entityManager()->getDefinition('node');
|
Chris@0
|
113 $this->assertTrue($entity_type->isTranslatable(), 'Nodes are translatable.');
|
Chris@0
|
114
|
Chris@0
|
115 // Prepare the field translations.
|
Chris@0
|
116 $entity_type_id = 'entity_test';
|
Chris@0
|
117 field_test_entity_info_translatable($entity_type_id, TRUE);
|
Chris@0
|
118 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
119 ->getStorage($entity_type_id)
|
Chris@0
|
120 ->create(['type' => $this->field->getTargetBundle()]);
|
Chris@0
|
121 $field_translations = [];
|
Chris@0
|
122 $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
|
Chris@0
|
123 $entity->langcode->value = reset($available_langcodes);
|
Chris@0
|
124 foreach ($available_langcodes as $langcode) {
|
Chris@0
|
125 $field_translations[$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
|
Chris@0
|
126 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
127 $translation->{$this->fieldName}->setValue($field_translations[$langcode]);
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 // Save and reload the field translations.
|
Chris@0
|
131 $entity = $this->entitySaveReload($entity);
|
Chris@0
|
132
|
Chris@0
|
133 // Check if the correct values were saved/loaded.
|
Chris@0
|
134 foreach ($field_translations as $langcode => $items) {
|
Chris@0
|
135 $result = TRUE;
|
Chris@0
|
136 foreach ($items as $delta => $item) {
|
Chris@0
|
137 $result = $result && $item['value'] == $entity->getTranslation($langcode)->{$this->fieldName}[$delta]->value;
|
Chris@0
|
138 }
|
Chris@0
|
139 $this->assertTrue($result, format_string('%language translation correctly handled.', ['%language' => $langcode]));
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 // Test default values.
|
Chris@17
|
143 $field_name_default = mb_strtolower($this->randomMachineName() . '_field_name');
|
Chris@0
|
144 $field_storage_definition = $this->fieldStorageDefinition;
|
Chris@0
|
145 $field_storage_definition['field_name'] = $field_name_default;
|
Chris@0
|
146 $field_storage = FieldStorageConfig::create($field_storage_definition);
|
Chris@0
|
147 $field_storage->save();
|
Chris@0
|
148
|
Chris@0
|
149 $field_definition = $this->fieldDefinition;
|
Chris@0
|
150 $field_definition['field_storage'] = $field_storage;
|
Chris@0
|
151 $field_definition['default_value'] = [['value' => rand(1, 127)]];
|
Chris@0
|
152 $field = FieldConfig::create($field_definition);
|
Chris@0
|
153 $field->save();
|
Chris@0
|
154
|
Chris@0
|
155 $translation_langcodes = array_slice($available_langcodes, 0, 2);
|
Chris@0
|
156 asort($translation_langcodes);
|
Chris@0
|
157 $translation_langcodes = array_values($translation_langcodes);
|
Chris@0
|
158
|
Chris@0
|
159 $values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]];
|
Chris@0
|
160 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
161 ->getStorage($entity_type_id)
|
Chris@0
|
162 ->create($values);
|
Chris@0
|
163 foreach ($translation_langcodes as $langcode) {
|
Chris@0
|
164 $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
|
Chris@0
|
165 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
166 $translation->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 $field_langcodes = array_keys($entity->getTranslationLanguages());
|
Chris@0
|
170 sort($field_langcodes);
|
Chris@0
|
171 $this->assertEqual($translation_langcodes, $field_langcodes, 'Missing translations did not get a default value.');
|
Chris@0
|
172
|
Chris@0
|
173 // @todo Test every translation once the Entity Translation API allows for
|
Chris@0
|
174 // multilingual defaults.
|
Chris@0
|
175 $langcode = $entity->language()->getId();
|
Chris@0
|
176 $this->assertEqual($entity->getTranslation($langcode)->{$field_name_default}->getValue(), $field->getDefaultValueLiteral(), format_string('Default value correctly populated for language %language.', ['%language' => $langcode]));
|
Chris@0
|
177
|
Chris@0
|
178 // Check that explicit empty values are not overridden with default values.
|
Chris@0
|
179 foreach ([NULL, []] as $empty_items) {
|
Chris@0
|
180 $values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]];
|
Chris@0
|
181 $entity = entity_create($entity_type_id, $values);
|
Chris@0
|
182 foreach ($translation_langcodes as $langcode) {
|
Chris@0
|
183 $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
|
Chris@0
|
184 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
185 $translation->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
|
Chris@0
|
186 $translation->{$field_name_default}->setValue($empty_items);
|
Chris@0
|
187 $values[$field_name_default][$langcode] = $empty_items;
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 foreach ($entity->getTranslationLanguages() as $langcode => $language) {
|
Chris@0
|
191 $this->assertEquals([], $entity->getTranslation($langcode)->{$field_name_default}->getValue(), format_string('Empty value correctly populated for language %language.', ['%language' => $langcode]));
|
Chris@0
|
192 }
|
Chris@0
|
193 }
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Tests field access.
|
Chris@0
|
198 *
|
Chris@0
|
199 * Regression test to verify that fieldAccess() can be called while only
|
Chris@0
|
200 * passing the required parameters.
|
Chris@0
|
201 *
|
Chris@0
|
202 * @see https://www.drupal.org/node/2404739
|
Chris@0
|
203 */
|
Chris@0
|
204 public function testFieldAccess() {
|
Chris@0
|
205 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler($this->entityType);
|
Chris@0
|
206 $this->assertTrue($access_control_handler->fieldAccess('view', $this->field));
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 }
|