ContextualFiltersBlockContextTest.php 5.89 KB
Newer Older
1 2
<?php

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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
use Drupal\views\Tests\ViewTestData;

/**
 * A test for contextual filters exposed as block context.
 *
 * @group views
 */
class ContextualFiltersBlockContextTest extends ViewTestBase {

  use AssertPageCacheContextsAndTagsTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = ['block', 'block_test_views', 'views_ui', 'node'];
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  /**
   * Views used by this test.
   *
   * @var array
   */
  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}
   */
50 51
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    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();
  }

  /**
   * Tests exposed context.
   */
  public function testBlockContext() {
    $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');
84
    $this->assertTrue($definition['context_definitions']['nid'] instanceof ContextDefinitionInterface);
85
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
86
    $context = $definition['context_definitions']['nid'];
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    $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',
113
      'context_mapping' => ['nid' => '@node.node_route_context:node'],
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    ];
    $this->assertEqual($block->getPlugin()->getConfiguration(), $expected_settings, 'Block settings are correct.');

    // Make sure view behaves as expected.
    $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');

    // 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');
131
    $this->assertTrue($definition['context_definitions']['created'] instanceof ContextDefinitionInterface);
132
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
133
    $context = $definition['context_definitions']['created'];
134 135 136 137
    $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.');

138
    $this->assertTrue($definition['context_definitions']['vid'] instanceof ContextDefinitionInterface);
139
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
140
    $context = $definition['context_definitions']['vid'];
141 142 143 144
    $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.');

145
    $this->assertTrue($definition['context_definitions']['title'] instanceof ContextDefinitionInterface);
146
    /** @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context */
147
    $context = $definition['context_definitions']['title'];
148 149 150 151 152 153
    $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.');
  }

}