View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Database\Database;
use Drupal\search\SearchIndexInterface;
use Drupal\Tests\BrowserTestBase;
class SearchNodeUpdateAndDeletionTest extends BrowserTestBase {
protected static $modules = [
'node',
'search',
];
protected $defaultTheme = 'stark';
public $testUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->testUser = $this
->drupalCreateUser([
'access content',
'search content',
]);
$this
->drupalLogin($this->testUser);
}
public function testSearchIndexUpdateOnNodeChange() {
$node = $this
->drupalCreateNode([
'title' => 'Someone who says Ni!',
'body' => [
[
'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_index = \Drupal::service('search.index');
assert($search_index instanceof SearchIndexInterface);
$edit = [
'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();
$edit = [
'keys' => 'shrubbery',
];
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText($node
->label());
}
public function testSearchIndexUpdateOnNodeDeletion() {
$node = $this
->drupalCreateNode([
'title' => 'No dragons here',
'body' => [
[
'value' => 'Again: No dragons here',
],
],
'type' => 'page',
]);
$node_search_plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$node_search_plugin
->updateIndex();
$edit = [
'keys' => 'dragons',
];
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText($node
->label());
$connection = Database::getConnection();
$search_index_dataset = $connection
->select('search_index', 'si')
->fields('si', [
'sid',
])
->condition('type', 'node_search')
->condition('word', 'dragons')
->execute()
->fetchField();
$this
->assertNotEqual($search_index_dataset, FALSE, t('Node info found on the search_index'));
$node
->delete();
$search_index_dataset = $connection
->select('search_index', 'si')
->fields('si', [
'sid',
])
->condition('type', 'node_search')
->condition('word', 'dragons')
->execute()
->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());
}
}