You are here

public function SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeChange in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php \Drupal\Tests\search\Functional\SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeChange()

Tests that the search index info is properly updated when a node changes.

File

core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php, line 49

Class

SearchNodeUpdateAndDeletionTest
Tests search index is updated properly when nodes are removed or updated.

Namespace

Drupal\Tests\search\Functional

Code

public function testSearchIndexUpdateOnNodeChange() {

  // Create a node.
  $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');

  // Update the search index.
  $node_search_plugin
    ->updateIndex();
  $search_index = \Drupal::service('search.index');
  assert($search_index instanceof SearchIndexInterface);

  // Search the node to verify it appears in search results
  $edit = [
    'keys' => 'knights',
  ];
  $this
    ->drupalPostForm('search/node', $edit, t('Search'));
  $this
    ->assertText($node
    ->label());

  // Update the node
  $node->body->value = "We want a shrubbery!";
  $node
    ->save();

  // Run indexer again
  $node_search_plugin
    ->updateIndex();

  // Search again to verify the new text appears in test results.
  $edit = [
    'keys' => 'shrubbery',
  ];
  $this
    ->drupalPostForm('search/node', $edit, t('Search'));
  $this
    ->assertText($node
    ->label());
}