annotate core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php @ 4:a9cd425dd02b

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