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