You are here

public function LocaleStringTest::testStringCrudApi 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::testStringCrudApi()
  2. 9 core/modules/locale/tests/src/Kernel/LocaleStringTest.php \Drupal\Tests\locale\Kernel\LocaleStringTest::testStringCrudApi()

Tests CRUD API.

File

core/modules/locale/tests/src/Kernel/LocaleStringTest.php, line 53

Class

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

Namespace

Drupal\Tests\locale\Kernel

Code

public function testStringCrudApi() {

  // Create source string.
  $source = $this
    ->buildSourceString()
    ->save();
  $this
    ->assertNotEmpty($source->lid);

  // Load strings by lid and source.
  $string1 = $this->storage
    ->findString([
    'lid' => $source->lid,
  ]);
  $this
    ->assertEquals($source, $string1);
  $string2 = $this->storage
    ->findString([
    'source' => $source->source,
    'context' => $source->context,
  ]);
  $this
    ->assertEquals($source, $string2);
  $string3 = $this->storage
    ->findString([
    'source' => $source->source,
    'context' => '',
  ]);
  $this
    ->assertNull($string3);

  // Check version handling and updating.
  $this
    ->assertEquals('none', $source->version);
  $string = $this->storage
    ->findTranslation([
    'lid' => $source->lid,
  ]);
  $this
    ->assertEquals(\Drupal::VERSION, $string->version);

  // Create translation and find it by lid and source.
  $langcode = 'es';
  $translation = $this
    ->createTranslation($source, $langcode);
  $this
    ->assertEquals(LOCALE_NOT_CUSTOMIZED, $translation->customized);
  $string1 = $this->storage
    ->findTranslation([
    'language' => $langcode,
    'lid' => $source->lid,
  ]);
  $this
    ->assertEquals($translation->translation, $string1->translation);
  $string2 = $this->storage
    ->findTranslation([
    'language' => $langcode,
    'source' => $source->source,
    'context' => $source->context,
  ]);
  $this
    ->assertEquals($translation->translation, $string2->translation);
  $translation
    ->setCustomized()
    ->save();
  $translation = $this->storage
    ->findTranslation([
    'language' => $langcode,
    'lid' => $source->lid,
  ]);
  $this
    ->assertEquals(LOCALE_CUSTOMIZED, $translation->customized);

  // Delete translation.
  $translation
    ->delete();
  $deleted = $this->storage
    ->findTranslation([
    'language' => $langcode,
    'lid' => $source->lid,
  ]);
  $this
    ->assertNull($deleted->translation);

  // Create some translations and then delete string and all of its
  // translations.
  $lid = $source->lid;
  $this
    ->createAllTranslations($source);
  $search = $this->storage
    ->getTranslations([
    'lid' => $source->lid,
  ]);
  $this
    ->assertCount(3, $search);
  $source
    ->delete();
  $string = $this->storage
    ->findString([
    'lid' => $lid,
  ]);
  $this
    ->assertNull($string);
  $deleted = $search = $this->storage
    ->getTranslations([
    'lid' => $lid,
  ]);
  $this
    ->assertEmpty($deleted);

  // Tests that locations of different types and arbitrary lengths can be
  // added to a source string. Too long locations will be cut off.
  $source_string = $this
    ->buildSourceString();
  $source_string
    ->addLocation('javascript', $this
    ->randomString(8));
  $source_string
    ->addLocation('configuration', $this
    ->randomString(50));
  $source_string
    ->addLocation('code', $this
    ->randomString(100));
  $source_string
    ->addLocation('path', $location = $this
    ->randomString(300));
  $source_string
    ->save();
  $rows = $this->container
    ->get('database')
    ->select('locales_location')
    ->fields('locales_location')
    ->condition('sid', $source_string->lid)
    ->execute()
    ->fetchAllAssoc('type');
  $this
    ->assertCount(4, $rows);
  $this
    ->assertEquals(substr($location, 0, 255), $rows['path']->name);
}