Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Tests / ViewElementTest.php
1 <?php
2
3 namespace Drupal\views\Tests;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests the view render element.
9  *
10  * @group views
11  */
12 class ViewElementTest extends ViewTestBase {
13
14   /**
15    * Views used by this test.
16    *
17    * @var array
18    */
19   public static $testViews = ['test_view_embed'];
20
21   protected function setUp() {
22     parent::setUp();
23
24     $this->enableViewsTestModule();
25   }
26
27   /**
28    * Tests the rendered output and form output of a view element.
29    */
30   public function testViewElement() {
31     /** @var \Drupal\Core\Render\RendererInterface $renderer */
32     $renderer = $this->container->get('renderer');
33     $view = Views::getView('test_view_embed');
34
35     // Get the render array, #embed must be FALSE since this is the default
36     // display.
37     $render = $view->buildRenderable();
38     $this->assertEqual($render['#embed'], FALSE);
39     $this->setRawContent($renderer->renderRoot($render));
40
41     $xpath = $this->xpath('//div[@class="views-element-container"]');
42     $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
43
44     $xpath = $this->xpath('//div[@class="view-content"]');
45     $this->assertTrue($xpath, 'The view content has been found in the rendered output.');
46     // There should be 5 rows in the results.
47     $xpath = $this->xpath('//div[@class="view-content"]/div');
48     $this->assertEqual(count($xpath), 5);
49
50     // Test a form.
51     $this->drupalGet('views_test_data_element_form');
52
53     $xpath = $this->xpath('//div[@class="views-element-container js-form-wrapper form-wrapper"]');
54     $this->assertTrue($xpath, 'The view container has been found on the form.');
55
56     $xpath = $this->xpath('//div[@class="view-content"]');
57     $this->assertTrue($xpath, 'The view content has been found on the form.');
58     // There should be 5 rows in the results.
59     $xpath = $this->xpath('//div[@class="view-content"]/div');
60     $this->assertEqual(count($xpath), 5);
61
62     // Add an argument and save the view.
63     $view->displayHandlers->get('default')->overrideOption('arguments', [
64       'age' => [
65         'default_action' => 'ignore',
66         'title' => '',
67         'default_argument_type' => 'fixed',
68         'validate' => [
69           'type' => 'none',
70           'fail' => 'not found',
71         ],
72         'break_phrase' => FALSE,
73         'not' => FALSE,
74         'id' => 'age',
75         'table' => 'views_test_data',
76         'field' => 'age',
77         'plugin_id' => 'numeric',
78       ]
79     ]);
80     $view->save();
81
82     // Test the render array again.
83     $view = Views::getView('test_view_embed');
84     $render = $view->buildRenderable(NULL, [25]);
85     $this->setRawContent($renderer->renderRoot($render));
86     // There should be 1 row in the results, 'John' arg 25.
87     $xpath = $this->xpath('//div[@class="view-content"]/div');
88     $this->assertEqual(count($xpath), 1);
89
90     // Test that the form has the same expected result.
91     $this->drupalGet('views_test_data_element_form');
92     $xpath = $this->xpath('//div[@class="view-content"]/div');
93     $this->assertEqual(count($xpath), 1);
94   }
95
96   /**
97    * Tests the rendered output and form output of a view element, using the
98    * embed display plugin.
99    */
100   public function testViewElementEmbed() {
101     /** @var \Drupal\Core\Render\RendererInterface $renderer */
102     $renderer = $this->container->get('renderer');
103     $view = Views::getView('test_view_embed');
104
105     // Get the render array, #embed must be TRUE since this is an embed display.
106     $render = $view->buildRenderable('embed_1');
107     $this->assertEqual($render['#embed'], TRUE);
108     $this->setRawContent($renderer->renderRoot($render));
109
110     $xpath = $this->xpath('//div[@class="views-element-container"]');
111     $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
112
113     $xpath = $this->xpath('//div[@class="view-content"]');
114     $this->assertTrue($xpath, 'The view content has been found in the rendered output.');
115     // There should be 5 rows in the results.
116     $xpath = $this->xpath('//div[@class="view-content"]/div');
117     $this->assertEqual(count($xpath), 5);
118
119     // Test a form.
120     $this->drupalGet('views_test_data_element_embed_form');
121
122     $xpath = $this->xpath('//div[@class="views-element-container js-form-wrapper form-wrapper"]');
123     $this->assertTrue($xpath, 'The view container has been found on the form.');
124
125     $xpath = $this->xpath('//div[@class="view-content"]');
126     $this->assertTrue($xpath, 'The view content has been found on the form.');
127     // There should be 5 rows in the results.
128     $xpath = $this->xpath('//div[@class="view-content"]/div');
129     $this->assertEqual(count($xpath), 5);
130
131     // Add an argument and save the view.
132     $view->displayHandlers->get('default')->overrideOption('arguments', [
133       'age' => [
134         'default_action' => 'ignore',
135         'title' => '',
136         'default_argument_type' => 'fixed',
137         'validate' => [
138           'type' => 'none',
139           'fail' => 'not found',
140         ],
141         'break_phrase' => FALSE,
142         'not' => FALSE,
143         'id' => 'age',
144         'table' => 'views_test_data',
145         'field' => 'age',
146         'plugin_id' => 'numeric',
147       ]
148     ]);
149     $view->save();
150
151     // Test the render array again.
152     $view = Views::getView('test_view_embed');
153     $render = $view->buildRenderable('embed_1', [25]);
154     $this->setRawContent($renderer->renderRoot($render));
155     // There should be 1 row in the results, 'John' arg 25.
156     $xpath = $this->xpath('//div[@class="view-content"]/div');
157     $this->assertEqual(count($xpath), 1);
158
159     // Test that the form has the same expected result.
160     $this->drupalGet('views_test_data_element_embed_form');
161     $xpath = $this->xpath('//div[@class="view-content"]/div');
162     $this->assertEqual(count($xpath), 1);
163
164     // Tests the render array with an exposed filter.
165     $view = Views::getView('test_view_embed');
166     $render = $view->buildRenderable('embed_2');
167     $this->setRawContent($renderer->renderRoot($render));
168
169     // Ensure that the exposed form is rendered.
170     $this->assertEqual(1, count($this->xpath('//form[@class="views-exposed-form"]')));
171   }
172
173 }