You are here

class EntityStatusTest in Search API 8

Tests the "Entity status" processor.

@group search_api

Hierarchy

Expanded class hierarchy of EntityStatusTest

File

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

Namespace

Drupal\Tests\search_api\Unit\Processor
View source
class EntityStatusTest extends UnitTestCase {
  use TestItemsTrait;

  /**
   * The processor to be tested.
   *
   * @var \Drupal\search_api\Plugin\search_api\processor\EntityStatus
   */
  protected $processor;

  /**
   * The test index.
   *
   * @var \Drupal\search_api\IndexInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $index;

  /**
   * The test index's potential datasources.
   *
   * @var \Drupal\search_api\Datasource\DatasourceInterface[]
   */
  protected $datasources = [];

  /**
   * Creates a new processor object for use in the tests.
   */
  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;
    }
  }

  /**
   * Tests whether supportsIndex() returns TRUE for an index containing nodes.
   *
   * @param string[]|null $datasource_ids
   *   The IDs of datasources the index should have, or NULL if it should have
   *   all of them.
   * @param bool $expected
   *   Whether the processor is supposed to support that index.
   *
   * @dataProvider supportsIndexDataProvider
   */
  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));

    // In supportsIndex(), the entity status processor will use the entity type
    // manager to get the definition of each datasource's entity type and then
    // check whether it implements \Drupal\Core\Entity\EntityPublishedInterface.
    // We therefore need to ensure each of these calls returns an appropriate
    // value.
    $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));
  }

  /**
   * Provides data for the testSupportsIndex() tests.
   *
   * @return array[]
   *   Array of parameter arrays for testSupportsIndex().
   */
  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,
      ],
    ];
  }

  /**
   * Tests if unpublished/inactive entities are removed from the indexed items.
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityStatusTest::$datasources protected property The test index's potential datasources.
EntityStatusTest::$index protected property The test index.
EntityStatusTest::$processor protected property The processor to be tested.
EntityStatusTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
EntityStatusTest::supportsIndexDataProvider public function Provides data for the testSupportsIndex() tests.
EntityStatusTest::testAlterItems public function Tests if unpublished/inactive entities are removed from the indexed items.
EntityStatusTest::testSupportsIndex public function Tests whether supportsIndex() returns TRUE for an index containing nodes.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TestItemsTrait::$container protected property The class container.
TestItemsTrait::$itemIds protected property The used item IDs for test items.
TestItemsTrait::createItems public function Creates a certain number of test items.
TestItemsTrait::createSingleFieldItem public function Creates an array with a single item which has the given field.
TestItemsTrait::setUpMockContainer protected function Adds a container with several mock services commonly needed by our tests.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.