You are here

public function NodeTypeTranslationTest::testNodeTypeTitleLabelTranslation 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::testNodeTypeTitleLabelTranslation()

Tests the node type title label translation.

File

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

Class

NodeTypeTranslationTest
Ensures that node types translation work correctly.

Namespace

Drupal\Tests\node\Functional

Code

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

  // Edit the title label for it to be displayed on the translation form.
  $this
    ->drupalGet("admin/structure/types/manage/{$type}");
  $this
    ->submitForm([
    'title_label' => 'Edited title',
  ], 'Save content type');

  // Assert that the title label is displayed on the translation form with the right value.
  $this
    ->drupalGet("admin/structure/types/manage/{$type}/translate/{$langcode}/add");
  $this
    ->assertSession()
    ->pageTextContains('Edited title');

  // Translate the title label.
  $this
    ->submitForm([
    "translation[config_names][core.base_field_override.node.{$type}.title][label]" => 'Translated title',
  ], 'Save translation');

  // Assert that the right title label is displayed on the node add form. The
  // translations are created in this test; therefore, the assertions do not
  // use t(). If t() were used then the correct langcodes would need to be
  // provided.
  $this
    ->drupalGet("node/add/{$type}");
  $this
    ->assertSession()
    ->pageTextContains('Edited title');
  $this
    ->drupalGet("{$langcode}/node/add/{$type}");
  $this
    ->assertSession()
    ->pageTextContains('Translated title');

  // Add an e-mail field.
  $this
    ->drupalGet("admin/structure/types/manage/{$type}/fields/add-field");
  $this
    ->submitForm([
    'new_storage_type' => 'email',
    'label' => 'Email',
    'field_name' => 'email',
  ], 'Save and continue');
  $this
    ->submitForm([], 'Save field settings');
  $this
    ->submitForm([], 'Save settings');
  $type = mb_strtolower($this
    ->randomMachineName(16));
  $name = $this
    ->randomString();
  $this
    ->drupalCreateContentType([
    'type' => $type,
    'name' => $name,
  ]);

  // Set tabs.
  $this
    ->drupalPlaceBlock('local_tasks_block', [
    'primary' => TRUE,
  ]);

  // Change default language.
  $this
    ->drupalGet('admin/config/regional/language');
  $this
    ->submitForm([
    'site_default_language' => 'es',
  ], 'Save configuration');

  // Try re-using the email field.
  $this
    ->drupalGet("es/admin/structure/types/manage/{$type}/fields/add-field");
  $this
    ->submitForm([
    'existing_storage_name' => 'field_email',
    'existing_storage_label' => 'Email',
  ], 'Save and continue');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet("es/admin/structure/types/manage/{$type}/fields/node.{$type}.field_email/translate");
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains("The configuration objects have different language codes so they cannot be translated");
}