You are here

public function LocaleStringTest::testStringSearchAPI in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/src/Tests/LocaleStringTest.php \Drupal\locale\Tests\LocaleStringTest::testStringSearchAPI()

Test Search API loading multiple objects.

File

core/modules/locale/src/Tests/LocaleStringTest.php, line 118
Contains \Drupal\locale\Tests\LocaleStringTest.

Class

LocaleStringTest
Tests the locale string storage, string objects and data API.

Namespace

Drupal\locale\Tests

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(array(
    'source' => $prefix . $this
      ->randomMachineName(100),
  ))
    ->save();
  $source2 = $this
    ->buildSourceString(array(
    'source' => $prefix . $this
      ->randomMachineName(100),
  ))
    ->save();
  $source3 = $this
    ->buildSourceString()
    ->save();

  // Load all source strings.
  $strings = $this->storage
    ->getStrings(array());
  $this
    ->assertEqual(count($strings), 3, 'Found 3 source strings in the database.');

  // Load all source strings matching a given string.
  $filter_options['filters'] = array(
    'source' => $prefix,
  );
  $strings = $this->storage
    ->getStrings(array(), $filter_options);
  $this
    ->assertEqual(count($strings), 2, 'Found 2 strings using some string filter.');

  // Not customized translations.
  $translate1 = $this
    ->createAllTranslations($source1);

  // Customized translations.
  $this
    ->createAllTranslations($source2, array(
    'customized' => LOCALE_CUSTOMIZED,
  ));

  // Try quick search function with different field combinations.
  $langcode = 'es';
  $found = $this->storage
    ->findTranslation(array(
    'language' => $langcode,
    'source' => $source1->source,
    'context' => $source1->context,
  ));
  $this
    ->assertTrue($found && isset($found->language) && isset($found->translation) && !$found
    ->isNew(), 'Translation found searching by source and context.');
  $this
    ->assertEqual($found->translation, $translate1[$langcode]->translation, 'Found the right translation.');

  // Now try a translation not found.
  $found = $this->storage
    ->findTranslation(array(
    'language' => $langcode,
    'source' => $source3->source,
    'context' => $source3->context,
  ));
  $this
    ->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found
    ->isNew(), 'Translation not found but source string found.');

  // Load all translations. For next queries we'll be loading only translated
  // strings.
  $translations = $this->storage
    ->getTranslations(array(
    'translated' => TRUE,
  ));
  $this
    ->assertEqual(count($translations), 2 * $language_count, 'Created and retrieved all translations for source strings.');

  // Load all customized translations.
  $translations = $this->storage
    ->getTranslations(array(
    'customized' => LOCALE_CUSTOMIZED,
    'translated' => TRUE,
  ));
  $this
    ->assertEqual(count($translations), $language_count, 'Retrieved all customized translations for source strings.');

  // Load all Spanish customized translations.
  $translations = $this->storage
    ->getTranslations(array(
    'language' => 'es',
    'customized' => LOCALE_CUSTOMIZED,
    'translated' => TRUE,
  ));
  $this
    ->assertEqual(count($translations), 1, 'Found only Spanish and customized translations.');

  // Load all source strings without translation (1).
  $translations = $this->storage
    ->getStrings(array(
    'translated' => FALSE,
  ));
  $this
    ->assertEqual(count($translations), 1, 'Found 1 source string without translations.');

  // Load Spanish translations using string filter.
  $filter_options['filters'] = array(
    'source' => $prefix,
  );
  $translations = $this->storage
    ->getTranslations(array(
    'language' => 'es',
  ), $filter_options);
  $this
    ->assertEqual(count($translations), 2, 'Found 2 translations using some string filter.');
}