You are here

public function SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeDeletion 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::testSearchIndexUpdateOnNodeDeletion()
  2. 10 core/modules/search/tests/src/Functional/SearchNodeUpdateAndDeletionTest.php \Drupal\Tests\search\Functional\SearchNodeUpdateAndDeletionTest::testSearchIndexUpdateOnNodeDeletion()

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

File

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

Class

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

Namespace

Drupal\Tests\search\Functional

Code

public function testSearchIndexUpdateOnNodeDeletion() {

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

  // Update the search index.
  $node_search_plugin
    ->updateIndex();

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

  // Get the node info from the search index tables.
  $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'));

  // Delete the node.
  $node
    ->delete();

  // Check if the node info is gone from the search table.
  $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'));

  // Search again to verify the node doesn't appear anymore.
  $this
    ->drupalPostForm('search/node', $edit, t('Search'));
  $this
    ->assertNoText($node
    ->label());
}