5 * Contains \Drupal\Tests\Core\Plugin\Context\ContextTest.
8 namespace Drupal\Tests\Core\Plugin\Context;
10 use Drupal\Core\Cache\CacheableDependencyInterface;
11 use Drupal\Core\Plugin\Context\Context;
12 use Drupal\Core\TypedData\TypedDataInterface;
13 use Drupal\Core\TypedData\TypedDataManagerInterface;
14 use Drupal\Tests\UnitTestCase;
15 use Symfony\Component\DependencyInjection\Container;
18 * @coversDefaultClass \Drupal\Core\Plugin\Context\Context
21 class ContextTest extends UnitTestCase {
24 * The mocked context definition object.
26 * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $contextDefinition;
31 * The mocked Typed Data manager.
33 * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
35 protected $typedDataManager;
38 * The mocked Typed Data object.
40 * @var \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit_Framework_MockObject_MockObject
47 protected function setUp() {
50 $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
54 * @covers ::getContextValue
56 public function testDefaultValue() {
57 $this->setUpDefaultValue('test');
59 $context = new Context($this->contextDefinition);
60 $context->setTypedDataManager($this->typedDataManager);
61 $this->assertEquals('test', $context->getContextValue());
65 * @covers ::getContextData
67 public function testDefaultDataValue() {
68 $this->setUpDefaultValue('test');
70 $context = new Context($this->contextDefinition);
71 $context->setTypedDataManager($this->typedDataManager);
72 $this->assertEquals($this->typedData, $context->getContextData());
76 * @covers ::getContextData
78 public function testNullDataValue() {
79 $this->setUpDefaultValue(NULL);
81 $context = new Context($this->contextDefinition);
82 $context->setTypedDataManager($this->typedDataManager);
83 $this->assertEquals($this->typedData, $context->getContextData());
87 * @covers ::setContextValue
89 public function testSetContextValueTypedData() {
91 $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
92 ->setMethods(['getDefaultValue', 'getDataDefinition'])
93 ->getMockForAbstractClass();
95 $typed_data = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
96 $context = new Context($this->contextDefinition, $typed_data);
97 $this->assertSame($typed_data, $context->getContextData());
101 * @covers ::setContextValue
103 public function testSetContextValueCacheableDependency() {
104 $container = new Container();
105 $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\CacheContextsManager')
106 ->disableOriginalConstructor()
108 $container->set('cache_contexts_manager', $cache_context_manager);
109 $cache_context_manager->expects($this->any())
110 ->method('validateTokens')
112 ->willReturn(['route']);
113 \Drupal::setContainer($container);
115 $this->contextDefinition = $this->getMock('Drupal\Core\Plugin\Context\ContextDefinitionInterface');
117 $context = new Context($this->contextDefinition);
118 $context->setTypedDataManager($this->typedDataManager);
119 $cacheable_dependency = $this->getMock('Drupal\Tests\Core\Plugin\Context\TypedDataCacheableDependencyInterface');
120 $cacheable_dependency->expects($this->once())
121 ->method('getCacheTags')
122 ->willReturn(['node:1']);
123 $cacheable_dependency->expects($this->once())
124 ->method('getCacheContexts')
125 ->willReturn(['route']);
126 $cacheable_dependency->expects($this->once())
127 ->method('getCacheMaxAge')
130 $context = Context::createFromContext($context, $cacheable_dependency);
131 $this->assertSame($cacheable_dependency, $context->getContextData());
132 $this->assertEquals(['node:1'], $context->getCacheTags());
133 $this->assertEquals(['route'], $context->getCacheContexts());
134 $this->assertEquals(60, $context->getCacheMaxAge());
138 * Set up mocks for the getDefaultValue() method call.
140 * @param mixed $default_value
141 * The default value to assign to the mock context definition.
143 protected function setUpDefaultValue($default_value = NULL) {
144 $mock_data_definition = $this->getMock('Drupal\Core\TypedData\DataDefinitionInterface');
146 $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
147 ->setMethods(['getDefaultValue', 'getDataDefinition'])
148 ->getMockForAbstractClass();
150 $this->contextDefinition->expects($this->once())
151 ->method('getDefaultValue')
152 ->willReturn($default_value);
154 $this->contextDefinition->expects($this->once())
155 ->method('getDataDefinition')
156 ->willReturn($mock_data_definition);
158 $this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
160 $this->typedDataManager->expects($this->once())
162 ->with($mock_data_definition, $default_value)
163 ->willReturn($this->typedData);
169 * Test interface used for mocking.
171 interface TypedDataCacheableDependencyInterface extends CacheableDependencyInterface, TypedDataInterface {}