You are here

class EntityContextDefinitionDeprecationTest in Drupal 8

Test deprecated use of ContextDefinition as an EntityContextDefinition.

@coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition

@group Plugin @group legacy

Hierarchy

Expanded class hierarchy of EntityContextDefinitionDeprecationTest

See also

https://www.drupal.org/node/2976400

File

core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionDeprecationTest.php, line 25

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class EntityContextDefinitionDeprecationTest extends UnitTestCase {

  /**
   * The context definition under test.
   *
   * @var \Drupal\Core\Plugin\Context\ContextDefinition
   */
  protected $definition;

  /**
   * The compatibility layer property on the context definition under test.
   *
   * @var \ReflectionProperty
   */
  protected $compatibilityLayer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Mock container services needed for constraint validation.
    $constraint_manager = $this
      ->prophesize(ConstraintManager::class);
    $constraint_manager
      ->create(Argument::type('string'), Argument::any())
      ->willReturn(TRUE);
    $typed_data_manager = $this
      ->prophesize(TypedDataManagerInterface::class);
    $typed_data_manager
      ->getValidationConstraintManager()
      ->willReturn($constraint_manager
      ->reveal());
    $validator = $this
      ->prophesize(ValidatorInterface::class)
      ->reveal();
    $typed_data_manager
      ->getValidator()
      ->willReturn($validator);
    $container = new ContainerBuilder();
    $container
      ->set('typed_data_manager', $typed_data_manager
      ->reveal());
    \Drupal::setContainer($container);

    // Create a deprecated entity context definition and prepare the
    // compatibility layer to be overridden.
    $this->definition = new ContextDefinition('entity:node');

    // The code paths we're testing are private and protected, so use reflection
    // to manipulate protected properties.
    $reflector = new \ReflectionObject($this->definition);

    // Ensure that the BC object was created correctly.
    $this
      ->assertTrue($reflector
      ->hasProperty('entityContextDefinition'));
    $this->compatibilityLayer = $reflector
      ->getProperty('entityContextDefinition');
    $this->compatibilityLayer
      ->setAccessible(TRUE);
    $this
      ->assertInstanceOf(EntityContextDefinition::class, $this->compatibilityLayer
      ->getValue($this->definition));
  }

  /**
   * Test that the BC layer survives serialization and unserialization.
   *
   * @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.
   */
  public function testSerialization() {
    $this->definition
      ->addConstraint('foo');
    $definition = unserialize(serialize($this->definition));
    $this
      ->assertEquals([
      'foo' => NULL,
    ], $definition
      ->getConstraints());
    $bc_layer = $this->compatibilityLayer
      ->getValue($definition);
    $this
      ->assertInstanceOf(EntityContextDefinition::class, $bc_layer);
  }

  /**
   * Test that getConstraints() proxies to the compatibility layer.
   *
   * @covers ::getConstraints
   * @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.
   */
  public function testGetConstraints() {
    $bc_mock = $this
      ->getMockBuilder(EntityContextDefinition::class)
      ->setMethods([
      'getConstraints',
    ])
      ->getMock();
    $constraints = [
      'test_constraint',
    ];
    $bc_mock
      ->expects($this
      ->once())
      ->method('getConstraints')
      ->willReturn($constraints);
    $this->compatibilityLayer
      ->setValue($this->definition, $bc_mock);
    $this
      ->assertSame($constraints, $this->definition
      ->getConstraints());
  }

  /**
   * Test that getConstraint() proxies to the compatibility layer.
   *
   * @covers ::getConstraint
   * @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.
   */
  public function testGetConstraint() {
    $bc_mock = $this
      ->getMockBuilder(EntityContextDefinition::class)
      ->setMethods([
      'getConstraint',
    ])
      ->getMock();
    $bc_mock
      ->expects($this
      ->once())
      ->method('getConstraint')
      ->with('constraint_name')
      ->willReturn('test_constraint');
    $this->compatibilityLayer
      ->setValue($this->definition, $bc_mock);
    $this
      ->assertSame('test_constraint', $this->definition
      ->getConstraint('constraint_name'));
  }

  /**
   * Test that setConstraints() proxies to the compatibility layer.
   *
   * @covers ::setConstraints
   * @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.
   */
  public function testSetConstraints() {
    $bc_mock = $this
      ->getMockBuilder(EntityContextDefinition::class)
      ->setMethods([
      'setConstraints',
    ])
      ->getMock();
    $constraints = [
      'TestConstraint' => [],
    ];
    $bc_mock
      ->expects($this
      ->once())
      ->method('setConstraints')
      ->with($constraints)
      ->willReturnSelf();
    $this->compatibilityLayer
      ->setValue($this->definition, $bc_mock);
    $this
      ->assertSame($this->definition, $this->definition
      ->setConstraints($constraints));
  }

  /**
   * Test that addConstraint() proxies to the compatibility layer.
   *
   * @covers ::addConstraint
   * @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.
   */
  public function testAddConstraint() {
    $bc_mock = $this
      ->getMockBuilder(EntityContextDefinition::class)
      ->setMethods([
      'addConstraint',
    ])
      ->getMock();
    $options = [
      'options',
    ];
    $bc_mock
      ->expects($this
      ->once())
      ->method('addConstraint')
      ->with('constraint_name', $options)
      ->willReturnSelf();
    $this->compatibilityLayer
      ->setValue($this->definition, $bc_mock);
    $this
      ->assertSame($this->definition, $this->definition
      ->addConstraint('constraint_name', $options));
  }

  /**
   * Test that isSatisfiedBy() calls the compatibility layer.
   *
   * @covers ::isSatisfiedBy
   * @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.
   */
  public function testIsSatisfiedBy() {

    // Ensure that the BC object's getSampleValues() method is called during
    // validation.
    $bc_mock = $this
      ->getMockBuilder(EntityContextDefinition::class)
      ->setMethods([
      'getSampleValues',
    ])
      ->getMock();
    $bc_mock
      ->expects($this
      ->atLeastOnce())
      ->method('getSampleValues')
      ->willReturn([]);
    $this->compatibilityLayer
      ->setValue($this->definition, $bc_mock);
    $this->definition
      ->isSatisfiedBy(new Context($this->definition));
  }

  /**
   * Test that getConstraintObjects() adds the EntityType constraint.
   *
   * @covers ::getConstraintObjects
   * @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.
   */
  public function testGetConstraintObjects() {
    $reflector = new \ReflectionObject($this->definition);
    $method = $reflector
      ->getMethod('getConstraintObjects');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertArrayHasKey('EntityType', $method
      ->invoke($this->definition));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityContextDefinitionDeprecationTest::$compatibilityLayer protected property The compatibility layer property on the context definition under test.
EntityContextDefinitionDeprecationTest::$definition protected property The context definition under test.
EntityContextDefinitionDeprecationTest::setUp protected function Overrides UnitTestCase::setUp
EntityContextDefinitionDeprecationTest::testAddConstraint public function Test that addConstraint() proxies to the compatibility layer.
EntityContextDefinitionDeprecationTest::testGetConstraint public function Test that getConstraint() proxies to the compatibility layer.
EntityContextDefinitionDeprecationTest::testGetConstraintObjects public function Test that getConstraintObjects() adds the EntityType constraint.
EntityContextDefinitionDeprecationTest::testGetConstraints public function Test that getConstraints() proxies to the compatibility layer.
EntityContextDefinitionDeprecationTest::testIsSatisfiedBy public function Test that isSatisfiedBy() calls the compatibility layer.
EntityContextDefinitionDeprecationTest::testSerialization public function Test that the BC layer survives serialization and unserialization.
EntityContextDefinitionDeprecationTest::testSetConstraints public function Test that setConstraints() proxies to the compatibility layer.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.