View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class ContextualFiltersBlockContextTest extends ViewTestBase {
use AssertPageCacheContextsAndTagsTrait;
public static $modules = [
'block',
'block_test_views',
'views_ui',
'node',
];
protected $defaultTheme = 'classy';
public static $testViews = [
'test_view_block_with_context',
];
protected $nodeType;
protected $nodes;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), [
'block_test_views',
]);
$this
->enableViewsTestModule();
$this->nodeType = $this->container
->get('entity_type.manager')
->getStorage('node_type')
->create([
'name' => 'Test node type',
'type' => 'test',
]);
$this->nodeType
->save();
$this->nodes[0] = $this->container
->get('entity_type.manager')
->getStorage('node')
->create([
'type' => $this->nodeType
->id(),
'title' => 'First test node',
]);
$this->nodes[0]
->save();
$this->nodes[1] = $this->container
->get('entity_type.manager')
->getStorage('node')
->create([
'type' => $this->nodeType
->id(),
'title' => 'Second test node',
]);
$this->nodes[1]
->save();
}
public function testBlockContext() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer views',
'administer blocks',
]));
$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']);
$context = $definition['context_definitions']['nid'];
$this
->assertEqual($context
->getDataType(), 'entity:node', 'Context definition data type is correct.');
$this
->assertEqual($context
->getLabel(), 'Content: ID', 'Context definition label is correct.');
$this
->assertFalse($context
->isRequired(), 'Context is not required.');
$this
->drupalGet('admin/structure/block/add/views_block:test_view_block_with_context-block_1/classy', [
'query' => [
'region' => 'content',
],
]);
$edit = [
'settings[context_mapping][nid]' => '@node.node_route_context:node',
];
$this
->drupalPostForm(NULL, $edit, 'Save block');
$block = $this->container
->get('entity_type.manager')
->getStorage('block')
->load('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
->assertEqual($block
->getPlugin()
->getConfiguration(), $expected_settings, 'Block settings are correct.');
$this
->drupalGet('<front>');
$this
->assertText('Test view: No results found.');
$this
->drupalGet($this->nodes[0]
->toUrl());
$this
->assertText('Test view row: First test node');
$this
->drupalGet($this->nodes[1]
->toUrl());
$this
->assertText('Test view row: Second test node');
$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']);
$context = $definition['context_definitions']['created'];
$this
->assertEqual($context
->getDataType(), 'integer', 'Context definition data type is correct.');
$this
->assertEqual($context
->getLabel(), 'Content: Authored on', 'Context definition label is correct.');
$this
->assertFalse($context
->isRequired(), 'Context is not required.');
$this
->assertInstanceOf(ContextDefinitionInterface::class, $definition['context_definitions']['vid']);
$context = $definition['context_definitions']['vid'];
$this
->assertEqual($context
->getDataType(), 'integer', 'Context definition data type is correct.');
$this
->assertEqual($context
->getLabel(), 'Content: Revision ID', 'Context definition label is correct.');
$this
->assertFalse($context
->isRequired(), 'Context is not required.');
$this
->assertInstanceOf(ContextDefinitionInterface::class, $definition['context_definitions']['title']);
$context = $definition['context_definitions']['title'];
$this
->assertEqual($context
->getDataType(), 'string', 'Context definition data type is correct.');
$this
->assertEqual($context
->getLabel(), 'Content: Title', 'Context definition label is correct.');
$this
->assertFalse($context
->isRequired(), 'Context is not required.');
}
}