1a14692fb03731505d40f0a16df57aca501efcd3
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Context / EntityContextDefinitionDeprecationTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Context;
4
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;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
14  *
15  * @group Plugin
16  * @group legacy
17  */
18 class EntityContextDefinitionDeprecationTest extends UnitTestCase {
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25
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);
29
30     $typed_data_manager = $this->prophesize('\Drupal\Core\TypedData\TypedDataManagerInterface');
31     $typed_data_manager->getValidationConstraintManager()->willReturn($constraint_manager->reveal());
32
33     $validator = $this->prophesize('\Symfony\Component\Validator\Validator\ValidatorInterface')
34       ->reveal();
35     $typed_data_manager->getValidator()->willReturn($validator);
36
37     $container = new ContainerBuilder();
38     $container->set('typed_data_manager', $typed_data_manager->reveal());
39     \Drupal::setContainer($container);
40   }
41
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');
47     // The code paths we're testing are private and protected, so use reflection
48     // to manipulate protected properties.
49     $reflector = new \ReflectionObject($definition);
50
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));
56
57     // Ensure that getConstraintObjects() adds the EntityType constraint.
58     $method = $reflector->getMethod('getConstraintObjects');
59     $method->setAccessible(TRUE);
60     $this->assertArrayHasKey('EntityType', $method->invoke($definition));
61
62     // Ensure that the BC object's getSampleValues() method is called during
63     // validation.
64     $bc_mock = $this->getMockBuilder(EntityContextDefinition::class)
65       ->setMethods(['getSampleValues'])
66       ->getMock();
67
68     $bc_mock->expects($this->atLeastOnce())
69       ->method('getSampleValues')
70       ->willReturn([]);
71     $property->setValue($definition, $bc_mock);
72     $definition->isSatisfiedBy(new Context($definition));
73
74     // Ensure that the BC layer survives serialization and unserialization.
75     $definition = unserialize(serialize($definition));
76     $this->assertInstanceOf(EntityContextDefinition::class, $property->getValue($definition));
77   }
78
79 }