You are here

public function ContextHandlerTest::testApplyContextMapping in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 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 233
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_data = StringData::createInstance(DataDefinition::create('string'));
  $context_hit_data
    ->setValue('foo');
  $context_hit = $this
    ->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
  $context_hit
    ->expects($this
    ->atLeastOnce())
    ->method('getContextData')
    ->will($this
    ->returnValue($context_hit_data));
  $context_miss_data = StringData::createInstance(DataDefinition::create('string'));
  $context_miss_data
    ->setValue('bar');
  $context_hit
    ->expects($this
    ->atLeastOnce())
    ->method('hasContextValue')
    ->willReturn(TRUE);
  $context_miss = $this
    ->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
  $context_miss
    ->expects($this
    ->never())
    ->method('getContextData');
  $contexts = array(
    'hit' => $context_hit,
    'miss' => $context_miss,
  );
  $context_definition = $this
    ->getMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
  $plugin = $this
    ->getMock('Drupal\\Core\\Plugin\\ContextAwarePluginInterface');
  $plugin
    ->expects($this
    ->once())
    ->method('getContextMapping')
    ->willReturn([]);
  $plugin
    ->expects($this
    ->once())
    ->method('getContextDefinitions')
    ->will($this
    ->returnValue(array(
    'hit' => $context_definition,
  )));
  $plugin
    ->expects($this
    ->once())
    ->method('setContextValue')
    ->with('hit', $context_hit_data);

  // Make sure that the cacheability metadata is passed to the plugin context.
  $plugin_context = $this
    ->getMock('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);
}