You are here

public function NodeFieldMultilingualTest::testMultilingualNodeForm in Drupal 9

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

Tests whether field languages are correctly set through the node form.

File

core/modules/node/tests/src/Functional/NodeFieldMultilingualTest.php, line 71

Class

NodeFieldMultilingualTest
Tests multilingual support for fields.

Namespace

Drupal\Tests\node\Functional

Code

public function testMultilingualNodeForm() {

  // Create "Basic page" content.
  $langcode = language_get_default_langcode('node', 'page');
  $title_key = 'title[0][value]';
  $title_value = $this
    ->randomMachineName(8);
  $body_key = 'body[0][value]';
  $body_value = $this
    ->randomMachineName(16);

  // Create node to edit.
  $edit = [];
  $edit[$title_key] = $title_value;
  $edit[$body_key] = $body_value;
  $this
    ->drupalGet('node/add/page');
  $this
    ->submitForm($edit, 'Save');

  // Check that the node exists in the database.
  $node = $this
    ->drupalGetNodeByTitle($edit[$title_key]);
  $this
    ->assertNotEmpty($node, 'Node found in database.');
  $this
    ->assertSame($langcode, $node
    ->language()
    ->getId());
  $this
    ->assertSame($body_value, $node->body->value);

  // Change node language.
  $langcode = 'it';
  $this
    ->drupalGet("node/{$node->id()}/edit");
  $edit = [
    $title_key => $this
      ->randomMachineName(8),
    'langcode[0][value]' => $langcode,
  ];
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle($edit[$title_key], TRUE);
  $this
    ->assertNotEmpty($node, 'Node found in database.');
  $this
    ->assertSame($langcode, $node
    ->language()
    ->getId());
  $this
    ->assertSame($body_value, $node->body->value);

  // Enable content language URL detection.
  $this->container
    ->get('language_negotiator')
    ->saveConfiguration(LanguageInterface::TYPE_CONTENT, [
    LanguageNegotiationUrl::METHOD_ID => 0,
  ]);

  // Test multilingual field language fallback logic.
  $this
    ->drupalGet("it/node/{$node->id()}");

  // Verify that body is correctly displayed using Italian as requested
  // language.
  $this
    ->assertSession()
    ->pageTextContains($body_value);
  $this
    ->drupalGet("node/{$node->id()}");

  // Verify that body is correctly displayed using English as requested
  // language.
  $this
    ->assertSession()
    ->pageTextContains($body_value);
}