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
113 * @uses \Drupal\Component\Utility\SafeMarkup
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();
122 // Follow code paths for both multiple and non-multiple definitions.
123 $create_definition_method = 'createDataDefinition';
125 $create_definition_method = 'createListDataDefinition';
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)
134 ['valid', $mock_data_definition],
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()
143 'getTypedDataManager',
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);
157 $this->setExpectedException(\Exception::class);
158 $mock_context_definition->getDataDefinition();
162 * Data provider for testGetConstraint
164 public function providerGetConstraint() {
166 [NULL, [], 'nonexistent_constraint_name'],
170 'constraint_name' => 'not_null',
178 * @dataProvider providerGetConstraint
179 * @covers ::getConstraint
182 public function testGetConstraint($expected, $constraint_array, $constraint) {
183 $mock_context_definition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinition')
184 ->disableOriginalConstructor()
189 $mock_context_definition->expects($this->once())
190 ->method('getConstraints')
191 ->willReturn($constraint_array);
193 $this->assertEquals($expected, $mock_context_definition->getConstraint($constraint));
197 * @covers ::getDefaultValue
198 * @covers ::setDefaultValue
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());