function EntityTranslationTest::doTestLanguageChange

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

Executes the entity language change test for the given entity type.

Parameters

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

1 call to EntityTranslationTest::doTestLanguageChange()
EntityTranslationTest::testLanguageChange in core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
Tests that changing entity language does not break field language.

File

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

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestLanguageChange($entity_type) {
    $langcode_key = $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getKey('langcode');
    $controller = $this->entityTypeManager
        ->getStorage($entity_type);
    $langcode = $this->langcodes[0];
    // check that field languages match entity language regardless of field
    // translatability.
    $values = [
        $langcode_key => $langcode,
        $this->fieldName => $this->randomMachineName(),
        $this->untranslatableFieldName => $this->randomMachineName(),
    ];
    $entity = $controller->create($values);
    foreach ([
        $this->fieldName,
        $this->untranslatableFieldName,
    ] as $field_name) {
        $this->assertEqual($entity->get($field_name)
            ->getLangcode(), $langcode, 'Field language works as expected.');
    }
    // Check that field languages keep matching entity language even after
    // changing it.
    $langcode = $this->langcodes[1];
    $entity->{$langcode_key}->value = $langcode;
    foreach ([
        $this->fieldName,
        $this->untranslatableFieldName,
    ] as $field_name) {
        $this->assertEqual($entity->get($field_name)
            ->getLangcode(), $langcode, 'Field language works as expected after changing entity language.');
    }
    // Check that entity translation does not affect the language of original
    // field values and untranslatable ones.
    $langcode = $this->langcodes[0];
    $entity->addTranslation($this->langcodes[2], [
        $this->fieldName => $this->randomMachineName(),
    ]);
    $entity->{$langcode_key}->value = $langcode;
    foreach ([
        $this->fieldName,
        $this->untranslatableFieldName,
    ] as $field_name) {
        $this->assertEqual($entity->get($field_name)
            ->getLangcode(), $langcode, 'Field language works as expected after translating the entity and changing language.');
    }
    // Check that setting the default language to an existing translation
    // language causes an exception to be thrown.
    try {
        $entity->{$langcode_key}->value = $this->langcodes[2];
        $this->fail('An exception is thrown when setting the default language to an existing translation language');
    } catch (\InvalidArgumentException $e) {
        // Expected exception; just continue testing.
    }
}

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