function EntityContextDefinitionIsSatisfiedTest::testIsSatisfiedByGenerateBundledEntity

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

@covers ::isSatisfiedBy @covers ::dataTypeMatches @covers ::getSampleValues @covers ::getConstraintObjects

@dataProvider providerTestIsSatisfiedByGenerateBundledEntity

File

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

Class

EntityContextDefinitionIsSatisfiedTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Plugin%21Context%21EntityContextDefinition.php/class/EntityContextDefinition/11.x" title="Defines a class to provide entity context definitions." class="local">\Drupal\Core\Plugin\Context\EntityContextDefinition</a> @group Plugin

Namespace

Drupal\Tests\Core\Plugin\Context

Code

public function testIsSatisfiedByGenerateBundledEntity($expected, array $requirement_bundles, array $candidate_bundles, ?array $bundles_to_instantiate = NULL) : void {
    // 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);
}

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