ExcludeSpecifiedItemsProcessorTest.php 6.4 KB
Newer Older
1
2
3
<?php
/**
 * @file
4
 * Contains \Drupal\Tests\facets\Plugin\Processor\ExcludeSpecifiedItemsProcessorTest.
5
6
 */

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

9
10
11
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\ExcludeSpecifiedItemsProcessor;
use Drupal\facets\Result\Result;
12
13
14
15
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
Joris Vercammen's avatar
Joris Vercammen committed
16
17
 * Unit test for processor.
 *
18
 * @group facets
19
20
21
22
23
24
 */
class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {

  /**
   * The processor to be tested.
   *
25
   * @var \Drupal\facets\processor\BuildProcessorInterface
26
27
28
29
30
31
   */
  protected $processor;

  /**
   * An array containing the results before the processor has ran.
   *
32
   * @var \Drupal\facets\Result\Result[]
33
   */
Joris Vercammen's avatar
Joris Vercammen committed
34
  protected $originalResults;
35
36
37
38
39
40
41

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();

Joris Vercammen's avatar
Joris Vercammen committed
42
    $this->originalResults = [
43
44
45
      new Result('llama', 'llama', 10),
      new Result('badger', 'badger', 5),
      new Result('duck', 'duck', 15),
46
      new Result('snbke', 'snbke', 10),
47
48
49
50
51
      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),
52
      new Result('snaaaaaake', 'snaaaaaake', 10),
53
54
55
56
57
    ];

    $processor_id = 'exclude_specified_items';
    $this->processor = new ExcludeSpecifiedItemsProcessor([], $processor_id, []);

Joris Vercammen's avatar
Joris Vercammen committed
58
    $processor_definitions = [
59
60
      $processor_id => [
        'id' => $processor_id,
61
        'class' => 'Drupal\facets\Plugin\facets\processor\ExcludeSpecifiedItemsProcessor',
62
63
64
      ],
    ];

65
    $manager = $this->getMockBuilder('Drupal\facets\Processor\ProcessorPluginManager')
66
67
      ->disableOriginalConstructor()
      ->getMock();
Joris Vercammen's avatar
Joris Vercammen committed
68
    $manager->expects($this->any())
69
      ->method('getDefinitions')
Joris Vercammen's avatar
Joris Vercammen committed
70
      ->willReturn($processor_definitions);
Joris Vercammen's avatar
Joris Vercammen committed
71
    $manager->expects($this->any())
72
73
74
75
      ->method('createInstance')
      ->willReturn($this->processor);

    $container_builder = new ContainerBuilder();
76
    $container_builder->set('plugin.manager.facets.processor', $manager);
77
78
79
80
    \Drupal::setContainer($container_builder);
  }

  /**
81
   * Tests no filtering happens.
82
83
84
   */
  public function testNoFilter() {
    $facet = new Facet([], 'facet');
Joris Vercammen's avatar
Joris Vercammen committed
85
    $facet->setResults($this->originalResults);
86
87
88
89
90
91
92
93
    $facet->addProcessor([
      'processor_id' => 'exclude_specified_items',
      'weights' => [],
      'settings' => [
        'exclude' => 'alpaca',
        'regex' => 0,
      ],
    ]);
94
95
    $this->processor->setConfiguration([
      'exclude' => 'alpaca',
Joris Vercammen's avatar
Joris Vercammen committed
96
      'regex' => 0,
97
    ]);
Joris Vercammen's avatar
Joris Vercammen committed
98
    $filtered_results = $this->processor->build($facet, $this->originalResults);
99

Joris Vercammen's avatar
Joris Vercammen committed
100
    $this->assertCount(count($this->originalResults), $filtered_results);
101
102
103
  }

  /**
104
   * Tests filtering happens for string filter.
105
106
107
   */
  public function testStringFilter() {
    $facet = new Facet([], 'facet');
Joris Vercammen's avatar
Joris Vercammen committed
108
    $facet->setResults($this->originalResults);
109
110
111
112
113
114
115
116
    $facet->addProcessor([
      'processor_id' => 'exclude_specified_items',
      'weights' => [],
      'settings' => [
        'exclude' => 'alpaca',
        'regex' => 0,
      ],
    ]);
117
118
    $this->processor->setConfiguration([
      'exclude' => 'llama',
Joris Vercammen's avatar
Joris Vercammen committed
119
      'regex' => 0,
120
    ]);
Joris Vercammen's avatar
Joris Vercammen committed
121
    $filtered_results = $this->processor->build($facet, $this->originalResults);
122

Joris Vercammen's avatar
Joris Vercammen committed
123
    $this->assertCount((count($this->originalResults) - 1), $filtered_results);
124
125
126
127

    foreach ($filtered_results as $result) {
      $this->assertNotEquals('llama', $result->getDisplayValue());
    }
128
129
130
  }

  /**
131
   * Tests filtering happens for regex filter.
132
   *
133
134
   * @dataProvider provideRegexTests
   */
