3 namespace Drupal\Tests\node\Functional\Views;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\views\Plugin\views\PluginBase;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\Views;
13 * Tests node language fields, filters, and sorting.
17 class NodeLanguageTest extends NodeTestBase {
22 public static $modules = ['language', 'node_test_views'];
25 * Views used by this test.
29 public static $testViews = ['test_language'];
32 * List of node titles by language.
36 public $nodeTitles = [];
41 protected function setUp($import_test_views = TRUE) {
44 // Create Page content type.
45 if ($this->profile != 'standard') {
46 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
47 ViewTestData::createTestViews(get_class($this), ['node_test_views']);
50 // Add two new languages.
51 ConfigurableLanguage::createFromLangcode('fr')->save();
52 ConfigurableLanguage::createFromLangcode('es')->save();
54 // Make the body field translatable. The title is already translatable by
56 $field_storage = FieldStorageConfig::loadByName('node', 'body');
57 $field_storage->setTranslatable(TRUE);
58 $field_storage->save();
60 // Set up node titles. They should not include the words "French",
61 // "English", or "Spanish", as there is a language field in the view
62 // that prints out those words.
64 LanguageInterface::LANGCODE_NOT_SPECIFIED => [
81 // Create nodes with translations.
82 foreach ($this->nodeTitles['es'] as $index => $title) {
83 $node = $this->drupalCreateNode(['title' => $title, 'langcode' => 'es', 'type' => 'page', 'promote' => 1]);
84 foreach (['en', 'fr'] as $langcode) {
85 if (isset($this->nodeTitles[$langcode][$index])) {
86 $translation = $node->addTranslation($langcode, ['title' => $this->nodeTitles[$langcode][$index]]);
87 $translation->body->value = $this->randomMachineName(32);
92 // Create non-translatable nodes.
93 foreach ($this->nodeTitles[LanguageInterface::LANGCODE_NOT_SPECIFIED] as $index => $title) {
94 $node = $this->drupalCreateNode(['title' => $title, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'type' => 'page', 'promote' => 1]);
95 $node->body->value = $this->randomMachineName(32);
99 $this->container->get('router.builder')->rebuild();
101 $user = $this->drupalCreateUser(['access content overview', 'access content']);
102 $this->drupalLogin($user);
106 * Tests translation language filter, field, and sort.
108 public function testLanguages() {
109 // Test the page with no arguments. It is filtered to Spanish and French.
110 // The page shows node titles and languages.
111 $this->drupalGet('test-language');
112 $message = 'French/Spanish page';
114 // Test that the correct nodes are shown.
115 foreach ($this->nodeTitles as $langcode => $list) {
116 foreach ($list as $title) {
117 if ($langcode == 'en') {
118 $this->assertNoText($title, $title . ' does not appear on ' . $message);
121 $this->assertText($title, $title . ' does appear on ' . $message);
126 // Test that the language field value is shown.
127 $this->assertNoText('English', 'English language is not shown on ' . $message);
128 $this->assertText('French', 'French language is shown on ' . $message);
129 $this->assertText('Spanish', 'Spanish language is shown on ' . $message);
131 // Test page sorting, which is by language code, ascending. So the
132 // Spanish nodes should appear before the French nodes.
133 $page = $this->getTextContent();
136 foreach ($this->nodeTitles['es'] as $title) {
137 $pos_es_max = max($pos_es_max, strpos($page, $title));
139 foreach ($this->nodeTitles['fr'] as $title) {
140 $pos_fr_min = min($pos_fr_min, strpos($page, $title));
142 $this->assertTrue($pos_es_max < $pos_fr_min, 'Spanish translations appear before French on ' . $message);
144 // Test the argument -- filter to just Spanish.
145 $this->drupalGet('test-language/es');
146 // This time, test just the language field.
147 $message = 'Spanish argument page';
148 $this->assertNoText('English', 'English language is not shown on ' . $message);
149 $this->assertNoText('French', 'French language is not shown on ' . $message);
150 $this->assertText('Spanish', 'Spanish language is shown on ' . $message);
152 // Test the front page view filter. Only node titles in the current language
153 // should be displayed on the front page by default.
154 foreach ($this->nodeTitles as $langcode => $titles) {
155 // The frontpage view does not display content without a language.
156 if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
159 $this->drupalGet(($langcode == 'en' ? '' : "$langcode/") . 'node');
160 foreach ($titles as $title) {
161 $this->assertText($title);
163 foreach ($this->nodeTitles as $control_langcode => $control_titles) {
164 if ($langcode != $control_langcode) {
165 foreach ($control_titles as $title) {
166 $this->assertNoText($title);
172 // Test the node admin view filter. By default all translations should show.
173 $this->drupalGet('admin/content');
174 foreach ($this->nodeTitles as $titles) {
175 foreach ($titles as $title) {
176 $this->assertText($title);
179 // When filtered, only the specific languages should show.
180 foreach ($this->nodeTitles as $langcode => $titles) {
181 $this->drupalGet('admin/content', ['query' => ['langcode' => $langcode]]);
182 foreach ($titles as $title) {
183 $this->assertText($title);
185 foreach ($this->nodeTitles as $control_langcode => $control_titles) {
186 if ($langcode != $control_langcode) {
187 foreach ($control_titles as $title) {
188 $this->assertNoText($title);
194 // Override the config for the front page view, so that the language
195 // filter is set to the site default language instead. This should just
196 // show the English nodes, no matter what the content language is.
197 $config = $this->config('views.view.frontpage');
198 $config->set('display.default.display_options.filters.langcode.value', [PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT => PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT]);
200 foreach ($this->nodeTitles as $langcode => $titles) {
201 if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
204 $this->drupalGet(($langcode == 'en' ? '' : "$langcode/") . 'node');
205 foreach ($this->nodeTitles as $control_langcode => $control_titles) {
206 foreach ($control_titles as $title) {
207 if ($control_langcode == 'en') {
208 $this->assertText($title, 'English title is shown when filtering is site default');
211 $this->assertNoText($title, 'Non-English title is not shown when filtering is site default');
217 // Override the config so that the language filter is set to the UI
218 // language, and make that have a fixed value of 'es'.
220 // IMPORTANT: Make sure this part of the test is last -- it is changing
221 // language configuration!
222 $config->set('display.default.display_options.filters.langcode.value', ['***LANGUAGE_language_interface***' => '***LANGUAGE_language_interface***']);
224 $language_config = $this->config('language.types');
225 $language_config->set('negotiation.language_interface.enabled', ['language-selected' => 1]);
226 $language_config->save();
227 $language_config = $this->config('language.negotiation');
228 $language_config->set('selected_langcode', 'es');
229 $language_config->save();
231 // With a fixed language selected, there is no language-based URL.
232 $this->drupalGet('node');
233 foreach ($this->nodeTitles as $control_langcode => $control_titles) {
234 foreach ($control_titles as $title) {
235 if ($control_langcode == 'es') {
236 $this->assertText($title, 'Spanish title is shown when filtering is fixed UI language');
239 $this->assertNoText($title, 'Non-Spanish title is not shown when filtering is fixed UI language');
246 * Tests native name display in language field.
248 public function testNativeLanguageField() {
249 $this->assertLanguageNames();
251 // Modify test view to display native language names and set translations.
252 $config = $this->config('views.view.test_language');
253 $config->set('display.default.display_options.fields.langcode.settings.native_language', TRUE);
255 \Drupal::languageManager()->getLanguageConfigOverride('fr', 'language.entity.fr')->set('label', 'Français')->save();
256 \Drupal::languageManager()->getLanguageConfigOverride('es', 'language.entity.es')->set('label', 'Español')->save();
257 $this->assertLanguageNames(TRUE);
259 // Modify test view to use the views built-in language field and test that.
260 \Drupal::state()->set('node_test_views.use_basic_handler', TRUE);
261 Views::viewsData()->clear();
262 $config = $this->config('views.view.test_language');
263 $config->set('display.default.display_options.fields.langcode.native_language', FALSE);
264 $config->clear('display.default.display_options.fields.langcode.settings');
265 $config->clear('display.default.display_options.fields.langcode.type');
266 $config->set('display.default.display_options.fields.langcode.plugin_id', 'language');
268 $this->assertLanguageNames();
269 $config->set('display.default.display_options.fields.langcode.native_language', TRUE)->save();
270 $this->assertLanguageNames(TRUE);
274 * Asserts the presence of language names in their English or native forms.
276 * @param bool $native
277 * (optional) Whether to assert the language name in its native form.
279 protected function assertLanguageNames($native = FALSE) {
280 $this->drupalGet('test-language');
282 $this->assertText('Français', 'French language shown in native form.');
283 $this->assertText('Español', 'Spanish language shown in native form.');
284 $this->assertNoText('French', 'French language not shown in English.');
285 $this->assertNoText('Spanish', 'Spanish language not shown in English.');
288 $this->assertNoText('Français', 'French language not shown in native form.');
289 $this->assertNoText('Español', 'Spanish language not shown in native form.');
290 $this->assertText('French', 'French language shown in English.');
291 $this->assertText('Spanish', 'Spanish language shown in English.');