7dead3d32e230afe92145ffa451adcc8127c569c
[yaffs-website] / web / core / tests / 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       ->setMethods(['validateTokens'])
108       ->getMock();
109     $container->set('cache_contexts_manager', $cache_context_manager);
110     $cache_context_manager->expects($this->any())
111       ->method('validateTokens')
112       ->with(['route'])
113       ->willReturn(['route']);
114     \Drupal::setContainer($container);
115
116     $this->contextDefinition = $this->getMock('Drupal\Core\Plugin\Context\ContextDefinitionInterface');
117
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')
129       ->willReturn(60);
130
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());
136   }
137
138   /**
139    * Set up mocks for the getDefaultValue() method call.
140    *
141    * @param mixed $default_value
142    *   The default value to assign to the mock context definition.
143    */
144   protected function setUpDefaultValue($default_value = NULL) {
145     $mock_data_definition = $this->getMock('Drupal\Core\TypedData\DataDefinitionInterface');
146
147     $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
148       ->setMethods(['getDefaultValue', 'getDataDefinition'])
149       ->getMockForAbstractClass();
150
151     $this->contextDefinition->expects($this->once())
152       ->method('getDefaultValue')
153       ->willReturn($default_value);
154
155     $this->contextDefinition->expects($this->once())
156       ->method('getDataDefinition')
157       ->willReturn($mock_data_definition);
158
159     $this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
160
161     $this->typedDataManager->expects($this->once())
162       ->method('create')
163       ->with($mock_data_definition, $default_value)
164       ->willReturn($this->typedData);
165   }
166
167 }
168
169 /**
170  * Test interface used for mocking.
171  */
172 interface TypedDataCacheableDependencyInterface extends CacheableDependencyInterface, TypedDataInterface {}