You are here

public function NodeTypeTranslationTest::testNodeTypeTranslation in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php \Drupal\Tests\node\Functional\NodeTypeTranslationTest::testNodeTypeTranslation()

Tests the node type translation.

File

core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php, line 101

Class

NodeTypeTranslationTest
Ensures that node types translation work correctly.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeTypeTranslation() {
  $type = mb_strtolower($this
    ->randomMachineName(16));
  $name = $this
    ->randomString();
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalCreateContentType([
    'type' => $type,
    'name' => $name,
  ]);

  // Translate the node type name.
  $langcode = $this->additionalLangcodes[0];
  $translated_name = $langcode . '-' . $name;
  $edit = [
    "translation[config_names][node.type.{$type}][name]" => $translated_name,
  ];

  // Edit the title label to avoid having an exception when we save the translation.
  $this
    ->drupalGet("admin/structure/types/manage/{$type}/translate/{$langcode}/add");
  $this
    ->submitForm($edit, 'Save translation');

  // Check the name is translated without admin theme for editing.
  $this
    ->drupalGet('admin/appearance');
  $this
    ->submitForm([
    'use_admin_theme' => '0',
  ], 'Save configuration');
  $this
    ->drupalGet("{$langcode}/node/add/{$type}");

  // This is a Spanish page, so ensure the text asserted is translated in
  // Spanish and not French by adding the langcode option.
  $this
    ->assertSession()
    ->responseContains(t('Create @name', [
    '@name' => $translated_name,
  ], [
    'langcode' => $langcode,
  ]));

  // Check the name is translated with admin theme for editing.
  $this
    ->drupalGet('admin/appearance');
  $this
    ->submitForm([
    'use_admin_theme' => '1',
  ], 'Save configuration');
  $this
    ->drupalGet("{$langcode}/node/add/{$type}");

  // This is a Spanish page, so ensure the text asserted is translated in
  // Spanish and not French by adding the langcode option.
  $this
    ->assertSession()
    ->responseContains(t('Create @name', [
    '@name' => $translated_name,
  ], [
    'langcode' => $langcode,
  ]));
}