You are here

public function LocaleStringTest::testStringSearchApi in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/tests/src/Kernel/LocaleStringTest.php \Drupal\Tests\locale\Kernel\LocaleStringTest::testStringSearchApi()
  2. 9 core/modules/locale/tests/src/Kernel/LocaleStringTest.php \Drupal\Tests\locale\Kernel\LocaleStringTest::testStringSearchApi()

Tests 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\Kernel

Code

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
    ->assertNotNull($found, 'Translation not found searching by source and context.');
  $this
    ->assertNotNull($found->language);
  $this
    ->assertNotNull($found->translation);
  $this
    ->assertFalse($found
    ->isNew());
  $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
    ->assertNotNull($found);
  $this
    ->assertSame($source3->lid, $found->lid);
  $this
    ->assertNull($found->translation);
  $this
    ->assertTrue($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);
}