You are here

class IgnoreCharacterTest in Search API 8

Tests the "Ignore characters" processor.

@group search_api

Hierarchy

Expanded class hierarchy of IgnoreCharacterTest

See also

\Drupal\search_api\Plugin\search_api\processor\IgnoreCharacter

File

tests/src/Unit/Processor/IgnoreCharacterTest.php, line 18

Namespace

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

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

  /**
   * Tests preprocessing with different ignorable character sets.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
   * @param string $expected_value
   *   The expected processed value.
   * @param string[] $character_classes
   *   The "ignorable_classes" setting to set on the processor.
   *
   * @dataProvider ignoreCharacterSetsDataProvider
   */
  public function testIgnoreCharacterSets($passed_value, $expected_value, array $character_classes) {
    $this->processor
      ->setConfiguration([
      'ignorable_classes' => $character_classes,
    ]);
    $this
      ->invokeMethod('process', [
      &$passed_value,
      'text',
    ]);
    $this
      ->assertEquals($expected_value, $passed_value);
  }

  /**
   * Data provider for testValueConfiguration().
   */
  public function ignoreCharacterSetsDataProvider() {
    return [
      [
        'word_s',
        'words',
        [
          'Pc' => 'Pc',
        ],
      ],
      [
        'word⁔s',
        'words',
        [
          'Pc' => 'Pc',
        ],
      ],
      [
        'word〜s',
        'words',
        [
          'Pd' => 'Pd',
        ],
      ],
      [
        'w–ord⸗s',
        'words',
        [
          'Pd' => 'Pd',
        ],
      ],
      [
        'word⌉s',
        'words',
        [
          'Pe' => 'Pe',
        ],
      ],
      [
        'word⦊s〕',
        'words',
        [
          'Pe' => 'Pe',
        ],
      ],
      [
        'word»s',
        'words',
        [
          'Pf' => 'Pf',
        ],
      ],
      [
        'word⸍s',
        'words',
        [
          'Pf' => 'Pf',
        ],
      ],
      [
        'word⸂s',
        'words',
        [
          'Pi' => 'Pi',
        ],
      ],
      [
        'w«ord⸉s',
        'words',
        [
          'Pi' => 'Pi',
        ],
      ],
      [
        'words%',
        'words',
        [
          'Po' => 'Po',
        ],
      ],
      [
        'wo*rd/s',
        'words',
        [
          'Po' => 'Po',
        ],
      ],
      [
        'word༺s',
        'words',
        [
          'Ps' => 'Ps',
        ],
      ],
      [
        'w❮ord⌈s',
        'words',
        [
          'Ps' => 'Ps',
        ],
      ],
      [
        'word៛s',
        'words',
        [
          'Sc' => 'Sc',
        ],
      ],
      [
        'wo₥rd₦s',
        'words',
        [
          'Sc' => 'Sc',
        ],
      ],
      [
        'w˓ords',
        'words',
        [
          'Sk' => 'Sk',
        ],
      ],
      [
        'wo˘rd˳s',
        'words',
        [
          'Sk' => 'Sk',
        ],
      ],
      [
        'word×s',
        'words',
        [
          'Sm' => 'Sm',
        ],
      ],
      [
        'wo±rd؈s',
        'words',
        [
          'Sm' => 'Sm',
        ],
      ],
      [
        'wo᧧rds',
        'words',
        [
          'So' => 'So',
        ],
      ],
      [
        'w᧶ord᧲s',
        'words',
        [
          'So' => 'So',
        ],
      ],
      [
        "wor\nds",
        'words',
        [
          'Cc' => 'Cc',
        ],
      ],
      [
        "wo\frds",
        'words',
        [
          'Cc' => 'Cc',
        ],
      ],
      [
        'word۝s',
        'words',
        [
          'Cf' => 'Cf',
        ],
      ],
      [
        'wo᠎rd؁s',
        'words',
        [
          'Cf' => 'Cf',
        ],
      ],
      [
        'words',
        'words',
        [
          'Co' => 'Co',
        ],
      ],
      [
        'wo󿿽rds',
        'words',
        [
          'Co' => 'Co',
        ],
      ],
      [
        'wordॊs',
        'words',
        [
          'Mc' => 'Mc',
        ],
      ],
      [
        'worौdংs',
        'words',
        [
          'Mc' => 'Mc',
        ],
      ],
      [
        'wo⃞rds',
        'words',
        [
          'Me' => 'Me',
        ],
      ],
      [
        'wor⃤⃟ds',
        'words',
        [
          'Me' => 'Me',
        ],
      ],
      [
        'woྰrds',
        'words',
        [
          'Mn' => 'Mn',
        ],
      ],
      [
        'worྵdྶs',
        'words',
        [
          'Mn' => 'Mn',
        ],
      ],
      [
        'woྰrds',
        'words',
        [
          'Mn' => 'Mn',
          'Pd' => 'Pd',
          'Pe' => 'Pe',
        ],
      ],
      [
        'worྵdྶs',
        'words',
        [
          'Mn' => 'Mn',
          'Pd' => 'Pd',
          'Pe' => 'Pe',
        ],
      ],
    ];
  }

  /**
   * Tests preprocessing with the "Ignorable characters" setting.
   *
   * @param string $passed_value
   *   The value that should be passed into process().
   * @param string $expected_value
   *   The expected processed value.
   * @param string $ignorable
   *   The "ignorable" setting to set on the processor.
   *
   * @dataProvider ignorableCharactersDataProvider
   */
  public function testIgnorableCharacters($passed_value, $expected_value, $ignorable) {
    $this->processor
      ->setConfiguration([
      'ignorable' => $ignorable,
      'ignorable_classes' => [],
    ]);
    $this
      ->invokeMethod('process', [
      &$passed_value,
      'text',
    ]);
    $this
      ->assertSame($expected_value, $passed_value);
  }

  /**
   * Provides sets of test parameters for testIgnorableCharacters().
   *
   * @return array
   *   Sets of arguments for testIgnorableCharacters().
   */
  public function ignorableCharactersDataProvider() {
    return [
      [
        'abcde',
        'ace',
        '[bd]',
      ],
      [
        [
          'abcde',
          'abcdef',
        ],
        [
          'ace',
          'ace',
        ],
        '[bdf]',
      ],
      [
        "ab.c'de",
        "a.'de",
        '[b-c]',
      ],
      [
        'foo 13$%& (bar)[93]',
        'foo $%& (bar)[]',
        '\\d',
      ],
    ];
  }

  /**
   * Tests whether "IS NULL" conditions are correctly kept.
   *
   * @see https://www.drupal.org/project/search_api/issues/3212925
   */
  public function testIsNullConditions() {
    $this
      ->setUpMockContainer();
    $index = $this
      ->createMock(IndexInterface::class);
    $index
      ->method('getFields')
      ->willReturn([
      'field' => (new Field($index, 'field'))
        ->setType('string'),
    ]);
    $this->processor
      ->setIndex($index);
    $passed_value = NULL;
    $this
      ->invokeMethod('processConditionValue', [
      &$passed_value,
    ]);
    $this
      ->assertSame(NULL, $passed_value);
    $condition = new Condition('field', NULL);
    $conditions = [
      $condition,
    ];
    $this
      ->invokeMethod('processConditions', [
      &$conditions,
    ]);
    $this
      ->assertSame([
      $condition,
    ], $conditions);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IgnoreCharacterTest::ignorableCharactersDataProvider public function Provides sets of test parameters for testIgnorableCharacters().
IgnoreCharacterTest::ignoreCharacterSetsDataProvider public function Data provider for testValueConfiguration().
IgnoreCharacterTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
IgnoreCharacterTest::testIgnorableCharacters public function Tests preprocessing with the "Ignorable characters" setting.
IgnoreCharacterTest::testIgnoreCharacterSets public function Tests preprocessing with different ignorable character sets.
IgnoreCharacterTest::testIsNullConditions public function Tests whether "IS NULL" conditions are correctly kept.
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.
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.