Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / node / tests / src / Functional / Views / NodeLanguageTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional\Views;
4
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;
11
12 /**
13  * Tests node language fields, filters, and sorting.
14  *
15  * @group node
16  */
17 class NodeLanguageTest extends NodeTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['language', 'node_test_views'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_language'];
30
31   /**
32    * List of node titles by language.
33    *
34    * @var array
35    */
36   public $nodeTitles = [];
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp($import_test_views = TRUE) {
42     parent::setUp(FALSE);
43
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']);
48     }
49
50     // Add two new languages.
51     ConfigurableLanguage::createFromLangcode('fr')->save();
52     ConfigurableLanguage::createFromLangcode('es')->save();
53
54     // Make the body field translatable. The title is already translatable by
55     // definition.
56     $field_storage = FieldStorageConfig::loadByName('node', 'body');
57     $field_storage->setTranslatable(TRUE);
58     $field_storage->save();
59
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.
63     $this->nodeTitles = [
64       LanguageInterface::LANGCODE_NOT_SPECIFIED => [
65         'First node und',
66       ],
67       'es' => [
68         'Primero nodo es',
69         'Segundo nodo es',
70         'Tercera nodo es',
71       ],
72       'en' => [
73         'First node en',
74         'Second node en',
75       ],
76       'fr' => [
77         'Premier nœud fr',
78       ],
79     ];
80
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);
88         }
89       }
90       $node->save();
91     }
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);
96       $node->save();
97     }
98
99     $this->container->get('router.builder')->rebuild();
100
101     $user = $this->drupalCreateUser(['access content overview', 'access content']);
102     $this->drupalLogin($user);
103   }
104
105   /**
106    * Tests translation language filter, field, and sort.
107    */
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';
113
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);
119         }
120         else {
121           $this->assertText($title, $title . ' does appear on ' . $message);
122         }
123       }
124     }
125
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);
130
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();
134     $pos_es_max = 0;
135     $pos_fr_min = 10000;
136     foreach ($this->nodeTitles['es'] as $title) {
137       $pos_es_max = max($pos_es_max, strpos($page, $title));
138     }
139     foreach ($this->nodeTitles['fr'] as $title) {
140       $pos_fr_min = min($pos_fr_min, strpos($page, $title));
141     }
142     $this->assertTrue($pos_es_max < $pos_fr_min, 'Spanish translations appear before French on ' . $message);
143
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);
151
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) {
157         continue;
158       }
159       $this->drupalGet(($langcode == 'en' ? '' : "$langcode/") . 'node');
160       foreach ($titles as $title) {
161         $this->assertText($title);
162       }
163       foreach ($this->nodeTitles as $control_langcode => $control_titles) {
164         if ($langcode != $control_langcode) {
165           foreach ($control_titles as $title) {
166             $this->assertNoText($title);
167           }
168         }
169       }
170     }
171
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);
177       }
178     }
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);
184       }
185       foreach ($this->nodeTitles as $control_langcode => $control_titles) {
186         if ($langcode != $control_langcode) {
187           foreach ($control_titles as $title) {
188             $this->assertNoText($title);
189           }
190         }
191       }
192     }
193
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]);
199     $config->save();
200     foreach ($this->nodeTitles as $langcode => $titles) {
201       if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
202         continue;
203       }
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');
209           }
210           else {
211             $this->assertNoText($title, 'Non-English title is not shown when filtering is site default');
212           }
213         }
214       }
215     }
216
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'.
219     //
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***']);
223     $config->save();
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();
230
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');
237         }
238         else {
239           $this->assertNoText($title, 'Non-Spanish title is not shown when filtering is fixed UI language');
240         }
241       }
242     }
243   }
244
245   /**
246    * Tests native name display in language field.
247    */
248   public function testNativeLanguageField() {
249     $this->assertLanguageNames();
250
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);
254     $config->save();
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);
258
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');
267     $config->save();
268     $this->assertLanguageNames();
269     $config->set('display.default.display_options.fields.langcode.native_language', TRUE)->save();
270     $this->assertLanguageNames(TRUE);
271   }
272
273   /**
274    * Asserts the presence of language names in their English or native forms.
275    *
276    * @param bool $native
277    *   (optional) Whether to assert the language name in its native form.
278    */
279   protected function assertLanguageNames($native = FALSE) {
280     $this->drupalGet('test-language');
281     if ($native) {
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.');
286     }
287     else {
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.');
292     }
293   }
294
295 }