function ContextHandlerTest::testApplyContextMapping

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php \Drupal\KernelTests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php \Drupal\KernelTests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  2. 9 core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php \Drupal\Tests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php \Drupal\KernelTests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  4. 8.9.x core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php \Drupal\Tests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  5. 10 core/tests/Drupal/KernelTests/Core/Plugin/ContextHandlerTest.php \Drupal\KernelTests\Core\Plugin\ContextHandlerTest::testApplyContextMapping()
  6. 10 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 302

Class

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

Namespace

Drupal\Tests\Core\Plugin

Code

public function testApplyContextMapping() : void {
    $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')
        ->willReturn([
        '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);
}

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