diff --git a/core/modules/node/lib/Drupal/node/Entity/Node.php b/core/modules/node/lib/Drupal/node/Entity/Node.php index de94ae8b3b4cf4b8dcc1ee6aba628c8b7c80bec4..21887caa48efc1b455bfdee7e04139f9a65bd9fa 100644 --- a/core/modules/node/lib/Drupal/node/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Entity/Node.php @@ -125,6 +125,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()); + } } /** @@ -133,9 +139,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 c1ccf4e96fd5695a0e341db2a6856a660ce965de..0ad789d4f48e039e6beed699930bc6578297d638 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -2122,15 +2122,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(). */ 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 0000000000000000000000000000000000000000..b03b1c83c4130249100a38fe409c8545ed81dc8f --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php @@ -0,0 +1,111 @@ +<?php +/** + * @file + * Definition of Drupal\search\Tests\SearchNodeUpdateAndDeletionTest. + */ + +namespace Drupal\search\Tests; + +/** + * Tests search index info is updated properly on node updates / deletions. + */ +class SearchNodeUpdateAndDeletionTest extends SearchTestBase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array(); + + public $test_user; + + public static function getInfo() { + return array( + 'name' => '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()); + } + +}