You are here

class TransliterationTest in Search API 8

Tests the "Transliteration" processor.

@group search_api

Hierarchy

Expanded class hierarchy of TransliterationTest

See also

\Drupal\search_api\Plugin\search_api\processor\Transliteration

File

tests/src/Unit/Processor/TransliterationTest.php, line 17

Namespace

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

  /**
   * A test index mock to use for tests.
   *
   * @var \Drupal\search_api\IndexInterface
   */
  protected $index;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->index = $this
      ->createMock(IndexInterface::class);
    $this
      ->setUpMockContainer();
    $this->processor = new Transliteration([], 'transliteration', []);
    $this->processor
      ->setLangcode('en');
    $transliterator = $this
      ->createMock(TransliterationInterface::class);
    $transliterate = function ($string, $langcode = 'en', $unknown_character = '?', $max_length = NULL) {
      return "translit-{$string}-{$langcode}{$unknown_character}{$max_length}";
    };
    $transliterator
      ->expects($this
      ->any())
      ->method('transliterate')
      ->will($this
      ->returnCallback($transliterate));

    /** @var \Drupal\Component\Transliteration\TransliterationInterface $transliterator */
    $this->processor
      ->setTransliterator($transliterator);
  }

  /**
   * Tests that integers are not affected.
   */
  public function testTransliterationWithInteger() {
    $field_value = 5;

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    $items = $this
      ->createSingleFieldItem($this->index, 'int', $field_value, $field);
    $this->processor
      ->preprocessIndexItems($items);
    $this
      ->assertEquals([
      $field_value,
    ], $field
      ->getValues(), 'Integer not affected by transliteration.');
  }

  /**
   * Tests that floating point numbers are not affected.
   */
  public function testTransliterationWithDouble() {
    $field_value = 3.14;

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    $items = $this
      ->createSingleFieldItem($this->index, 'double', $field_value, $field);
    $this->processor
      ->preprocessIndexItems($items);
    $this
      ->assertEquals([
      $field_value,
    ], $field
      ->getValues(), 'Floating point number not affected by transliteration.');
  }

  /**
   * Tests that strings are affected.
   */
  public function testTransliterationWithString() {
    $field_value = 'test_string';

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    $items = $this
      ->createSingleFieldItem($this->index, 'string', $field_value, $field);
    $this->processor
      ->preprocessIndexItems($items);
    $expected_value = "translit-{$field_value}-en?";
    $this
      ->assertEquals([
      $expected_value,
    ], $field
      ->getValues(), 'Strings are correctly transliterated.');
  }

}

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.
TransliterationTest::$index protected property A test index mock to use for tests.
TransliterationTest::setUp public function Overrides UnitTestCase::setUp
TransliterationTest::testTransliterationWithDouble public function Tests that floating point numbers are not affected.
TransliterationTest::testTransliterationWithInteger public function Tests that integers are not affected.
TransliterationTest::testTransliterationWithString public function Tests that strings are affected.
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.