function NodeSearchTest::testNodeReindexDefaultRevision

Tests that previous revisions of a node are not re-indexed.

The idea is not to save URL aliases or execute certain procedures if the node being processed is not the default revision.

See also

\Drupal\node\Hook\NodeSearchHooks::nodeUpdate()

File

core/modules/node/tests/src/Kernel/NodeSearchTest.php, line 54

Class

NodeSearchTest
Tests node search integration.

Namespace

Drupal\Tests\node\Kernel

Code

public function testNodeReindexDefaultRevision() : void {
  $node = $this->createNode([
    'type' => 'article',
    'title' => 'Initial Title 1',
  ]);
  $initialRevisionId = $node->getRevisionId();
  $node->setTitle($this->randomMachineName());
  $node->setNewRevision();
  $node->save();
  // Set up the search configuration and the index.
  $plugin = \Drupal::service('plugin.manager.search')->createInstance('node_search');
  // Update the index.
  $plugin->updateIndex();
  $nodeStorage = \Drupal::service('entity_type.manager')->getStorage('node');
  $old_revision = $nodeStorage->loadRevision($initialRevisionId);
  $old_revision->save();
  // Check that updating a non-default revision did not trigger a reindex.
  $result = \Drupal::database()->select('search_dataset', 'sd')
    ->fields('sd', [
    'sid',
    'type',
    'reindex',
  ])
    ->condition('reindex', 0, '>')
    ->execute()
    ->fetchAll();
  $this->assertCount(0, $result);
}

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