More updates to stop using dev or alpha or beta versions.
[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    * @uses \Drupal\Component\Utility\SafeMarkup
114    */
115   public function testGetDataDefinitionInvalidType($is_multiple) {
116     // Since we're trying to make getDataDefinition() throw an exception in
117     // isolation, we use a data type which is not valid.
118     $data_type = 'not_valid';
119     $mock_data_definition = $this->getMockBuilder('\Drupal\Core\TypedData\ListDataDefinitionInterface')
120       ->getMockForAbstractClass();
121
122     // Follow code paths for both multiple and non-multiple definitions.
123     $create_definition_method = 'createDataDefinition';
124     if ($is_multiple) {
125       $create_definition_method = 'createListDataDefinition';
126     }
127     $mock_data_manager = $this->getMock(TypedDataManagerInterface::class);
128     // Our mocked data manager will return NULL for a non-valid data type. This
129     // will eventually cause getDataDefinition() to throw an exception.
130     $mock_data_manager->expects($this->once())
131       ->method($create_definition_method)
132       ->willReturnMap([
133         ['not_valid', NULL],
134         ['valid', $mock_data_definition],
135       ]);
136
137     // Mock a ContextDefinition object with expectations for only the methods
138     // that will be called before the expected exception.
139     $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
140       ->disableOriginalConstructor()
141       ->setMethods([
142         'isMultiple',
143         'getTypedDataManager',
144         'getDataType',
145       ])
146       ->getMock();
147     $mock_context_definition->expects($this->once())
148       ->method('isMultiple')
149       ->willReturn($is_multiple);
150     $mock_context_definition->expects($this->once())
151       ->method('getTypedDataManager')
152       ->willReturn($mock_data_manager);
153     $mock_context_definition
154       ->method('getDataType')
155       ->willReturn($data_type);
156
157     $this->setExpectedException(\Exception::class);
158     $mock_context_definition->getDataDefinition();
159   }
160
161   /**
162    * Data provider for testGetConstraint
163    */
164   public function providerGetConstraint() {
165     return [
166       [NULL, [], 'nonexistent_constraint_name'],
167       [
168         'not_null',
169         [
170           'constraint_name' => 'not_null',
171         ],
172         'constraint_name',
173       ],
174     ];
175   }
176
177   /**
178    * @dataProvider providerGetConstraint
179    * @covers ::getConstraint
180    * @uses \Drupal
181    */
182   public function testGetConstraint($expected, $constraint_array, $constraint) {
183     $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
184       ->disableOriginalConstructor()
185       ->setMethods([
186         'getConstraints',
187       ])
188       ->getMock();
189     $mock_context_definition->expects($this->once())
190       ->method('getConstraints')
191       ->willReturn($constraint_array);
192
193     $this->assertEquals($expected, $mock_context_definition->getConstraint($constraint));
194   }
195
196   /**
197    * @covers ::getDefaultValue
198    * @covers ::setDefaultValue
199    */
200   public function testDefaultValue() {
201     $context_definition = new ContextDefinition();
202     $this->assertNull($context_definition->getDefaultValue());
203     $context_definition->setDefaultValue('test');
204     $this->assertSame('test', $context_definition->getDefaultValue());
205   }
206
207 }