Version 1
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / ViewsBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views\Plugin\Block\ViewsBlock;
6 use Drupal\views\Tests\ViewTestData;
7 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
8 use Drupal\views\Views;
9
10 /**
11  * Tests native behaviors of the block views plugin.
12  *
13  * @group views
14  */
15 class ViewsBlockTest extends ViewsKernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['block', 'block_test_views'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_view_block'];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp($import_test_views = TRUE) {
35     parent::setUp();
36
37     ViewTestData::createTestViews(get_class($this), ['block_test_views']);
38   }
39
40   /**
41    * Tests that ViewsBlock::getMachineNameSuggestion() produces the right value.
42    *
43    * @see \Drupal\views\Plugin\Block::getmachineNameSuggestion()
44    */
45   public function testMachineNameSuggestion() {
46     $plugin_definition = [
47       'provider' => 'views',
48     ];
49     $plugin_id = 'views_block:test_view_block-block_1';
50     $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
51
52     $this->assertEqual($views_block->getMachineNameSuggestion(), 'views_block__test_view_block_block_1');
53   }
54
55   /**
56    * Tests that ViewsBlock::build() produces the right output with title tokens.
57    *
58    * @see \Drupal\views\Plugin\Block::build()
59    */
60   public function testBuildWithTitleToken() {
61     $view = Views::getView('test_view_block');
62     $view->setDisplay();
63
64     $sorts = [
65       'name' => [
66         'id' => 'name',
67         'field' => 'name',
68         'table' => 'views_test_data',
69         'plugin_id' => 'standard',
70         'order' => 'asc',
71       ],
72     ];
73     // Set the title to the 'name' field in the first row and add a sort order
74     // for consistent results on different databases.
75     $view->display_handler->setOption('title', '{{ name }}');
76     $view->display_handler->setOption('sorts', $sorts);
77     $view->save();
78
79     $plugin_definition = [
80       'provider' => 'views',
81     ];
82     $plugin_id = 'views_block:test_view_block-block_1';
83     $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
84
85     $build = $views_block->build();
86     $this->assertEquals('George', $build['#title']['#markup']);
87   }
88
89   /**
90    * Tests ViewsBlock::build() with a title override.
91    *
92    * @see \Drupal\views\Plugin\Block::build()
93    */
94   public function testBuildWithTitleOverride() {
95     $view = Views::getView('test_view_block');
96     $view->setDisplay();
97
98     // Add a fixed argument that sets a title and save the view.
99     $view->displayHandlers->get('default')->overrideOption('arguments', [
100       'name' => [
101         'default_action' => 'default',
102         'title_enable' => TRUE,
103         'title' => 'Overridden title',
104         'default_argument_type' => 'fixed',
105         'default_argument_options' => [
106           'argument' => 'fixed'
107         ],
108         'validate' => [
109           'type' => 'none',
110           'fail' => 'not found',
111         ],
112         'id' => 'name',
113         'table' => 'views_test_data',
114         'field' => 'name',
115         'plugin_id' => 'string',
116       ]
117     ]);
118     $view->save();
119
120     $plugin_definition = [
121       'provider' => 'views',
122     ];
123     $plugin_id = 'views_block:test_view_block-block_1';
124     $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
125
126     $build = $views_block->build();
127     $this->assertEquals('Overridden title', $build['#title']['#markup']);
128   }
129
130 }