Issue #2098015 by slv_: Fixed Node delete is not correctly removing search information; node update is using a hook instead of method.
parent
ba69ad0a3c
commit
00780f28dd
|
@ -125,6 +125,12 @@ class Node extends ContentEntityBase implements NodeInterface {
|
|||
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 @@ class Node extends ContentEntityBase implements NodeInterface {
|
|||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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().
|
||||
*/
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue