annotate core/modules/views/tests/src/Kernel/ViewElementTest.php @ 19:fa3358dc1485 tip

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