View source
<?php
namespace Drupal\Tests\feeds\Unit;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\feeds\EntityFinder;
class EntityFinderTest extends FeedsUnitTestCase {
protected $entityTypeManager;
protected $entityStorage;
protected $entityRepository;
public function setUp() {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityStorage = $this
->prophesize(EntityStorageInterface::class);
$this->entityTypeManager
->getStorage('foo')
->willReturn($this->entityStorage);
$this->entityRepository = $this
->prophesize(EntityRepositoryInterface::class);
}
protected function createEntityFinderInstance() {
return new EntityFinder($this->entityTypeManager
->reveal(), $this->entityRepository
->reveal());
}
public function testFindEntities() {
$entity_query = $this
->prophesize(QueryInterface::class);
$entity_query
->range(0, 1)
->willReturn($entity_query);
$entity_query
->condition('field_ref', 1)
->willReturn($entity_query);
$entity_query
->execute()
->willReturn([
12,
]);
$this->entityStorage
->getQuery()
->willReturn($entity_query)
->shouldBeCalled();
$entity_ids = $this
->createEntityFinderInstance()
->findEntities('foo', 'field_ref', 1);
$this
->assertEquals([
12,
], $entity_ids);
}
public function testFindEntitiesNotFound() {
$entity_query = $this
->prophesize(QueryInterface::class);
$entity_query
->range(0, 1)
->willReturn($entity_query);
$entity_query
->condition('field_ref', 1)
->willReturn($entity_query);
$entity_query
->execute()
->willReturn([]);
$this->entityStorage
->getQuery()
->willReturn($entity_query)
->shouldBeCalled();
$entity_ids = $this
->createEntityFinderInstance()
->findEntities('foo', 'field_ref', 1);
$this
->assertEquals([], $entity_ids);
}
public function testFindMultipleEntities() {
$entity_query = $this
->prophesize(QueryInterface::class);
$entity_query
->range(0, 1)
->shouldNotBeCalled();
$entity_query
->condition('field_ref', 1)
->willReturn($entity_query);
$entity_query
->execute()
->willReturn([
12,
13,
14,
]);
$this->entityStorage
->getQuery()
->willReturn($entity_query)
->shouldBeCalled();
$entity_ids = $this
->createEntityFinderInstance()
->findEntities('foo', 'field_ref', 1, [], TRUE);
$this
->assertEquals([
12,
13,
14,
], $entity_ids);
}
public function testFindEntitiesWithBundleRestriction() {
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->getKey('bundle')
->willReturn('type')
->shouldBeCalled();
$this->entityTypeManager
->getDefinition('foo')
->willReturn($entity_type
->reveal())
->shouldBeCalled();
$entity_query = $this
->prophesize(QueryInterface::class);
$entity_query
->condition('type', [
'qux',
], 'IN')
->willReturn($entity_query);
$entity_query
->range(0, 1)
->willReturn($entity_query);
$entity_query
->condition('field_ref', 1)
->willReturn($entity_query);
$entity_query
->execute()
->willReturn([
16,
]);
$this->entityStorage
->getQuery()
->willReturn($entity_query)
->shouldBeCalled();
$entity_ids = $this
->createEntityFinderInstance()
->findEntities('foo', 'field_ref', 1, [
'qux',
]);
$this
->assertEquals([
16,
], $entity_ids);
}
public function testFindEntitiesByUuid() {
$entity = $this
->prophesize(ContentEntityInterface::class);
$entity
->id()
->willReturn(17);
$this->entityRepository
->loadEntityByUuid('foo', '31835cb0-7302-403b-92ab-c228547d13fc')
->willReturn($entity);
$entity_ids = $this
->createEntityFinderInstance()
->findEntities('foo', 'uuid', '31835cb0-7302-403b-92ab-c228547d13fc');
$this
->assertEquals([
17,
], $entity_ids);
}
public function testFindEntitiesByUuidNotFound() {
$this->entityRepository
->loadEntityByUuid('foo', '31835cb0-7302-403b-92ab-c228547d13fc')
->willReturn(NULL);
$entity_ids = $this
->createEntityFinderInstance()
->findEntities('foo', 'uuid', '31835cb0-7302-403b-92ab-c228547d13fc');
$this
->assertEquals([], $entity_ids);
}
}