Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Entity / FieldEntityTranslationTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Entity;
4
5 use Drupal\Core\Language\Language;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Tests\views\Functional\ViewTestBase;
10 use Symfony\Component\CssSelector\CssSelectorConverter;
11
12 /**
13  * Tests the rendering of fields (base fields) and their translations.
14  *
15  * @group views
16  */
17 class FieldEntityTranslationTest extends ViewTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['language', 'locale', 'content_translation', 'node'];
23
24   /**
25    * {@inheritdoc}
26    */
27   public static $testViews = ['test_entity_field_renderers'];
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp($import_test_views = TRUE) {
33     parent::setUp($import_test_views);
34
35     $node_type = NodeType::create([
36       'type' => 'article',
37       'label' => 'Article',
38     ]);
39     $node_type->save();
40
41     /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
42     $content_translation_manager = \Drupal::service('content_translation.manager');
43
44     $content_translation_manager->setEnabled('node', 'article', TRUE);
45
46     $language = ConfigurableLanguage::create([
47       'id' => 'es',
48       'label' => 'Spanish',
49     ]);
50     $language->save();
51     // Rebuild the container to setup the language path processors.
52     $this->rebuildContainer();
53   }
54
55   /**
56    * Tests that different translation mechanisms can be used for base fields.
57    */
58   public function testTranslationRows() {
59     $node = Node::create([
60       'type' => 'article',
61       'title' => 'example EN',
62       'sticky' => FALSE,
63     ]);
64     $node->save();
65
66     $translation = $node->addTranslation('es');
67     $translation->title->value = 'example ES';
68     $translation->sticky->value = TRUE;
69     $translation->save();
70
71     $this->drupalGet('test_entity_field_renderers/entity_translation');
72     $this->assertRows([
73       [
74         'title' => 'example EN',
75         'sticky' => 'Off',
76       ],
77       [
78         'title' => 'example ES',
79         'sticky' => 'On',
80       ],
81     ]);
82
83     $this->drupalGet('test_entity_field_renderers/entity_default');
84     $this->assertRows([
85       [
86         'title' => 'example EN',
87         'sticky' => 'Off',
88       ],
89       [
90         'title' => 'example EN',
91         'sticky' => 'Off',
92       ],
93     ]);
94
95     $this->drupalGet('test_entity_field_renderers/site_default');
96     $this->assertRows([
97       [
98         'title' => 'example EN',
99         'sticky' => 'Off',
100       ],
101       [
102         'title' => 'example EN',
103         'sticky' => 'Off',
104       ],
105     ]);
106
107     $this->drupalGet('test_entity_field_renderers/language_interface');
108     $this->assertRows([
109       [
110         'title' => 'example EN',
111         'sticky' => 'Off',
112       ],
113       [
114         'title' => 'example EN',
115         'sticky' => 'Off',
116       ],
117     ]);
118
119     $this->drupalGet('test_entity_field_renderers/language_interface', ['language' => new Language(['id' => 'es'])]);
120     $this->assertRows([
121       [
122         'title' => 'example ES',
123         'sticky' => 'On',
124       ],
125       [
126         'title' => 'example ES',
127         'sticky' => 'On',
128       ],
129     ]);
130
131     $this->drupalGet('test_entity_field_renderers/en');
132     $this->assertRows([
133       [
134         'title' => 'example EN',
135         'sticky' => 'Off',
136       ],
137       [
138         'title' => 'example EN',
139         'sticky' => 'Off',
140       ],
141     ]);
142
143     $this->drupalGet('test_entity_field_renderers/es');
144     $this->assertRows([
145       [
146         'title' => 'example ES',
147         'sticky' => 'On',
148       ],
149       [
150         'title' => 'example ES',
151         'sticky' => 'On',
152       ],
153     ]);
154   }
155
156   /**
157    * Ensures that the rendered results are working as expected.
158    *
159    * @param array $expected
160    *   The expected rows of the result.
161    */
162   protected function assertRows($expected = []) {
163     $actual = [];
164     $rows = $this->cssSelect('div.views-row');
165     foreach ($rows as $row) {
166       $actual[] = [
167         'title' => $row->find('xpath', (new CssSelectorConverter())->toXPath('.views-field-title span.field-content a'))->getText(),
168         'sticky' => $row->find('xpath', (new CssSelectorConverter())->toXPath('.views-field-sticky span.field-content'))->getText(),
169       ];
170     }
171     $this->assertEqual($actual, $expected);
172   }
173
174 }