function SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeChange

Same name and namespace in other branches
  1. 8.9.x core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php \Drupal\Tests\search\Functional\SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeChange()
  2. 10 core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php \Drupal\Tests\search\Functional\SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeChange()
  3. 11.x 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 52

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->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->pageTextContains($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->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->pageTextContains($node->label());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.