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()
107 ->setMethods(['validateTokens'])
109 $container->set('cache_contexts_manager', $cache_context_manager);
110 $cache_context_manager->expects($this->any())
111 ->method('validateTokens')
113 ->willReturn(['route']);
114 \Drupal::setContainer($container);
116 $this->contextDefinition = $this->getMock('Drupal\Core\Plugin\Context\ContextDefinitionInterface');
118 $context = new Context($this->contextDefinition);
119 $context->setTypedDataManager($this->typedDataManager);
120 $cacheable_dependency = $this->getMock('Drupal\Tests\Core\Plugin\Context\TypedDataCacheableDependencyInterface');
121 $cacheable_dependency->expects($this->once())
122 ->method('getCacheTags')
123 ->willReturn(['node:1']);
124 $cacheable_dependency->expects($this->once())
125 ->method('getCacheContexts')
126 ->willReturn(['route']);
127 $cacheable_dependency->expects($this->once())
128 ->method('getCacheMaxAge')
131 $context = Context::createFromContext($context, $cacheable_dependency);
132 $this->assertSame($cacheable_dependency, $context->getContextData());
133 $this->assertEquals(['node:1'], $context->getCacheTags());
134 $this->assertEquals(['route'], $context->getCacheContexts());
135 $this->assertEquals(60, $context->getCacheMaxAge());
139 * Set up mocks for the getDefaultValue() method call.
141 * @param mixed $default_value
142 * The default value to assign to the mock context definition.
144 protected function setUpDefaultValue($default_value = NULL) {
145 $mock_data_definition = $this->getMock('Drupal\Core\TypedData\DataDefinitionInterface');
147 $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
148 ->setMethods(['getDefaultValue', 'getDataDefinition'])
149 ->getMockForAbstractClass();
151 $this->contextDefinition->expects($this->once())
152 ->method('getDefaultValue')
153 ->willReturn($default_value);
155 $this->contextDefinition->expects($this->once())
156 ->method('getDataDefinition')
157 ->willReturn($mock_data_definition);
159 $this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
161 $this->typedDataManager->expects($this->once())
163 ->with($mock_data_definition, $default_value)
164 ->willReturn($this->typedData);
170 * Test interface used for mocking.
172 interface TypedDataCacheableDependencyInterface extends CacheableDependencyInterface, TypedDataInterface {}