EntityFetchByFieldTest.php in Rules 8.3
File
tests/src/Unit/Integration/RulesAction/EntityFetchByFieldTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
class EntityFetchByFieldTest extends RulesEntityIntegrationTestBase {
protected $action;
protected function setUp() : void {
parent::setUp();
$this->action = $this->actionManager
->createInstance('rules_entity_fetch_by_field');
}
public function testSummary() {
$this
->assertEquals('Fetch entities by field', $this->action
->summary());
}
public function testActionExecutionWithNoLimit() {
$entity_type = 'entity_test';
$field_name = 'test_field';
$field_value = 'llama';
$entities = [];
for ($i = 0; $i < 2; $i++) {
$entity = $this
->prophesize(EntityInterface::class);
$entities[] = $entity
->reveal();
}
$entity_storage = $this
->prophesize(EntityStorageInterface::class);
$entity_storage
->loadByProperties([
$field_name => $field_value,
])
->willReturn($entities);
$this->entityTypeManager
->getStorage($entity_type)
->willReturn($entity_storage
->reveal());
$this->action
->setContextValue('type', $entity_type)
->setContextValue('field_name', $field_name)
->setContextValue('field_value', $field_value)
->execute();
$this
->assertEquals($entities, $this->action
->getProvidedContext('entity_fetched')
->getContextValue('entity_fetched'));
}
public function testActionExecutionWithLimit() {
$entity_type = 'entity_test';
$field_name = 'test_field';
$field_value = 'llama';
$limit = 2;
$entities = array_map(function () {
return $this
->prophesize(EntityInterface::class)
->reveal();
}, range(1, $limit));
$entity_ids = range(1, $limit);
$query = $this
->prophesize(QueryInterface::class);
$query
->condition($field_name, $field_value, '=')
->willReturn($query
->reveal())
->shouldBeCalledTimes(1);
$query
->range(0, $limit)
->willReturn($query
->reveal())
->shouldBeCalledTimes(1);
$query
->execute()
->willReturn($entity_ids)
->shouldBeCalledTimes(1);
$entity_storage = $this
->prophesize(EntityStorageInterface::class);
$entity_storage
->loadMultiple($entity_ids)
->willReturn($entities)
->shouldBeCalledTimes(1);
$entity_storage
->getQuery()
->willReturn($query)
->shouldBeCalledTimes(1);
$this->entityTypeManager
->getStorage($entity_type)
->willReturn($entity_storage
->reveal())
->shouldBeCalledTimes(1);
$this->action
->setContextValue('type', $entity_type)
->setContextValue('field_name', $field_name)
->setContextValue('field_value', $field_value)
->setContextValue('limit', $limit)
->execute();
$this
->assertEquals($entities, $this->action
->getProvidedContext('entity_fetched')
->getContextValue('entity_fetched'));
}
public function testActionExecutionProvidedContextEntityType() {
$entity_type = 'entity_test';
$field_name = 'test_field';
$field_value = 'llama';
$entities = [];
for ($i = 0; $i < 2; $i++) {
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$entities[] = $entity;
}
$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);
$this->action
->setContextValue('type', $entity_type)
->setContextValue('field_name', $field_name)
->setContextValue('field_value', $field_value)
->execute();
}
public function testRefiningContextDefinitions() {
$this->action
->setContextValue('type', 'entity_test');
$this->action
->refineContextDefinitions([]);
$this
->assertEquals($this->action
->getProvidedContextDefinition('entity_fetched')
->getDataType(), 'entity:entity_test');
}
}