summaryrefslogtreecommitdiffstats
path: root/tests/src/Unit/Plugin
diff options
context:
space:
mode:
authorheartdriven2015-11-09 12:42:35 (GMT)
committerJoris Vercammen2015-11-09 12:42:35 (GMT)
commit11a509d9d54386cf69efe24675b230227cca551a (patch)
tree293b6e57ffa2ead1c8f1e650ee416486aa7656c0 /tests/src/Unit/Plugin
parent566c5a60692f67004304a43a3f30aadca55fb4db (diff)
Issue #2598294 by borisson_, Tom Verhaeghe: Create Facet processors - Exclude specified items
Diffstat (limited to 'tests/src/Unit/Plugin')
-rw-r--r--tests/src/Unit/Plugin/processor/ExcludeSpecifiedItemsProcessorTest.php178
1 files changed, 178 insertions, 0 deletions
diff --git a/tests/src/Unit/Plugin/processor/ExcludeSpecifiedItemsProcessorTest.php b/tests/src/Unit/Plugin/processor/ExcludeSpecifiedItemsProcessorTest.php
new file mode 100644
index 0000000..9e927e4
--- /dev/null
+++ b/tests/src/Unit/Plugin/processor/ExcludeSpecifiedItemsProcessorTest.php
@@ -0,0 +1,178 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\Tests\facetapi\Plugin\Processor\ExcludeSpecifiedItemsProcessorTest.
+ */
+
+namespace Drupal\Tests\facetapi\Unit\Plugin\Processor;
+
+use Drupal\facetapi\Entity\Facet;
+use Drupal\facetapi\Plugin\facetapi\processor\ExcludeSpecifiedItemsProcessor;
+use Drupal\facetapi\Result\Result;
+use Drupal\Tests\UnitTestCase;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * @group facetapi
+ */
+class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {
+
+ /**
+ * The processor to be tested.
+ *
+ * @var \Drupal\facetapi\processor\BuildProcessorInterface
+ */
+ protected $processor;
+
+ /**
+ * An array containing the results before the processor has ran.
+ *
+ * @var \Drupal\facetapi\Result\Result[]
+ */
+ protected $original_results;
+
+ /**
+ * Creates a new processor object for use in the tests.
+ */
+ protected function setUp() {
+ parent::setUp();
+
+ $this->original_results = [
+ new Result('llama', 'llama', 10),
+ new Result('badger', 'badger', 5),
+ new Result('duck', 'duck', 15),
+ 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('snbke', 'snbke', 10),
+ ];
+
+ $processor_id = 'exclude_specified_items';
+ $this->processor = new ExcludeSpecifiedItemsProcessor([], $processor_id, []);
+
+ $processorDefinitions = [
+ $processor_id => [
+ 'id' => $processor_id,
+ 'class' => 'Drupal\facetapi\Plugin\facetapi\processor\ExcludeSpecifiedItemsProcessor',
+ ],
+ ];
+
+ $manager = $this->getMockBuilder('Drupal\facetapi\Processor\ProcessorPluginManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $manager->expects($this->once())
+ ->method('getDefinitions')
+ ->willReturn($processorDefinitions);
+ $manager->expects($this->once())
+ ->method('createInstance')
+ ->willReturn($this->processor);
+
+ $container_builder = new ContainerBuilder();
+ $container_builder->set('plugin.manager.facetapi.processor', $manager);
+ \Drupal::setContainer($container_builder);
+ }
+
+ /**
+ * Test no filtering happens.
+ */
+ public function testNoFilter() {
+ $facet = new Facet([], 'facet');
+ $facet->setResults($this->original_results);
+ $facet->setOption('processors', [
+ 'exclude_specified_items' => [
+ 'settings' => [
+ 'exclude' => 'alpaca',
+ 'regex' => 0
+ ],
+ ],
+ ]);
+ $this->processor->setConfiguration([
+ 'exclude' => 'alpaca',
+ 'regex' => 0
+ ]);
+ $filtered_results = $this->processor->build($facet, $this->original_results);
+
+ $this->assertCount(count($this->original_results), $filtered_results);
+ }
+
+ /**
+ * Test filtering happens for string filter
+ */
+ public function testStringFilter() {
+ $facet = new Facet([], 'facet');
+ $facet->setResults($this->original_results);
+ $facet->setOption('processors', [
+ 'exclude_specified_items' => [
+ 'settings' => [
+ 'exclude' => 'llama',
+ 'regex' => 0
+ ],
+ ],
+ ]);
+ $this->processor->setConfiguration([
+ 'exclude' => 'llama',
+ 'regex' => 0
+ ]);
+ $filtered_results = $this->processor->build($facet, $this->original_results);
+
+ $this->assertCount((count($this->original_results) -1), $filtered_results);
+ }
+
+ /**
+ * Test filtering happens for string filter
+ * @dataProvider provideRegexTests
+ */
+ public function testRegexFilter($regex, $expectedResults) {
+ $facet = new Facet([], 'facet');
+ $facet->setResults($this->original_results);
+ $facet->setOption('processors', [
+ 'exclude_specified_items' => [
+ 'settings' => [
+ 'exclude' => $regex,
+ 'regex' => 1
+ ],
+ ],
+ ]);
+ $this->processor->setConfiguration([
+ 'exclude' => $regex,
+ 'regex' => 1
+ ]);
+ $filtered_results = $this->processor->build($facet, $this->original_results);
+
+ $this->assertCount(count($expectedResults), $filtered_results);
+
+ foreach ($filtered_results as $res) {
+ $this->assertTrue(in_array($res->getDisplayValue(), $expectedResults));
+ }
+ }
+
+ /**
+ * Provide multiple data sets for ::testRegexFilter
+ */
+ public function provideRegexTests() {
+ return [
+ [
+ 'test',
+ ['llama', 'duck', 'badger', 'snake', 'snaake', 'snaaake', 'snaaaake', 'snaaaaake', 'snbke']
+ ],
+ [
+ 'llama',
+ ['badger', 'duck', 'snake', 'snaake', 'snaaake', 'snaaaake', 'snaaaaake', 'snbke']
+ ],
+ [
+ 'duck',
+ ['llama', 'badger', 'snake', 'snaake', 'snaaake', 'snaaaake', 'snaaaaake', 'snbke']
+ ],
+ [
+ 'sn(.*)ke',
+ ['llama', 'duck', 'badger']
+ ],
+ [
+ 'sn(a*)ke',
+ ['llama', 'duck', 'badger', 'snbke']
+ ],
+ ];
+ }
+}