function LocaleContentTest::testContentTypeLanguageConfiguration

Same name and namespace in other branches
  1. 10 core/modules/locale/tests/src/Functional/LocaleContentTest.php \Drupal\Tests\locale\Functional\LocaleContentTest::testContentTypeLanguageConfiguration()
  2. 11.x core/modules/locale/tests/src/Functional/LocaleContentTest.php \Drupal\Tests\locale\Functional\LocaleContentTest::testContentTypeLanguageConfiguration()
  3. 9 core/modules/locale/tests/src/Functional/LocaleContentTest.php \Drupal\Tests\locale\Functional\LocaleContentTest::testContentTypeLanguageConfiguration()
  4. main core/modules/locale/tests/src/Functional/LocaleContentTest.php \Drupal\Tests\locale\Functional\LocaleContentTest::testContentTypeLanguageConfiguration()

Test if a content type can be set to multilingual and language is present.

File

core/modules/locale/tests/src/Functional/LocaleContentTest.php, line 66

Class

LocaleContentTest
Tests you can enable multilingual support on content types and configure a language for a node.

Namespace

Drupal\Tests\locale\Functional

Code

public function testContentTypeLanguageConfiguration() {
  $type1 = $this->drupalCreateContentType();
  $type2 = $this->drupalCreateContentType();
  // User to add and remove language.
  $admin_user = $this->drupalCreateUser([
    'administer languages',
    'administer content types',
    'access administration pages',
  ]);
  // User to create a node.
  $web_user = $this->drupalCreateUser([
    "create {$type1->id()} content",
    "create {$type2->id()} content",
    "edit any {$type2->id()} content",
  ]);
  // Add custom language.
  $this->drupalLogin($admin_user);
  // Code for the language.
  $langcode = 'xx';
  // The English name for the language.
  $name = $this->randomMachineName(16);
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'label' => $name,
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];
  $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
  // Set the content type to use multilingual support.
  $this->drupalGet("admin/structure/types/manage/{$type2->id()}");
  $this->assertText(t('Language settings'), 'Multilingual support widget present on content type configuration form.');
  $edit = [
    'language_configuration[language_alterable]' => TRUE,
  ];
  $this->drupalPostForm("admin/structure/types/manage/{$type2->id()}", $edit, t('Save content type'));
  $this->assertRaw(t('The content type %type has been updated.', [
    '%type' => $type2->label(),
  ]));
  $this->drupalLogout();
  \Drupal::languageManager()->reset();
  // Verify language selection is not present on the node add form.
  $this->drupalLogin($web_user);
  $this->drupalGet("node/add/{$type1->id()}");
  // Verify language select list is not present.
  $this->assertNoFieldByName('langcode[0][value]', NULL, 'Language select not present on the node add form.');
  // Verify language selection appears on the node add form.
  $this->drupalGet("node/add/{$type2->id()}");
  // Verify language select list is present.
  $this->assertFieldByName('langcode[0][value]', NULL, 'Language select present on the node add form.');
  // Ensure language appears.
  $this->assertText($name, 'Language present.');
  // Create a node.
  $node_title = $this->randomMachineName();
  $node_body = $this->randomMachineName();
  $edit = [
    'type' => $type2->id(),
    'title' => $node_title,
    'body' => [
      [
        'value' => $node_body,
      ],
    ],
    'langcode' => $langcode,
  ];
  $node = $this->drupalCreateNode($edit);
  // Edit the content and ensure correct language is selected.
  $path = 'node/' . $node->id() . '/edit';
  $this->drupalGet($path);
  $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', 'Correct language selected.');
  // Ensure we can change the node language.
  $edit = [
    'langcode[0][value]' => 'en',
  ];
  $this->drupalPostForm($path, $edit, t('Save'));
  $this->assertText(t('@title has been updated.', [
    '@title' => $node_title,
  ]));
  // Verify that the creation message contains a link to a node.
  $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
    ':href' => 'node/' . $node->id(),
  ]);
  $this->assert(isset($view_link), 'The message area contains the link to the edited node');
  $this->drupalLogout();
}

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