You are here

class TermsParseModeTest in Search API 8

Tests functionality of the "Multiple words" parse mode.

@coversDefaultClass \Drupal\search_api\Plugin\search_api\parse_mode\Terms

@group search_api

Hierarchy

Expanded class hierarchy of TermsParseModeTest

File

tests/src/Unit/TermsParseModeTest.php, line 15

Namespace

Drupal\Tests\search_api\Unit
View source
class TermsParseModeTest extends UnitTestCase {

  /**
   * The parse mode plugin to test.
   *
   * @var \Drupal\search_api\Plugin\search_api\parse_mode\Terms
   */
  protected $plugin;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->plugin = new Terms([], '', []);
  }

  /**
   * Tests parsing the keys.
   *
   * @param mixed $keys
   *   The keywords to parse.
   * @param array $expected
   *   The expected parsed keys array.
   *
   * @dataProvider parseInputTestDataProvider
   */
  public function testParseInput($keys, array $expected) {
    $parsed = $this->plugin
      ->parseInput($keys);
    $this
      ->assertEquals($expected, $parsed);
  }

  /**
   * Provides test data for testParseInput().
   *
   * @return array[]
   *   An array of argument arrays for testParseInput().
   *
   * @see \Drupal\Tests\search_api\Unit\TermsParseModeTest::testParseInput()
   */
  public function parseInputTestDataProvider() {
    return [
      'normal keywords' => [
        'keys' => 'foo bar',
        'expected' => [
          '#conjunction' => 'AND',
          'foo',
          'bar',
        ],
      ],
      'quoted phrase' => [
        'keys' => '"cogito ergo sum"',
        'expected' => [
          '#conjunction' => 'AND',
          'cogito ergo sum',
        ],
      ],
      'single-word quotes' => [
        'keys' => '"foo"',
        'expected' => [
          '#conjunction' => 'AND',
          'foo',
        ],
      ],
      'negated keyword' => [
        'keys' => '-foo',
        'expected' => [
          '#conjunction' => 'AND',
          [
            '#negation' => TRUE,
            '#conjunction' => 'AND',
            'foo',
          ],
        ],
      ],
      'negated phrase' => [
        'keys' => '-"cogito ergo sum"',
        'expected' => [
          '#conjunction' => 'AND',
          [
            '#conjunction' => 'AND',
            '#negation' => TRUE,
            'cogito ergo sum',
          ],
        ],
      ],
      'keywords with stand-alone dash' => [
        'keys' => 'foo - bar',
        'expected' => [
          '#conjunction' => 'AND',
          'foo',
          'bar',
        ],
      ],
      'really complicated search' => [
        'keys' => 'pos  -neg "quoted pos with -minus" - -"quoted neg"',
        'expected' => [
          '#conjunction' => 'AND',
          'pos',
          [
            '#negation' => TRUE,
            '#conjunction' => 'AND',
            'neg',
          ],
          'quoted pos with -minus',
          [
            '#negation' => TRUE,
            '#conjunction' => 'AND',
            'quoted neg',
          ],
        ],
      ],
      'multi-byte space' => [
        'keys' => '神奈川県 連携',
        'expected' => [
          '#conjunction' => 'AND',
          '神奈川県',
          '連携',
        ],
      ],
    ];
  }

}

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.
TermsParseModeTest::$plugin protected property The parse mode plugin to test.
TermsParseModeTest::parseInputTestDataProvider public function Provides test data for testParseInput().
TermsParseModeTest::setUp protected function Overrides UnitTestCase::setUp
TermsParseModeTest::testParseInput public function Tests parsing the keys.
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.