class ExcludeSpecifiedItemsProcessorTest in Facets 8
Unit test for processor.
@group facets
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\facets\Unit\Plugin\processor\ExcludeSpecifiedItemsProcessorTest
Expanded class hierarchy of ExcludeSpecifiedItemsProcessorTest
File
- tests/
src/ Unit/ Plugin/ processor/ ExcludeSpecifiedItemsProcessorTest.php, line 17
Namespace
Drupal\Tests\facets\Unit\Plugin\processorView source
class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\processor\BuildProcessorInterface
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
$this->originalResults = [
new Result($facet, 'llama', 'llama', 10),
new Result($facet, 'badger', 'badger', 5),
new Result($facet, 'duck', 'duck', 15),
new Result($facet, 'snbke', 'snbke', 10),
new Result($facet, 'snake', 'snake', 10),
new Result($facet, 'snaake', 'snaake', 10),
new Result($facet, 'snaaake', 'snaaake', 10),
new Result($facet, 'snaaaake', 'snaaaake', 10),
new Result($facet, 'snaaaaake', 'snaaaaake', 10),
new Result($facet, 'snaaaaaake', 'snaaaaaake', 10),
];
$processor_id = 'exclude_specified_items';
$this->processor = new ExcludeSpecifiedItemsProcessor([], $processor_id, [
'id' => "display_value_widget_order",
'label' => "Sort by display value",
'description' => "Sorts the widget results by display value.",
'default_enabled' => TRUE,
'stages' => [
"build" => 50,
],
]);
$processor_definitions = [
$processor_id => [
'id' => $processor_id,
'class' => ExcludeSpecifiedItemsProcessor::class,
],
];
$manager = $this
->getMockBuilder(ProcessorPluginManager::class)
->disableOriginalConstructor()
->getMock();
$manager
->expects($this
->any())
->method('getDefinitions')
->willReturn($processor_definitions);
$manager
->expects($this
->any())
->method('createInstance')
->willReturn($this->processor);
$container_builder = new ContainerBuilder();
$container_builder
->set('plugin.manager.facets.processor', $manager);
\Drupal::setContainer($container_builder);
}
/**
* Tests no filtering happens.
*/
public function testNoFilter() {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
$this->processor
->setConfiguration([
'exclude' => 'alpaca',
'regex' => 0,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(count($this->originalResults), $filtered_results);
}
/**
* Tests filtering happens for string filter.
*/
public function testStringFilter() {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
$this->processor
->setConfiguration([
'exclude' => 'llama',
'regex' => 0,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(count($this->originalResults) - 1, $filtered_results);
foreach ($filtered_results as $result) {
$this
->assertNotEquals('llama', $result
->getDisplayValue());
}
}
/**
* Tests filtering happens for string filter.
*/
public function testMultiString() {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
$this->processor
->setConfiguration([
'exclude' => 'llama,badger',
'regex' => 0,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(count($this->originalResults) - 2, $filtered_results);
foreach ($filtered_results as $result) {
$this
->assertNotEquals('llama', $result
->getDisplayValue());
$this
->assertNotEquals('badger', $result
->getDisplayValue());
}
}
/**
* Tests filtering happens for string filter.
*/
public function testMultiStringTrim() {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
$this->processor
->setConfiguration([
'exclude' => 'llama, badger',
'regex' => 0,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(count($this->originalResults) - 2, $filtered_results);
foreach ($filtered_results as $result) {
$this
->assertNotEquals('llama', $result
->getDisplayValue());
$this
->assertNotEquals('badger', $result
->getDisplayValue());
}
}
/**
* Tests invert filtering happens for string filter.
*/
public function testInvertStringFilter() {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
'invert' => 1,
],
]);
$this->processor
->setConfiguration([
'exclude' => 'llama',
'regex' => 0,
'invert' => 1,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(1, $filtered_results);
foreach ($filtered_results as $result) {
$this
->assertEquals('llama', $result
->getDisplayValue());
}
}
/**
* Tests filtering happens for string filter.
*/
public function testInvertMultiString() {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
'invert' => 1,
],
]);
$this->processor
->setConfiguration([
'exclude' => 'llama,badger',
'regex' => 0,
'invert' => 1,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(2, $filtered_results);
$filtered_results_values = [];
foreach ($filtered_results as $result) {
$filtered_results_values[] = $result
->getDisplayValue();
}
$this
->assertContains('llama', $filtered_results_values);
$this
->assertContains('badger', $filtered_results_values);
}
/**
* Tests filtering happens for regex filter.
*
* @dataProvider provideRegexTests
*/
public function testRegexFilter($regex, $expected_results) {
$facet = new Facet([], 'facets_facet');
$facet
->setResults($this->originalResults);
$facet
->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
$this->processor
->setConfiguration([
'exclude' => $regex,
'regex' => 1,
]);
$filtered_results = $this->processor
->build($facet, $this->originalResults);
$this
->assertCount(count($expected_results), $filtered_results);
foreach ($filtered_results as $res) {
$this
->assertContains($res
->getDisplayValue(), $expected_results);
}
}
/**
* Provides multiple data sets for ::testRegexFilter.
*/
public function provideRegexTests() {
return [
[
'test',
[
'llama',
'duck',
'badger',
'snake',
'snaake',
'snaaake',
'snaaaake',
'snaaaaake',
'snaaaaaake',
'snbke',
],
],
[
'llama',
[
'badger',
'duck',
'snake',
'snaake',
'snaaake',
'snaaaake',
'snaaaaake',
'snaaaaaake',
'snbke',
],
],
[
'duck',
[
'llama',
'badger',
'snake',
'snaake',
'snaaake',
'snaaaake',
'snaaaaake',
'snaaaaaake',
'snbke',
],
],
[
'sn(.*)ke',
[
'llama',
'duck',
'badger',
],
],
[
'sn(a*)ke',
[
'llama',
'duck',
'badger',
'snbke',
],
],
[
'sn(a+)ke',
[
'llama',
'duck',
'badger',
'snbke',
],
],
[
'sn(a{3,5})ke',
[
'llama',
'duck',
'badger',
'snake',
'snaake',
'snaaaaaake',
'snbke',
],
],
];
}
/**
* Tests configuration.
*/
public function testConfiguration() {
$config = $this->processor
->defaultConfiguration();
$this
->assertEquals([
'exclude' => '',
'regex' => 0,
'invert' => 0,
], $config);
}
/**
* Tests testDescription().
*/
public function testDescription() {
$this
->assertEquals('Sorts the widget results by display value.', $this->processor
->getDescription());
}
/**
* Tests isHidden().
*/
public function testIsHidden() {
$this
->assertEquals(FALSE, $this->processor
->isHidden());
}
/**
* Tests isLocked().
*/
public function testIsLocked() {
$this
->assertEquals(FALSE, $this->processor
->isLocked());
}
/**
* Tests supportsStage().
*/
public function testSupportsStage() {
$this
->assertTrue($this->processor
->supportsStage('build'));
$this
->assertFalse($this->processor
->supportsStage('sort'));
}
/**
* Tests getDefaultWeight().
*/
public function testGetDefaultWeight() {
$this
->assertEquals(50, $this->processor
->getDefaultWeight('build'));
$this
->assertEquals(0, $this->processor
->getDefaultWeight('sort'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExcludeSpecifiedItemsProcessorTest:: |
protected | property | An array containing the results before the processor has ran. | |
ExcludeSpecifiedItemsProcessorTest:: |
protected | property | The processor to be tested. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Provides multiple data sets for ::testRegexFilter. | |
ExcludeSpecifiedItemsProcessorTest:: |
protected | function |
Creates a new processor object for use in the tests. Overrides UnitTestCase:: |
|
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests configuration. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests testDescription(). | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests getDefaultWeight(). | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests filtering happens for string filter. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests invert filtering happens for string filter. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests isHidden(). | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests isLocked(). | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests filtering happens for string filter. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests filtering happens for string filter. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests no filtering happens. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests filtering happens for regex filter. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests filtering happens for string filter. | |
ExcludeSpecifiedItemsProcessorTest:: |
public | function | Tests supportsStage(). | |
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. | |
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. |