annotate core/modules/field/tests/src/Kernel/TranslationTest.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\field\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Unicode;
Chris@0 6 use Drupal\field\Entity\FieldConfig;
Chris@0 7 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 8 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Tests multilanguage fields logic.
Chris@0 12 *
Chris@0 13 * The following tests will check the multilanguage logic in field handling.
Chris@0 14 *
Chris@0 15 * @group field
Chris@0 16 */
Chris@0 17 class TranslationTest extends FieldKernelTestBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Modules to enable.
Chris@0 21 *
Chris@0 22 * node is required because the tests alter the node entity type.
Chris@0 23 *
Chris@0 24 * @var array
Chris@0 25 */
Chris@0 26 public static $modules = ['language', 'node'];
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The name of the field to use in this test.
Chris@0 30 *
Chris@0 31 * @var string
Chris@0 32 */
Chris@0 33 protected $fieldName;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The name of the entity type to use in this test.
Chris@0 37 *
Chris@0 38 * @var string
Chris@0 39 */
Chris@0 40 protected $entityType = 'test_entity';
Chris@0 41
Chris@0 42 /**
Chris@0 43 * An array defining the field storage to use in this test.
Chris@0 44 *
Chris@0 45 * @var array
Chris@0 46 */
Chris@0 47 protected $fieldStorageDefinition;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * An array defining the field to use in this test.
Chris@0 51 *
Chris@0 52 * @var array
Chris@0 53 */
Chris@0 54 protected $fieldDefinition;
Chris@0 55
Chris@0 56 /**
Chris@0 57 * The field storage to use in this test.
Chris@0 58 *
Chris@0 59 * @var \Drupal\field\Entity\FieldStorageConfig
Chris@0 60 */
Chris@0 61 protected $fieldStorage;
Chris@0 62
Chris@0 63 /**
Chris@0 64 * The field to use in this test.
Chris@0 65 *
Chris@0 66 * @var \Drupal\field\Entity\FieldConfig
Chris@0 67 */
Chris@0 68 protected $field;
Chris@0 69
Chris@0 70 /**
Chris@0 71 * {@inheritdoc}
Chris@0 72 */
Chris@0 73 protected function setUp() {
Chris@0 74 parent::setUp();
Chris@0 75
Chris@0 76 $this->installConfig(['language']);
Chris@0 77
Chris@0 78 $this->fieldName = Unicode::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@0 143 $field_name_default = Unicode::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 }