View source
<?php
namespace Drupal\Tests\search_api\Unit\Processor;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\search_api\processor\EntityStatus;
use Drupal\search_api\Utility\Utility;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
class EntityStatusTest extends UnitTestCase {
use TestItemsTrait;
protected $processor;
protected $index;
protected $datasources = [];
protected function setUp() {
parent::setUp();
$this
->setUpMockContainer();
$this->processor = new EntityStatus([], 'entity_status', []);
$this->index = $this
->createMock(IndexInterface::class);
foreach ([
'node',
'comment',
'user',
'file',
] as $entity_type) {
$datasource = $this
->createMock(DatasourceInterface::class);
$datasource
->expects($this
->any())
->method('getEntityTypeId')
->will($this
->returnValue($entity_type));
$this->datasources["entity:{$entity_type}"] = $datasource;
}
}
public function testSupportsIndex(array $datasource_ids = NULL, $expected) {
if ($datasource_ids !== NULL) {
$datasource_ids = array_flip($datasource_ids);
$this->datasources = array_intersect_key($this->datasources, $datasource_ids);
}
$this->index
->method('getDatasources')
->will($this
->returnValue($this->datasources));
$self = $this;
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->method('getDefinition')
->willReturnCallback(function ($entity_type_id) use ($self) {
$entity_type = $self
->createMock(EntityTypeInterface::class);
$publishable = in_array($entity_type_id, [
'node',
'comment',
]);
$entity_type
->method('entityClassImplements')
->willReturnMap([
[
EntityPublishedInterface::class,
$publishable,
],
]);
return $entity_type;
});
$this->container
->set('entity_type.manager', $entity_type_manager);
$this
->assertEquals($expected, EntityStatus::supportsIndex($this->index));
}
public function supportsIndexDataProvider() {
return [
'all datasources' => [
NULL,
TRUE,
],
'node datasource' => [
[
'entity:node',
],
TRUE,
],
'comment datasource' => [
[
'entity:comment',
],
TRUE,
],
'user datasource' => [
[
'entity:user',
],
TRUE,
],
'file datasource' => [
[
'entity:file',
],
FALSE,
],
];
}
public function testAlterItems() {
$entity_types = [
'node' => [
'class' => Node::class,
'method' => 'isPublished',
],
'comment' => [
'class' => Comment::class,
'method' => 'isPublished',
],
'user' => [
'class' => User::class,
'method' => 'isActive',
],
'file' => [
'class' => File::class,
],
];
$fields_helper = \Drupal::getContainer()
->get('search_api.fields_helper');
$items = [];
foreach ($entity_types as $entity_type => $info) {
$datasource_id = "entity:{$entity_type}";
foreach ([
1 => TRUE,
2 => FALSE,
] as $i => $status) {
$item_id = Utility::createCombinedId($datasource_id, "{$i}:en");
$item = $fields_helper
->createItem($this->index, $item_id, $this->datasources[$datasource_id]);
$entity = $this
->getMockBuilder($info['class'])
->disableOriginalConstructor()
->getMock();
if (isset($info['method'])) {
$entity
->method($info['method'])
->will($this
->returnValue($status));
}
$item
->setOriginalObject(EntityAdapter::createFromEntity($entity));
$items[$item_id] = $item;
}
}
$this->processor
->alterIndexedItems($items);
$expected = [
Utility::createCombinedId('entity:node', '1:en'),
Utility::createCombinedId('entity:comment', '1:en'),
Utility::createCombinedId('entity:user', '1:en'),
Utility::createCombinedId('entity:file', '1:en'),
Utility::createCombinedId('entity:file', '2:en'),
];
$this
->assertEquals($expected, array_keys($items));
}
}