View source
<?php
namespace Drupal\Tests\node\Functional\Views;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Plugin\views\PluginBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class NodeLanguageTest extends NodeTestBase {
protected static $modules = [
'language',
'node_test_views',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_language',
];
public $nodeTitles = [];
protected function setUp($import_test_views = TRUE, $modules = []) : void {
parent::setUp(FALSE, $modules);
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
ViewTestData::createTestViews(static::class, [
'node_test_views',
]);
}
ConfigurableLanguage::createFromLangcode('fr')
->save();
ConfigurableLanguage::createFromLangcode('es')
->save();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
$this->nodeTitles = [
LanguageInterface::LANGCODE_NOT_SPECIFIED => [
'First node und',
],
'es' => [
'Primero nodo es',
'Segundo nodo es',
'Tercera nodo es',
],
'en' => [
'First node en',
'Second node en',
],
'fr' => [
'Premier nœud fr',
],
];
foreach ($this->nodeTitles['es'] as $index => $title) {
$node = $this
->drupalCreateNode([
'title' => $title,
'langcode' => 'es',
'type' => 'page',
'promote' => 1,
]);
foreach ([
'en',
'fr',
] as $langcode) {
if (isset($this->nodeTitles[$langcode][$index])) {
$translation = $node
->addTranslation($langcode, [
'title' => $this->nodeTitles[$langcode][$index],
]);
$translation->body->value = $this
->randomMachineName(32);
}
}
$node
->save();
}
foreach ($this->nodeTitles[LanguageInterface::LANGCODE_NOT_SPECIFIED] as $index => $title) {
$node = $this
->drupalCreateNode([
'title' => $title,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'type' => 'page',
'promote' => 1,
]);
$node->body->value = $this
->randomMachineName(32);
$node
->save();
}
$user = $this
->drupalCreateUser([
'access content overview',
'access content',
]);
$this
->drupalLogin($user);
}
public function testLanguages() {
$this
->drupalGet('test-language');
$message = 'French/Spanish page';
foreach ($this->nodeTitles as $langcode => $list) {
foreach ($list as $title) {
if ($langcode == 'en') {
$this
->assertSession()
->pageTextNotContains($title);
}
else {
$this
->assertSession()
->pageTextContains($title);
}
}
}
$this
->assertSession()
->pageTextNotContains('English');
$this
->assertSession()
->pageTextContains('French');
$this
->assertSession()
->pageTextContains('Spanish');
$page = $this
->getTextContent();
$pos_es_max = 0;
$pos_fr_min = 10000;
foreach ($this->nodeTitles['es'] as $title) {
$pos_es_max = max($pos_es_max, strpos($page, $title));
}
foreach ($this->nodeTitles['fr'] as $title) {
$pos_fr_min = min($pos_fr_min, strpos($page, $title));
}
$this
->assertLessThan($pos_fr_min, $pos_es_max, "The Spanish translation should appear before the French one on {$message}.");
$this
->drupalGet('test-language/es');
$message = 'Spanish argument page';
$this
->assertSession()
->pageTextNotContains('English');
$this
->assertSession()
->pageTextNotContains('French');
$this
->assertSession()
->pageTextContains('Spanish');
foreach ($this->nodeTitles as $langcode => $titles) {
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
continue;
}
$this
->drupalGet(($langcode == 'en' ? '' : "{$langcode}/") . 'node');
foreach ($titles as $title) {
$this
->assertSession()
->pageTextContains($title);
}
foreach ($this->nodeTitles as $control_langcode => $control_titles) {
if ($langcode != $control_langcode) {
foreach ($control_titles as $title) {
$this
->assertSession()
->pageTextNotContains($title);
}
}
}
}
$this
->drupalGet('admin/content');
foreach ($this->nodeTitles as $titles) {
foreach ($titles as $title) {
$this
->assertSession()
->pageTextContains($title);
}
}
foreach ($this->nodeTitles as $langcode => $titles) {
$this
->drupalGet('admin/content', [
'query' => [
'langcode' => $langcode,
],
]);
foreach ($titles as $title) {
$this
->assertSession()
->pageTextContains($title);
}
foreach ($this->nodeTitles as $control_langcode => $control_titles) {
if ($langcode != $control_langcode) {
foreach ($control_titles as $title) {
$this
->assertSession()
->pageTextNotContains($title);
}
}
}
}
$config = $this
->config('views.view.frontpage');
$config
->set('display.default.display_options.filters.langcode.value', [
PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT => PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT,
]);
$config
->save();
foreach ($this->nodeTitles as $langcode => $titles) {
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
continue;
}
$this
->drupalGet(($langcode == 'en' ? '' : "{$langcode}/") . 'node');
foreach ($this->nodeTitles as $control_langcode => $control_titles) {
foreach ($control_titles as $title) {
if ($control_langcode == 'en') {
$this
->assertSession()
->pageTextContains($title);
}
else {
$this
->assertSession()
->pageTextNotContains($title);
}
}
}
}
$config
->set('display.default.display_options.filters.langcode.value', [
'***LANGUAGE_language_interface***' => '***LANGUAGE_language_interface***',
]);
$config
->save();
$language_config = $this
->config('language.types');
$language_config
->set('negotiation.language_interface.enabled', [
'language-selected' => 1,
]);
$language_config
->save();
$language_config = $this
->config('language.negotiation');
$language_config
->set('selected_langcode', 'es');
$language_config
->save();
$this
->drupalGet('node');
foreach ($this->nodeTitles as $control_langcode => $control_titles) {
foreach ($control_titles as $title) {
if ($control_langcode == 'es') {
$this
->assertSession()
->pageTextContains($title);
}
else {
$this
->assertSession()
->pageTextNotContains($title);
}
}
}
}
public function testNativeLanguageField() {
$this
->assertLanguageNames();
$config = $this
->config('views.view.test_language');
$config
->set('display.default.display_options.fields.langcode.settings.native_language', TRUE);
$config
->save();
\Drupal::languageManager()
->getLanguageConfigOverride('fr', 'language.entity.fr')
->set('label', 'Français')
->save();
\Drupal::languageManager()
->getLanguageConfigOverride('es', 'language.entity.es')
->set('label', 'Español')
->save();
$this
->assertLanguageNames(TRUE);
\Drupal::state()
->set('node_test_views.use_basic_handler', TRUE);
Views::viewsData()
->clear();
$config = $this
->config('views.view.test_language');
$config
->set('display.default.display_options.fields.langcode.native_language', FALSE);
$config
->clear('display.default.display_options.fields.langcode.settings');
$config
->clear('display.default.display_options.fields.langcode.type');
$config
->set('display.default.display_options.fields.langcode.plugin_id', 'language');
$config
->save();
$this
->assertLanguageNames();
$config
->set('display.default.display_options.fields.langcode.native_language', TRUE)
->save();
$this
->assertLanguageNames(TRUE);
}
protected function assertLanguageNames(bool $native = FALSE) : void {
$this
->drupalGet('test-language');
if ($native) {
$this
->assertSession()
->pageTextContains('Français');
$this
->assertSession()
->pageTextContains('Español');
$this
->assertSession()
->pageTextNotContains('French');
$this
->assertSession()
->pageTextNotContains('Spanish');
}
else {
$this
->assertSession()
->pageTextNotContains('Français');
$this
->assertSession()
->pageTextNotContains('Español');
$this
->assertSession()
->pageTextContains('French');
$this
->assertSession()
->pageTextContains('Spanish');
}
}
}