class RoleFilterTest in Search API 8
Tests the "Role filter" processor.
@group search_api
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api\Unit\Processor\RoleFilterTest uses TestItemsTrait
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\ProcessorView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
RoleFilterTest:: |
protected | property | The test items to use. | |
RoleFilterTest:: |
protected | property | The processor to be tested. | |
RoleFilterTest:: |
protected | function |
Creates a new processor object for use in the tests. Overrides UnitTestCase:: |
|
RoleFilterTest:: |
public | function | Tests preprocessing search items with an exclusive filter. | |
RoleFilterTest:: |
public | function | Tests preprocessing search items with an inclusive filter. | |
TestItemsTrait:: |
protected | property | The class container. | |
TestItemsTrait:: |
protected | property | The used item IDs for test items. | |
TestItemsTrait:: |
public | function | Creates a certain number of test items. | |
TestItemsTrait:: |
public | function | Creates an array with a single item which has the given field. | |
TestItemsTrait:: |
protected | function | Adds a container with several mock services commonly needed by our tests. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |