Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Kernel / ViewElementTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests the view render element.
9  *
10  * @group views
11  */
12 class ViewElementTest extends ViewsKernelTestBase {
13
14   /**
15    * Views used by this test.
16    *
17    * @var array
18    */
19   public static $testViews = ['test_view_embed'];
20
21   /**
22    * Tests the rendered output and form output of a view element.
23    */
24   public function testViewElement() {
25     /** @var \Drupal\Core\Render\RendererInterface $renderer */
26     $renderer = $this->container->get('renderer');
27     $view = Views::getView('test_view_embed');
28
29     // Get the render array, #embed must be FALSE since this is the default
30     // display.
31     $render = $view->buildRenderable();
32     $this->assertEqual($render['#embed'], FALSE);
33     $this->setRawContent($renderer->renderRoot($render));
34
35     $xpath = $this->xpath('//div[@class="views-element-container"]');
36     $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
37
38     // There should be 5 rows in the results.
39     $xpath = $this->xpath('//div[@class="views-row"]');
40     $this->assertEqual(count($xpath), 5);
41
42     // Add an argument and save the view.
43     $view->displayHandlers->get('default')->overrideOption('arguments', [
44       'age' => [
45         'default_action' => 'ignore',
46         'title' => '',
47         'default_argument_type' => 'fixed',
48         'validate' => [
49           'type' => 'none',
50           'fail' => 'not found',
51         ],
52         'break_phrase' => FALSE,
53         'not' => FALSE,
54         'id' => 'age',
55         'table' => 'views_test_data',
56         'field' => 'age',
57         'plugin_id' => 'numeric',
58       ],
59     ]);
60     $view->save();
61
62     // Test the render array again.
63     $view = Views::getView('test_view_embed');
64     $render = $view->buildRenderable(NULL, [25]);
65     $this->setRawContent($renderer->renderRoot($render));
66     // There should be 1 row in the results, 'John' arg 25.
67     $xpath = $this->xpath('//div[@class="views-row"]');
68     $this->assertEqual(count($xpath), 1);
69   }
70
71   /**
72    * Tests the rendered output and form output of a view element, using the
73    * embed display plugin.
74    */
75   public function testViewElementEmbed() {
76     /** @var \Drupal\Core\Render\RendererInterface $renderer */
77     $renderer = $this->container->get('renderer');
78     $view = Views::getView('test_view_embed');
79
80     // Get the render array, #embed must be TRUE since this is an embed display.
81     $render = $view->buildRenderable('embed_1');
82     $this->assertEqual($render['#embed'], TRUE);
83     $this->setRawContent($renderer->renderRoot($render));
84
85     $xpath = $this->xpath('//div[@class="views-element-container"]');
86     $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
87
88     // There should be 5 rows in the results.
89     $xpath = $this->xpath('//div[@class="views-row"]');
90     $this->assertEqual(count($xpath), 5);
91
92     // Add an argument and save the view.
93     $view->displayHandlers->get('default')->overrideOption('arguments', [
94       'age' => [
95         'default_action' => 'ignore',
96         'title' => '',
97         'default_argument_type' => 'fixed',
98         'validate' => [
99           'type' => 'none',
100           'fail' => 'not found',
101         ],
102         'break_phrase' => FALSE,
103         'not' => FALSE,
104         'id' => 'age',
105         'table' => 'views_test_data',
106         'field' => 'age',
107         'plugin_id' => 'numeric',
108       ],
109     ]);
110     $view->save();
111
112     // Test the render array again.
113     $view = Views::getView('test_view_embed');
114     $render = $view->buildRenderable('embed_1', [25]);
115     $this->setRawContent($renderer->renderRoot($render));
116     // There should be 1 row in the results, 'John' arg 25.
117     $xpath = $this->xpath('//div[@class="views-row"]');
118     $this->assertEqual(count($xpath), 1);
119
120     // Tests the render array with an exposed filter.
121     $view = Views::getView('test_view_embed');
122     $render = $view->buildRenderable('embed_2');
123     $this->setRawContent($renderer->renderRoot($render));
124
125     // Ensure that the exposed form is rendered.
126     $this->assertEqual(1, count($this->xpath('//form[@class="views-exposed-form"]')));
127   }
128
129 }