ExcludeSpecifiedItemsProcessorTest.php 5.78 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;

/**
borisson_'s avatar
borisson_ 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
   */
borisson_'s avatar
borisson_ 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();

borisson_'s avatar
borisson_ 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, []);

borisson_'s avatar
borisson_ 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 68 69
      ->disableOriginalConstructor()
      ->getMock();
    $manager->expects($this->once())
      ->method('getDefinitions')
borisson_'s avatar
borisson_ committed
70
      ->willReturn($processor_definitions);
71 72 73 74 75
    $manager->expects($this->once())
      ->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');
borisson_'s avatar
borisson_ 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',
borisson_'s avatar
borisson_ committed
96
      'regex' => 0,
97
    ]);
borisson_'s avatar
borisson_ committed
98
    $filtered_results = $this->processor->build($facet, $this->originalResults);
99

borisson_'s avatar
borisson_ 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');
borisson_'s avatar
borisson_ 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',
borisson_'s avatar
borisson_ committed
119
      'regex' => 0,
120
    ]);
borisson_'s avatar
borisson_ committed
121
    $filtered_results = $this->processor->build($facet, $this->originalResults);
122

borisson_'s avatar
borisson_ 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
   */
borisson_'s avatar
borisson_ committed
135
  public function testRegexFilter($regex, $expected_results) {
136
    $facet = new Facet([], 'facet');
borisson_'s avatar
borisson_ 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,
borisson_'s avatar
borisson_ committed
148
      'regex' => 1,
149
    ]);
borisson_'s avatar
borisson_ committed
150
    $filtered_results = $this->processor->build($facet, $this->originalResults);
151

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

    foreach ($filtered_results as $res) {
borisson_'s avatar
borisson_ 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',
borisson_'s avatar
borisson_ 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',
borisson_'s avatar
borisson_ 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',
borisson_'s avatar
borisson_ 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',
borisson_'s avatar
borisson_ committed
209 210 211 212 213
        [
          'llama',
          'duck',
          'badger',
        ],
214 215 216
      ],
      [
        'sn(a*)ke',
borisson_'s avatar
borisson_ committed
217 218 219 220 221 222
        [
          'llama',
          'duck',
          'badger',
          'snbke',
        ],
223
      ],
224 225
      [
        'sn(a+)ke',
borisson_'s avatar
borisson_ committed
226 227 228 229 230 231
        [
          'llama',
          'duck',
          'badger',
          'snbke',
        ],
232 233 234
      ],
      [
        'sn(a{3,5})ke',
borisson_'s avatar
borisson_ committed
235 236 237 238 239 240 241 242 243
        [
          'llama',
          'duck',
          'badger',
          'snake',
          'snaake',
          'snaaaaaake',
          'snbke',
        ],
244
      ],
245 246
    ];
  }
borisson_'s avatar
borisson_ committed
247

248
}