class EntityContextDefinitionIsSatisfiedTest

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php \Drupal\Tests\Core\Plugin\Context\EntityContextDefinitionIsSatisfiedTest
  2. 10 core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php \Drupal\Tests\Core\Plugin\Context\EntityContextDefinitionIsSatisfiedTest
  3. 11.x core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php \Drupal\Tests\Core\Plugin\Context\EntityContextDefinitionIsSatisfiedTest

@coversDefaultClass \Drupal\Core\Plugin\Context\EntityContextDefinition @group Plugin

Hierarchy

Expanded class hierarchy of EntityContextDefinitionIsSatisfiedTest

File

core/tests/Drupal/Tests/Core/Plugin/Context/EntityContextDefinitionIsSatisfiedTest.php, line 29

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class EntityContextDefinitionIsSatisfiedTest extends UnitTestCase {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity type bundle info.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
     */
    protected $entityTypeBundleInfo;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $namespaces = new \ArrayObject([
            'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData',
            'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation',
            'Drupal\\Core\\Entity' => $this->root . '/core/lib/Drupal/Core/Entity',
        ]);
        $cache_backend = new NullBackend('cache');
        $module_handler = $this->prophesize(ModuleHandlerInterface::class);
        $class_resolver = $this->prophesize(ClassResolverInterface::class);
        $class_resolver->getInstanceFromDefinition(Argument::type('string'))
            ->will(function ($arguments) {
            $class_name = $arguments[0];
            return new $class_name();
        });
        $type_data_manager = new TypedDataManager($namespaces, $cache_backend, $module_handler->reveal(), $class_resolver->reveal());
        $type_data_manager->setValidationConstraintManager(new ConstraintManager($namespaces, $cache_backend, $module_handler->reveal()));
        $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
        $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class);
        $string_translation = new TranslationManager(new LanguageDefault([]));
        $container = new ContainerBuilder();
        $container->set('typed_data_manager', $type_data_manager);
        $container->set('entity_type.manager', $this->entityTypeManager
            ->reveal());
        $container->set('entity_type.bundle.info', $this->entityTypeBundleInfo
            ->reveal());
        $container->set('string_translation', $string_translation);
        \Drupal::setContainer($container);
    }
    
    /**
     * Asserts that the requirement is satisfied as expected.
     *
     * @param bool $expected
     *   The expected outcome.
     * @param \Drupal\Core\Plugin\Context\ContextDefinition $requirement
     *   The requirement to check against.
     * @param \Drupal\Core\Plugin\Context\ContextDefinition $definition
     *   The context definition to check.
     * @param mixed $value
     *   (optional) The value to set on the context, defaults to NULL.
     *
     * @internal
     */
    protected function assertRequirementIsSatisfied(bool $expected, ContextDefinition $requirement, ContextDefinition $definition, $value = NULL) : void {
        $context = new EntityContext($definition, $value);
        $this->assertSame($expected, $requirement->isSatisfiedBy($context));
    }
    
    /**
     * @covers ::isSatisfiedBy
     * @covers ::dataTypeMatches
     * @covers ::getSampleValues
     * @covers ::getConstraintObjects
     *
     * @dataProvider providerTestIsSatisfiedBy
     */
    public function testIsSatisfiedBy($expected, ContextDefinition $requirement, ContextDefinition $definition, $value = NULL) {
        $entity_storage = $this->prophesize(EntityStorageInterface::class);
        $content_entity_storage = $this->prophesize(ContentEntityStorageInterface::class);
        $this->entityTypeManager
            ->getStorage('test_config')
            ->willReturn($entity_storage->reveal());
        $this->entityTypeManager
            ->getStorage('test_content')
            ->willReturn($content_entity_storage->reveal());
        $config_entity_type = new EntityType([
            'id' => 'test_config',
        ]);
        $content_entity_type = new EntityType([
            'id' => 'test_content',
        ]);
        $this->entityTypeManager
            ->getDefinition('test_config')
            ->willReturn($config_entity_type);
        $this->entityTypeManager
            ->getDefinition('test_content')
            ->willReturn($content_entity_type);
        $this->entityTypeManager
            ->getDefinitions()
            ->willReturn([
            'test_config' => $config_entity_type,
            'test_content' => $content_entity_type,
        ]);
        $this->entityTypeBundleInfo
            ->getBundleInfo('test_config')
            ->willReturn([
            'test_config' => [
                'label' => 'test_config',
            ],
        ]);
        $this->entityTypeBundleInfo
            ->getBundleInfo('test_content')
            ->willReturn([
            'test_content' => [
                'label' => 'test_content',
            ],
        ]);
        $this->assertRequirementIsSatisfied($expected, $requirement, $definition, $value);
    }
    
    /**
     * Provides test data for ::testIsSatisfiedBy().
     */
    public function providerTestIsSatisfiedBy() {
        $data = [];
        $content = new EntityType([
            'id' => 'test_content',
        ]);
        $config = new EntityType([
            'id' => 'test_config',
        ]);
        // Entities without bundles.
        $data['content entity, matching type, no value'] = [
            TRUE,
            EntityContextDefinition::fromEntityType($content),
            EntityContextDefinition::fromEntityType($content),
        ];
        $entity = $this->prophesize(ContentEntityInterface::class)
            ->willImplement(\IteratorAggregate::class);
        $entity->getIterator()
            ->willReturn(new \ArrayIterator([]));
        $entity->getCacheContexts()
            ->willReturn([]);
        $entity->getCacheTags()
            ->willReturn([]);
        $entity->getCacheMaxAge()
            ->willReturn(0);
        $entity->getEntityTypeId()
            ->willReturn('test_content');
        $data['content entity, matching type, correct value'] = [
            TRUE,
            EntityContextDefinition::fromEntityType($content),
            EntityContextDefinition::fromEntityType($content),
            $entity->reveal(),
        ];
        $data['content entity, incorrect manual constraint'] = [
            TRUE,
            EntityContextDefinition::fromEntityType($content),
            EntityContextDefinition::fromEntityType($content)->addConstraint('EntityType', 'test_config'),
        ];
        $data['config entity, matching type, no value'] = [
            TRUE,
            EntityContextDefinition::fromEntityType($config),
            EntityContextDefinition::fromEntityType($config),
        ];
        $data['generic entity requirement, specific context'] = [
            TRUE,
            new ContextDefinition('entity'),
            EntityContextDefinition::fromEntityType($config),
        ];
        $data['specific requirement, generic entity context'] = [
            FALSE,
            EntityContextDefinition::fromEntityType($content),
            new ContextDefinition('entity'),
        ];
        return $data;
    }
    
    /**
     * @covers ::isSatisfiedBy
     * @covers ::dataTypeMatches
     * @covers ::getSampleValues
     * @covers ::getConstraintObjects
     *
     * @dataProvider providerTestIsSatisfiedByGenerateBundledEntity
     */
    public function testIsSatisfiedByGenerateBundledEntity($expected, array $requirement_bundles, array $candidate_bundles, array $bundles_to_instantiate = NULL) {
        // If no bundles are explicitly specified, instantiate all bundles.
        if (!$bundles_to_instantiate) {
            $bundles_to_instantiate = $candidate_bundles;
        }
        $content_entity_storage = $this->prophesize(ContentEntityStorageInterface::class);
        foreach ($bundles_to_instantiate as $bundle) {
            $entity = $this->prophesize(ContentEntityInterface::class)
                ->willImplement(\IteratorAggregate::class);
            $entity->getEntityTypeId()
                ->willReturn('test_content');
            $entity->getIterator()
                ->willReturn(new \ArrayIterator([]));
            $entity->bundle()
                ->willReturn($bundle);
            $content_entity_storage->create([
                'the_bundle_key' => $bundle,
            ])
                ->willReturn($entity->reveal())
                ->shouldBeCalled();
        }
        // Creating entities with sample values can lead to performance issues when
        // called many times. Ensure that createWithSampleValues() is not called.
        $content_entity_storage->createWithSampleValues(Argument::any())
            ->shouldNotBeCalled();
        $entity_type = new EntityType([
            'id' => 'test_content',
            'entity_keys' => [
                'bundle' => 'the_bundle_key',
            ],
        ]);
        $this->entityTypeManager
            ->getStorage('test_content')
            ->willReturn($content_entity_storage->reveal());
        $this->entityTypeManager
            ->getDefinition('test_content')
            ->willReturn($entity_type);
        $this->entityTypeManager
            ->getDefinitions()
            ->willReturn([
            'test_content' => $entity_type,
        ]);
        $this->entityTypeBundleInfo
            ->getBundleInfo('test_content')
            ->willReturn([
            'first_bundle' => [
                'label' => 'First bundle',
            ],
            'second_bundle' => [
                'label' => 'Second bundle',
            ],
            'third_bundle' => [
                'label' => 'Third bundle',
            ],
        ]);
        $requirement = EntityContextDefinition::fromEntityType($entity_type);
        if ($requirement_bundles) {
            $requirement->addConstraint('Bundle', $requirement_bundles);
        }
        $definition = EntityContextDefinition::fromEntityType($entity_type)->addConstraint('Bundle', $candidate_bundles);
        $this->assertRequirementIsSatisfied($expected, $requirement, $definition);
    }
    
    /**
     * Provides test data for ::testIsSatisfiedByGenerateBundledEntity().
     */
    public function providerTestIsSatisfiedByGenerateBundledEntity() {
        $data = [];
        $data['no requirement'] = [
            TRUE,
            [],
            [
                'first_bundle',
            ],
        ];
        $data['single requirement'] = [
            TRUE,
            [
                'first_bundle',
            ],
            [
                'first_bundle',
            ],
        ];
        $data['single requirement, multiple candidates, satisfies last candidate'] = [
            TRUE,
            [
                'third_bundle',
            ],
            [
                'first_bundle',
                'second_bundle',
                'third_bundle',
            ],
        ];
        $data['single requirement, multiple candidates, satisfies first candidate'] = [
            TRUE,
            [
                'first_bundle',
            ],
            [
                'first_bundle',
                'second_bundle',
                'third_bundle',
            ],
            // Once the first match is found, subsequent candidates are not checked.
[
                'first_bundle',
            ],
        ];
        $data['unsatisfied requirement'] = [
            FALSE,
            [
                'second_bundle',
            ],
            [
                'first_bundle',
                'third_bundle',
            ],
        ];
        $data['multiple requirements'] = [
            TRUE,
            [
                'first_bundle',
                'second_bundle',
            ],
            [
                'first_bundle',
            ],
        ];
        return $data;
    }
    
    /**
     * @covers ::isSatisfiedBy
     * @covers ::dataTypeMatches
     * @covers ::getSampleValues
     * @covers ::getConstraintObjects
     *
     * @dataProvider providerTestIsSatisfiedByPassBundledEntity
     */
    public function testIsSatisfiedByPassBundledEntity($expected, $requirement_constraint) {
        $entity_type = new EntityType([
            'id' => 'test_content',
        ]);
        $this->entityTypeManager
            ->getDefinitions()
            ->willReturn([
            'test_content' => $entity_type,
        ]);
        $this->entityTypeManager
            ->getDefinition('test_content')
            ->willReturn($entity_type);
        $this->entityTypeManager
            ->getStorage('test_content')
            ->shouldNotBeCalled();
        $this->entityTypeBundleInfo
            ->getBundleInfo('test_content')
            ->willReturn([
            'first_bundle' => [
                'label' => 'First bundle',
            ],
            'second_bundle' => [
                'label' => 'Second bundle',
            ],
            'third_bundle' => [
                'label' => 'Third bundle',
            ],
        ]);
        $entity = $this->prophesize(ContentEntityInterface::class)
            ->willImplement(\IteratorAggregate::class);
        $entity->getEntityTypeId()
            ->willReturn('test_content');
        $entity->getIterator()
            ->willReturn(new \ArrayIterator([]));
        $entity->getCacheContexts()
            ->willReturn([]);
        $entity->getCacheTags()
            ->willReturn([]);
        $entity->getCacheMaxAge()
            ->willReturn(0);
        $entity->bundle()
            ->willReturn('third_bundle');
        $requirement = EntityContextDefinition::fromEntityTypeId('test_content');
        if ($requirement_constraint) {
            $requirement->addConstraint('Bundle', $requirement_constraint);
        }
        $definition = EntityContextDefinition::fromEntityTypeId('test_content');
        $this->assertRequirementIsSatisfied($expected, $requirement, $definition, $entity->reveal());
    }
    
    /**
     * Provides test data for ::testIsSatisfiedByPassBundledEntity().
     */
    public function providerTestIsSatisfiedByPassBundledEntity() {
        $data = [];
        $data[] = [
            TRUE,
            [],
        ];
        $data[] = [
            FALSE,
            [
                'first_bundle',
            ],
        ];
        $data[] = [
            FALSE,
            [
                'second_bundle',
            ],
        ];
        $data[] = [
            TRUE,
            [
                'third_bundle',
            ],
        ];
        $data[] = [
            TRUE,
            [
                'first_bundle',
                'second_bundle',
                'third_bundle',
            ],
        ];
        $data[] = [
            FALSE,
            [
                'first_bundle',
                'second_bundle',
            ],
        ];
        $data[] = [
            TRUE,
            [
                'first_bundle',
                'third_bundle',
            ],
        ];
        $data[] = [
            TRUE,
            [
                'second_bundle',
                'third_bundle',
            ],
        ];
        return $data;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityContextDefinitionIsSatisfiedTest::$entityTypeBundleInfo protected property The entity type bundle info.
EntityContextDefinitionIsSatisfiedTest::$entityTypeManager protected property The entity type manager.
EntityContextDefinitionIsSatisfiedTest::assertRequirementIsSatisfied protected function Asserts that the requirement is satisfied as expected.
EntityContextDefinitionIsSatisfiedTest::providerTestIsSatisfiedBy public function Provides test data for ::testIsSatisfiedBy().
EntityContextDefinitionIsSatisfiedTest::providerTestIsSatisfiedByGenerateBundledEntity public function Provides test data for ::testIsSatisfiedByGenerateBundledEntity().
EntityContextDefinitionIsSatisfiedTest::providerTestIsSatisfiedByPassBundledEntity public function Provides test data for ::testIsSatisfiedByPassBundledEntity().
EntityContextDefinitionIsSatisfiedTest::setUp protected function Overrides UnitTestCase::setUp
EntityContextDefinitionIsSatisfiedTest::testIsSatisfiedBy public function @covers ::isSatisfiedBy
@covers ::dataTypeMatches
@covers ::getSampleValues
@covers ::getConstraintObjects
EntityContextDefinitionIsSatisfiedTest::testIsSatisfiedByGenerateBundledEntity public function @covers ::isSatisfiedBy
@covers ::dataTypeMatches
@covers ::getSampleValues
@covers ::getConstraintObjects
EntityContextDefinitionIsSatisfiedTest::testIsSatisfiedByPassBundledEntity public function @covers ::isSatisfiedBy
@covers ::dataTypeMatches
@covers ::getSampleValues
@covers ::getConstraintObjects
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.