View source
<?php
namespace Drupal\node\Tests;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\LanguageInterface;
class NodeFieldMultilingualTest extends WebTestBase {
public static $modules = array(
'node',
'language',
);
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$admin_user = $this
->drupalCreateUser(array(
'administer languages',
'administer content types',
'access administration pages',
'create page content',
'edit own page content',
));
$this
->drupalLogin($admin_user);
ConfigurableLanguage::createFromLangcode('it')
->save();
$edit = array(
'language_interface[enabled][language-url]' => '1',
);
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$edit = array(
'language_configuration[language_alterable]' => TRUE,
);
$this
->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
$this
->assertRaw(t('The content type %type has been updated.', array(
'%type' => 'Basic page',
)), 'Basic page content type has been updated.');
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
}
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 = array();
$edit[$title_key] = $title_value;
$edit[$body_key] = $body_value;
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit[$title_key]);
$this
->assertTrue($node, 'Node found in database.');
$this
->assertTrue($node
->language()
->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly set.');
$langcode = 'it';
$this
->drupalGet("node/{$node->id()}/edit");
$edit = array(
$title_key => $this
->randomMachineName(8),
'langcode[0][value]' => $langcode,
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit[$title_key], TRUE);
$this
->assertTrue($node, 'Node found in database.');
$this
->assertTrue($node
->language()
->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly changed.');
$this->container
->get('language_negotiator')
->saveConfiguration(LanguageInterface::TYPE_CONTENT, array(
LanguageNegotiationUrl::METHOD_ID => 0,
));
$this
->drupalGet("it/node/{$node->id()}");
$this
->assertRaw($body_value, 'Body correctly displayed using Italian as requested language');
$this
->drupalGet("node/{$node->id()}");
$this
->assertRaw($body_value, 'Body correctly displayed using English as requested language');
}
function testMultilingualDisplaySettings() {
$title_key = 'title[0][value]';
$title_value = $this
->randomMachineName(8);
$body_key = 'body[0][value]';
$body_value = $this
->randomMachineName(16);
$edit = array();
$edit[$title_key] = $title_value;
$edit[$body_key] = $body_value;
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit[$title_key]);
$this
->assertTrue($node, 'Node found in database.');
$this
->drupalGet('node/' . $node
->id());
$body = $this
->xpath('//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]//div[contains(concat(" ", normalize-space(@class), " "), :content-class)]/descendant::p', array(
':node-class' => ' node ',
':content-class' => 'node__content',
));
$this
->assertEqual(current($body), $node->body->value, 'Node body found.');
}
}