More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / search / src / Tests / SearchLanguageTest.php
1 <?php
2
3 namespace Drupal\search\Tests;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests advanced search with different languages added.
10  *
11  * @group search
12  */
13 class SearchLanguageTest extends SearchTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['language'];
21
22   /**
23    * Array of nodes available to search.
24    *
25    * @var \Drupal\node\NodeInterface[]
26    */
27   protected $searchableNodes;
28
29   protected function setUp() {
30     parent::setUp();
31
32     // Create and log in user.
33     $test_user = $this->drupalCreateUser(['access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration']);
34     $this->drupalLogin($test_user);
35
36     // Add a new language.
37     ConfigurableLanguage::createFromLangcode('es')->save();
38
39     // Make the body field translatable. The title is already translatable by
40     // definition. The parent class has already created the article and page
41     // content types.
42     $field_storage = FieldStorageConfig::loadByName('node', 'body');
43     $field_storage->setTranslatable(TRUE);
44     $field_storage->save();
45
46     // Create a few page nodes with multilingual body values.
47     $default_format = filter_default_format();
48     $nodes = [
49       [
50         'title' => 'First node en',
51         'type' => 'page',
52         'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]],
53         'langcode' => 'en',
54       ],
55       [
56         'title' => 'Second node this is the Spanish title',
57         'type' => 'page',
58         'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]],
59         'langcode' => 'es',
60       ],
61       [
62         'title' => 'Third node en',
63         'type' => 'page',
64         'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]],
65         'langcode' => 'en',
66       ],
67     ];
68     $this->searchableNodes = [];
69     foreach ($nodes as $setting) {
70       $this->searchableNodes[] = $this->drupalCreateNode($setting);
71     }
72
73     // Add English translation to the second node.
74     $translation = $this->searchableNodes[1]->addTranslation('en', ['title' => 'Second node en']);
75     $translation->body->value = $this->randomMachineName(32);
76     $this->searchableNodes[1]->save();
77
78     // Add Spanish translation to the third node.
79     $translation = $this->searchableNodes[2]->addTranslation('es', ['title' => 'Third node es']);
80     $translation->body->value = $this->randomMachineName(32);
81     $this->searchableNodes[2]->save();
82
83     // Update the index and then run the shutdown method.
84     $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
85     $plugin->updateIndex();
86     search_update_totals();
87   }
88
89   public function testLanguages() {
90     // Add predefined language.
91     $edit = ['predefined_langcode' => 'fr'];
92     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
93     $this->assertText('French', 'Language added successfully.');
94
95     // Now we should have languages displayed.
96     $this->drupalGet('search/node');
97     $this->assertText(t('Languages'), 'Languages displayed to choose from.');
98     $this->assertText(t('English'), 'English is a possible choice.');
99     $this->assertText(t('French'), 'French is a possible choice.');
100
101     // Ensure selecting no language does not make the query different.
102     $this->drupalPostForm('search/node', [], t('Advanced search'));
103     $this->assertUrl(\Drupal::url('search.view_node_search', [], ['query' => ['keys' => ''], 'absolute' => TRUE]), [], 'Correct page redirection, no language filtering.');
104
105     // Pick French and ensure it is selected.
106     $edit = ['language[fr]' => TRUE];
107     $this->drupalPostForm('search/node', $edit, t('Advanced search'));
108     // Get the redirected URL.
109     $url = $this->getUrl();
110     $parts = parse_url($url);
111     $query_string = isset($parts['query']) ? rawurldecode($parts['query']) : '';
112     $this->assertTrue(strpos($query_string, '=language:fr') !== FALSE, 'Language filter language:fr add to the query string.');
113
114     // Search for keyword node and language filter as Spanish.
115     $edit = ['keys' => 'node', 'language[es]' => TRUE];
116     $this->drupalPostForm('search/node', $edit, t('Advanced search'));
117     // Check for Spanish results.
118     $this->assertLink('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results');
119     $this->assertLink('Third node es', 0, 'Third node Spanish found in search results');
120     // Ensure that results don't contain other language nodes.
121     $this->assertNoLink('First node en', 'Search results do not contain first English node');
122     $this->assertNoLink('Second node en', 'Search results do not contain second English node');
123     $this->assertNoLink('Third node en', 'Search results do not contain third English node');
124
125     // Change the default language and delete English.
126     $path = 'admin/config/regional/language';
127     $this->drupalGet($path);
128     $this->assertFieldChecked('edit-site-default-language-en', 'Default language updated.');
129     $edit = [
130       'site_default_language' => 'fr',
131     ];
132     $this->drupalPostForm($path, $edit, t('Save configuration'));
133     $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
134     $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
135   }
136
137 }