e6ad236a7226d61ca2cd55f87161db8c76593e02
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Context / ContextDefinitionTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Context;
4
5 use Drupal\Core\Plugin\Context\ContextDefinition;
6 use Drupal\Core\TypedData\TypedDataManagerInterface;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * Tests the ContextDefinition class.
11  *
12  * @group Plugin
13  *
14  * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
15  */
16 class ContextDefinitionTest extends UnitTestCase {
17
18   /**
19    * Very simple data provider.
20    */
21   public function providerGetDataDefinition() {
22     return [
23       [TRUE],
24       [FALSE],
25     ];
26   }
27
28   /**
29    * @dataProvider providerGetDataDefinition
30    * @covers ::getDataDefinition
31    * @uses \Drupal
32    */
33   public function testGetDataDefinition($is_multiple) {
34     $data_type = 'valid';
35     $mock_data_definition = $this->getMockBuilder('\Drupal\Core\TypedData\ListDataDefinitionInterface')
36       ->setMethods([
37         'setLabel',
38         'setDescription',
39         'setRequired',
40         'getConstraints',
41         'setConstraints',
42       ])
43       ->getMockForAbstractClass();
44     $mock_data_definition->expects($this->once())
45       ->method('setLabel')
46       ->willReturnSelf();
47     $mock_data_definition->expects($this->once())
48       ->method('setDescription')
49       ->willReturnSelf();
50     $mock_data_definition->expects($this->once())
51       ->method('setRequired')
52       ->willReturnSelf();
53     $mock_data_definition->expects($this->once())
54       ->method('getConstraints')
55       ->willReturn([]);
56     $mock_data_definition->expects($this->once())
57       ->method('setConstraints')
58       ->willReturn(NULL);
59
60     // Follow code paths for both multiple and non-multiple definitions.
61     $create_definition_method = 'createDataDefinition';
62     if ($is_multiple) {
63       $create_definition_method = 'createListDataDefinition';
64     }
65     $mock_data_manager = $this->getMock(TypedDataManagerInterface::class);
66     // Our mocked data manager will return our mocked data definition for a
67     // valid data type.
68     $mock_data_manager->expects($this->once())
69       ->method($create_definition_method)
70       ->willReturnMap([
71         ['not_valid', NULL],
72         ['valid', $mock_data_definition],
73       ]);
74
75     // Mock a ContextDefinition object, setting up expectations for many of the
76     // methods.
77     $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
78       ->disableOriginalConstructor()
79       ->setMethods([
80         'isMultiple',
81         'getTypedDataManager',
82         'getDataType',
83         'getLabel',
84         'getDescription',
85         'isRequired',
86         'getConstraints',
87         'setConstraints',
88       ])
89       ->getMock();
90     $mock_context_definition->expects($this->once())
91       ->method('isMultiple')
92       ->willReturn($is_multiple);
93     $mock_context_definition->expects($this->once())
94       ->method('getTypedDataManager')
95       ->willReturn($mock_data_manager);
96     $mock_context_definition->expects($this->once())
97       ->method('getDataType')
98       ->willReturn($data_type);
99     $mock_context_definition->expects($this->once())
100       ->method('getConstraints')
101       ->willReturn([]);
102
103     $this->assertSame(
104       $mock_data_definition,
105       $mock_context_definition->getDataDefinition()
106     );
107   }
108
109   /**
110    * @dataProvider providerGetDataDefinition
111    * @covers ::getDataDefinition
112    * @uses \Drupal
113    */
114   public function testGetDataDefinitionInvalidType($is_multiple) {
115     // Since we're trying to make getDataDefinition() throw an exception in
116     // isolation, we use a data type which is not valid.
117     $data_type = 'not_valid';
118     $mock_data_definition = $this->getMockBuilder('\Drupal\Core\TypedData\ListDataDefinitionInterface')
119       ->getMockForAbstractClass();
120
121     // Follow code paths for both multiple and non-multiple definitions.
122     $create_definition_method = 'createDataDefinition';
123     if ($is_multiple) {
124       $create_definition_method = 'createListDataDefinition';
125     }
126     $mock_data_manager = $this->getMock(TypedDataManagerInterface::class);
127     // Our mocked data manager will return NULL for a non-valid data type. This
128     // will eventually cause getDataDefinition() to throw an exception.
129     $mock_data_manager->expects($this->once())
130       ->method($create_definition_method)
131       ->willReturnMap([
132         ['not_valid', NULL],
133         ['valid', $mock_data_definition],
134       ]);
135
136     // Mock a ContextDefinition object with expectations for only the methods
137     // that will be called before the expected exception.
138     $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
139       ->disableOriginalConstructor()
140       ->setMethods([
141         'isMultiple',
142         'getTypedDataManager',
143         'getDataType',
144       ])
145       ->getMock();
146     $mock_context_definition->expects($this->once())
147       ->method('isMultiple')
148       ->willReturn($is_multiple);
149     $mock_context_definition->expects($this->once())
150       ->method('getTypedDataManager')
151       ->willReturn($mock_data_manager);
152     $mock_context_definition
153       ->method('getDataType')
154       ->willReturn($data_type);
155
156     $this->setExpectedException(\Exception::class);
157     $mock_context_definition->getDataDefinition();
158   }
159
160   /**
161    * Data provider for testGetConstraint
162    */
163   public function providerGetConstraint() {
164     return [
165       [NULL, [], 'nonexistent_constraint_name'],
166       [
167         'not_null',
168         [
169           'constraint_name' => 'not_null',
170         ],
171         'constraint_name',
172       ],
173     ];
174   }
175
176   /**
177    * @dataProvider providerGetConstraint
178    * @covers ::getConstraint
179    * @uses \Drupal
180    */
181   public function testGetConstraint($expected, $constraint_array, $constraint) {
182     $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
183       ->disableOriginalConstructor()
184       ->setMethods([
185         'getConstraints',
186       ])
187       ->getMock();
188     $mock_context_definition->expects($this->once())
189       ->method('getConstraints')
190       ->willReturn($constraint_array);
191
192     $this->assertEquals($expected, $mock_context_definition->getConstraint($constraint));
193   }
194
195   /**
196    * @covers ::getDefaultValue
197    * @covers ::setDefaultValue
198    */
199   public function testDefaultValue() {
200     $context_definition = new ContextDefinition();
201     $this->assertNull($context_definition->getDefaultValue());
202     $context_definition->setDefaultValue('test');
203     $this->assertSame('test', $context_definition->getDefaultValue());
204   }
205
206 }