diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php index e69de29..4ac1d24 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchNodeUpdateAndDeletionTest.php @@ -0,0 +1,114 @@ + 'Search and 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 is properly updated when a node changes. + */ + function testSearchInfoIsUpdatedOnNodeChange() { + // 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 a node is removed from the search index when it's deleted. + */ + function testNodeInfoIsRemovedOnDeletion() { + // 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. + if (db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", array(':word' => 'dragons'))->fetchField() === FALSE) { + $this->fail(t('No node info found on the search_index')); + } + // If there's no info on the search index, makes no sense to test if it's + // removed after removing the node, as that could lead to a false positive. + else { + // 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()); + } + + } + +}