3 namespace Drupal\Tests\Core\Plugin\Context;
5 use Drupal\Core\Plugin\Context\ContextDefinition;
6 use Drupal\Core\TypedData\TypedDataManagerInterface;
7 use Drupal\Tests\UnitTestCase;
10 * Tests the ContextDefinition class.
14 * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
16 class ContextDefinitionTest extends UnitTestCase {
19 * Very simple data provider.
21 public function providerGetDataDefinition() {
29 * @dataProvider providerGetDataDefinition
30 * @covers ::getDataDefinition
33 public function testGetDataDefinition($is_multiple) {
35 $mock_data_definition = $this->getMockBuilder('\Drupal\Core\TypedData\ListDataDefinitionInterface')
43 ->getMockForAbstractClass();
44 $mock_data_definition->expects($this->once())
47 $mock_data_definition->expects($this->once())
48 ->method('setDescription')
50 $mock_data_definition->expects($this->once())
51 ->method('setRequired')
53 $mock_data_definition->expects($this->once())
54 ->method('getConstraints')
56 $mock_data_definition->expects($this->once())
57 ->method('setConstraints')
60 // Follow code paths for both multiple and non-multiple definitions.
61 $create_definition_method = 'createDataDefinition';
63 $create_definition_method = 'createListDataDefinition';
65 $mock_data_manager = $this->getMock(TypedDataManagerInterface::class);
66 // Our mocked data manager will return our mocked data definition for a
68 $mock_data_manager->expects($this->once())
69 ->method($create_definition_method)
72 ['valid', $mock_data_definition],
75 // Mock a ContextDefinition object, setting up expectations for many of the
77 $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
78 ->disableOriginalConstructor()
81 'getTypedDataManager',
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')
104 $mock_data_definition,
105 $mock_context_definition->getDataDefinition()
110 * @dataProvider providerGetDataDefinition
111 * @covers ::getDataDefinition
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();
121 // Follow code paths for both multiple and non-multiple definitions.
122 $create_definition_method = 'createDataDefinition';
124 $create_definition_method = 'createListDataDefinition';
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)
133 ['valid', $mock_data_definition],
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()
142 'getTypedDataManager',
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);
156 $this->setExpectedException(\Exception::class);
157 $mock_context_definition->getDataDefinition();
161 * Data provider for testGetConstraint
163 public function providerGetConstraint() {
165 [NULL, [], 'nonexistent_constraint_name'],
169 'constraint_name' => 'not_null',
177 * @dataProvider providerGetConstraint
178 * @covers ::getConstraint
181 public function testGetConstraint($expected, $constraint_array, $constraint) {
182 $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
183 ->disableOriginalConstructor()
188 $mock_context_definition->expects($this->once())
189 ->method('getConstraints')
190 ->willReturn($constraint_array);
192 $this->assertEquals($expected, $mock_context_definition->getConstraint($constraint));
196 * @covers ::getDefaultValue
197 * @covers ::setDefaultValue
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());