3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
6 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
7 use Drupal\Tests\views\Functional\ViewTestBase;
8 use Drupal\views\Tests\ViewTestData;
11 * A test for contextual filters exposed as block context.
15 class ContextualFiltersBlockContextTest extends ViewTestBase {
17 use AssertPageCacheContextsAndTagsTrait;
24 public static $modules = ['block', 'block_test_views', 'views_ui', 'node'];
27 * Views used by this test.
31 public static $testViews = ['test_view_block_with_context'];
36 * @var \Drupal\node\NodeTypeInterface
43 * @var \Drupal\node\NodeInterface[]
50 protected function setUp($import_test_views = TRUE) {
51 parent::setUp($import_test_views);
53 ViewTestData::createTestViews(get_class($this), ['block_test_views']);
54 $this->enableViewsTestModule();
56 $this->nodeType = $this->container->get('entity_type.manager')
57 ->getStorage('node_type')
59 'name' => 'Test node type',
62 $this->nodeType->save();
64 $this->nodes[0] = $this->container->get('entity_type.manager')
66 ->create(['type' => $this->nodeType->id(), 'title' => 'First test node']);
67 $this->nodes[0]->save();
69 $this->nodes[1] = $this->container->get('entity_type.manager')
71 ->create(['type' => $this->nodeType->id(), 'title' => 'Second test node']);
72 $this->nodes[1]->save();
76 * Tests exposed context.
78 public function testBlockContext() {
79 $this->drupalLogin($this->drupalCreateUser(['administer views', 'administer blocks']));
81 // Check if context was correctly propagated to the block.
82 $definition = $this->container->get('plugin.manager.block')
83 ->getDefinition('views_block:test_view_block_with_context-block_1');
84 $this->assertTrue($definition['context']['nid'] instanceof ContextDefinitionInterface);
85 /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
86 $context = $definition['context']['nid'];
87 $this->assertEqual($context->getDataType(), 'entity:node', 'Context definition data type is correct.');
88 $this->assertEqual($context->getLabel(), 'Content: ID', 'Context definition label is correct.');
89 $this->assertFalse($context->isRequired(), 'Context is not required.');
91 // Place test block via block UI to check if contexts are correctly exposed.
93 'admin/structure/block/add/views_block:test_view_block_with_context-block_1/classy',
94 ['query' => ['region' => 'content']]
97 'settings[context_mapping][nid]' => '@node.node_route_context:node',
99 $this->drupalPostForm(NULL, $edit, 'Save block');
101 // Check if mapping saved correctly.
102 /** @var \Drupal\block\BlockInterface $block */
103 $block = $this->container->get('entity_type.manager')
104 ->getStorage('block')
105 ->load('views_block__test_view_block_with_context_block_1');
106 $expected_settings = [
107 'id' => 'views_block:test_view_block_with_context-block_1',
109 'provider' => 'views',
110 'label_display' => 'visible',
112 'items_per_page' => 'none',
113 'context_mapping' => ['nid' => '@node.node_route_context:node'],
115 $this->assertEqual($block->getPlugin()->getConfiguration(), $expected_settings, 'Block settings are correct.');
117 // Make sure view behaves as expected.
118 $this->drupalGet('<front>');
119 $this->assertText('Test view: No results found.');
121 $this->drupalGet($this->nodes[0]->toUrl());
122 $this->assertText('Test view row: First test node');
124 $this->drupalGet($this->nodes[1]->toUrl());
125 $this->assertText('Test view row: Second test node');
127 // Check the second block which should expose two integer contexts, one
128 // based on the numeric plugin and the other based on numeric validation.
129 $definition = $this->container->get('plugin.manager.block')
130 ->getDefinition('views_block:test_view_block_with_context-block_2');
131 $this->assertTrue($definition['context']['created'] instanceof ContextDefinitionInterface);
132 /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
133 $context = $definition['context']['created'];
134 $this->assertEqual($context->getDataType(), 'integer', 'Context definition data type is correct.');
135 $this->assertEqual($context->getLabel(), 'Content: Authored on', 'Context definition label is correct.');
136 $this->assertFalse($context->isRequired(), 'Context is not required.');
138 $this->assertTrue($definition['context']['vid'] instanceof ContextDefinitionInterface);
139 /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
140 $context = $definition['context']['vid'];
141 $this->assertEqual($context->getDataType(), 'integer', 'Context definition data type is correct.');
142 $this->assertEqual($context->getLabel(), 'Content: Revision ID', 'Context definition label is correct.');
143 $this->assertFalse($context->isRequired(), 'Context is not required.');
145 $this->assertTrue($definition['context']['title'] instanceof ContextDefinitionInterface);
146 /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
147 $context = $definition['context']['title'];
148 $this->assertEqual($context->getDataType(), 'string', 'Context definition data type is correct.');
149 $this->assertEqual($context->getLabel(), 'Content: Title', 'Context definition label is correct.');
150 $this->assertFalse($context->isRequired(), 'Context is not required.');