You are here

protected function IntegrationTest::checkContentEntityTracking in Search API 8

Tests whether the tracking information is properly maintained.

Will especially test the bundle option of the content entity datasource.

1 call to IntegrationTest::checkContentEntityTracking()
IntegrationTest::testFramework in tests/src/Functional/IntegrationTest.php
Tests various operations via the Search API's admin UI.

File

tests/src/Functional/IntegrationTest.php, line 527

Class

IntegrationTest
Tests the overall functionality of the Search API framework and admin UI.

Namespace

Drupal\Tests\search_api\Functional

Code

protected function checkContentEntityTracking() {

  // Initially there should be no tracked items, because there are no nodes.
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(0, $tracked_items, 'No items are tracked yet.');

  // Add two articles and two pages (one of them "invisible" to Search API).
  $article1 = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $this
    ->drupalCreateNode([
    'type' => 'page',
  ]);
  $page2 = Node::create([
    'body' => [
      [
        'value' => $this
          ->randomMachineName(32),
        'format' => filter_default_format(),
      ],
    ],
    'title' => $this
      ->randomMachineName(8),
    'type' => 'page',
    'uid' => \Drupal::currentUser()
      ->id(),
  ]);
  $page2->search_api_skip_tracking = TRUE;
  $page2
    ->save();

  // The 3 new nodes without "search_api_skip_tracking" property set should
  // have been added to the tracking table immediately.
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(3, $tracked_items, 'Three items are tracked.');
  $this
    ->getCalledMethods('backend');
  $page2
    ->delete();
  $methods = $this
    ->getCalledMethods('backend');
  $this
    ->assertEquals([], $methods, 'Tracking of a delete operation could successfully be prevented.');

  // Test disabling the index.
  $settings_path = $this
    ->getIndexPath('edit');
  $this
    ->drupalGet($settings_path);
  $edit = [
    'status' => FALSE,
    'datasource_configs[entity:node][bundles][default]' => 0,
    'datasource_configs[entity:node][bundles][selected][article]' => FALSE,
    'datasource_configs[entity:node][bundles][selected][page]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(0, $tracked_items, 'No items are tracked.');

  // Test re-enabling the index.
  $this
    ->drupalGet($settings_path);
  $edit = [
    'status' => TRUE,
    'datasource_configs[entity:node][bundles][default]' => 0,
    'datasource_configs[entity:node][bundles][selected][article]' => TRUE,
    'datasource_configs[entity:node][bundles][selected][page]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(3, $tracked_items, 'Three items are tracked.');

  // Uncheck "default" and don't select any bundles. This should remove all
  // items from the tracking table.
  $edit = [
    'status' => TRUE,
    'datasource_configs[entity:node][bundles][default]' => 0,
    'datasource_configs[entity:node][bundles][selected][article]' => FALSE,
    'datasource_configs[entity:node][bundles][selected][page]' => FALSE,
  ];
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(0, $tracked_items, 'No items are tracked.');

  // Leave "default" unchecked and select the "article" bundle. This should
  // re-add the two articles to the tracking table.
  $edit = [
    'status' => TRUE,
    'datasource_configs[entity:node][bundles][default]' => 0,
    'datasource_configs[entity:node][bundles][selected][article]' => TRUE,
    'datasource_configs[entity:node][bundles][selected][page]' => FALSE,
  ];
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(2, $tracked_items, 'Two items are tracked.');

  // Leave "default" unchecked and select only the "page" bundle. This should
  // result in only the page being present in the tracking table.
  $edit = [
    'status' => TRUE,
    'datasource_configs[entity:node][bundles][default]' => 0,
    'datasource_configs[entity:node][bundles][selected][article]' => FALSE,
    'datasource_configs[entity:node][bundles][selected][page]' => TRUE,
  ];
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(1, $tracked_items, 'One item is tracked.');

  // Check "default" again and select the "article" bundle. This shouldn't
  // change the tracking table, which should still only contain the page.
  $edit = [
    'status' => TRUE,
    'datasource_configs[entity:node][bundles][default]' => 1,
    'datasource_configs[entity:node][bundles][selected][article]' => TRUE,
    'datasource_configs[entity:node][bundles][selected][page]' => FALSE,
  ];
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(1, $tracked_items, 'One item is tracked.');

  // Leave "default" checked but now select only the "page" bundle. This
  // should result in only the articles being tracked.
  $edit = [
    'status' => TRUE,
    'datasource_configs[entity:node][bundles][default]' => 1,
    'datasource_configs[entity:node][bundles][selected][article]' => FALSE,
    'datasource_configs[entity:node][bundles][selected][page]' => TRUE,
  ];
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(2, $tracked_items, 'Two items are tracked.');

  // Index items, then check whether updating an article is handled correctly.
  $this
    ->triggerPostRequestIndexing();
  $this
    ->getCalledMethods('backend');
  $article1
    ->save();
  $methods = $this
    ->getCalledMethods('backend');
  $this
    ->assertEquals([], $methods, 'No items were indexed right away (before end of page request).');
  $this
    ->triggerPostRequestIndexing();
  $methods = $this
    ->getCalledMethods('backend');
  $this
    ->assertEquals([
    'indexItems',
  ], $methods, 'Update successfully tracked.');
  $article1->search_api_skip_tracking = TRUE;
  $article1
    ->save();
  $methods = $this
    ->getCalledMethods('backend');
  $this
    ->assertEquals([], $methods, 'Tracking of entity update successfully prevented.');
  unset($article1->search_api_skip_tracking);

  // Delete an article. That should remove it from the item table.
  $article1
    ->delete();
  $tracked_items = $this
    ->countTrackedItems();
  $this
    ->assertEquals(1, $tracked_items, 'One item is tracked.');
}