You are here

public function ContextHandlerTest::testApplyContextMapping in Drupal 10

Same name in this branch
  1. 10 core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php \Drupal\Tests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  2. 10 core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php \Drupal\KernelTests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php \Drupal\Tests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  2. 9 core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php \Drupal\Tests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()

@covers ::applyContextMapping

File

core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php, line 315
Contains \Drupal\Tests\Core\Plugin\ContextHandlerTest.

Class

ContextHandlerTest
@coversDefaultClass \Drupal\Core\Plugin\Context\ContextHandler @group Plugin

Namespace

Drupal\Tests\Core\Plugin

Code

public function testApplyContextMapping() {
  $context_hit = $this
    ->createMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
  $context_hit
    ->expects($this
    ->atLeastOnce())
    ->method('hasContextValue')
    ->willReturn(TRUE);
  $context_miss = $this
    ->createMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
  $contexts = [
    'hit' => $context_hit,
    'miss' => $context_miss,
  ];
  $context_definition = $this
    ->createMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
  $plugin = $this
    ->createMock('Drupal\\Core\\Plugin\\ContextAwarePluginInterface');
  $plugin
    ->expects($this
    ->once())
    ->method('getContextMapping')
    ->willReturn([]);
  $plugin
    ->expects($this
    ->once())
    ->method('getContextDefinitions')
    ->will($this
    ->returnValue([
    'hit' => $context_definition,
  ]));
  $plugin
    ->expects($this
    ->once())
    ->method('setContext')
    ->with('hit', $context_hit);

  // Make sure that the cacheability metadata is passed to the plugin context.
  $plugin_context = $this
    ->createMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
  $plugin_context
    ->expects($this
    ->once())
    ->method('addCacheableDependency')
    ->with($context_hit);
  $plugin
    ->expects($this
    ->once())
    ->method('getContext')
    ->with('hit')
    ->willReturn($plugin_context);
  $this->contextHandler
    ->applyContextMapping($plugin, $contexts);
}