You are here

class StopwordsTest in Search API 8

Tests the "Stopwords" processor.

@group search_api

Hierarchy

Expanded class hierarchy of StopwordsTest

See also

\Drupal\search_api\Plugin\search_api\processor\Stopwords

File

tests/src/Unit/Processor/StopwordsTest.php, line 16

Namespace

Drupal\Tests\search_api\Unit\Processor
View source
class StopwordsTest extends UnitTestCase {
  use ProcessorTestTrait, TestItemsTrait;

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setUpMockContainer();
    $this->processor = new Stopwords([], 'stopwords', []);
  }

  /**
   * Tests the process() method of the Stopwords processor.
   *
   * @param string $passed_value
   *   The string that should be passed to process().
   * @param string $expected_value
   *   The expected altered string.
   * @param string[] $stopwords
   *   The stopwords with which to configure the test processor.
   *
   * @dataProvider processDataProvider
   */
  public function testProcess($passed_value, $expected_value, array $stopwords) {
    $this->processor
      ->setConfiguration([
      'stopwords' => $stopwords,
    ]);
    $this
      ->invokeMethod('process', [
      &$passed_value,
    ]);
    $this
      ->assertEquals($expected_value, $passed_value);
  }

  /**
   * Data provider for testStopwords().
   *
   * Processor checks for exact case, and tokenized content.
   */
  public function processDataProvider() {
    return [
      [
        'or',
        '',
        [
          'or',
        ],
      ],
      [
        'orb',
        'orb',
        [
          'or',
        ],
      ],
      [
        'for',
        'for',
        [
          'or',
        ],
      ],
      [
        'ordor',
        'ordor',
        [
          'or',
        ],
      ],
      [
        'ÄÖÜÀÁ<>»«û',
        'ÄÖÜÀÁ<>»«û',
        [
          'stopword1',
          'ÄÖÜÀÁ<>»«',
          'stopword3',
        ],
      ],
      [
        'ÄÖÜÀÁ',
        '',
        [
          'stopword1',
          'ÄÖÜÀÁ',
          'stopword3',
        ],
      ],
      [
        'ÄÖÜÀÁ stopword1',
        'ÄÖÜÀÁ stopword1',
        [
          'stopword1',
          'ÄÖÜÀÁ',
          'stopword3',
        ],
      ],
    ];
  }

  /**
   * Tests the processor's preprocessSearchQuery() method.
   */
  public function testPreprocessSearchQuery() {
    $index = $this
      ->createMock(IndexInterface::class);
    $index
      ->expects($this
      ->any())
      ->method('status')
      ->will($this
      ->returnValue(TRUE));

    /** @var \Drupal\search_api\IndexInterface $index */
    $this->processor
      ->setIndex($index);
    $query = \Drupal::getContainer()
      ->get('search_api.query_helper')
      ->createQuery($index);
    $keys = [
      '#conjunction' => 'AND',
      'foo',
      'bar',
      'bar foo',
    ];
    $query
      ->keys($keys);
    $configuration = [
      'stopwords' => [
        'foobar',
        'bar',
        'barfoo',
      ],
    ];
    $this->processor
      ->setConfiguration($configuration);
    $this->processor
      ->preprocessSearchQuery($query);
    unset($keys[1]);
    $this
      ->assertEquals($keys, $query
      ->getKeys());
    $this
      ->assertEquals([
      'bar',
    ], $query
      ->getResults()
      ->getIgnoredSearchKeys());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
ProcessorTestTrait::$processor protected property The tested processor.
ProcessorTestTrait::invokeMethod protected function Invokes a method on the processor.
StopwordsTest::processDataProvider public function Data provider for testStopwords().
StopwordsTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
StopwordsTest::testPreprocessSearchQuery public function Tests the processor's preprocessSearchQuery() method.
StopwordsTest::testProcess public function Tests the process() method of the Stopwords processor.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.