You are here

class RoleFilterTest in Search API 8

Tests the "Role filter" processor.

@group search_api

Hierarchy

Expanded class hierarchy of RoleFilterTest

See also

\Drupal\search_api\Plugin\search_api\processor\RoleFilter

File

tests/src/Unit/Processor/RoleFilterTest.php, line 21

Namespace

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

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

  /**
   * The test items to use.
   *
   * @var \Drupal\search_api\Item\ItemInterface[]
   */
  protected $items = [];

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setUpMockContainer();
    $this->processor = new RoleFilter([], 'role_filter', []);

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $this
      ->createMock(IndexInterface::class);
    $node_datasource = $this
      ->createMock(DatasourceInterface::class);
    $node_datasource
      ->expects($this
      ->any())
      ->method('getEntityTypeId')
      ->will($this
      ->returnValue('node'));

    /** @var \Drupal\search_api\Datasource\DatasourceInterface $node_datasource */
    $user_datasource = $this
      ->createMock(DatasourceInterface::class);
    $user_datasource
      ->expects($this
      ->any())
      ->method('getEntityTypeId')
      ->will($this
      ->returnValue('user'));

    /** @var \Drupal\search_api\Datasource\DatasourceInterface $user_datasource */
    $fields_helper = \Drupal::getContainer()
      ->get('search_api.fields_helper');
    $item = $fields_helper
      ->createItem($index, Utility::createCombinedId('entity:node', '1:en'), $node_datasource);
    $node = $this
      ->getMockBuilder(TestNodeInterface::class)
      ->disableOriginalConstructor()
      ->getMock();

    /** @var \Drupal\node\NodeInterface $node */
    $item
      ->setOriginalObject(EntityAdapter::createFromEntity($node));
    $this->items[$item
      ->getId()] = $item;
    $item = $fields_helper
      ->createItem($index, Utility::createCombinedId('entity:user', '1:en'), $user_datasource);
    $account1 = $this
      ->getMockBuilder(TestUserInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $account1
      ->expects($this
      ->any())
      ->method('getRoles')
      ->will($this
      ->returnValue([
      'authenticated' => 'authenticated',
      'editor' => 'editor',
    ]));

    /** @var \Drupal\user\UserInterface $account1 */
    $item
      ->setOriginalObject(EntityAdapter::createFromEntity($account1));
    $this->items[$item
      ->getId()] = $item;
    $item = $fields_helper
      ->createItem($index, Utility::createCombinedId('entity:user', '2:en'), $user_datasource);
    $account2 = $this
      ->getMockBuilder(TestUserInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $account2
      ->expects($this
      ->any())
      ->method('getRoles')
      ->will($this
      ->returnValue([
      'authenticated' => 'authenticated',
    ]));

    /** @var \Drupal\user\UserInterface $account2 */
    $item
      ->setOriginalObject(EntityAdapter::createFromEntity($account2));
    $this->items[$item
      ->getId()] = $item;
  }

  /**
   * Tests preprocessing search items with an inclusive filter.
   */
  public function testFilterInclusive() {
    $configuration['roles'] = [
      'authenticated',
    ];
    $configuration['default'] = 0;
    $this->processor
      ->setConfiguration($configuration);
    $this->processor
      ->alterIndexedItems($this->items);
    $this
      ->assertTrue(!empty($this->items[Utility::createCombinedId('entity:user', '1:en')]), 'User with two roles was not removed.');
    $this
      ->assertTrue(!empty($this->items[Utility::createCombinedId('entity:user', '2:en')]), 'User with only the authenticated role was not removed.');
    $this
      ->assertTrue(!empty($this->items[Utility::createCombinedId('entity:node', '1:en')]), 'Node item was not removed.');
  }

  /**
   * Tests preprocessing search items with an exclusive filter.
   */
  public function testFilterExclusive() {
    $configuration['roles'] = [
      'editor',
    ];
    $configuration['default'] = 1;
    $this->processor
      ->setConfiguration($configuration);
    $this->processor
      ->alterIndexedItems($this->items);
    $this
      ->assertTrue(empty($this->items[Utility::createCombinedId('entity:user', '1:en')]), 'User with editor role was successfully removed.');
    $this
      ->assertTrue(!empty($this->items[Utility::createCombinedId('entity:user', '2:en')]), 'User without the editor role was not removed.');
    $this
      ->assertTrue(!empty($this->items[Utility::createCombinedId('entity:node', '1:en')]), 'Node item was not removed.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
RoleFilterTest::$items protected property The test items to use.
RoleFilterTest::$processor protected property The processor to be tested.
RoleFilterTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
RoleFilterTest::testFilterExclusive public function Tests preprocessing search items with an exclusive filter.
RoleFilterTest::testFilterInclusive public function Tests preprocessing search items with an inclusive filter.
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.