public function EntityTranslationTest::testDeleteEntityTranslation in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testDeleteEntityTranslation()
- 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php \Drupal\KernelTests\Core\Entity\EntityTranslationTest::testDeleteEntityTranslation()
Tests if entity translation statuses are correct after removing two translation.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityTranslationTest.php, line 834
Class
- EntityTranslationTest
- Tests entity translation functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testDeleteEntityTranslation() {
$entity_type = 'entity_test_mul';
$controller = $this->entityTypeManager
->getStorage($entity_type);
// Create a translatable test field.
$field_storage = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => 'translatable_test_field',
'type' => 'field_test',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => $this
->randomMachineName(),
'bundle' => $entity_type,
]);
$field
->save();
// Create an untranslatable test field.
$field_storage = FieldStorageConfig::create([
'entity_type' => $entity_type,
'field_name' => 'untranslatable_test_field',
'type' => 'field_test',
'translatable' => FALSE,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => $this
->randomMachineName(),
'bundle' => $entity_type,
]);
$field
->save();
// Create an entity with both translatable and untranslatable test fields.
$values = [
'name' => $this
->randomString(),
'translatable_test_field' => $this
->randomString(),
'untranslatable_test_field' => $this
->randomString(),
];
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $controller
->create($values);
foreach ($this->langcodes as $langcode) {
$entity
->addTranslation($langcode, $values);
}
$entity
->save();
// Assert there are no deleted languages in the lists yet.
$this
->assertNull(\Drupal::state()
->get('entity_test.delete.translatable_test_field'));
$this
->assertNull(\Drupal::state()
->get('entity_test.delete.untranslatable_test_field'));
// Remove the second and third langcodes from the entity.
$entity
->removeTranslation('l1');
$entity
->removeTranslation('l2');
$entity
->save();
// Ensure that for the translatable test field the second and third
// langcodes are in the deleted languages list.
$actual = \Drupal::state()
->get('entity_test.delete.translatable_test_field');
$expected_translatable = [
'l1',
'l2',
];
sort($actual);
sort($expected_translatable);
$this
->assertEquals($expected_translatable, $actual);
// Ensure that the untranslatable test field is untouched.
$this
->assertNull(\Drupal::state()
->get('entity_test.delete.untranslatable_test_field'));
// Delete the entity, which removes all remaining translations.
$entity
->delete();
// All languages have been deleted now.
$actual = \Drupal::state()
->get('entity_test.delete.translatable_test_field');
$expected_translatable[] = 'en';
$expected_translatable[] = 'l0';
sort($actual);
sort($expected_translatable);
$this
->assertEquals($expected_translatable, $actual);
// The untranslatable field is shared and only deleted once, for the
// default langcode.
$actual = \Drupal::state()
->get('entity_test.delete.untranslatable_test_field');
$expected_untranslatable = [
'en',
];
sort($actual);
sort($expected_untranslatable);
$this
->assertEquals($expected_untranslatable, $actual);
}