function ContextualFiltersBlockContextTest::testBlockContext

Same name in other branches
  1. 9 core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php \Drupal\Tests\views\Functional\Plugin\ContextualFiltersBlockContextTest::testBlockContext()
  2. 8.9.x core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php \Drupal\Tests\views\Functional\Plugin\ContextualFiltersBlockContextTest::testBlockContext()
  3. 10 core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php \Drupal\Tests\views\Functional\Plugin\ContextualFiltersBlockContextTest::testBlockContext()

Tests exposed context.

File

core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php, line 88

Class

ContextualFiltersBlockContextTest
A test for contextual filters exposed as block context.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testBlockContext() : void {
    $this->drupalLogin($this->drupalCreateUser([
        'administer views',
        'administer blocks',
    ]));
    // Check if context was correctly propagated to the block.
    $definition = $this->container
        ->get('plugin.manager.block')
        ->getDefinition('views_block:test_view_block_with_context-block_1');
    $this->assertInstanceOf(ContextDefinitionInterface::class, $definition['context_definitions']['nid']);
    
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
    $context = $definition['context_definitions']['nid'];
    $this->assertEquals('entity:node', $context->getDataType(), 'Context definition data type is correct.');
    $this->assertEquals('Content: ID', $context->getLabel(), 'Context definition label is correct.');
    $this->assertFalse($context->isRequired(), 'Context is not required.');
    // Place test block via block UI to check if contexts are correctly exposed.
    $this->drupalGet('admin/structure/block/add/views_block:test_view_block_with_context-block_1/stark', [
        'query' => [
            'region' => 'content',
        ],
    ]);
    $edit = [
        'settings[context_mapping][nid]' => '@node.node_route_context:node',
    ];
    $this->submitForm($edit, 'Save block');
    // Check if mapping saved correctly.
    
    /** @var \Drupal\block\BlockInterface $block */
    $block = $this->container
        ->get('entity_type.manager')
        ->getStorage('block')
        ->load('stark_views_block__test_view_block_with_context_block_1');
    $expected_settings = [
        'id' => 'views_block:test_view_block_with_context-block_1',
        'label' => '',
        'provider' => 'views',
        'label_display' => 'visible',
        'views_label' => '',
        'items_per_page' => 'none',
        'context_mapping' => [
            'nid' => '@node.node_route_context:node',
        ],
    ];
    $this->assertEquals($expected_settings, $block->getPlugin()
        ->getConfiguration(), 'Block settings are correct.');
    // Make sure view behaves as expected.
    $this->drupalGet('<front>');
    $this->assertSession()
        ->pageTextContains('Test view: No results found.');
    $this->drupalGet($this->nodes[0]
        ->toUrl());
    $this->assertSession()
        ->pageTextContains('Test view row: First test node');
    $this->drupalGet($this->nodes[1]
        ->toUrl());
    $this->assertSession()
        ->pageTextContains('Test view row: Second test node');
    // Check the second block which should expose two integer contexts, one
    // based on the numeric plugin and the other based on numeric validation.
    $definition = $this->container
        ->get('plugin.manager.block')
        ->getDefinition('views_block:test_view_block_with_context-block_2');
    $this->assertInstanceOf(ContextDefinitionInterface::class, $definition['context_definitions']['created']);
    
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
    $context = $definition['context_definitions']['created'];
    $this->assertEquals('integer', $context->getDataType(), 'Context definition data type is correct.');
    $this->assertEquals('Content: Authored on', $context->getLabel(), 'Context definition label is correct.');
    $this->assertFalse($context->isRequired(), 'Context is not required.');
    $this->assertInstanceOf(ContextDefinitionInterface::class, $definition['context_definitions']['vid']);
    
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
    $context = $definition['context_definitions']['vid'];
    $this->assertEquals('integer', $context->getDataType(), 'Context definition data type is correct.');
    $this->assertEquals('Content: Revision ID', $context->getLabel(), 'Context definition label is correct.');
    $this->assertFalse($context->isRequired(), 'Context is not required.');
    $this->assertInstanceOf(ContextDefinitionInterface::class, $definition['context_definitions']['title']);
    
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
    $context = $definition['context_definitions']['title'];
    $this->assertEquals('string', $context->getDataType(), 'Context definition data type is correct.');
    $this->assertEquals('Content: Title', $context->getLabel(), 'Context definition label is correct.');
    $this->assertFalse($context->isRequired(), 'Context is not required.');
}

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