class ViewsBlockTest

Same name in this branch
  1. 9 core/modules/views/tests/src/Unit/Plugin/Block/ViewsBlockTest.php \Drupal\Tests\views\Unit\Plugin\Block\ViewsBlockTest
Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Unit/Plugin/Block/ViewsBlockTest.php \Drupal\Tests\views\Unit\Plugin\Block\ViewsBlockTest
  2. 11.x core/modules/views/tests/src/Kernel/Plugin/ViewsBlockTest.php \Drupal\Tests\views\Kernel\Plugin\ViewsBlockTest
  3. 11.x core/modules/views_ui/tests/src/Kernel/ViewsBlockTest.php \Drupal\Tests\views_ui\Kernel\ViewsBlockTest
  4. 10 core/modules/views/tests/src/Unit/Plugin/Block/ViewsBlockTest.php \Drupal\Tests\views\Unit\Plugin\Block\ViewsBlockTest
  5. 10 core/modules/views/tests/src/Kernel/Plugin/ViewsBlockTest.php \Drupal\Tests\views\Kernel\Plugin\ViewsBlockTest
  6. 10 core/modules/views_ui/tests/src/Kernel/ViewsBlockTest.php \Drupal\Tests\views_ui\Kernel\ViewsBlockTest
  7. 8.9.x core/modules/views/tests/src/Unit/Plugin/Block/ViewsBlockTest.php \Drupal\Tests\views\Unit\Plugin\Block\ViewsBlockTest
  8. 8.9.x core/modules/views/tests/src/Kernel/Plugin/ViewsBlockTest.php \Drupal\Tests\views\Kernel\Plugin\ViewsBlockTest

Tests native behaviors of the block views plugin.

@group views

Hierarchy

Expanded class hierarchy of ViewsBlockTest

File

core/modules/views/tests/src/Kernel/Plugin/ViewsBlockTest.php, line 15

Namespace

Drupal\Tests\views\Kernel\Plugin
View source
class ViewsBlockTest extends ViewsKernelTestBase {
  
  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'block',
    'block_test_views',
  ];
  
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = [
    'test_view_block',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE) : void {
    parent::setUp();
    ViewTestData::createTestViews(static::class, [
      'block_test_views',
    ]);
  }
  
  /**
   * Tests that ViewsBlock::getMachineNameSuggestion() produces the right value.
   *
   * @see \Drupal\views\Plugin\Block::getMachineNameSuggestion()
   */
  public function testMachineNameSuggestion() {
    $plugin_definition = [
      'provider' => 'views',
    ];
    $plugin_id = 'views_block:test_view_block-block_1';
    $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
    $this->assertEquals('views_block__test_view_block_block_1', $views_block->getMachineNameSuggestion());
  }
  
  /**
   * Tests that ViewsBlock::build() produces the right output with title tokens.
   *
   * @see \Drupal\views\Plugin\Block::build()
   */
  public function testBuildWithTitleToken() {
    $view = Views::getView('test_view_block');
    $view->setDisplay();
    $sorts = [
      'name' => [
        'id' => 'name',
        'field' => 'name',
        'table' => 'views_test_data',
        'plugin_id' => 'standard',
        'order' => 'asc',
      ],
    ];
    // Set the title to the 'name' field in the first row and add a sort order
    // for consistent results on different databases.
    $view->display_handler
      ->setOption('title', '{{ name }}');
    $view->display_handler
      ->setOption('sorts', $sorts);
    $view->save();
    $plugin_definition = [
      'provider' => 'views',
    ];
    $plugin_id = 'views_block:test_view_block-block_1';
    $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
    $build = $views_block->build();
    $this->assertEquals('George', $build['#title']['#markup']);
  }
  
  /**
   * Tests ViewsBlock::build() with a title override.
   *
   * @see \Drupal\views\Plugin\Block::build()
   */
  public function testBuildWithTitleOverride() {
    $view = Views::getView('test_view_block');
    $view->setDisplay();
    // Add a fixed argument that sets a title and save the view.
    $view->displayHandlers
      ->get('default')
      ->overrideOption('arguments', [
      'name' => [
        'default_action' => 'default',
        'title_enable' => TRUE,
        'title' => 'Overridden title',
        'default_argument_type' => 'fixed',
        'default_argument_options' => [
          'argument' => 'fixed',
        ],
        'validate' => [
          'type' => 'none',
          'fail' => 'not found',
        ],
        'id' => 'name',
        'table' => 'views_test_data',
        'field' => 'name',
        'plugin_id' => 'string',
      ],
    ]);
    $view->save();
    $plugin_definition = [
      'provider' => 'views',
    ];
    $plugin_id = 'views_block:test_view_block-block_1';
    $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
    $build = $views_block->build();
    $this->assertEquals('Overridden title', $build['#title']['#markup']);
  }
  
  /**
   * Tests that ViewsBlock::getPreviewFallbackString() produces the right value.
   *
   * @see \Drupal\views\Plugin\Block\ViewsBlockBase::getPreviewFallbackString()
   */
  public function testGetPreviewFallbackString() {
    $plugin_definition = [
      'provider' => 'views',
    ];
    $plugin_id = 'views_block:test_view_block-block_1';
    $views_block = ViewsBlock::create($this->container, [], $plugin_id, $plugin_definition);
    $this->assertEquals('"test_view_block::block_1" views block', $views_block->getPreviewFallbackString());
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.