You are here

public function NewResultsCheckTest::testGetNewResults in Search API Saved Searches 8

Tests whether new results are correctly retrieved.

@dataProvider getNewResultsDataProvider

@covers ::getNewResults

Parameters

string|null $date_field: The date field which the saved search type should be configured to use.

int[]|null $expected_new_results: (optional) The expected new results' entity IDs, or NULL if the check is expected to not return any new results.

array $type_options: (optional) Further options to set on the saved search type.

int|null $expected_result_count: (optional) The expected number of new results, if different from the count of $expected_new_results.

File

tests/src/Kernel/NewResultsCheckTest.php, line 102

Class

NewResultsCheckTest
Tests the functionality of "new results" checks.

Namespace

Drupal\Tests\search_api_saved_searches\Kernel

Code

public function testGetNewResults($date_field, array $expected_new_results = NULL, array $type_options = [], int $expected_result_count = NULL) {

  // Use a test time service to easily manipulate the "created" date.
  $time = new TestTimeService();
  $this->container
    ->set('datetime.time', $time);
  if ($date_field || $type_options) {
    $type = SavedSearchType::load('default');
    $options = $type_options + $type
      ->getOptions();
    if ($date_field) {
      $options['date_field']['database_search_index'] = $date_field;
    }
    $type
      ->set('options', $options);
    $type
      ->save();
  }
  $query = $this->index
    ->query()
    ->addCondition('type', 'article')
    ->sort('id', QueryInterface::SORT_ASC);

  // Execute query to simulate normal workflow (and test for regressions of
  // #2955617/#2955617).
  $results = $query
    ->execute();
  $this
    ->assertEquals(2, $results
    ->getResultCount());
  $this
    ->assertEquals($this
    ->getItemIds([
    4,
    5,
  ]), array_keys($results
    ->getResultItems()));

  // An item added between search execution and saving the search shouldn't
  // matter for the "Determine by result IDs" approach (since the query
  // shouldn't be re-executed).
  $this
    ->addTestEntity(6, [
    'name' => 'test 6',
    'type' => 'article',
  ]);
  $this->index
    ->indexItems();
  $search = SavedSearch::create([
    'type' => 'default',
    'query' => $query,
  ]);
  $search
    ->save();
  $time
    ->advanceTime(10);

  // Add some more test entities, one of them with the wrong type to be
  // matched (9) and one with an old "created" timestamp to confuse the "date
  // field" detection method (8).
  // @todo Remove explicit "created" values once #2809515 gets fixed.
  $this
    ->addTestEntity(7, [
    'name' => 'test 7',
    'type' => 'article',
    'created' => $time
      ->getRequestTime(),
  ]);
  $time
    ->advanceTime(10);
  $this
    ->addTestEntity(8, [
    'name' => 'test 8',
    'type' => 'article',
    'created' => $time
      ->getRequestTime() - 86400,
  ]);
  $time
    ->advanceTime(10);
  $this
    ->addTestEntity(9, [
    'name' => 'test 9',
    'type' => 'item',
    'created' => $time
      ->getRequestTime(),
  ]);
  $time
    ->advanceTime(10);
  $this
    ->addTestEntity(10, [
    'name' => 'test 10',
    'type' => 'article',
    'created' => $time
      ->getRequestTime(),
  ]);
  $this->index
    ->indexItems();
  $search = SavedSearch::load($search
    ->id());
  $results = $this->container
    ->get('search_api_saved_searches.new_results_check')
    ->getNewResults($search);
  if ($expected_new_results === NULL) {
    $this
      ->assertNull($results);
  }
  else {
    $this
      ->assertNotNull($results);
    $this
      ->assertEquals($expected_result_count ?? count($expected_new_results), $results
      ->getResultCount());
    $this
      ->assertEquals($this
      ->getItemIds($expected_new_results), array_keys($results
      ->getResultItems()));
  }
}