ContextHandlerTest.php

Same filename in this branch
  1. 10 core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php
Same filename in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php
  2. 9 core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php
  4. 8.9.x core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php
  5. 11.x core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php
  6. 11.x core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php

Namespace

Drupal\KernelTests\Core\Plugin

File

core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\KernelTests\Core\Plugin;

use Drupal\Core\Plugin\Context\ContextHandler;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;

/**
 * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextHandler
 *
 * @group Plugin
 */
class ContextHandlerTest extends KernelTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'entity_test',
        'user',
    ];
    
    /**
     * @covers ::applyContextMapping
     */
    public function testApplyContextMapping() : void {
        $entity = EntityTest::create([]);
        $context_definition = EntityContextDefinition::fromEntity($entity);
        $context = EntityContext::fromEntity($entity);
        $definition = [
            'context_definitions' => [
                'a_context_id' => $context_definition,
            ],
        ];
        $plugin = new TestContextAwarePlugin([], 'test_plugin_id', $definition);
        (new ContextHandler())->applyContextMapping($plugin, [
            'a_context_id' => $context,
        ]);
        $result = $plugin->getContext('a_context_id');
        $this->assertInstanceOf(EntityContext::class, $result);
        $this->assertSame($context, $result);
    }
    
    /**
     * @covers ::applyContextMapping
     */
    public function testApplyContextMappingAlreadyApplied() : void {
        $entity = EntityTest::create([]);
        $context_definition = EntityContextDefinition::fromEntity($entity);
        $context = EntityContext::fromEntity($entity);
        $definition = [
            'context_definitions' => [
                'a_context_id' => $context_definition,
            ],
        ];
        $plugin = new TestContextAwarePlugin([], 'test_plugin_id', $definition);
        $plugin->setContext('a_context_id', $context);
        (new ContextHandler())->applyContextMapping($plugin, []);
        $result = $plugin->getContext('a_context_id');
        $this->assertInstanceOf(EntityContext::class, $result);
        $this->assertSame($context, $result);
    }

}

/**
 * Provides a test implementation of a context-aware plugin.
 */
class TestContextAwarePlugin extends PluginBase implements ContextAwarePluginInterface {
    use ContextAwarePluginTrait;

}

Classes

Title Deprecated Summary
ContextHandlerTest @coversDefaultClass \Drupal\Core\Plugin\Context\ContextHandler
TestContextAwarePlugin Provides a test implementation of a context-aware plugin.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.