View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
class NodeFieldMultilingualTest extends BrowserTestBase {
protected static $modules = [
'node',
'language',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$admin_user = $this
->drupalCreateUser([
'administer languages',
'administer content types',
'access administration pages',
'create page content',
'edit own page content',
]);
$this
->drupalLogin($admin_user);
ConfigurableLanguage::createFromLangcode('it')
->save();
$edit = [
'language_interface[enabled][language-url]' => '1',
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$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.");
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
}
public function testMultilingualNodeForm() {
$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);
$edit = [];
$edit[$title_key] = $title_value;
$edit[$body_key] = $body_value;
$this
->drupalGet('node/add/page');
$this
->submitForm($edit, 'Save');
$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);
$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);
$this->container
->get('language_negotiator')
->saveConfiguration(LanguageInterface::TYPE_CONTENT, [
LanguageNegotiationUrl::METHOD_ID => 0,
]);
$this
->drupalGet("it/node/{$node->id()}");
$this
->assertSession()
->pageTextContains($body_value);
$this
->drupalGet("node/{$node->id()}");
$this
->assertSession()
->pageTextContains($body_value);
}
public function testMultilingualDisplaySettings() {
$title_key = 'title[0][value]';
$title_value = $this
->randomMachineName(8);
$body_key = 'body[0][value]';
$body_value = $this
->randomMachineName(16);
$edit = [];
$edit[$title_key] = $title_value;
$edit[$body_key] = $body_value;
$this
->drupalGet('node/add/page');
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit[$title_key]);
$this
->assertNotEmpty($node, 'Node found in database.');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->elementTextEquals('xpath', "//article[contains(concat(' ', normalize-space(@class), ' '), ' node ')]//div[contains(concat(' ', normalize-space(@class), ' '), 'node__content')]/descendant::p", $node->body->value);
}
}