3 namespace Drupal\Tests\views\Kernel\Plugin;
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;
11 * Tests native behaviors of the block views plugin.
15 class ViewsBlockTest extends ViewsKernelTestBase {
22 public static $modules = ['block', 'block_test_views'];
25 * Views used by this test.
29 public static $testViews = ['test_view_block'];
34 protected function setUp($import_test_views = TRUE) {
37 ViewTestData::createTestViews(get_class($this), ['block_test_views']);
41 * Tests that ViewsBlock::getMachineNameSuggestion() produces the right value.
43 * @see \Drupal\views\Plugin\Block::getmachineNameSuggestion()
45 public function testMachineNameSuggestion() {
46 $plugin_definition = [
47 'provider' => 'views',
49 $plugin_id = 'views_block:test_view_block-block_1';
50 $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
52 $this->assertEqual($views_block->getMachineNameSuggestion(), 'views_block__test_view_block_block_1');
56 * Tests that ViewsBlock::build() produces the right output with title tokens.
58 * @see \Drupal\views\Plugin\Block::build()
60 public function testBuildWithTitleToken() {
61 $view = Views::getView('test_view_block');
68 'table' => 'views_test_data',
69 'plugin_id' => 'standard',
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);
79 $plugin_definition = [
80 'provider' => 'views',
82 $plugin_id = 'views_block:test_view_block-block_1';
83 $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
85 $build = $views_block->build();
86 $this->assertEquals('George', $build['#title']['#markup']);
90 * Tests ViewsBlock::build() with a title override.
92 * @see \Drupal\views\Plugin\Block::build()
94 public function testBuildWithTitleOverride() {
95 $view = Views::getView('test_view_block');
98 // Add a fixed argument that sets a title and save the view.
99 $view->displayHandlers->get('default')->overrideOption('arguments', [
101 'default_action' => 'default',
102 'title_enable' => TRUE,
103 'title' => 'Overridden title',
104 'default_argument_type' => 'fixed',
105 'default_argument_options' => [
106 'argument' => 'fixed',
110 'fail' => 'not found',
113 'table' => 'views_test_data',
115 'plugin_id' => 'string',
120 $plugin_definition = [
121 'provider' => 'views',
123 $plugin_id = 'views_block:test_view_block-block_1';
124 $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
126 $build = $views_block->build();
127 $this->assertEquals('Overridden title', $build['#title']['#markup']);