2190d5391878a071362afe8e3e1b781fb0c0d165
[yaffs-website] / Drupal / Tests / Core / Plugin / Context / ContextTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Plugin\Context\ContextTest.
6  */
7
8 namespace Drupal\Tests\Core\Plugin\Context;
9
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;
16
17 /**
18  * @coversDefaultClass \Drupal\Core\Plugin\Context\Context
19  * @group Plugin
20  */
21 class ContextTest extends UnitTestCase {
22
23   /**
24    * The mocked context definition object.
25    *
26    * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $contextDefinition;
29
30   /**
31    * The mocked Typed Data manager.
32    *
33    * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
34    */
35   protected $typedDataManager;
36
37   /**
38    * The mocked Typed Data object.
39    *
40    * @var \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit_Framework_MockObject_MockObject
41    */
42   protected $typedData;
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUp() {
48     parent::setUp();
49
50     $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
51   }
52
53   /**
54    * @covers ::getContextValue
55    */
56   public function testDefaultValue() {
57     $this->setUpDefaultValue('test');
58
59     $context = new Context($this->contextDefinition);
60     $context->setTypedDataManager($this->typedDataManager);
61     $this->assertEquals('test', $context->getContextValue());
62   }
63
64   /**
65    * @covers ::getContextData
66    */
67   public function testDefaultDataValue() {
68     $this->setUpDefaultValue('test');
69
70     $context = new Context($this->contextDefinition);
71     $context->setTypedDataManager($this->typedDataManager);
72     $this->assertEquals($this->typedData, $context->getContextData());
73   }
74
75   /**
76    * @covers ::getContextData
77    */
78   public function testNullDataValue() {
79     $this->setUpDefaultValue(NULL);
80
81     $context = new Context($this->contextDefinition);
82     $context->setTypedDataManager($this->typedDataManager);
83     $this->assertEquals($this->typedData, $context->getContextData());
84   }
85
86   /**
87    * @covers ::setContextValue
88    */
89   public function testSetContextValueTypedData() {
90
91     $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
92       ->setMethods(['getDefaultValue', 'getDataDefinition'])
93       ->getMockForAbstractClass();
94
95     $typed_data = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
96     $context = new Context($this->contextDefinition, $typed_data);
97     $this->assertSame($typed_data, $context->getContextData());
98   }
99
100   /**
101    * @covers ::setContextValue
102    */
103   public function testSetContextValueCacheableDependency() {
104     $container = new Container();
105     $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\CacheContextsManager')
106       ->disableOriginalConstructor()
107       ->getMock();
108     $container->set('cache_contexts_manager', $cache_context_manager);
109     $cache_context_manager->expects($this->any())
110       ->method('validateTokens')
111       ->with(['route'])
112       ->willReturn(['route']);
113     \Drupal::setContainer($container);
114
115     $this->contextDefinition = $this->getMock('Drupal\Core\Plugin\Context\ContextDefinitionInterface');
116
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')
128       ->willReturn(60);
129
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());
135   }
136
137   /**
138    * Set up mocks for the getDefaultValue() method call.
139    *
140    * @param mixed $default_value
141    *   The default value to assign to the mock context definition.
142    */
143   protected function setUpDefaultValue($default_value = NULL) {
144     $mock_data_definition = $this->getMock('Drupal\Core\TypedData\DataDefinitionInterface');
145
146     $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
147       ->setMethods(['getDefaultValue', 'getDataDefinition'])
148       ->getMockForAbstractClass();
149
150     $this->contextDefinition->expects($this->once())
151       ->method('getDefaultValue')
152       ->willReturn($default_value);
153
154     $this->contextDefinition->expects($this->once())
155       ->method('getDataDefinition')
156       ->willReturn($mock_data_definition);
157
158     $this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
159
160     $this->typedDataManager->expects($this->once())
161       ->method('create')
162       ->with($mock_data_definition, $default_value)
163       ->willReturn($this->typedData);
164   }
165
166 }
167
168 /**
169  * Test interface used for mocking.
170  */
171 interface TypedDataCacheableDependencyInterface extends CacheableDependencyInterface, TypedDataInterface {}