View source
<?php
namespace Drupal\search\Tests;
class SearchNodeUpdateAndDeletionTest extends SearchTestBase {
public static $modules = array();
public $testUser;
protected function setUp() {
parent::setUp();
$this->testUser = $this
->drupalCreateUser(array(
'access content',
'search content',
));
$this
->drupalLogin($this->testUser);
}
function testSearchIndexUpdateOnNodeChange() {
$node = $this
->drupalCreateNode(array(
'title' => 'Someone who says Ni!',
'body' => array(
array(
'value' => "We are the knights who say Ni!",
),
),
'type' => 'page',
));
$node_search_plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$node_search_plugin
->updateIndex();
search_update_totals();
$edit = array(
'keys' => 'knights',
);
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText($node
->label());
$node->body->value = "We want a shrubbery!";
$node
->save();
$node_search_plugin
->updateIndex();
search_update_totals();
$edit = array(
'keys' => 'shrubbery',
);
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText($node
->label());
}
function testSearchIndexUpdateOnNodeDeletion() {
$node = $this
->drupalCreateNode(array(
'title' => 'No dragons here',
'body' => array(
array(
'value' => 'Again: No dragons here',
),
),
'type' => 'page',
));
$node_search_plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$node_search_plugin
->updateIndex();
search_update_totals();
$edit = array(
'keys' => 'dragons',
);
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText($node
->label());
$search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", array(
':word' => 'dragons',
))
->fetchField();
$this
->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index'));
$node
->delete();
$search_index_dataset = db_query("SELECT sid FROM {search_index} WHERE type = 'node_search' AND word = :word", array(
':word' => 'dragons',
))
->fetchField();
$this
->assertFalse($search_index_dataset, t('Node info successfully removed from search_index'));
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertNoText($node
->label());
}
}