diff --git a/core/modules/block/tests/modules/block_test_views/test_views/views.view.test_view_block_with_context.yml b/core/modules/block/tests/modules/block_test_views/test_views/views.view.test_view_block_with_context.yml index 5eaf8c3..bbd8480 100644 --- a/core/modules/block/tests/modules/block_test_views/test_views/views.view.test_view_block_with_context.yml +++ b/core/modules/block/tests/modules/block_test_views/test_views/views.view.test_view_block_with_context.yml @@ -58,17 +58,14 @@ display: id: title table: node_field_data field: title - settings: - link_to_entity: true - plugin_id: field relationship: none group_type: group admin_label: '' label: '' exclude: false alter: - alter_text: false - text: '' + alter_text: true + text: 'Test view row: {{ title }}' make_link: false path: '' absolute: false @@ -97,7 +94,7 @@ display: element_class: '' element_label_type: '' element_label_class: '' - element_label_colon: true + element_label_colon: false element_wrapper_type: '' element_wrapper_class: '' element_default_classes: true @@ -107,6 +104,8 @@ display: hide_alter_empty: true click_sort_column: value type: string + settings: + link_to_entity: true group_column: value group_columns: { } group_rows: true @@ -117,6 +116,7 @@ display: multi_type: separator separator: ', ' field_api_classes: false + plugin_id: field filters: status: value: true @@ -148,17 +148,28 @@ display: title: test_view_block_with_context header: { } footer: { } - empty: { } + empty: + area_text_custom: + id: area_text_custom + table: views + field: area_text_custom + relationship: none + group_type: group + admin_label: '' + empty: true + tokenize: false + content: 'Test view: No results found.' + plugin_id: text_custom relationships: { } arguments: nid: id: nid - table: node + table: node_field_data field: nid relationship: none group_type: group admin_label: '' - default_action: ignore + default_action: empty exception: value: all title_enable: false @@ -191,7 +202,7 @@ display: not: false entity_type: node entity_field: nid - plugin_id: numeric + plugin_id: node_nid display_extenders: { } cache_metadata: contexts: @@ -201,6 +212,8 @@ display: - 'user.node_grants:view' - user.permissions cacheable: false + max-age: -1 + tags: { } block_1: display_plugin: block id: block_1 @@ -216,3 +229,5 @@ display: - 'user.node_grants:view' - user.permissions cacheable: false + max-age: -1 + tags: { } diff --git a/core/modules/views/src/Tests/Plugin/ContextualFiltersBlockContextTest.php b/core/modules/views/src/Tests/Plugin/ContextualFiltersBlockContextTest.php index 4e106c0..f53f769 100644 --- a/core/modules/views/src/Tests/Plugin/ContextualFiltersBlockContextTest.php +++ b/core/modules/views/src/Tests/Plugin/ContextualFiltersBlockContextTest.php @@ -2,6 +2,7 @@ namespace Drupal\views\Tests\Plugin; +use Drupal\Core\Plugin\Context\ContextDefinitionInterface; use Drupal\views\Tests\ViewTestData; use Drupal\views\Tests\ViewTestBase; use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; @@ -30,6 +31,20 @@ class ContextualFiltersBlockContextTest extends ViewTestBase { public static $testViews = ['test_view_block_with_context']; /** + * Test node type. + * + * @var \Drupal\node\NodeTypeInterface + */ + protected $nodeType; + + /** + * Test nodes. + * + * @var \Drupal\node\NodeInterface[] + */ + protected $nodes; + + /** * {@inheritdoc} */ protected function setUp() { @@ -37,6 +52,24 @@ protected function setUp() { 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(); } /** @@ -44,13 +77,52 @@ protected function setUp() { */ public function testBlockContext() { $this->drupalLogin($this->drupalCreateUser(['administer views', 'administer blocks'])); - $this->drupalGet('admin/structure/block'); - $this->clickLinkPartialName('Place block'); - $this->drupalPlaceBlock('views_block:test_view_block_with_context-block_1', ['label' => 'test_view_block_with_context-block_1:1']); - - $definition = \Drupal::service('plugin.manager.block')->getDefinition('views_block:test_view_block_with_context-block_1'); + // 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->assertTrue($definition['context']['nid']); + /** @var ContextDefinitionInterface $context */ + $context = $definition['context']['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.'); + + // 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/classy', + ['query' => ['region' => 'content']] + ); + $edit = [ + 'settings[context_mapping][nid]' => '@node.node_route_context:node', + ]; + $this->drupalPostForm(NULL, $edit, 'Save block'); + + // Check if mapping saved correctly. + /** @var \Drupal\block\BlockInterface $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.'); + + // Make sure view behaves as expected. + $this->drupalGet(''); + $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'); } }