You are here

protected function EntityFormTest::doTestMultilingualFormCRUD in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest::doTestMultilingualFormCRUD()

Executes the multilingual form CRUD tests for the given entity type ID.

Parameters

string $entity_type_id: The ID of entity type to run the tests with.

1 call to EntityFormTest::doTestMultilingualFormCRUD()
EntityFormTest::testMultilingualFormCRUD in core/modules/system/tests/src/Functional/Entity/EntityFormTest.php
Tests basic multilingual form CRUD functionality.

File

core/modules/system/tests/src/Functional/Entity/EntityFormTest.php, line 151

Class

EntityFormTest
Tests the entity form.

Namespace

Drupal\Tests\system\Functional\Entity

Code

protected function doTestMultilingualFormCRUD($entity_type_id) {
  $name1 = $this
    ->randomMachineName(8);
  $name1_ro = $this
    ->randomMachineName(9);
  $name2_ro = $this
    ->randomMachineName(11);
  $edit = [
    'name[0][value]' => $name1,
    'field_test_text[0][value]' => $this
      ->randomMachineName(16),
  ];
  $this
    ->drupalGet($entity_type_id . '/add');
  $this
    ->submitForm($edit, 'Save');
  $entity = $this
    ->loadEntityByName($entity_type_id, $name1);
  $this
    ->assertNotNull($entity, new FormattableMarkup('%entity_type: Entity found in the database.', [
    '%entity_type' => $entity_type_id,
  ]));

  // Add a translation to the newly created entity without using the Content
  // translation module.
  $entity
    ->addTranslation('ro', [
    'name' => $name1_ro,
  ])
    ->save();
  $translated_entity = $this
    ->loadEntityByName($entity_type_id, $name1)
    ->getTranslation('ro');
  $this
    ->assertEquals($name1_ro, $translated_entity->name->value, new FormattableMarkup('%entity_type: The translation has been added.', [
    '%entity_type' => $entity_type_id,
  ]));
  $edit['name[0][value]'] = $name2_ro;
  $this
    ->drupalGet('ro/' . $entity_type_id . '/manage/' . $entity
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');
  $translated_entity = $this
    ->loadEntityByName($entity_type_id, $name1)
    ->getTranslation('ro');
  $this
    ->assertNotNull($translated_entity, new FormattableMarkup('%entity_type: Modified translation found in the database.', [
    '%entity_type' => $entity_type_id,
  ]));
  $this
    ->assertEquals($name2_ro, $translated_entity->name->value, new FormattableMarkup('%entity_type: The name of the translation has been modified.', [
    '%entity_type' => $entity_type_id,
  ]));
  $this
    ->drupalGet('ro/' . $entity_type_id . '/manage/' . $entity
    ->id() . '/edit');
  $this
    ->clickLink('Delete');
  $this
    ->submitForm([], 'Delete Romanian translation');
  $entity = $this
    ->loadEntityByName($entity_type_id, $name1);
  $this
    ->assertNotNull($entity, new FormattableMarkup('%entity_type: The original entity still exists.', [
    '%entity_type' => $entity_type_id,
  ]));
  $this
    ->assertFalse($entity
    ->hasTranslation('ro'), new FormattableMarkup('%entity_type: Entity translation does not exist anymore.', [
    '%entity_type' => $entity_type_id,
  ]));
}