Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / Views / RowPluginTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional\Views;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests the node row plugin.
9  *
10  * @group node
11  * @see \Drupal\node\Plugin\views\row\NodeRow
12  */
13 class RowPluginTest extends NodeTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['node'];
21
22   /**
23    * Views used by this test.
24    *
25    * @var array
26    */
27   public static $testViews = ['test_node_row_plugin'];
28
29   /**
30    * Contains all nodes used by this test.
31    *
32    * @var array
33    */
34   protected $nodes;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp($import_test_views = TRUE) {
40     parent::setUp($import_test_views);
41
42     $this->drupalCreateContentType(['type' => 'article']);
43
44     // Create two nodes.
45     for ($i = 0; $i < 2; $i++) {
46       $this->nodes[] = $this->drupalCreateNode(
47         [
48           'type' => 'article',
49           'body' => [
50             [
51               'value' => $this->randomMachineName(42),
52               'format' => filter_default_format(),
53               'summary' => $this->randomMachineName(),
54             ],
55           ],
56         ]
57       );
58     }
59   }
60
61   /**
62    * Tests the node row plugin.
63    */
64   public function testRowPlugin() {
65     /** @var \Drupal\Core\Render\RendererInterface $renderer */
66     $renderer = $this->container->get('renderer');
67     $view = Views::getView('test_node_row_plugin');
68     $view->initDisplay();
69     $view->setDisplay('page_1');
70     $view->initStyle();
71     $view->rowPlugin->options['view_mode'] = 'full';
72
73     // Test with view_mode full.
74     $output = $view->preview();
75     $output = $renderer->renderRoot($output);
76     foreach ($this->nodes as $node) {
77       $this->assertFalse(strpos($output, $node->body->summary) !== FALSE, 'Make sure the teaser appears in the output of the view.');
78       $this->assertTrue(strpos($output, $node->body->value) !== FALSE, 'Make sure the full text appears in the output of the view.');
79     }
80
81     // Test with teasers.
82     $view->rowPlugin->options['view_mode'] = 'teaser';
83     $output = $view->preview();
84     $output = $renderer->renderRoot($output);
85     foreach ($this->nodes as $node) {
86       $this->assertTrue(strpos($output, $node->body->summary) !== FALSE, 'Make sure the teaser appears in the output of the view.');
87       $this->assertFalse(strpos($output, $node->body->value) !== FALSE, 'Make sure the full text does not appears in the output of the view if teaser is set as viewmode.');
88     }
89   }
90
91 }