comparison core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8 * Tests search index is updated properly when nodes are removed or updated.
9 *
10 * @group search
11 */
12 class SearchNodeUpdateAndDeletionTest extends BrowserTestBase {
13
14 /**
15 * {@inheritdoc}
16 */
17 protected static $modules = ['node', 'search'];
18
19 /**
20 * A user with permission to access and search content.
21 *
22 * @var \Drupal\user\UserInterface
23 */
24 public $testUser;
25
26 protected function setUp() {
27 parent::setUp();
28
29 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
30
31 // Create a test user and log in.
32 $this->testUser = $this->drupalCreateUser(['access content', 'search content']);
33 $this->drupalLogin($this->testUser);
34 }
35
36 /**
37 * Tests that the search index info is properly updated when a node changes.
38 */
39 public function testSearchIndexUpdateOnNodeChange() {
40 // Create a node.
41 $node = $this->drupalCreateNode([
42 'title' => 'Someone who says Ni!',
43 'body' => [['value' => "We are the knights who say Ni!"]],
44 'type' => 'page',
45 ]);
46
47 $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
48 // Update the search index.
49 $node_search_plugin->updateIndex();
50 search_update_totals();
51
52 // Search the node to verify it appears in search results
53 $edit = ['keys' => 'knights'];
54 $this->drupalPostForm('search/node', $edit, t('Search'));
55 $this->assertText($node->label());
56
57 // Update the node
58 $node->body->value = "We want a shrubbery!";
59 $node->save();
60
61 // Run indexer again
62 $node_search_plugin->updateIndex();
63 search_update_totals();
64
65 // Search again to verify the new text appears in test results.
66 $edit = ['keys' => 'shrubbery'];
67 $this->drupalPostForm('search/node', $edit, t('Search'));
68 $this->assertText($node->label());
69 }
70
71 /**
72 * Tests that the search index info is updated when a node is deleted.
73 */
74 public function testSearchIndexUpdateOnNodeDeletion() {
75 // Create a node.
76 $node = $this->drupalCreateNode([
77 'title' => 'No dragons here',
78 'body' => [['value' => 'Again: No dragons here']],
79 'type' => 'page',
80 ]);
81
82 $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
83 // Update the search index.
84 $node_search_plugin->updateIndex();
85 search_update_totals();
86
87 // Search the node to verify it appears in search results
88 $edit = ['keys' => 'dragons'];
89 $this->drupalPostForm('search/node', $edit, t('Search'));
90 $this->assertText($node->label());
91
92 // Get the node info from the search index tables.
93 $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons'])
94 ->fetchField();
95 $this->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index'));
96
97 // Delete the node.
98 $node->delete();
99
100 // Check if the node info is gone from the search table.
101 $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons'])
102 ->fetchField();
103 $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index'));
104
105 // Search again to verify the node doesn't appear anymore.
106 $this->drupalPostForm('search/node', $edit, t('Search'));
107 $this->assertNoText($node->label());
108 }
109
110 }