You are here

protected function SearchApiWebTest::checkIndexingOrder in Search API 7

Tests whether items are indexed in the right order.

The indexing order should always be that new items are indexed before changed ones, and only then the changed items in the order of their change.

This method also assures that this behavior is even observed when indexing temporarily fails.

See also

https://drupal.org/node/2115127

1 call to SearchApiWebTest::checkIndexingOrder()
SearchApiWebTest::testFramework in ./search_api.test
Tests correct admin UI, indexing and search behavior.

File

./search_api.test, line 516
Contains the SearchApiWebTest and the SearchApiUnitTest classes.

Class

SearchApiWebTest
Class for testing Search API functionality via the UI.

Code

protected function checkIndexingOrder() {

  // Set cron batch size to 1 so not all items will get indexed right away.
  // This also ensures that later, when indexing of a single item will be
  // rejected by using the "search_api_test_indexing_break" variable, this
  // will have the effect of rejecting "all" items of a batch (since that
  // batch only consists of a single item).
  $values = array(
    'options[cron_limit]' => 1,
  );
  $this
    ->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $values, t('Save settings'));
  $this
    ->assertText(t('The search index was successfully edited.'));

  // Manually clear the server's item storage – that way, the items will still
  // count as  indexed for the Search API, but won't be returned in searches.
  // We do this so we have finer-grained control over the order in which items
  // are indexed.
  $this
    ->server()
    ->deleteItems();
  $results = $this
    ->doSearch();
  $this
    ->assertEqual($results['result count'], 0, 'Indexed items were successfully deleted from the server.');
  $this
    ->assertEqual(array_keys($results['results']), array(), 'Indexed items were successfully deleted from the server.');

  // Now insert some new items, and mark others as changed. Make sure that
  // each action has a unique timestamp, so the order will be correct.
  $this
    ->drupalGet('search_api_test/touch/8');
  $this
    ->insertItems(1);

  // item 11
  sleep(1);
  $this
    ->drupalGet('search_api_test/touch/2');
  $this
    ->insertItems(1);

  // item 12
  sleep(1);
  $this
    ->drupalGet('search_api_test/touch/5');
  $this
    ->insertItems(1);

  // item 13
  sleep(1);
  $this
    ->drupalGet('search_api_test/touch/8');
  $this
    ->insertItems(1);

  // item 14
  // Check whether the status display is right.
  $this
    ->checkIndexStatus(7, 14, FALSE, 0);

  // Indexing order should now be: 11, 12, 13, 14, 8, 2, 4. Let's try it out!
  // First manually index one item, and see if it's 11.
  $values = array(
    'limit' => 1,
  );
  $this
    ->drupalPost(NULL, $values, t('Index now'));
  $this
    ->assertText(t('Successfully indexed @count item.', array(
    '@count' => 1,
  )));
  $this
    ->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), "Index errors warning isn't displayed.");
  $this
    ->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
  $this
    ->checkIndexStatus(8, 14, FALSE, 1);
  $results = $this
    ->doSearch();
  $this
    ->assertEqual($results['result count'], 1, 'Indexing order test 1: correct result count.');
  $this
    ->assertEqual(array_keys($results['results']), array(
    11,
  ), 'Indexing order test 1: correct results.');

  // Now index with a cron run, but stop at item 8.
  variable_set('search_api_test_indexing_break', 8);
  $this
    ->cronRun();

  // Now just the four new items should have been indexed.
  $results = $this
    ->doSearch();
  $this
    ->assertEqual($results['result count'], 4, 'Indexing order test 2: correct result count.');
  $this
    ->assertEqual(array_keys($results['results']), array(
    11,
    12,
    13,
    14,
  ), 'Indexing order test 2: correct results.');

  // This time stop at item 5 (should be the last one).
  variable_set('search_api_test_indexing_break', 5);
  $this
    ->cronRun();

  // Now all new and changed items should have been indexed, except item 5.
  $results = $this
    ->doSearch();
  $this
    ->assertEqual($results['result count'], 6, 'Indexing order test 3: correct result count.');
  $this
    ->assertEqual(array_keys($results['results']), array(
    2,
    8,
    11,
    12,
    13,
    14,
  ), 'Indexing order test 3: correct results.');

  // Index the remaining item.
  variable_set('search_api_test_indexing_break', 0);
  $this
    ->cronRun();

  // Now all new and changed items should have been indexed.
  $results = $this
    ->doSearch();
  $this
    ->assertEqual($results['result count'], 7, 'Indexing order test 4: correct result count.');
  $this
    ->assertEqual(array_keys($results['results']), array(
    2,
    5,
    8,
    11,
    12,
    13,
    14,
  ), 'Indexing order test 4: correct results.');
}