3 namespace Drupal\search\Tests;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests advanced search with different languages added.
13 class SearchLanguageTest extends SearchTestBase {
20 public static $modules = ['language'];
23 * Array of nodes available to search.
25 * @var \Drupal\node\NodeInterface[]
27 protected $searchableNodes;
29 protected function setUp() {
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);
36 // Add a new language.
37 ConfigurableLanguage::createFromLangcode('es')->save();
39 // Make the body field translatable. The title is already translatable by
40 // definition. The parent class has already created the article and page
42 $field_storage = FieldStorageConfig::loadByName('node', 'body');
43 $field_storage->setTranslatable(TRUE);
44 $field_storage->save();
46 // Create a few page nodes with multilingual body values.
47 $default_format = filter_default_format();
50 'title' => 'First node en',
52 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]],
56 'title' => 'Second node this is the Spanish title',
58 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]],
62 'title' => 'Third node en',
64 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]],
68 $this->searchableNodes = [];
69 foreach ($nodes as $setting) {
70 $this->searchableNodes[] = $this->drupalCreateNode($setting);
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();
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();
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();
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.');
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.');
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.');
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.');
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');
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.');
130 'site_default_language' => 'fr',
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'));