function EntityTranslationTest::doTestEntityLanguageMethods

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestEntityLanguageMethods()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestEntityLanguageMethods()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::doTestEntityLanguageMethods()

Executes the entity language method tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityTranslationTest::doTestEntityLanguageMethods()
EntityTranslationTest::testEntityLanguageMethods in core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
Tests language related methods of the Entity class.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php, line 37

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestEntityLanguageMethods($entity_type) {
    $langcode_key = $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getKey('langcode');
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'name' => 'test',
        'user_id' => $this->container
            ->get('current_user')
            ->id(),
    ]);
    $this->assertEqual($entity->language()
        ->getId(), $this->languageManager
        ->getDefaultLanguage()
        ->getId(), new FormattableMarkup('%entity_type: Entity created with API has default language.', [
        '%entity_type' => $entity_type,
    ]));
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'name' => 'test',
        'user_id' => \Drupal::currentUser()->id(),
        $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $this->assertEqual($entity->language()
        ->getId(), LanguageInterface::LANGCODE_NOT_SPECIFIED, new FormattableMarkup('%entity_type: Entity language not specified.', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEmpty($entity->getTranslationLanguages(FALSE), new FormattableMarkup('%entity_type: No translations are available', [
        '%entity_type' => $entity_type,
    ]));
    // Set the value in default language.
    $entity->set($this->fieldName, [
        0 => [
            'value' => 'default value',
        ],
    ]);
    // Get the value.
    $field = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)
        ->get($this->fieldName);
    $this->assertEqual($field->value, 'default value', new FormattableMarkup('%entity_type: Untranslated value retrieved.', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEqual($field->getLangcode(), LanguageInterface::LANGCODE_NOT_SPECIFIED, new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
        '%entity_type' => $entity_type,
    ]));
    // Try to get add a translation to language neutral entity.
    try {
        $entity->addTranslation($this->langcodes[1]);
        $this->fail('Adding a translation to a language-neutral entity results in an error.');
    } catch (\InvalidArgumentException $e) {
        // Expected exception; just continue testing.
    }
    // Now, make the entity language-specific by assigning a language and test
    // translating it.
    $default_langcode = $this->langcodes[0];
    $entity->{$langcode_key}->value = $default_langcode;
    $entity->{$this->fieldName} = [];
    $this->assertEqual($entity->language(), \Drupal::languageManager()->getLanguage($this->langcodes[0]), new FormattableMarkup('%entity_type: Entity language retrieved.', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEmpty($entity->getTranslationLanguages(FALSE), new FormattableMarkup('%entity_type: No translations are available', [
        '%entity_type' => $entity_type,
    ]));
    // Set the value in default language.
    $entity->set($this->fieldName, [
        0 => [
            'value' => 'default value',
        ],
    ]);
    // Get the value.
    $field = $entity->get($this->fieldName);
    $this->assertEqual($field->value, 'default value', new FormattableMarkup('%entity_type: Untranslated value retrieved.', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEqual($field->getLangcode(), $default_langcode, new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
        '%entity_type' => $entity_type,
    ]));
    // Set a translation.
    $entity->addTranslation($this->langcodes[1])
        ->set($this->fieldName, [
        0 => [
            'value' => 'translation 1',
        ],
    ]);
    $field = $entity->getTranslation($this->langcodes[1])->{$this->fieldName};
    $this->assertEqual($field->value, 'translation 1', new FormattableMarkup('%entity_type: Translated value set.', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEqual($field->getLangcode(), $this->langcodes[1], new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
        '%entity_type' => $entity_type,
    ]));
    // Make sure the untranslated value stays.
    $field = $entity->get($this->fieldName);
    $this->assertEqual($field->value, 'default value', 'Untranslated value stays.');
    $this->assertEqual($field->getLangcode(), $default_langcode, 'Untranslated value has the expected langcode.');
    $translations[$this->langcodes[1]] = \Drupal::languageManager()->getLanguage($this->langcodes[1]);
    $this->assertEqual($entity->getTranslationLanguages(FALSE), $translations, 'Translations retrieved.');
    // Try to get a value using a language code for a non-existing translation.
    try {
        $entity->getTranslation($this->langcodes[2])
            ->get($this->fieldName)->value;
        $this->fail('Getting a non existing translation results in an error.');
    } catch (\InvalidArgumentException $e) {
        // Expected exception; just continue testing.
    }
    // Try to get a not available translation.
    $this->assertNull($entity->addTranslation($this->langcodes[2])
        ->get($this->fieldName)->value, new FormattableMarkup('%entity_type: A translation that is not available is NULL.', [
        '%entity_type' => $entity_type,
    ]));
    // Try to get a value using an invalid language code.
    try {
        $entity->getTranslation('invalid')
            ->get($this->fieldName)->value;
        $this->fail('Getting an invalid translation results in an error.');
    } catch (\InvalidArgumentException $e) {
        // Expected exception; just continue testing.
    }
    // Try to set a value using an invalid language code.
    try {
        $entity->getTranslation('invalid')
            ->set($this->fieldName, NULL);
        $this->fail(new FormattableMarkup('%entity_type: Setting a translation for an invalid language throws an exception.', [
            '%entity_type' => $entity_type,
        ]));
    } catch (\InvalidArgumentException $e) {
        // Expected exception; just continue testing.
    }
    // Set the value in default language.
    $field_name = 'field_test_text';
    $entity->getTranslation($this->langcodes[1])
        ->set($field_name, [
        0 => [
            'value' => 'default value2',
        ],
    ]);
    // Get the value.
    $field = $entity->get($field_name);
    $this->assertEqual($field->value, 'default value2', new FormattableMarkup('%entity_type: Untranslated value set into a translation in non-strict mode.', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEqual($field->getLangcode(), $default_langcode, new FormattableMarkup('%entity_type: Field object has the expected langcode.', [
        '%entity_type' => $entity_type,
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.