View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\node\NodeInterface;
class LocaleContentTest extends BrowserTestBase {
protected static $modules = [
'node',
'locale',
];
protected $defaultTheme = 'classy';
public function testMachineNameLTR() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'administer content types',
'access administration pages',
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/types/add');
$type = $this
->assertSession()
->fieldExists('type');
$this
->assertSame('ltr', $type
->getAttribute('dir'));
$edit = [];
$edit['predefined_langcode'] = 'ar';
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$edit = [
'site_default_language' => 'ar',
];
$this
->drupalGet('admin/config/regional/language');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/structure/types/add');
$type = $this
->assertSession()
->fieldExists('type');
$this
->assertSame('ltr', $type
->getAttribute('dir'));
}
public function testContentTypeLanguageConfiguration() {
$type1 = $this
->drupalCreateContentType();
$type2 = $this
->drupalCreateContentType();
$admin_user = $this
->drupalCreateUser([
'administer languages',
'administer content types',
'access administration pages',
]);
$web_user = $this
->drupalCreateUser([
"create {$type1->id()} content",
"create {$type2->id()} content",
"edit any {$type2->id()} content",
]);
$this
->drupalLogin($admin_user);
$langcode = 'xx';
$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');
$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();
$this
->drupalLogin($web_user);
$this
->drupalGet("node/add/{$type1->id()}");
$this
->assertSession()
->fieldNotExists('langcode[0][value]');
$this
->drupalGet("node/add/{$type2->id()}");
$this
->assertSession()
->fieldExists('langcode[0][value]');
$this
->assertSession()
->pageTextContains($name);
$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);
$path = 'node/' . $node
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->responseContains('<option value="' . $langcode . '" selected="selected">' . $name . '</option>');
$edit = [
'langcode[0][value]' => 'en',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains($node_title . ' has been updated.');
$xpath = $this
->assertSession()
->buildXPathQuery('//div[@data-drupal-messages]//a[contains(@href, :href)]', [
':href' => 'node/' . $node
->id(),
]);
$this
->assertSession()
->elementExists('xpath', $xpath);
$this
->drupalLogout();
}
public function testContentTypeDirLang() {
$type = $this
->drupalCreateContentType();
$admin_user = $this
->drupalCreateUser([
'administer languages',
'administer content types',
'access administration pages',
]);
$web_user = $this
->drupalCreateUser([
"create {$type->id()} content",
"edit own {$type->id()} content",
]);
$this
->drupalLogin($admin_user);
$edit = [];
$edit['predefined_langcode'] = 'ar';
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$edit = [];
$edit['predefined_langcode'] = 'es';
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
\Drupal::languageManager()
->reset();
$this
->drupalGet("admin/structure/types/manage/{$type->id()}");
$edit = [
'language_configuration[language_alterable]' => TRUE,
];
$this
->drupalGet("admin/structure/types/manage/{$type->id()}");
$this
->submitForm($edit, 'Save content type');
$this
->assertSession()
->pageTextContains("The content type {$type->label()} has been updated.");
$this
->drupalLogout();
$this
->drupalLogin($web_user);
$nodes = [];
foreach ([
'en',
'es',
'ar',
] as $langcode) {
$nodes[$langcode] = $this
->drupalCreateNode([
'langcode' => $langcode,
'type' => $type
->id(),
'promote' => NodeInterface::PROMOTED,
]);
}
$this
->drupalGet('node/' . $nodes['en']
->id());
$element = $this
->cssSelect('article.node[lang="en"]');
$this
->assertTrue(empty($element), 'The lang tag has not been assigned to the English node.');
$element = $this
->cssSelect('article.node[dir="ltr"]');
$this
->assertTrue(empty($element), 'The dir tag has not been assigned to the English node.');
$this
->drupalGet('node/' . $nodes['ar']
->id());
$element = $this
->cssSelect('article.node[lang="ar"][dir="rtl"]');
$this
->assertTrue(!empty($element), 'The lang and dir tags have been assigned correctly to the Arabic node.');
$this
->drupalGet('node/' . $nodes['es']
->id());
$element = $this
->cssSelect('article.node[lang="es"]');
$this
->assertTrue(!empty($element), 'The lang tag has been assigned correctly to the Spanish node.');
$element = $this
->cssSelect('article.node[lang="es"][dir="ltr"]');
$this
->assertTrue(empty($element), 'The dir tag has not been assigned to the Spanish node.');
}
}