Chris@17: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@17: Chris@17: // Create a test user and log in. Chris@17: $this->testUser = $this->drupalCreateUser(['access content', 'search content']); Chris@17: $this->drupalLogin($this->testUser); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests that the search index info is properly updated when a node changes. Chris@17: */ Chris@17: public function testSearchIndexUpdateOnNodeChange() { Chris@17: // Create a node. Chris@17: $node = $this->drupalCreateNode([ Chris@17: 'title' => 'Someone who says Ni!', Chris@17: 'body' => [['value' => "We are the knights who say Ni!"]], Chris@17: 'type' => 'page', Chris@17: ]); Chris@17: Chris@17: $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); Chris@17: // Update the search index. Chris@17: $node_search_plugin->updateIndex(); Chris@17: search_update_totals(); Chris@17: Chris@17: // Search the node to verify it appears in search results Chris@17: $edit = ['keys' => 'knights']; Chris@17: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@17: $this->assertText($node->label()); Chris@17: Chris@17: // Update the node Chris@17: $node->body->value = "We want a shrubbery!"; Chris@17: $node->save(); Chris@17: Chris@17: // Run indexer again Chris@17: $node_search_plugin->updateIndex(); Chris@17: search_update_totals(); Chris@17: Chris@17: // Search again to verify the new text appears in test results. Chris@17: $edit = ['keys' => 'shrubbery']; Chris@17: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@17: $this->assertText($node->label()); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests that the search index info is updated when a node is deleted. Chris@17: */ Chris@17: public function testSearchIndexUpdateOnNodeDeletion() { Chris@17: // Create a node. Chris@17: $node = $this->drupalCreateNode([ Chris@17: 'title' => 'No dragons here', Chris@17: 'body' => [['value' => 'Again: No dragons here']], Chris@17: 'type' => 'page', Chris@17: ]); Chris@17: Chris@17: $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); Chris@17: // Update the search index. Chris@17: $node_search_plugin->updateIndex(); Chris@17: search_update_totals(); Chris@17: Chris@17: // Search the node to verify it appears in search results Chris@17: $edit = ['keys' => 'dragons']; Chris@17: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@17: $this->assertText($node->label()); Chris@17: Chris@17: // Get the node info from the search index tables. Chris@17: $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons']) Chris@17: ->fetchField(); Chris@17: $this->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index')); Chris@17: Chris@17: // Delete the node. Chris@17: $node->delete(); Chris@17: Chris@17: // Check if the node info is gone from the search table. Chris@17: $search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", [':word' => 'dragons']) Chris@17: ->fetchField(); Chris@17: $this->assertFalse($search_index_dataset, t('Node info successfully removed from search_index')); Chris@17: Chris@17: // Search again to verify the node doesn't appear anymore. Chris@17: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@17: $this->assertNoText($node->label()); Chris@17: } Chris@17: Chris@17: }