Newer
Older
Tom Verhaeghe
committed
<?php
/**
* @file
* Contains \Drupal\Tests\facets\Plugin\Processor\ExcludeSpecifiedItemsProcessorTest.
Tom Verhaeghe
committed
*/
Joris Vercammen
committed
namespace Drupal\Tests\facets\Unit\Plugin\processor;
Tom Verhaeghe
committed
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\ExcludeSpecifiedItemsProcessor;
use Drupal\facets\Result\Result;
Tom Verhaeghe
committed
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @group facets
Tom Verhaeghe
committed
*/
class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\processor\BuildProcessorInterface
Tom Verhaeghe
committed
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
Tom Verhaeghe
committed
*/
Tom Verhaeghe
committed
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
Tom Verhaeghe
committed
new Result('llama', 'llama', 10),
new Result('badger', 'badger', 5),
new Result('duck', 'duck', 15),
new Result('snbke', 'snbke', 10),
Tom Verhaeghe
committed
new Result('snake', 'snake', 10),
new Result('snaake', 'snaake', 10),
new Result('snaaake', 'snaaake', 10),
new Result('snaaaake', 'snaaaake', 10),
new Result('snaaaaake', 'snaaaaake', 10),
new Result('snaaaaaake', 'snaaaaaake', 10),
Tom Verhaeghe
committed
];
$processor_id = 'exclude_specified_items';
$this->processor = new ExcludeSpecifiedItemsProcessor([], $processor_id, []);
Tom Verhaeghe
committed
$processor_id => [
'id' => $processor_id,
'class' => 'Drupal\facets\Plugin\facets\processor\ExcludeSpecifiedItemsProcessor',
Tom Verhaeghe
committed
],
];
$manager = $this->getMockBuilder('Drupal\facets\Processor\ProcessorPluginManager')
Tom Verhaeghe
committed
->disableOriginalConstructor()
->getMock();
$manager->expects($this->once())
->method('getDefinitions')
Tom Verhaeghe
committed
$manager->expects($this->once())
->method('createInstance')
->willReturn($this->processor);
$container_builder = new ContainerBuilder();
$container_builder->set('plugin.manager.facets.processor', $manager);
Tom Verhaeghe
committed
\Drupal::setContainer($container_builder);
}
/**
* Test no filtering happens.
*/
public function testNoFilter() {
$facet = new Facet([], 'facet');
$facet->setResults($this->originalResults);
Tom Verhaeghe
committed
$facet->setOption('processors', [
'exclude_specified_items' => [
'settings' => [
'exclude' => 'alpaca',
Tom Verhaeghe
committed
],
],
]);
Christian Adamski
committed
$facet->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
Tom Verhaeghe
committed
$this->processor->setConfiguration([
'exclude' => 'alpaca',
Tom Verhaeghe
committed
]);
$filtered_results = $this->processor->build($facet, $this->originalResults);
Tom Verhaeghe
committed
$this->assertCount(count($this->originalResults), $filtered_results);
Tom Verhaeghe
committed
}
/**
* Test filtering happens for string filter.
Tom Verhaeghe
committed
*/
public function testStringFilter() {
$facet = new Facet([], 'facet');
$facet->setResults($this->originalResults);
Tom Verhaeghe
committed
$facet->setOption('processors', [
'exclude_specified_items' => [
'settings' => [
'exclude' => 'llama',
Tom Verhaeghe
committed
],
],
]);
Christian Adamski
committed
$facet->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
Tom Verhaeghe
committed
$this->processor->setConfiguration([
'exclude' => 'llama',
Tom Verhaeghe
committed
]);
$filtered_results = $this->processor->build($facet, $this->originalResults);
Tom Verhaeghe
committed
$this->assertCount((count($this->originalResults) - 1), $filtered_results);
foreach ($filtered_results as $result) {
$this->assertNotEquals('llama', $result->getDisplayValue());
}
Tom Verhaeghe
committed
}
/**
* Test filtering happens for regex filter.
Tom Verhaeghe
committed
* @dataProvider provideRegexTests
*/
public function testRegexFilter($regex, $expected_results) {
Tom Verhaeghe
committed
$facet = new Facet([], 'facet');
$facet->setResults($this->originalResults);
Tom Verhaeghe
committed
$facet->setOption('processors', [
'exclude_specified_items' => [
'settings' => [
'exclude' => $regex,
Tom Verhaeghe
committed
],
],
]);
Christian Adamski
committed
$facet->addProcessor([
'processor_id' => 'exclude_specified_items',
'weights' => [],
'settings' => [
'exclude' => 'alpaca',
'regex' => 0,
],
]);
Tom Verhaeghe
committed
$this->processor->setConfiguration([
'exclude' => $regex,
Tom Verhaeghe
committed
]);
$filtered_results = $this->processor->build($facet, $this->originalResults);
Tom Verhaeghe
committed
$this->assertCount(count($expected_results), $filtered_results);
Tom Verhaeghe
committed
foreach ($filtered_results as $res) {
$this->assertTrue(in_array($res->getDisplayValue(), $expected_results));
Tom Verhaeghe
committed
}
}
/**
* Provide multiple data sets for ::testRegexFilter.
Tom Verhaeghe
committed
*/
public function provideRegexTests() {
return [
[
'test',
[
'llama',
'duck',
'badger',
'snake',
'snaake',
'snaaake',
'snaaaake',
'snaaaaake',
'snaaaaaake',
'snbke',
],
Tom Verhaeghe
committed
],
[
'llama',
[
'badger',
'duck',
'snake',
'snaake',
'snaaake',
'snaaaake',
'snaaaaake',
'snaaaaaake',
'snbke',
],
Tom Verhaeghe
committed
],
[
'duck',
[
'llama',
'badger',
'snake',
'snaake',
'snaaake',
'snaaaake',
'snaaaaake',
'snaaaaaake',
'snbke',
],
Tom Verhaeghe
committed
],
[
'sn(.*)ke',
[
'llama',
'duck',
'badger',
],
Tom Verhaeghe
committed
],
[
'sn(a*)ke',
[
'llama',
'duck',
'badger',
'snbke',
],
Tom Verhaeghe
committed
],
[
'llama',
'duck',
'badger',
'snbke',
],
[
'llama',
'duck',
'badger',
'snake',
'snaake',
'snaaaaaake',
'snbke',
],
Tom Verhaeghe
committed
];
}