summaryrefslogtreecommitdiffstats
path: root/tests/src/Unit/Plugin/processor/CountLimitProcessorTest.php
blob: ed87d3db7682f6aed8add8b340493cb69d3003d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

/**
 * @file
 * Contains \Drupal\Tests\facets\Plugin\Processor\CountLimitProcessorTest.
 */

namespace Drupal\Tests\facets\Unit\Plugin\processor;

use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\CountLimitProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Unit test for processor.
 *
 * @group facets
 */
class CountLimitProcessorTest 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();

    $this->originalResults = [
      new Result('llama', 'llama', 10),
      new Result('badger', 'badger', 5),
      new Result('duck', 'duck', 15),
    ];

    $processor_id = 'count_limit';
    $this->processor = new CountLimitProcessor([], $processor_id, []);

    $processor_definitions = [
      $processor_id => [
        'id' => $processor_id,
        'class' => 'Drupal\facets\Plugin\facets\processor\CountLimitProcessor',
      ],
    ];

    $manager = $this->getMockBuilder('Drupal\facets\Processor\ProcessorPluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $manager->expects($this->once())
      ->method('getDefinitions')
      ->willReturn($processor_definitions);
    $manager->expects($this->once())
      ->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([], 'facet');
    $facet->setResults($this->originalResults);
    $facet->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => ['minimum_items' => 4],
    ]);
    $this->processor->setConfiguration(['minimum_items' => 4]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);

    $this->assertCount(3, $sorted_results);

    $this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
    $this->assertEquals('badger', $sorted_results[1]->getDisplayValue());
    $this->assertEquals('duck', $sorted_results[2]->getDisplayValue());
  }

  /**
   * Tests no filtering happens.
   */
  public function testMinEqualsValue() {
    $facet = new Facet([], 'facet');
    $facet->setResults($this->originalResults);
    $facet->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => ['minimum_items' => 5],
    ]);
    $this->processor->setConfiguration(['minimum_items' => 5]);

    $sorted_results = $this->processor->build($facet, $this->originalResults);

    $this->assertCount(3, $sorted_results);

    $this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
    $this->assertEquals('badger', $sorted_results[1]->getDisplayValue());
    $this->assertEquals('duck', $sorted_results[2]->getDisplayValue());
  }

  /**
   * Tests between minimum and maximum values.
   */
  public function testBetweenMinAndMaxValue() {
    $facet = new Facet([], 'facet');
    $facet->setResults($this->originalResults);
    $facet->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [],
    ]);

    $this->processor->setConfiguration(['minimum_items' => 6, 'maximum_items' => 14]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);
    $this->assertCount(1, $sorted_results);
    $this->assertEquals('llama', $sorted_results[0]->getDisplayValue());

    $this->processor->setConfiguration(['minimum_items' => 60, 'maximum_items' => 140]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);
    $this->assertCount(0, $sorted_results);

    $this->processor->setConfiguration(['minimum_items' => 1, 'maximum_items' => 10]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);
    $this->assertCount(2, $sorted_results);
  }

  /**
   * Tests maximum values.
   */
  public function testMaxValue() {
    $facet = new Facet([], 'facet');
    $facet->setResults($this->originalResults);
    $facet->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [],
    ]);

    $this->processor->setConfiguration(['maximum_items' => 14]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);
    $this->assertCount(2, $sorted_results);
    $this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
    $this->assertEquals('badger', $sorted_results[1]->getDisplayValue());

    $this->processor->setConfiguration(['maximum_items' => 140]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);
    $this->assertCount(3, $sorted_results);
    $this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
    $this->assertEquals('badger', $sorted_results[1]->getDisplayValue());
    $this->assertEquals('duck', $sorted_results[2]->getDisplayValue());

    $this->processor->setConfiguration(['maximum_items' => 1]);
    $sorted_results = $this->processor->build($facet, $this->originalResults);
    $this->assertCount(0, $sorted_results);
  }

  /**
   * Tests filtering of results.
   */
  public function testFilterResults() {
    $facet = new Facet([], 'facet');
    $facet->setResults($this->originalResults);
    $facet->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => ['minimum_items' => 8],
    ]);
    $this->processor->setConfiguration(['minimum_items' => 8]);

    $sorted_results = $this->processor->build($facet, $this->originalResults);

    $this->assertCount(2, $sorted_results);

    $this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
    $this->assertEquals('duck', $sorted_results[2]->getDisplayValue());
  }

}