Skip to content

Commit 92a3985

Browse files
committed
Issue #2998462 by AndyF, Baysaa, Siavash, tim.plunkett, millionleaves, fatmarker: Error adding Content Type Selection criteria or Context
(cherry picked from commit 14606cc)
1 parent 36cf199 commit 92a3985

File tree

2 files changed

+146
-23
lines changed

2 files changed

+146
-23
lines changed

lib/Drupal/Core/Plugin/Context/ContextDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function getConstraint($constraint_name) {
270270
public function setConstraints(array $constraints) {
271271
// If the backwards compatibility layer is present, delegate to that.
272272
if ($this->entityContextDefinition) {
273-
$this->entityContextDefinition->setConstraint();
273+
$this->entityContextDefinition->setConstraints($constraints);
274274
}
275275

276276
$this->constraints = $constraints;

tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionDeprecationTest.php

Lines changed: 145 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,173 @@
66
use Drupal\Core\Plugin\Context\Context;
77
use Drupal\Core\Plugin\Context\ContextDefinition;
88
use Drupal\Core\Plugin\Context\EntityContextDefinition;
9+
use Drupal\Core\TypedData\TypedDataManagerInterface;
10+
use Drupal\Core\Validation\ConstraintManager;
911
use Drupal\Tests\UnitTestCase;
1012
use Prophecy\Argument;
13+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1114

1215
/**
16+
* Test deprecated use of ContextDefinition as an EntityContextDefinition.
17+
*
1318
* @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
1419
*
1520
* @group Plugin
1621
* @group legacy
22+
*
23+
* @see https://www.drupal.org/node/2976400
1724
*/
1825
class EntityContextDefinitionDeprecationTest extends UnitTestCase {
1926

27+
/**
28+
* The context definition under test.
29+
*
30+
* @var \Drupal\Core\Plugin\Context\ContextDefinition
31+
*/
32+
protected $definition;
33+
34+
/**
35+
* The compatibility layer property on the context definition under test.
36+
*
37+
* @var \ReflectionProperty
38+
*/
39+
protected $compatibilityLayer;
40+
2041
/**
2142
* {@inheritdoc}
2243
*/
2344
protected function setUp() {
2445
parent::setUp();
2546

2647
// Mock container services needed for constraint validation.
27-
$constraint_manager = $this->prophesize('\Drupal\Core\Validation\ConstraintManager');
48+
$constraint_manager = $this->prophesize(ConstraintManager::class);
2849
$constraint_manager->create(Argument::type('string'), Argument::any())->willReturn(TRUE);
2950

30-
$typed_data_manager = $this->prophesize('\Drupal\Core\TypedData\TypedDataManagerInterface');
51+
$typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
3152
$typed_data_manager->getValidationConstraintManager()->willReturn($constraint_manager->reveal());
3253

33-
$validator = $this->prophesize('\Symfony\Component\Validator\Validator\ValidatorInterface')
54+
$validator = $this->prophesize(ValidatorInterface::class)
3455
->reveal();
3556
$typed_data_manager->getValidator()->willReturn($validator);
3657

3758
$container = new ContainerBuilder();
3859
$container->set('typed_data_manager', $typed_data_manager->reveal());
3960
\Drupal::setContainer($container);
40-
}
4161

42-
/**
43-
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
44-
*/
45-
public function testDeprecationNotice() {
46-
$definition = new ContextDefinition('entity:node');
62+
// Create a deprecated entity context definition and prepare the
63+
// compatibility layer to be overridden.
64+
$this->definition = new ContextDefinition('entity:node');
4765
// The code paths we're testing are private and protected, so use reflection
4866
// to manipulate protected properties.
49-
$reflector = new \ReflectionObject($definition);
67+
$reflector = new \ReflectionObject($this->definition);
5068

5169
// Ensure that the BC object was created correctly.
5270
$this->assertTrue($reflector->hasProperty('entityContextDefinition'));
53-
$property = $reflector->getProperty('entityContextDefinition');
54-
$property->setAccessible(TRUE);
55-
$this->assertInstanceOf(EntityContextDefinition::class, $property->getValue($definition));
71+
$this->compatibilityLayer = $reflector->getProperty('entityContextDefinition');
72+
$this->compatibilityLayer->setAccessible(TRUE);
73+
$this->assertInstanceOf(EntityContextDefinition::class, $this->compatibilityLayer->getValue($this->definition));
74+
}
5675

57-
// Ensure that getConstraintObjects() adds the EntityType constraint.
58-
$method = $reflector->getMethod('getConstraintObjects');
59-
$method->setAccessible(TRUE);
60-
$this->assertArrayHasKey('EntityType', $method->invoke($definition));
76+
/**
77+
* Test that the BC layer survives serialization and unserialization.
78+
*
79+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
80+
*/
81+
public function testSerialization() {
82+
$definition = unserialize(serialize($this->definition));
83+
$bc_layer = $this->compatibilityLayer->getValue($definition);
84+
$this->assertInstanceOf(EntityContextDefinition::class, $bc_layer);
85+
}
86+
87+
/**
88+
* Test that getConstraints() proxies to the compatibility layer.
89+
*
90+
* @covers ::getConstraints
91+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
92+
*/
93+
public function testGetConstraints() {
94+
$bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
95+
->setMethods(['getConstraints'])
96+
->getMock();
97+
98+
$constraints = ['test_constraint'];
99+
$bc_mock->expects($this->once())
100+
->method('getConstraints')
101+
->willReturn($constraints);
102+
$this->compatibilityLayer->setValue($this->definition, $bc_mock);
103+
104+
$this->assertSame($constraints, $this->definition->getConstraints());
105+
}
106+
107+
/**
108+
* Test that getConstraint() proxies to the compatibility layer.
109+
*
110+
* @covers ::getConstraint
111+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
112+
*/
113+
public function testGetConstraint() {
114+
$bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
115+
->setMethods(['getConstraint'])
116+
->getMock();
117+
118+
$bc_mock->expects($this->once())
119+
->method('getConstraint')
120+
->with('constraint_name')
121+
->willReturn('test_constraint');
122+
$this->compatibilityLayer->setValue($this->definition, $bc_mock);
123+
124+
$this->assertSame('test_constraint', $this->definition->getConstraint('constraint_name'));
125+
}
126+
127+
/**
128+
* Test that setConstraints() proxies to the compatibility layer.
129+
*
130+
* @covers ::setConstraints
131+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
132+
*/
133+
public function testSetConstraints() {
134+
$bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
135+
->setMethods(['setConstraints'])
136+
->getMock();
137+
138+
$constraints = ['TestConstraint' => []];
139+
$bc_mock->expects($this->once())
140+
->method('setConstraints')
141+
->with($constraints)
142+
->willReturnSelf();
143+
$this->compatibilityLayer->setValue($this->definition, $bc_mock);
144+
145+
$this->assertSame($this->definition, $this->definition->setConstraints($constraints));
146+
}
147+
148+
/**
149+
* Test that addConstraint() proxies to the compatibility layer.
150+
*
151+
* @covers ::addConstraint
152+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
153+
*/
154+
public function testAddConstraint() {
155+
$bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
156+
->setMethods(['addConstraint'])
157+
->getMock();
61158

159+
$options = ['options'];
160+
$bc_mock->expects($this->once())
161+
->method('addConstraint')
162+
->with('constraint_name', $options)
163+
->willReturnSelf();
164+
$this->compatibilityLayer->setValue($this->definition, $bc_mock);
165+
166+
$this->assertSame($this->definition, $this->definition->addConstraint('constraint_name', $options));
167+
}
168+
169+
/**
170+
* Test that isSatisfiedBy() calls the compatibility layer.
171+
*
172+
* @covers ::isSatisfiedBy
173+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
174+
*/
175+
public function testIsSatisfiedBy() {
62176
// Ensure that the BC object's getSampleValues() method is called during
63177
// validation.
64178
$bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
@@ -68,12 +182,21 @@ public function testDeprecationNotice() {
68182
$bc_mock->expects($this->atLeastOnce())
69183
->method('getSampleValues')
70184
->willReturn([]);
71-
$property->setValue($definition, $bc_mock);
72-
$definition->isSatisfiedBy(new Context($definition));
185+
$this->compatibilityLayer->setValue($this->definition, $bc_mock);
186+
$this->definition->isSatisfiedBy(new Context($this->definition));
187+
}
73188

74-
// Ensure that the BC layer survives serialization and unserialization.
75-
$definition = unserialize(serialize($definition));
76-
$this->assertInstanceOf(EntityContextDefinition::class, $property->getValue($definition));
189+
/**
190+
* Test that getConstraintObjects() adds the EntityType constraint.
191+
*
192+
* @covers ::getConstraintObjects
193+
* @expectedDeprecation Constructing a ContextDefinition object for an entity type is deprecated in Drupal 8.6.0. Use Drupal\Core\Plugin\Context\EntityContextDefinition instead. See https://www.drupal.org/node/2976400 for more information.
194+
*/
195+
public function testGetConstraintObjects() {
196+
$reflector = new \ReflectionObject($this->definition);
197+
$method = $reflector->getMethod('getConstraintObjects');
198+
$method->setAccessible(TRUE);
199+
$this->assertArrayHasKey('EntityType', $method->invoke($this->definition));
77200
}
78201

79202
}

0 commit comments

Comments
 (0)