3 namespace Drupal\search\Tests;
6 * Tests search index is updated properly when nodes are removed or updated.
10 class SearchNodeUpdateAndDeletionTest extends SearchTestBase {
17 public static $modules = [];
20 * A user with permission to access and search content.
22 * @var \Drupal\user\UserInterface
26 protected function setUp() {
29 // Create a test user and log in.
30 $this->testUser = $this->drupalCreateUser(['access content', 'search content']);
31 $this->drupalLogin($this->testUser);
35 * Tests that the search index info is properly updated when a node changes.
37 public function testSearchIndexUpdateOnNodeChange() {
39 $node = $this->drupalCreateNode([
40 'title' => 'Someone who says Ni!',
41 'body' => [['value' => "We are the knights who say Ni!"]],
45 $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
46 // Update the search index.
47 $node_search_plugin->updateIndex();
48 search_update_totals();
50 // Search the node to verify it appears in search results
51 $edit = ['keys' => 'knights'];
52 $this->drupalPostForm('search/node', $edit, t('Search'));
53 $this->assertText($node->label());
56 $node->body->value = "We want a shrubbery!";
60 $node_search_plugin->updateIndex();
61 search_update_totals();
63 // Search again to verify the new text appears in test results.
64 $edit = ['keys' => 'shrubbery'];
65 $this->drupalPostForm('search/node', $edit, t('Search'));
66 $this->assertText($node->label());
70 * Tests that the search index info is updated when a node is deleted.
72 public function testSearchIndexUpdateOnNodeDeletion() {
74 $node = $this->drupalCreateNode([
75 'title' => 'No dragons here',
76 'body' => [['value' => 'Again: No dragons here']],
80 $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
81 // Update the search index.
82 $node_search_plugin->updateIndex();
83 search_update_totals();
85 // Search the node to verify it appears in search results
86 $edit = ['keys' => 'dragons'];
87 $this->drupalPostForm('search/node', $edit, t('Search'));
88 $this->assertText($node->label());
90 // Get the node info from the search index tables.
91 $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons'])
93 $this->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index'));
98 // Check if the node info is gone from the search table.
99 $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons'])
101 $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index'));
103 // Search again to verify the node doesn't appear anymore.
104 $this->drupalPostForm('search/node', $edit, t('Search'));
105 $this->assertNoText($node->label());