diff --git a/core/modules/node/lib/Drupal/node/Entity/Node.php b/core/modules/node/lib/Drupal/node/Entity/Node.php index 1d13b76..a70afd3 100644 --- a/core/modules/node/lib/Drupal/node/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Entity/Node.php @@ -124,6 +124,12 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $ if ($this->isDefaultRevision()) { \Drupal::entityManager()->getAccessController('node')->writeGrants($this, $update); } + + // Reindex the node when it is updated. The node is automatically indexed + // when it is added, simply by being added to the node table. + if ($update) { + node_reindex_node_search($this->id()); + } } /** @@ -132,9 +138,10 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $ public static function preDelete(EntityStorageControllerInterface $storage_controller, array $entities) { parent::preDelete($storage_controller, $entities); - if (module_exists('search')) { + // Assure that all nodes deleted are removed from the search index. + if (\Drupal::moduleHandler()->moduleExists('search')) { foreach ($entities as $entity) { - search_reindex($entity->nid->value, 'node'); + search_reindex($entity->nid->value, 'node_search'); } } } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 376efa4..70c1094 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -2210,15 +2210,6 @@ function node_reindex_node_search($nid) { } /** - * Implements hook_node_update(). - */ -function node_node_update(EntityInterface $node) { - // Reindex the node when it is updated. The node is automatically indexed - // when it is added, simply by being added to the node table. - node_reindex_node_search($node->id()); -} - -/** * Implements hook_comment_insert(). */ function node_comment_insert($comment) { diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php new file mode 100644 index 0000000..b03b1c8 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php @@ -0,0 +1,111 @@ + 'Search index synchronization on node updating / removal', + 'description' => 'Tests search index is updated properly when nodes are removed or updated.', + 'group' => 'Search', + ); + } + + function setUp() { + parent::setUp(); + + // Create a test user and log in. + $this->test_user = $this->drupalCreateUser(array('access content', 'search content')); + $this->drupalLogin($this->test_user); + } + + /** + * Tests that the search index info is properly updated when a node changes. + */ + function testSearchIndexUpdateOnNodeChange() { + // Create a node. + $node = $this->drupalCreateNode(array( + 'title' => 'Someone who says Ni!', + 'body' => array(array('value' => "We are the knights who say Ni!")), + 'type' => 'page')); + + $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); + // Update the search index. + $node_search_plugin->updateIndex(); + search_update_totals(); + + // Search the node to verify it appears in search results + $edit = array('keys' => 'knights'); + $this->drupalPostForm('search/node', $edit, t('Search')); + $this->assertText($node->label()); + + // Update the node + $node->body->value = "We want a shrubbery!"; + $node->save(); + + // Run indexer again + $node_search_plugin->updateIndex(); + search_update_totals(); + + // Search again to verify the new text appears in test results. + $edit = array('keys' => 'shrubbery'); + $this->drupalPostForm('search/node', $edit, t('Search')); + $this->assertText($node->label()); + } + + /** + * Tests that the search index info is updated when a node is deleted. + */ + function testSearchIndexUpdateOnNodeDeletion() { + // Create a node. + $node = $this->drupalCreateNode(array( + 'title' => 'No dragons here', + 'body' => array(array('value' => 'Again: No dragons here')), + 'type' => 'page')); + + $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); + // Update the search index. + $node_search_plugin->updateIndex(); + search_update_totals(); + + // Search the node to verify it appears in search results + $edit = array('keys' => 'dragons'); + $this->drupalPostForm('search/node', $edit, t('Search')); + $this->assertText($node->label()); + + // Get the node info from the search index tables. + $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", array(':word' => 'dragons')) + ->fetchField(); + $this->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index')); + + // Delete the node. + $node->delete(); + + // Check if the node info is gone from the search table. + $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", array(':word' => 'dragons')) + ->fetchField(); + $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index')); + + // Search again to verify the node doesn't appear anymore. + $this->drupalPostForm('search/node', $edit, t('Search')); + $this->assertNoText($node->label()); + } + +}