Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / search / src / Tests / SearchNodeUpdateAndDeletionTest.php
1 <?php
2
3 namespace Drupal\search\Tests;
4
5 /**
6  * Tests search index is updated properly when nodes are removed or updated.
7  *
8  * @group search
9  */
10 class SearchNodeUpdateAndDeletionTest extends SearchTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = [];
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     // Create a test user and log in.
30     $this->testUser = $this->drupalCreateUser(['access content', 'search content']);
31     $this->drupalLogin($this->testUser);
32   }
33
34   /**
35    * Tests that the search index info is properly updated when a node changes.
36    */
37   public function testSearchIndexUpdateOnNodeChange() {
38     // Create a node.
39     $node = $this->drupalCreateNode([
40       'title' => 'Someone who says Ni!',
41       'body' => [['value' => "We are the knights who say Ni!"]],
42       'type' => 'page',
43     ]);
44
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();
49
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());
54
55     // Update the node
56     $node->body->value = "We want a shrubbery!";
57     $node->save();
58
59     // Run indexer again
60     $node_search_plugin->updateIndex();
61     search_update_totals();
62
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());
67   }
68
69   /**
70    * Tests that the search index info is updated when a node is deleted.
71    */
72   public function testSearchIndexUpdateOnNodeDeletion() {
73     // Create a node.
74     $node = $this->drupalCreateNode([
75       'title' => 'No dragons here',
76       'body' => [['value' => 'Again: No dragons here']],
77       'type' => 'page',
78     ]);
79
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();
84
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());
89
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'])
92       ->fetchField();
93     $this->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index'));
94
95     // Delete the node.
96     $node->delete();
97
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'])
100       ->fetchField();
101     $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index'));
102
103     // Search again to verify the node doesn't appear anymore.
104     $this->drupalPostForm('search/node', $edit, t('Search'));
105     $this->assertNoText($node->label());
106   }
107
108 }