comparison core/modules/search/tests/src/Functional/SearchLanguageTest.php @ 17:129ea1e6d783

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