function LocaleStringTest::testStringSearchApi
Same name in other branches
- 9 core/modules/locale/tests/src/Kernel/LocaleStringTest.php \Drupal\Tests\locale\Kernel\LocaleStringTest::testStringSearchApi()
- 10 core/modules/locale/tests/src/Kernel/LocaleStringTest.php \Drupal\Tests\locale\Kernel\LocaleStringTest::testStringSearchApi()
- 11.x core/modules/locale/tests/src/Kernel/LocaleStringTest.php \Drupal\Tests\locale\Kernel\LocaleStringTest::testStringSearchApi()
Test Search API loading multiple objects.
File
-
core/
modules/ locale/ tests/ src/ Kernel/ LocaleStringTest.php, line 124
Class
- LocaleStringTest
- Tests the locale string storage, string objects and data API.
Namespace
Drupal\Tests\locale\KernelCode
public function testStringSearchApi() {
$language_count = 3;
// Strings 1 and 2 will have some common prefix.
// Source 1 will have all translations, not customized.
// Source 2 will have all translations, customized.
// Source 3 will have no translations.
$prefix = $this->randomMachineName(100);
$source1 = $this->buildSourceString([
'source' => $prefix . $this->randomMachineName(100),
])
->save();
$source2 = $this->buildSourceString([
'source' => $prefix . $this->randomMachineName(100),
])
->save();
$source3 = $this->buildSourceString()
->save();
// Load all source strings.
$strings = $this->storage
->getStrings([]);
$this->assertCount(3, $strings);
// Load all source strings matching a given string.
$filter_options['filters'] = [
'source' => $prefix,
];
$strings = $this->storage
->getStrings([], $filter_options);
$this->assertCount(2, $strings);
// Not customized translations.
$translate1 = $this->createAllTranslations($source1);
// Customized translations.
$this->createAllTranslations($source2, [
'customized' => LOCALE_CUSTOMIZED,
]);
// Try quick search function with different field combinations.
$langcode = 'es';
$found = $this->storage
->findTranslation([
'language' => $langcode,
'source' => $source1->source,
'context' => $source1->context,
]);
$this->assertTrue($found && isset($found->language) && isset($found->translation) && !$found->isNew(), 'Translation not found searching by source and context.');
$this->assertEquals($translate1[$langcode]->translation, $found->translation);
// Now try a translation not found.
$found = $this->storage
->findTranslation([
'language' => $langcode,
'source' => $source3->source,
'context' => $source3->context,
]);
$this->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found->isNew());
// Load all translations. For next queries we'll be loading only translated
// strings.
$translations = $this->storage
->getTranslations([
'translated' => TRUE,
]);
$this->assertCount(2 * $language_count, $translations);
// Load all customized translations.
$translations = $this->storage
->getTranslations([
'customized' => LOCALE_CUSTOMIZED,
'translated' => TRUE,
]);
$this->assertCount($language_count, $translations);
// Load all Spanish customized translations.
$translations = $this->storage
->getTranslations([
'language' => 'es',
'customized' => LOCALE_CUSTOMIZED,
'translated' => TRUE,
]);
$this->assertCount(1, $translations);
// Load all source strings without translation (1).
$translations = $this->storage
->getStrings([
'translated' => FALSE,
]);
$this->assertCount(1, $translations);
// Load Spanish translations using string filter.
$filter_options['filters'] = [
'source' => $prefix,
];
$translations = $this->storage
->getTranslations([
'language' => 'es',
], $filter_options);
$this->assertCount(2, $translations);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.