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