function LocaleContentTest::testContentTypeLanguageConfiguration

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

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

File

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

Class

LocaleContentTest
Tests multilingual support for content types and individual nodes.

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->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add custom language');
    // Set the content type to use multilingual support.
    $this->drupalGet("admin/structure/types/manage/{$type2->id()}");
    $this->assertSession()
        ->pageTextContains('Language settings');
    $edit = [
        'language_configuration[language_alterable]' => TRUE,
    ];
    $this->drupalGet("admin/structure/types/manage/{$type2->id()}");
    $this->submitForm($edit, 'Save content type');
    $this->assertSession()
        ->pageTextContains("The content type {$type2->label()} has been updated.");
    $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->assertSession()
        ->fieldNotExists('langcode[0][value]');
    // Verify language selection appears on the node add form.
    $this->drupalGet("node/add/{$type2->id()}");
    // Verify language select list is present.
    $this->assertSession()
        ->fieldExists('langcode[0][value]');
    // Ensure language appears.
    $this->assertSession()
        ->pageTextContains($name);
    // 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->assertSession()
        ->responseContains('<option value="' . $langcode . '" selected="selected">' . $name . '</option>');
    // Ensure we can change the node language.
    $edit = [
        'langcode[0][value]' => 'en',
    ];
    $this->drupalGet($path);
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains($node_title . ' has been updated.');
    // Verify that the creation message contains a link to a node.
    $xpath = $this->assertSession()
        ->buildXPathQuery('//div[@data-drupal-messages]//a[contains(@href, :href)]', [
        ':href' => 'node/' . $node->id(),
    ]);
    $this->assertSession()
        ->elementExists('xpath', $xpath);
    $this->drupalLogout();
}

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