You are here

class StemmerTest in Snowball Stemmer 8

Same name in this branch
  1. 8 tests/src/Unit/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\StemmerTest
  2. 8 tests/src/Unit/Plugin/Processor/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\Plugin\Processor\StemmerTest
Same name and namespace in other branches
  1. 2.x tests/src/Unit/Plugin/Processor/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\Plugin\Processor\StemmerTest

Tests the "Stemmer" processor.

@coversDefaultClass \Drupal\snowball_stemmer\Plugin\search_api\processor\SnowballStemmer

@group snowball_stemmer

Hierarchy

Expanded class hierarchy of StemmerTest

File

tests/src/Unit/Plugin/Processor/StemmerTest.php, line 23

Namespace

Drupal\Tests\snowball_stemmer\Unit\Plugin\Processor
View source
class StemmerTest extends UnitTestCase {
  use ProcessorTestTrait;
  use TestItemsTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'search_api',
    'snowball_stemmer',
  ];

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setUpMockContainer();
    $this->stemmerService = $this
      ->getMockBuilder('Drupal\\snowball_stemmer\\Stemmer')
      ->disableOriginalConstructor()
      ->getMock();
    $this->container
      ->set('snowball_stemmer.stemmer', $this->stemmerService);
    $this->languageManager = $this
      ->createMock(LanguageManagerInterface::class);
    $this->container
      ->set('language_manager', $this->languageManager);
    \Drupal::setContainer($this->container);
    $this->processor = new SnowballStemmer([], 'string', []);
  }

  /**
   * Tests language set of preprocessIndexItems() method.
   *
   * @covers ::preprocessIndexItems
   */
  public function testPreprocessIndexItems() {
    $index = $this
      ->createMock(IndexInterface::class);
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setLanguage')
      ->with('en')
      ->willReturn(TRUE);
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('stem')
      ->with('backing')
      ->willReturn('back');
    $item_en = $this
      ->getMockBuilder(ItemInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $item_en
      ->method('getLanguage')
      ->willReturn('en');
    $field_en = new Field($index, 'foo');
    $field_en
      ->setType('text');
    $field_en
      ->setValues([
      new TextValue('backing'),
    ]);
    $item_en
      ->method('getFields')
      ->willReturn([
      'foo' => $field_en,
    ]);
    $this->processor
      ->preprocessIndexItems([
      $item_en,
    ]);
  }

  /**
   * Tests string explode of preprocessIndexItems() method.
   *
   * @covers ::preprocessIndexItems
   */
  public function testPreprocessWordsString() {
    $index = $this
      ->createMock(IndexInterface::class);
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setLanguage')
      ->with('nl')
      ->willReturn(TRUE);
    $this->stemmerService
      ->expects($this
      ->exactly(3))
      ->method('stem')
      ->will($this
      ->returnValueMap([
      [
        'twee',
        'twee',
      ],
      [
        'korte',
        'kort',
      ],
      [
        'zinnen',
        'zin',
      ],
    ]));
    $item = $this
      ->getMockBuilder(ItemInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $item
      ->method('getLanguage')
      ->willReturn('nl');
    $field = new Field($index, 'foo');
    $field
      ->setType('text');
    $field
      ->setValues([
      new TextValue('Twee korte zinnen.'),
    ]);
    $item
      ->method('getFields')
      ->willReturn([
      'foo' => $field,
    ]);
    $this->processor
      ->preprocessIndexItems([
      $item,
    ]);
  }

  /**
   * Tests string when language unknown.
   *
   * @covers ::preprocessIndexItems
   */
  public function testPreprocessUnknownLanguage() {
    $index = $this
      ->createMock(IndexInterface::class);
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setLanguage')
      ->with('xx')
      ->willReturn(FALSE);
    $this->stemmerService
      ->expects($this
      ->never())
      ->method('stem');
    $item = $this
      ->getMockBuilder(ItemInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $item
      ->method('getLanguage')
      ->willReturn('xx');
    $field = new Field($index, 'foo');
    $field
      ->setType('text');
    $field
      ->setValues([
      new TextValue('Anything.'),
    ]);
    $item
      ->method('getFields')
      ->willReturn([
      'foo' => $field,
    ]);
    $this->processor
      ->preprocessIndexItems([
      $item,
    ]);
  }

  /**
   * Tests string when more than one word sent.
   *
   * Occurs when string has not been tokenized, or if there is a quoted string.
   *
   * @covers ::preprocessIndexItems
   */
  public function testPreprocessMultiWordString() {
    $index = $this
      ->createMock(IndexInterface::class);
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setLanguage')
      ->with('en')
      ->willReturn(TRUE);
    $item = $this
      ->getMockBuilder(ItemInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $item
      ->method('getLanguage')
      ->willReturn('en');
    $field = new Field($index, 'foo');
    $field
      ->setType('text');
    $field
      ->setValues([
      new TextValue(" \tExtra  spaces \rappeared \n\tspaced-out  \r\n"),
    ]);
    $item
      ->method('getFields')
      ->willReturn([
      'foo' => $field,
    ]);
    $this->processor
      ->preprocessIndexItems([
      $item,
    ]);
  }

  /**
   * Tests preprocessSearchQuery() method.
   *
   * @covers ::preprocessIndexItems
   */
  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);
    $language = $this
      ->createMock(LanguageInterface::class);
    $language
      ->expects($this
      ->any())
      ->method('getId')
      ->will($this
      ->returnValue('en'));
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->will($this
      ->returnValue($language));
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setLanguage')
      ->with('en')
      ->will($this
      ->returnValue(TRUE));
    $this->stemmerService
      ->expects($this
      ->exactly(4))
      ->method('stem')
      ->withConsecutive([
      'fooing',
    ], [
      'bar',
    ], [
      'bary',
    ], [
      'foo',
    ])
      ->will($this
      ->onConsecutiveCalls('foo', 'bar', 'bar', 'foo'));
    $query = \Drupal::getContainer()
      ->get('search_api.query_helper')
      ->createQuery($index);
    $keys = [
      '#conjunction' => 'AND',
      'fooing',
      'bar',
      'bary foo',
    ];
    $query
      ->keys($keys);
    $this->processor
      ->preprocessSearchQuery($query);
    $this
      ->assertEquals([
      '#conjunction' => 'AND',
      'foo',
      'bar',
      'bar foo',
    ], $query
      ->getKeys());
  }

  /**
   * Tests preprocessSearchQuery() method with no language.
   *
   * @covers ::preprocessIndexItems
   */
  public function testPreprocessSearchQueryNoLanguage() {
    $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);
    $language = $this
      ->createMock(LanguageInterface::class);
    $language
      ->expects($this
      ->any())
      ->method('getId')
      ->will($this
      ->returnValue('xxx'));
    $this->languageManager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->will($this
      ->returnValue($language));
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setLanguage')
      ->with('xxx')
      ->will($this
      ->returnValue(FALSE));
    $this->stemmerService
      ->expects($this
      ->never())
      ->method('stem');
    $query = \Drupal::getContainer()
      ->get('search_api.query_helper')
      ->createQuery($index);
    $keys = [
      '#conjunction' => 'AND',
      'foo',
      'bar',
      'bar foo',
    ];
    $query
      ->keys($keys);
    $this->processor
      ->preprocessSearchQuery($query);
  }

  /**
   * Check exceptions/overrides configuration is set.
   */
  public function testOverrides() {
    $this->stemmerService
      ->expects($this
      ->once())
      ->method('setOverrides')
      ->with([
      'word' => 'overridden',
    ]);
    $this->processor
      ->setConfiguration([
      'exceptions' => [
        'word' => 'overridden',
      ],
    ]);
    $this->processor
      ->getStemmer();
  }

}

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.
StemmerTest::$modules public static property
StemmerTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
StemmerTest::testOverrides public function Check exceptions/overrides configuration is set.
StemmerTest::testPreprocessIndexItems public function Tests language set of preprocessIndexItems() method.
StemmerTest::testPreprocessMultiWordString public function Tests string when more than one word sent.
StemmerTest::testPreprocessSearchQuery public function Tests preprocessSearchQuery() method.
StemmerTest::testPreprocessSearchQueryNoLanguage public function Tests preprocessSearchQuery() method with no language.
StemmerTest::testPreprocessUnknownLanguage public function Tests string when language unknown.
StemmerTest::testPreprocessWordsString public function Tests string explode of preprocessIndexItems() method.
TestItemsTrait::$container protected property The class container.
TestItemsTrait::$itemIds protected property The used item IDs for test items.
TestItemsTrait::createItems public function Creates a certain number of test items.
TestItemsTrait::createSingleFieldItem public function Creates an array with a single item which has the given field.
TestItemsTrait::setUpMockContainer protected function Adds a container with several mock services commonly needed by our tests.
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.