You are here

public function EntityStatusTest::testAlterItems in Search API 8

Tests if unpublished/inactive entities are removed from the indexed items.

File

tests/src/Unit/Processor/EntityStatusTest.php, line 132

Class

EntityStatusTest
Tests the "Entity status" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

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

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $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));
}