comparison core/modules/search/tests/src/Functional/SearchExactTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4
5 /**
6 * Tests that searching for a phrase gets the correct page count.
7 *
8 * @group search
9 */
10 class SearchExactTest extends SearchTestBase {
11 /**
12 * Tests that the correct number of pager links are found for both keywords and phrases.
13 */
14 public function testExactQuery() {
15 // Log in with sufficient privileges.
16 $user = $this->drupalCreateUser(['create page content', 'search content']);
17 $this->drupalLogin($user);
18
19 $settings = [
20 'type' => 'page',
21 'title' => 'Simple Node',
22 ];
23 // Create nodes with exact phrase.
24 for ($i = 0; $i <= 17; $i++) {
25 $settings['body'] = [['value' => 'love pizza']];
26 $this->drupalCreateNode($settings);
27 }
28 // Create nodes containing keywords.
29 for ($i = 0; $i <= 17; $i++) {
30 $settings['body'] = [['value' => 'love cheesy pizza']];
31 $this->drupalCreateNode($settings);
32 }
33 // Create another node and save it for later.
34 $settings['body'] = [['value' => 'Druplicon']];
35 $node = $this->drupalCreateNode($settings);
36
37 // Update the search index.
38 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
39 search_update_totals();
40
41 // Refresh variables after the treatment.
42 $this->refreshVariables();
43
44 // Test that the correct number of pager links are found for keyword search.
45 $edit = ['keys' => 'love pizza'];
46 $this->drupalPostForm('search/node', $edit, t('Search'));
47 $this->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.');
48 $this->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.');
49 $this->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.');
50 $this->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.');
51
52 // Test that the correct number of pager links are found for exact phrase search.
53 $edit = ['keys' => '"love pizza"'];
54 $this->drupalPostForm('search/node', $edit, t('Search'));
55 $this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
56 $this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
57
58 // Check that with post settings turned on the post information is displayed.
59 $node_type_config = \Drupal::configFactory()->getEditable('node.type.page');
60 $node_type_config->set('display_submitted', TRUE);
61 $node_type_config->save();
62
63 $edit = ['keys' => 'Druplicon'];
64 $this->drupalPostForm('search/node', $edit, t('Search'));
65 $this->assertText($user->getUsername(), 'Basic page node displays author name when post settings are on.');
66 $this->assertText(format_date($node->getChangedTime(), 'short'), 'Basic page node displays post date when post settings are on.');
67
68 // Check that with post settings turned off the user and changed date
69 // information is not displayed.
70 $node_type_config->set('display_submitted', FALSE);
71 $node_type_config->save();
72 $edit = ['keys' => 'Druplicon'];
73 $this->drupalPostForm('search/node', $edit, t('Search'));
74 $this->assertNoText($user->getUsername(), 'Basic page node does not display author name when post settings are off.');
75 $this->assertNoText(format_date($node->getChangedTime(), 'short'), 'Basic page node does not display post date when post settings are off.');
76
77 }
78
79 }