UtilitiesTest.php in Search API Multilingual Solr Search 8
File
tests/src/Kernel/UtilitiesTest.php
View source
<?php
namespace Drupal\Tests\search_api_solr_multilingual\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api_solr\Utility\Utility as SASUtility;
use Drupal\search_api_solr_multilingual\Utility\Utility;
class UtilitiesTest extends KernelTestBase {
public static $modules = array(
'search_api_solr',
'search_api_solr_multilingual',
);
public function setUp() {
parent::setUp();
}
public function testLanguageSpecificFieldTypeNames() {
$this
->assertEquals('text_de', SASUtility::encodeSolrName('text_de'));
$encoded = SASUtility::encodeSolrName('text_de-at');
$this
->assertEquals('text_de_X2d_at', $encoded);
$this
->assertEquals('text_de-at', SASUtility::decodeSolrName($encoded));
$encoded = SASUtility::encodeSolrName('text_zh-Hant-HK');
$this
->assertEquals('text_zh_X2d_Hant_X2d_HK', $encoded);
$this
->assertEquals('text_zh-Hant-HK', SASUtility::decodeSolrName($encoded));
$encoded = SASUtility::encodeSolrName('text_de-CH-1901');
$this
->assertEquals('text_de_X2d_CH_X2d_1901', $encoded);
$this
->assertEquals('text_de-CH-1901', SASUtility::decodeSolrName($encoded));
}
public function testLanguageSpecificDynamicFieldNames() {
$this
->doDynamicFieldNameConversions();
$this
->doDynamicFieldNameConversions('ts', 'zh-Hant-HK', 'a_longer_field_name');
$this
->doDynamicFieldNameConversions('tm', 'de-CH-1901', 'sophisticated/field;NAME');
$this
->doDynamicFieldNameConversions('tm', 'en', 'tm;en_tm_en_repeated_sequences');
}
protected function doDynamicFieldNameConversions($prefix = 'tm', $langcode = 'de', $field = 'body') {
$sep = ';';
$dynamic_field_name = $prefix . '_' . $field;
$language_specific_prefix = $prefix . $sep . $langcode . '_';
$language_specific_field_name = $language_specific_prefix . $field;
$this
->assertEquals(Utility::getLanguageSpecificSolrDynamicFieldPrefix($prefix, $langcode), $language_specific_prefix);
$language_specific_dynamic_field_name = Utility::getLanguageSpecificSolrDynamicFieldNameForSolrDynamicFieldName($dynamic_field_name, $langcode);
$this
->assertEquals($language_specific_dynamic_field_name, $language_specific_field_name);
$language_unspecific_dynamic_field_name = Utility::getSolrDynamicFieldNameForLanguageSpecificSolrDynamicFieldName($language_specific_dynamic_field_name);
$this
->assertEquals($language_unspecific_dynamic_field_name, $dynamic_field_name);
$encoded_language_specific_dynamic_field_name = SASUtility::encodeSolrName($language_specific_dynamic_field_name);
$encoded_language_unspecific_dynamic_field_name = Utility::getSolrDynamicFieldNameForLanguageSpecificSolrDynamicFieldName($encoded_language_specific_dynamic_field_name);
$decoded_language_unspecific_dynamic_field_name = SASUtility::decodeSolrName($encoded_language_unspecific_dynamic_field_name);
$this
->assertEquals($decoded_language_unspecific_dynamic_field_name, $dynamic_field_name);
$field_definition = Utility::extractLanguageSpecificSolrDynamicFieldDefinition($encoded_language_specific_dynamic_field_name);
$this
->assertEquals($field_definition, SASUtility::encodeSolrName($language_specific_prefix) . '*');
$this
->assertEquals(Utility::getLanguageIdFromLanguageSpecificSolrDynamicFieldName($language_specific_field_name), $langcode);
$this
->assertFalse(Utility::getLanguageIdFromLanguageSpecificSolrDynamicFieldName($dynamic_field_name));
}
}