annotate core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php @ 19:fa3358dc1485 tip

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