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