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