3 namespace Drupal\Tests\views\Kernel;
5 use Drupal\views\Views;
8 * Tests the view render element.
12 class ViewElementTest extends ViewsKernelTestBase {
15 * Views used by this test.
19 public static $testViews = ['test_view_embed'];
22 * Tests the rendered output and form output of a view element.
24 public function testViewElement() {
25 /** @var \Drupal\Core\Render\RendererInterface $renderer */
26 $renderer = $this->container->get('renderer');
27 $view = Views::getView('test_view_embed');
29 // Get the render array, #embed must be FALSE since this is the default
31 $render = $view->buildRenderable();
32 $this->assertEqual($render['#embed'], FALSE);
33 $this->setRawContent($renderer->renderRoot($render));
35 $xpath = $this->xpath('//div[@class="views-element-container"]');
36 $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
38 // There should be 5 rows in the results.
39 $xpath = $this->xpath('//div[@class="views-row"]');
40 $this->assertEqual(count($xpath), 5);
42 // Add an argument and save the view.
43 $view->displayHandlers->get('default')->overrideOption('arguments', [
45 'default_action' => 'ignore',
47 'default_argument_type' => 'fixed',
50 'fail' => 'not found',
52 'break_phrase' => FALSE,
55 'table' => 'views_test_data',
57 'plugin_id' => 'numeric',
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);
72 * Tests the rendered output and form output of a view element, using the
73 * embed display plugin.
75 public function testViewElementEmbed() {
76 /** @var \Drupal\Core\Render\RendererInterface $renderer */
77 $renderer = $this->container->get('renderer');
78 $view = Views::getView('test_view_embed');
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));
85 $xpath = $this->xpath('//div[@class="views-element-container"]');
86 $this->assertTrue($xpath, 'The view container has been found in the rendered output.');
88 // There should be 5 rows in the results.
89 $xpath = $this->xpath('//div[@class="views-row"]');
90 $this->assertEqual(count($xpath), 5);
92 // Add an argument and save the view.
93 $view->displayHandlers->get('default')->overrideOption('arguments', [
95 'default_action' => 'ignore',
97 'default_argument_type' => 'fixed',
100 'fail' => 'not found',
102 'break_phrase' => FALSE,
105 'table' => 'views_test_data',
107 'plugin_id' => 'numeric',
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);
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));
125 // Ensure that the exposed form is rendered.
126 $this->assertEqual(1, count($this->xpath('//form[@class="views-exposed-form"]')));