3 namespace Drupal\views\Tests;
5 use Drupal\views\Views;
8 * Tests the view render element.
12 class ViewElementTest extends ViewTestBase {
15 * Views used by this test.
19 public static $testViews = ['test_view_embed'];
21 protected function setUp() {
24 $this->enableViewsTestModule();
28 * Tests the rendered output and form output of a view element.
30 public function testViewElement() {
31 /** @var \Drupal\Core\Render\RendererInterface $renderer */
32 $renderer = $this->container->get('renderer');
33 $view = Views::getView('test_view_embed');
35 // Get the render array, #embed must be FALSE since this is the default
37 $render = $view->buildRenderable();
38 $this->assertEqual($render['#embed'], FALSE);
39 $this->setRawContent($renderer->renderRoot($render));
41 $xpath = $this->xpath('//div[@class="views-element-container"]');
42 $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
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);
51 $this->drupalGet('views_test_data_element_form');
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.');
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);
62 // Add an argument and save the view.
63 $view->displayHandlers->get('default')->overrideOption('arguments', [
65 'default_action' => 'ignore',
67 'default_argument_type' => 'fixed',
70 'fail' => 'not found',
72 'break_phrase' => FALSE,
75 'table' => 'views_test_data',
77 'plugin_id' => 'numeric',
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);
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);
97 * Tests the rendered output and form output of a view element, using the
98 * embed display plugin.
100 public function testViewElementEmbed() {
101 /** @var \Drupal\Core\Render\RendererInterface $renderer */
102 $renderer = $this->container->get('renderer');
103 $view = Views::getView('test_view_embed');
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));
110 $xpath = $this->xpath('//div[@class="views-element-container"]');
111 $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
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);
120 $this->drupalGet('views_test_data_element_embed_form');
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.');
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);
131 // Add an argument and save the view.
132 $view->displayHandlers->get('default')->overrideOption('arguments', [
134 'default_action' => 'ignore',
136 'default_argument_type' => 'fixed',
139 'fail' => 'not found',
141 'break_phrase' => FALSE,
144 'table' => 'views_test_data',
146 'plugin_id' => 'numeric',
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);
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);
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));
169 // Ensure that the exposed form is rendered.
170 $this->assertEqual(1, count($this->xpath('//form[@class="views-exposed-form"]')));