Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\field\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
6 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
8 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Tests multilanguage fields logic that require a full environment.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group field
|
Chris@0
|
14 */
|
Chris@0
|
15 class TranslationWebTest extends FieldTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Modules to enable.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['language', 'field_test', 'entity_test'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The name of the field to use in this test.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var string
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $fieldName;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * The name of the entity type to use in this test.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var string
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $entityTypeId = 'entity_test_mulrev';
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * The field storage to use in this test.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var \Drupal\field\Entity\FieldStorageConfig
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $fieldStorage;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * The field to use in this test.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var \Drupal\field\Entity\FieldConfig
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $field;
|
Chris@0
|
51
|
Chris@0
|
52 protected function setUp() {
|
Chris@0
|
53 parent::setUp();
|
Chris@0
|
54
|
Chris@0
|
55 $this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
|
Chris@0
|
56
|
Chris@0
|
57 $field_storage = [
|
Chris@0
|
58 'field_name' => $this->fieldName,
|
Chris@0
|
59 'entity_type' => $this->entityTypeId,
|
Chris@0
|
60 'type' => 'test_field',
|
Chris@0
|
61 'cardinality' => 4,
|
Chris@0
|
62 ];
|
Chris@0
|
63 FieldStorageConfig::create($field_storage)->save();
|
Chris@0
|
64 $this->fieldStorage = FieldStorageConfig::load($this->entityTypeId . '.' . $this->fieldName);
|
Chris@0
|
65
|
Chris@0
|
66 $field = [
|
Chris@0
|
67 'field_storage' => $this->fieldStorage,
|
Chris@0
|
68 'bundle' => $this->entityTypeId,
|
Chris@0
|
69 ];
|
Chris@0
|
70 FieldConfig::create($field)->save();
|
Chris@0
|
71 $this->field = FieldConfig::load($this->entityTypeId . '.' . $field['bundle'] . '.' . $this->fieldName);
|
Chris@0
|
72
|
Chris@0
|
73 entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
|
Chris@0
|
74 ->setComponent($this->fieldName)
|
Chris@0
|
75 ->save();
|
Chris@0
|
76
|
Chris@0
|
77 for ($i = 0; $i < 3; ++$i) {
|
Chris@0
|
78 ConfigurableLanguage::create([
|
Chris@0
|
79 'id' => 'l' . $i,
|
Chris@0
|
80 'label' => $this->randomString(),
|
Chris@0
|
81 ])->save();
|
Chris@0
|
82 }
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Tests field translations when creating a new revision.
|
Chris@0
|
87 */
|
Chris@0
|
88 public function testFieldFormTranslationRevisions() {
|
Chris@0
|
89 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
|
Chris@0
|
90 $this->drupalLogin($web_user);
|
Chris@0
|
91
|
Chris@0
|
92 // Prepare the field translations.
|
Chris@0
|
93 field_test_entity_info_translatable($this->entityTypeId, TRUE);
|
Chris@0
|
94 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
95 ->getStorage($this->entityTypeId)
|
Chris@0
|
96 ->create();
|
Chris@0
|
97 $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages()));
|
Chris@0
|
98 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
99
|
Chris@0
|
100 // Store the field translations.
|
Chris@0
|
101 ksort($available_langcodes);
|
Chris@0
|
102 $entity->langcode->value = key($available_langcodes);
|
Chris@0
|
103 foreach ($available_langcodes as $langcode => $value) {
|
Chris@0
|
104 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
Chris@0
|
105 $translation->{$field_name}->value = $value + 1;
|
Chris@0
|
106 }
|
Chris@0
|
107 $entity->save();
|
Chris@0
|
108
|
Chris@0
|
109 // Create a new revision.
|
Chris@0
|
110 $edit = [
|
Chris@0
|
111 "{$field_name}[0][value]" => $entity->{$field_name}->value,
|
Chris@0
|
112 'revision' => TRUE,
|
Chris@0
|
113 ];
|
Chris@0
|
114 $this->drupalPostForm($this->entityTypeId . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
115
|
Chris@0
|
116 // Check translation revisions.
|
Chris@0
|
117 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
|
Chris@0
|
118 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Check if the field translation attached to the entity revision identified
|
Chris@0
|
123 * by the passed arguments were correctly stored.
|
Chris@0
|
124 */
|
Chris@0
|
125 private function checkTranslationRevisions($id, $revision_id, $available_langcodes) {
|
Chris@0
|
126 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
127 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
128 ->getStorage($this->entityTypeId)
|
Chris@0
|
129 ->loadRevision($revision_id);
|
Chris@0
|
130 foreach ($available_langcodes as $langcode => $value) {
|
Chris@0
|
131 $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1;
|
Chris@0
|
132 $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', ['@language' => $langcode, '@revision' => $entity->getRevisionId()]));
|
Chris@0
|
133 }
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 }
|