View source
<?php
namespace Drupal\node\Tests\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 {
public static $modules = array(
'language',
'node_test_views',
);
public static $testViews = array(
'test_language',
);
public $nodeTitles = [];
protected function setUp() {
parent::setUp(FALSE);
if ($this->profile != 'standard') {
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
ViewTestData::createTestViews(get_class($this), array(
'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 = array(
LanguageInterface::LANGCODE_NOT_SPECIFIED => array(
'First node und',
),
'es' => array(
'Primero nodo es',
'Segundo nodo es',
'Tercera nodo es',
),
'en' => array(
'First node en',
'Second node en',
),
'fr' => array(
'Premier nœud fr',
),
);
foreach ($this->nodeTitles['es'] as $index => $title) {
$node = $this
->drupalCreateNode(array(
'title' => $title,
'langcode' => 'es',
'type' => 'page',
'promote' => 1,
));
foreach (array(
'en',
'fr',
) as $langcode) {
if (isset($this->nodeTitles[$langcode][$index])) {
$translation = $node
->addTranslation($langcode, array(
'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(array(
'title' => $title,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'type' => 'page',
'promote' => 1,
));
$node->body->value = $this
->randomMachineName(32);
$node
->save();
}
$this->container
->get('router.builder')
->rebuild();
$user = $this
->drupalCreateUser(array(
'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
->assertNoText($title, $title . ' does not appear on ' . $message);
}
else {
$this
->assertText($title, $title . ' does appear on ' . $message);
}
}
}
$this
->assertNoText('English', 'English language is not shown on ' . $message);
$this
->assertText('French', 'French language is shown on ' . $message);
$this
->assertText('Spanish', 'Spanish language is shown on ' . $message);
$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
->assertTrue($pos_es_max < $pos_fr_min, 'Spanish translations appear before French on ' . $message);
$this
->drupalGet('test-language/es');
$message = 'Spanish argument page';
$this
->assertNoText('English', 'English language is not shown on ' . $message);
$this
->assertNoText('French', 'French language is not shown on ' . $message);
$this
->assertText('Spanish', 'Spanish language is shown on ' . $message);
foreach ($this->nodeTitles as $langcode => $titles) {
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
continue;
}
$this
->drupalGet(($langcode == 'en' ? '' : "{$langcode}/") . 'node');
foreach ($titles as $title) {
$this
->assertText($title);
}
foreach ($this->nodeTitles as $control_langcode => $control_titles) {
if ($langcode != $control_langcode) {
foreach ($control_titles as $title) {
$this
->assertNoText($title);
}
}
}
}
$this
->drupalGet('admin/content');
foreach ($this->nodeTitles as $titles) {
foreach ($titles as $title) {
$this
->assertText($title);
}
}
foreach ($this->nodeTitles as $langcode => $titles) {
$this
->drupalGet('admin/content', array(
'query' => array(
'langcode' => $langcode,
),
));
foreach ($titles as $title) {
$this
->assertText($title);
}
foreach ($this->nodeTitles as $control_langcode => $control_titles) {
if ($langcode != $control_langcode) {
foreach ($control_titles as $title) {
$this
->assertNoText($title);
}
}
}
}
$config = $this
->config('views.view.frontpage');
$config
->set('display.default.display_options.filters.langcode.value', array(
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
->assertText($title, 'English title is shown when filtering is site default');
}
else {
$this
->assertNoText($title, 'Non-English title is not shown when filtering is site default');
}
}
}
}
$config
->set('display.default.display_options.filters.langcode.value', array(
'***LANGUAGE_language_interface***' => '***LANGUAGE_language_interface***',
));
$config
->save();
$language_config = $this
->config('language.types');
$language_config
->set('negotiation.language_interface.enabled', array(
'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
->assertText($title, 'Spanish title is shown when filtering is fixed UI language');
}
else {
$this
->assertNoText($title, 'Non-Spanish title is not shown when filtering is fixed UI language');
}
}
}
}
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($native = FALSE) {
$this
->drupalGet('test-language');
if ($native) {
$this
->assertText('Français', 'French language shown in native form.');
$this
->assertText('Español', 'Spanish language shown in native form.');
$this
->assertNoText('French', 'French language not shown in English.');
$this
->assertNoText('Spanish', 'Spanish language not shown in English.');
}
else {
$this
->assertNoText('Français', 'French language not shown in native form.');
$this
->assertNoText('Español', 'Spanish language not shown in native form.');
$this
->assertText('French', 'French language shown in English.');
$this
->assertText('Spanish', 'Spanish language shown in English.');
}
}
}