Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Entity / RowEntityRenderersTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Entity;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
8 use Drupal\user\Entity\User;
9 use Drupal\views\Views;
10
11 /**
12  * Tests the entity row renderers.
13  *
14  * @group views
15  * @see \Drupal\views\Entity\Render\RendererBase
16  */
17 class RowEntityRenderersTest extends ViewsKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['field', 'filter', 'text', 'node', 'user', 'language', 'views_test_language'];
25
26   /**
27    * Views used by this test.
28    *
29    * @var array
30    */
31   public static $testViews = [
32     'test_entity_row_renderers',
33     'test_entity_row_renderers_revisions_base',
34   ];
35
36   /**
37    * An array of added languages.
38    *
39    * @var array
40    */
41   protected $langcodes;
42
43   /**
44    * An array of titles for each node per language.
45    *
46    * @var array
47    */
48   protected $expected;
49
50   /**
51    * The author of the test content.
52    *
53    * @var \Drupal\user\UserInterface
54    */
55   protected $testAuthor;
56
57   /**
58    * An array of IDs of the test content.
59    *
60    * @var array[]
61    */
62   protected $testIds;
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function setUp($import_test_views = TRUE) {
68     parent::setUp();
69
70     $this->installEntitySchema('node');
71     $this->installEntitySchema('user');
72     $this->installSchema('node', ['node_access']);
73     $this->installConfig(['node', 'language']);
74
75     // The entity.node.canonical route must exist when nodes are rendered.
76     $this->container->get('router.builder')->rebuild();
77
78     $this->langcodes = [\Drupal::languageManager()->getDefaultLanguage()->getId()];
79     for ($i = 0; $i < 2; $i++) {
80       $langcode = 'l' . $i;
81       $this->langcodes[] = $langcode;
82       ConfigurableLanguage::createFromLangcode($langcode)->save();
83     }
84
85     $this->testAuthor = User::create([
86       'name' => 'foo',
87     ]);
88     $this->testAuthor->save();
89
90     // Make sure we do not try to render non-existing user data.
91     $node_type = NodeType::create(['type' => 'test']);
92     $node_type->setDisplaySubmitted(FALSE);
93     $node_type->save();
94
95     $this->values = [];
96     $this->ids = [];
97     $controller = \Drupal::entityManager()->getStorage('node');
98     $langcode_index = 0;
99
100     for ($i = 0; $i < count($this->langcodes); $i++) {
101       // Create a node with a different default language each time.
102       $default_langcode = $this->langcodes[$langcode_index++];
103       $node = $controller->create(['type' => 'test', 'uid' => $this->testAuthor->id(), 'langcode' => $default_langcode]);
104       // Ensure the default language is processed first.
105       $langcodes = array_merge([$default_langcode], array_diff($this->langcodes, [$default_langcode]));
106
107       foreach ($langcodes as $langcode) {
108         // Ensure we have a predictable result order.
109         $this->values[$i][$langcode] = $i . '-' . $langcode . '-' . $this->randomMachineName();
110
111         if ($langcode != $default_langcode) {
112           $node->addTranslation($langcode, ['title' => $this->values[$i][$langcode]]);
113         }
114         else {
115           $node->setTitle($this->values[$i][$langcode]);
116         }
117
118         $node->save();
119
120         $this->ids[] = [
121           'nid' => $node->id(),
122           'uid' => $this->testAuthor->id(),
123         ];
124       }
125     }
126   }
127
128   /**
129    * Tests the entity row renderers.
130    */
131   public function testEntityRenderers() {
132     $this->checkLanguageRenderers('page_1', $this->values);
133   }
134
135   /**
136    * Tests the field row renderers.
137    */
138   public function testFieldRenderers() {
139     $this->checkLanguageRenderers('page_2', $this->values);
140   }
141
142   /**
143    * Tests the row renderer with a revision base table.
144    */
145   public function testRevisionBaseTable() {
146     $view = Views::getView('test_entity_row_renderers_revisions_base');
147     $view->execute();
148     $this->assertIdenticalResultset($view, $this->ids, ['nid' => 'nid', 'uid' => 'uid']);
149   }
150
151   /**
152    * Checks that the language renderer configurations work as expected.
153    *
154    * @param string $display
155    *   Name of display to test with.
156    * @param array $values
157    *   An array of node information which are each an array of node titles
158    *   associated with language keys appropriate for the translation of that
159    *   node.
160    */
161   protected function checkLanguageRenderers($display, $values) {
162     $expected = [
163       $values[0]['en'],
164       $values[0]['en'],
165       $values[0]['en'],
166       $values[1]['en'],
167       $values[1]['en'],
168       $values[1]['en'],
169       $values[2]['en'],
170       $values[2]['en'],
171       $values[2]['en'],
172     ];
173     $this->assertTranslations($display, '***LANGUAGE_language_content***', $expected, 'The current language renderer behaves as expected.');
174
175     $expected = [
176       $values[0]['en'],
177       $values[0]['en'],
178       $values[0]['en'],
179       $values[1]['l0'],
180       $values[1]['l0'],
181       $values[1]['l0'],
182       $values[2]['l1'],
183       $values[2]['l1'],
184       $values[2]['l1'],
185     ];
186     $this->assertTranslations($display, '***LANGUAGE_entity_default***', $expected, 'The default language renderer behaves as expected.');
187
188     $expected = [
189       $values[0]['en'],
190       $values[0]['l0'],
191       $values[0]['l1'],
192       $values[1]['en'],
193       $values[1]['l0'],
194       $values[1]['l1'],
195       $values[2]['en'],
196       $values[2]['l0'],
197       $values[2]['l1'],
198     ];
199     $this->assertTranslations($display, '***LANGUAGE_entity_translation***', $expected, 'The translation language renderer behaves as expected.');
200
201     $expected = [
202       $values[0][$this->langcodes[0]],
203       $values[0][$this->langcodes[0]],
204       $values[0][$this->langcodes[0]],
205       $values[1][$this->langcodes[0]],
206       $values[1][$this->langcodes[0]],
207       $values[1][$this->langcodes[0]],
208       $values[2][$this->langcodes[0]],
209       $values[2][$this->langcodes[0]],
210       $values[2][$this->langcodes[0]],
211     ];
212     $this->assertTranslations($display, '***LANGUAGE_site_default***', $expected, 'The site default language renderer behaves as expected.');
213
214     $expected = [
215       $values[0]['l0'],
216       $values[0]['l0'],
217       $values[0]['l0'],
218       $values[1]['l0'],
219       $values[1]['l0'],
220       $values[1]['l0'],
221       $values[2]['l0'],
222       $values[2]['l0'],
223       $values[2]['l0'],
224     ];
225     $this->assertTranslations($display, 'l0', $expected, 'The language specific renderer behaves as expected.');
226   }
227
228   /**
229    * Checks that the view results match the expected values.
230    *
231    * @param string $display
232    *   Name of display to test with.
233    * @param string $renderer_id
234    *   The id of the renderer to be tested.
235    * @param array $expected
236    *   An array of expected title translation values, one for each result row.
237    * @param string $message
238    *   (optional) A message to display with the assertion.
239    * @param string $group
240    *   (optional) The group this message is in.
241    *
242    * @return bool
243    *   TRUE if the assertion succeeded, FALSE otherwise.
244    */
245   protected function assertTranslations($display, $renderer_id, array $expected, $message = '', $group = 'Other') {
246     $view = Views::getView('test_entity_row_renderers');
247     $view->storage->invalidateCaches();
248     $view->setDisplay($display);
249     $view->getDisplay()->setOption('rendering_language', $renderer_id);
250     $view->preview();
251
252     $result = FALSE;
253     foreach ($expected as $index => $expected_output) {
254       if (!empty($view->result[$index])) {
255         $build = $view->rowPlugin->render($view->result[$index]);
256         $output = \Drupal::service('renderer')->renderRoot($build);
257         $result = strpos($output, $expected_output) !== FALSE;
258         if (!$result) {
259           break;
260         }
261       }
262       else {
263         $result = FALSE;
264         break;
265       }
266     }
267
268     return $this->assertTrue($result, $message, $group);
269   }
270
271 }