class TransliterationTest in Search API 8
Tests the "Transliteration" processor.
@group search_api
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api\Unit\Processor\TransliterationTest uses ProcessorTestTrait
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\ProcessorView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ProcessorTestTrait:: |
protected | property | The tested processor. | |
ProcessorTestTrait:: |
protected | function | Invokes a method on the processor. | |
TransliterationTest:: |
protected | property | A test index mock to use for tests. | |
TransliterationTest:: |
public | function |
Overrides UnitTestCase:: |
|
TransliterationTest:: |
public | function | Tests that floating point numbers are not affected. | |
TransliterationTest:: |
public | function | Tests that integers are not affected. | |
TransliterationTest:: |
public | function | Tests that strings are affected. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |