function EntityFetchByFieldTest::testActionExecutionProvidedContextEntityType

Tests that the action execution loads the entity from storage.

@covers ::execute

File

tests/src/Unit/Integration/RulesAction/EntityFetchByFieldTest.php, line 140

Class

EntityFetchByFieldTest
@coversDefaultClass \Drupal\rules\Plugin\RulesAction\EntityFetchByField[[api-linebreak]] @group RulesAction

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testActionExecutionProvidedContextEntityType() {
  // Create variables for action context values.
  $entity_type = 'entity_test';
  $field_name = 'test_field';
  $field_value = 'llama';
  // Create an array of dummy entities.
  $entities = [];
  for ($i = 0; $i < 2; $i++) {
    $entity = $this->prophesize(EntityInterface::class)
      ->reveal();
    $entities[] = $entity;
  }
  // Create dummy entity storage object.
  $entity_storage = $this->prophesize(EntityStorageInterface::class);
  $entity_storage->loadByProperties([
    $field_name => $field_value,
  ])
    ->willReturn($entities);
  $this->entityTypeManager
    ->getStorage($entity_type)
    ->willReturn($entity_storage->reveal())
    ->shouldBeCalledTimes(1);
  // Set context values for EntityFetchByField action and execute.
  $this->action
    ->setContextValue('type', $entity_type)
    ->setContextValue('field_name', $field_name)
    ->setContextValue('field_value', $field_value)
    ->execute();
}