You are here

public function EntityContextDefinitionIsSatisfiedTest::testIsSatisfiedByGenerateBundledEntity in Drupal 8

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()

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

@dataProvider providerTestIsSatisfiedByGenerateBundledEntity

File

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

Class

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

Namespace

Drupal\Tests\Core\Plugin\Context

Code

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);
}