3 namespace Drupal\Tests\Core\Plugin\Context;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Plugin\Context\Context;
7 use Drupal\Core\Plugin\Context\ContextDefinition;
8 use Drupal\Core\Plugin\Context\EntityContextDefinition;
9 use Drupal\Tests\UnitTestCase;
10 use Prophecy\Argument;
13 * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
18 class EntityContextDefinitionDeprecationTest extends UnitTestCase {
23 protected function setUp() {
26 // Mock container services needed for constraint validation.
27 $constraint_manager = $this->prophesize('\Drupal\Core\Validation\ConstraintManager');
28 $constraint_manager->create(Argument::type('string'), Argument::any())->willReturn(TRUE);
30 $typed_data_manager = $this->prophesize('\Drupal\Core\TypedData\TypedDataManagerInterface');
31 $typed_data_manager->getValidationConstraintManager()->willReturn($constraint_manager->reveal());
33 $validator = $this->prophesize('\Symfony\Component\Validator\Validator\ValidatorInterface')
35 $typed_data_manager->getValidator()->willReturn($validator);
37 $container = new ContainerBuilder();
38 $container->set('typed_data_manager', $typed_data_manager->reveal());
39 \Drupal::setContainer($container);
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.
45 public function testDeprecationNotice() {
46 $definition = new ContextDefinition('entity:node');
47 // The code paths we're testing are private and protected, so use reflection
48 // to manipulate protected properties.
49 $reflector = new \ReflectionObject($definition);
51 // Ensure that the BC object was created correctly.
52 $this->assertTrue($reflector->hasProperty('entityContextDefinition'));
53 $property = $reflector->getProperty('entityContextDefinition');
54 $property->setAccessible(TRUE);
55 $this->assertInstanceOf(EntityContextDefinition::class, $property->getValue($definition));
57 // Ensure that getConstraintObjects() adds the EntityType constraint.
58 $method = $reflector->getMethod('getConstraintObjects');
59 $method->setAccessible(TRUE);
60 $this->assertArrayHasKey('EntityType', $method->invoke($definition));
62 // Ensure that the BC object's getSampleValues() method is called during
64 $bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
65 ->setMethods(['getSampleValues'])
68 $bc_mock->expects($this->atLeastOnce())
69 ->method('getSampleValues')
71 $property->setValue($definition, $bc_mock);
72 $definition->isSatisfiedBy(new Context($definition));
74 // Ensure that the BC layer survives serialization and unserialization.
75 $definition = unserialize(serialize($definition));
76 $this->assertInstanceOf(EntityContextDefinition::class, $property->getValue($definition));