annotate core/modules/search/src/Tests/SearchNodeUpdateAndDeletionTest.php @ 1:1a348b17ec81

Logo and header background
author Chris Cannam
date Thu, 30 Nov 2017 14:56:35 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\search\Tests;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Tests search index is updated properly when nodes are removed or updated.
Chris@0 7 *
Chris@0 8 * @group search
Chris@0 9 */
Chris@0 10 class SearchNodeUpdateAndDeletionTest extends SearchTestBase {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Modules to enable.
Chris@0 14 *
Chris@0 15 * @var array
Chris@0 16 */
Chris@0 17 public static $modules = [];
Chris@0 18
Chris@0 19 /**
Chris@0 20 * A user with permission to access and search content.
Chris@0 21 *
Chris@0 22 * @var \Drupal\user\UserInterface
Chris@0 23 */
Chris@0 24 public $testUser;
Chris@0 25
Chris@0 26 protected function setUp() {
Chris@0 27 parent::setUp();
Chris@0 28
Chris@0 29 // Create a test user and log in.
Chris@0 30 $this->testUser = $this->drupalCreateUser(['access content', 'search content']);
Chris@0 31 $this->drupalLogin($this->testUser);
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Tests that the search index info is properly updated when a node changes.
Chris@0 36 */
Chris@0 37 public function testSearchIndexUpdateOnNodeChange() {
Chris@0 38 // Create a node.
Chris@0 39 $node = $this->drupalCreateNode([
Chris@0 40 'title' => 'Someone who says Ni!',
Chris@0 41 'body' => [['value' => "We are the knights who say Ni!"]],
Chris@0 42 'type' => 'page',
Chris@0 43 ]);
Chris@0 44
Chris@0 45 $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
Chris@0 46 // Update the search index.
Chris@0 47 $node_search_plugin->updateIndex();
Chris@0 48 search_update_totals();
Chris@0 49
Chris@0 50 // Search the node to verify it appears in search results
Chris@0 51 $edit = ['keys' => 'knights'];
Chris@0 52 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 53 $this->assertText($node->label());
Chris@0 54
Chris@0 55 // Update the node
Chris@0 56 $node->body->value = "We want a shrubbery!";
Chris@0 57 $node->save();
Chris@0 58
Chris@0 59 // Run indexer again
Chris@0 60 $node_search_plugin->updateIndex();
Chris@0 61 search_update_totals();
Chris@0 62
Chris@0 63 // Search again to verify the new text appears in test results.
Chris@0 64 $edit = ['keys' => 'shrubbery'];
Chris@0 65 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 66 $this->assertText($node->label());
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Tests that the search index info is updated when a node is deleted.
Chris@0 71 */
Chris@0 72 public function testSearchIndexUpdateOnNodeDeletion() {
Chris@0 73 // Create a node.
Chris@0 74 $node = $this->drupalCreateNode([
Chris@0 75 'title' => 'No dragons here',
Chris@0 76 'body' => [['value' => 'Again: No dragons here']],
Chris@0 77 'type' => 'page',
Chris@0 78 ]);
Chris@0 79
Chris@0 80 $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
Chris@0 81 // Update the search index.
Chris@0 82 $node_search_plugin->updateIndex();
Chris@0 83 search_update_totals();
Chris@0 84
Chris@0 85 // Search the node to verify it appears in search results
Chris@0 86 $edit = ['keys' => 'dragons'];
Chris@0 87 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 88 $this->assertText($node->label());
Chris@0 89
Chris@0 90 // Get the node info from the search index tables.
Chris@0 91 $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons'])
Chris@0 92 ->fetchField();
Chris@0 93 $this->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index'));
Chris@0 94
Chris@0 95 // Delete the node.
Chris@0 96 $node->delete();
Chris@0 97
Chris@0 98 // Check if the node info is gone from the search table.
Chris@0 99 $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons'])
Chris@0 100 ->fetchField();
Chris@0 101 $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index'));
Chris@0 102
Chris@0 103 // Search again to verify the node doesn't appear anymore.
Chris@0 104 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 105 $this->assertNoText($node->label());
Chris@0 106 }
Chris@0 107
Chris@0 108 }