Joris Vercammen's avatar
Joris Vercammen committed
135
  public function testRegexFilter($regex, $expected_results) {
136
    $facet = new Facet([], 'facet');
Joris Vercammen's avatar
Joris Vercammen committed
137
    $facet->setResults($this->originalResults);
138
139
140
141
142
143
144
145
    $facet->addProcessor([
      'processor_id' => 'exclude_specified_items',
      'weights' => [],
      'settings' => [
        'exclude' => 'alpaca',
        'regex' => 0,
      ],
    ]);
146
147
    $this->processor->setConfiguration([
      'exclude' => $regex,
Joris Vercammen's avatar
Joris Vercammen committed
148
      'regex' => 1,
149
    ]);
Joris Vercammen's avatar
Joris Vercammen committed
150
    $filtered_results = $this->processor->build($facet, $this->originalResults);
151

Joris Vercammen's avatar
Joris Vercammen committed
152
    $this->assertCount(count($expected_results), $filtered_results);
153
154

    foreach ($filtered_results as $res) {
Joris Vercammen's avatar
Joris Vercammen committed
155
      $this->assertTrue(in_array($res->getDisplayValue(), $expected_results));
156
157
158
159
    }
  }

  /**
160
   * Provides multiple data sets for ::testRegexFilter.
161
162
163
164
165
   */
  public function provideRegexTests() {
    return [
      [
        'test',
Joris Vercammen's avatar
Joris Vercammen committed
166
167
168
169
170
171
172
173
174
175
176
177
        [
          'llama',
          'duck',
          'badger',
          'snake',
          'snaake',
          'snaaake',
          'snaaaake',
          'snaaaaake',
          'snaaaaaake',
          'snbke',
        ],
178
179
180
      ],
      [
        'llama',
Joris Vercammen's avatar
Joris Vercammen committed
181
182
183
184
185
186
187
188
189
190
191
        [
          'badger',
          'duck',
          'snake',
          'snaake',
          'snaaake',
          'snaaaake',
          'snaaaaake',
          'snaaaaaake',
          'snbke',
        ],
192
193
194
      ],
      [
        'duck',
Joris Vercammen's avatar
Joris Vercammen committed
195
196
197
198
199
200
201
202
203
204
205
        [
          'llama',
          'badger',
          'snake',
          'snaake',
          'snaaake',
          'snaaaake',
          'snaaaaake',
          'snaaaaaake',
          'snbke',
        ],
206
207
208
      ],
      [
        'sn(.*)ke',
Joris Vercammen's avatar
Joris Vercammen committed
209
210
211
212
213
        [
          'llama',
          'duck',
          'badger',
        ],
214
215
216
      ],
      [
        'sn(a*)ke',
Joris Vercammen's avatar
Joris Vercammen committed
217
218
219
220
221
222
        [
          'llama',
          'duck',
          'badger',
          'snbke',
        ],
223
      ],
224
225
      [
        'sn(a+)ke',
Joris Vercammen's avatar
Joris Vercammen committed
226
227
228
229
230
231
        [
          'llama',
          'duck',
          'badger',
          'snbke',
        ],
232
233
234
      ],
      [
        'sn(a{3,5})ke',
Joris Vercammen's avatar
Joris Vercammen committed
235
236
237
238
239
240
241
242
243
        [
          'llama',
          'duck',
          'badger',
          'snake',
          'snaake',
          'snaaaaaake',
          'snbke',
        ],
244
      ],
245
246
    ];
  }
Joris Vercammen's avatar
Joris Vercammen committed
247

Joris Vercammen's avatar
Joris Vercammen committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  /**
   * Tests configuration.
   */
  public function testConfiguration() {
    $config = $this->processor->defaultConfiguration();
    $this->assertEquals(['exclude' => '', 'regex' => 0], $config);
  }

  /**
   * Tests testDescription().
   */
  public function testDescription() {
    $this->assertEquals('', $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());
  }

277
}