class NodeFieldMultilingualTest
Same name and namespace in other branches
- 11.x core/modules/node/tests/src/Functional/NodeFieldMultilingualTest.php \Drupal\Tests\node\Functional\NodeFieldMultilingualTest
- 10 core/modules/node/tests/src/Functional/NodeFieldMultilingualTest.php \Drupal\Tests\node\Functional\NodeFieldMultilingualTest
- 8.9.x core/modules/node/tests/src/Functional/NodeFieldMultilingualTest.php \Drupal\Tests\node\Functional\NodeFieldMultilingualTest
Tests multilingual support for fields.
@group node
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\node\Functional\NodeFieldMultilingualTest extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of NodeFieldMultilingualTest
File
-
core/
modules/ node/ tests/ src/ Functional/ NodeFieldMultilingualTest.php, line 16
Namespace
Drupal\Tests\node\FunctionalView source
class NodeFieldMultilingualTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'node',
'language',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Create Basic page node type.
$this->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
// Setup users.
$admin_user = $this->drupalCreateUser([
'administer languages',
'administer content types',
'access administration pages',
'create page content',
'edit own page content',
]);
$this->drupalLogin($admin_user);
// Add a new language.
ConfigurableLanguage::createFromLangcode('it')->save();
// Enable URL language detection and selection.
$edit = [
'language_interface[enabled][language-url]' => '1',
];
$this->drupalGet('admin/config/regional/language/detection');
$this->submitForm($edit, 'Save settings');
// Set "Basic page" content type to use multilingual support.
$edit = [
'language_configuration[language_alterable]' => TRUE,
];
$this->drupalGet('admin/structure/types/manage/page');
$this->submitForm($edit, 'Save content type');
$this->assertSession()
->pageTextContains("The content type Basic page has been updated.");
// Make node body translatable.
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage->setTranslatable(TRUE);
$field_storage->save();
}
/**
* Tests whether field languages are correctly set through the node form.
*/
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);
}
/**
* Tests multilingual field display settings.
*/
public function testMultilingualDisplaySettings() {
// Create "Basic page" content.
$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.');
// Check if node body is showed.
$this->drupalGet('node/' . $node->id());
$this->assertSession()
->elementTextEquals('xpath', "//article/div//p", $node->body->value);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.