summaryrefslogtreecommitdiffstats
path: root/tests/src/Unit/Plugin
diff options
context:
space:
mode:
authorJoris Vercammen2015-11-08 13:35:27 (GMT)
committerJoris Vercammen2015-11-08 13:35:27 (GMT)
commitba1d5de3c0f7f01deddd458c5e64a5599b82517e (patch)
treed46426421c97bf9c9bc04f5ad62b89fd87e6e58b /tests/src/Unit/Plugin
parentb7040f3fb979cc4f9b9f7d86020d39ba2cd61eed (diff)
Issue #2608228: Add tests for widgets
Diffstat (limited to 'tests/src/Unit/Plugin')
-rw-r--r--tests/src/Unit/Plugin/widget/CheckboxWidgetTest.php91
-rw-r--r--tests/src/Unit/Plugin/widget/LinksWidgetTest.php130
2 files changed, 221 insertions, 0 deletions
diff --git a/tests/src/Unit/Plugin/widget/CheckboxWidgetTest.php b/tests/src/Unit/Plugin/widget/CheckboxWidgetTest.php
new file mode 100644
index 0000000..2dfae71
--- /dev/null
+++ b/tests/src/Unit/Plugin/widget/CheckboxWidgetTest.php
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\facetapi\Plugin\widget\CheckboxWidgetTest.
+ */
+
+namespace Drupal\Tests\facetapi\Unit\Plugin\widget;
+
+use Drupal\facetapi\Entity\Facet;
+use Drupal\facetapi\Plugin\facetapi\Widget\CheckboxWidget;
+use Drupal\facetapi\Result\Result;
+use Drupal\Tests\UnitTestCase;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * @group facetapi
+ */
+class CheckboxWidgetTest extends UnitTestCase {
+
+ /**
+ * The processor to be tested.
+ *
+ * @var \drupal\facetapi\Widget\WidgetInterface
+ */
+ protected $widget;
+
+ /**
+ * 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();
+
+ /** @var \Drupal\facetapi\Result\Result[] $original_results */
+ $original_results = [
+ new Result('llama', 'Llama', 10),
+ new Result('badger', 'Badger', 20),
+ new Result('duck', 'Duck', 15),
+ new Result('alpaca', 'Alpaca', 9),
+ ];
+
+ foreach ($original_results as $original_result) {
+ $original_result->setUrl(new \Drupal\Core\Url('test'));
+ }
+ $this->original_results = $original_results;
+
+ $link_generator = $this->getMockBuilder('\Drupal\Core\Utility\LinkGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $link_generator->expects($this->atLeastOnce())
+ ->method('generate')
+ ->will($this->returnArgument(0));
+
+ $string_translation = $this->getMockBuilder('\Drupal\Core\StringTranslation\TranslationManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $container_builder = new ContainerBuilder();
+ $container_builder->set('link_generator', $link_generator);
+ $container_builder->set('string_translation', $string_translation);
+ \Drupal::setContainer($container_builder);
+
+ $this->widget = new CheckboxWidget();
+ }
+
+ /**
+ * Test widget
+ */
+ public function testNoFilterResults() {
+ $facet = new Facet([], 'facet');
+ $facet->setResults($this->original_results);
+
+ $output = $this->widget->build($facet);
+
+ $this->assertInternalType('array', $output);
+ $this->assertCount(4, $output['#items']);
+
+ $expected_links = ['Llama (10)', 'Badger (20)', 'Duck (15)', 'Alpaca (9)'];
+ foreach ($expected_links as $index => $value) {
+ $this->assertEquals($value, $output['#items'][$index]);
+ }
+ }
+
+}
diff --git a/tests/src/Unit/Plugin/widget/LinksWidgetTest.php b/tests/src/Unit/Plugin/widget/LinksWidgetTest.php
new file mode 100644
index 0000000..8d57e76
--- /dev/null
+++ b/tests/src/Unit/Plugin/widget/LinksWidgetTest.php
@@ -0,0 +1,130 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\facetapi\Plugin\widget\LinksWidgetTest.
+ */
+
+namespace Drupal\Tests\facetapi\Unit\Plugin\widget;
+
+use Drupal\facetapi\Entity\Facet;
+use Drupal\facetapi\Plugin\facetapi\Widget\LinksWidget;
+use Drupal\facetapi\Result\Result;
+use Drupal\Tests\UnitTestCase;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * @group facetapi
+ */
+class LinksWidgetTest extends UnitTestCase {
+
+ /**
+ * The processor to be tested.
+ *
+ * @var \drupal\facetapi\Widget\WidgetInterface
+ */
+ protected $widget;
+
+ /**
+ * 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();
+
+ /** @var \Drupal\facetapi\Result\Result[] $original_results */
+ $original_results = [
+ new Result('llama', 'Llama', 10),
+ new Result('badger', 'Badger', 20),
+ new Result('duck', 'Duck', 15),
+ new Result('alpaca', 'Alpaca', 9),
+ ];
+
+ foreach ($original_results as $original_result) {
+ $original_result->setUrl(new \Drupal\Core\Url('test'));
+ }
+ $this->original_results = $original_results;
+
+ $link_generator = $this->getMockBuilder('\Drupal\Core\Utility\LinkGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $link_generator->expects($this->atLeastOnce())
+ ->method('generate')
+ ->will($this->returnArgument(0));
+
+ $container_builder = new ContainerBuilder();
+ $container_builder->set('link_generator', $link_generator);
+ \Drupal::setContainer($container_builder);
+
+ $this->widget = new LinksWidget();
+ }
+
+ /**
+ * Test widget
+ */
+ public function testNoFilterResults() {
+ $facet = new Facet([], 'facet');
+ $facet->setResults($this->original_results);
+
+ $output = $this->widget->build($facet);
+
+ $this->assertInternalType('array', $output);
+ $this->assertCount(4, $output['#items']);
+
+ $expected_links = ['Llama (10)', 'Badger (20)', 'Duck (15)', 'Alpaca (9)'];
+ foreach ($expected_links as $index => $value) {
+ $this->assertEquals($value, $output['#items'][$index]);
+ }
+ }
+
+ /**
+ * Test widget
+ */
+ public function testHideEmptyCount() {
+ $original_results = $this->original_results;
+ $original_results[1] = new Result('badger', 'Badger', 0);
+
+ $facet = new Facet([], 'facet');
+ $facet->setResults($original_results);
+
+ $output = $this->widget->build($facet);
+
+ $this->assertInternalType('array', $output);
+ $this->assertCount(3, $output['#items']);
+
+ $expected_links = ['Llama (10)', 'Duck (15)', 'Alpaca (9)'];
+ foreach ($expected_links as $index => $value) {
+ $this->assertEquals($value, $output['#items'][$index]);
+ }
+ }
+
+ /**
+ * Test widget
+ */
+ public function testActiveItems() {
+ $original_results = $this->original_results;
+ $original_results[0]->setActiveState(TRUE);
+ $original_results[3]->setActiveState(TRUE);
+
+ $facet = new Facet([], 'facet');
+ $facet->setResults($original_results);
+
+ $output = $this->widget->build($facet);
+
+ $this->assertInternalType('array', $output);
+ $this->assertCount(4, $output['#items']);
+
+ $expected_links = ['(-) Llama (10)', 'Badger (20)', 'Duck (15)', '(-) Alpaca (9)'];
+ foreach ($expected_links as $index => $value) {
+ $this->assertEquals($value, $output['#items'][$index]);
+ }
+ }
+
+}
+