215ee2890f51c157e00d4824bb0c3361c141c843
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / ContextualFiltersBlockContextTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
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;
9
10 /**
11  * A test for contextual filters exposed as block context.
12  *
13  * @group views
14  */
15 class ContextualFiltersBlockContextTest extends ViewTestBase {
16
17   use AssertPageCacheContextsAndTagsTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['block', 'block_test_views', 'views_ui', 'node'];
25
26   /**
27    * Views used by this test.
28    *
29    * @var array
30    */
31   public static $testViews = ['test_view_block_with_context'];
32
33   /**
34    * Test node type.
35    *
36    * @var \Drupal\node\NodeTypeInterface
37    */
38   protected $nodeType;
39
40   /**
41    * Test nodes.
42    *
43    * @var \Drupal\node\NodeInterface[]
44    */
45   protected $nodes;
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp($import_test_views = TRUE) {
51     parent::setUp($import_test_views);
52
53     ViewTestData::createTestViews(get_class($this), ['block_test_views']);
54     $this->enableViewsTestModule();
55
56     $this->nodeType = $this->container->get('entity_type.manager')
57       ->getStorage('node_type')
58       ->create([
59         'name' => 'Test node type',
60         'type' => 'test',
61       ]);
62     $this->nodeType->save();
63
64     $this->nodes[0] = $this->container->get('entity_type.manager')
65       ->getStorage('node')
66       ->create(['type' => $this->nodeType->id(), 'title' => 'First test node']);
67     $this->nodes[0]->save();
68
69     $this->nodes[1] = $this->container->get('entity_type.manager')
70       ->getStorage('node')
71       ->create(['type' => $this->nodeType->id(), 'title' => 'Second test node']);
72     $this->nodes[1]->save();
73   }
74
75   /**
76    * Tests exposed context.
77    */
78   public function testBlockContext() {
79     $this->drupalLogin($this->drupalCreateUser(['administer views', 'administer blocks']));
80
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.');
90
91     // Place test block via block UI to check if contexts are correctly exposed.
92     $this->drupalGet(
93       'admin/structure/block/add/views_block:test_view_block_with_context-block_1/classy',
94       ['query' => ['region' => 'content']]
95     );
96     $edit = [
97       'settings[context_mapping][nid]' => '@node.node_route_context:node',
98     ];
99     $this->drupalPostForm(NULL, $edit, 'Save block');
100
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',
108       'label' => '',
109       'provider' => 'views',
110       'label_display' => 'visible',
111       'views_label' => '',
112       'items_per_page' => 'none',
113       'context_mapping' => ['nid' => '@node.node_route_context:node']
114     ];
115     $this->assertEqual($block->getPlugin()->getConfiguration(), $expected_settings, 'Block settings are correct.');
116
117     // Make sure view behaves as expected.
118     $this->drupalGet('<front>');
119     $this->assertText('Test view: No results found.');
120
121     $this->drupalGet($this->nodes[0]->toUrl());
122     $this->assertText('Test view row: First test node');
123
124     $this->drupalGet($this->nodes[1]->toUrl());
125     $this->assertText('Test view row: Second test node');
126
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.');
137
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.');
144
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.');
151   }
152
153 }