You are here

public function SavedSearchCrudTest::testPreSave in Search API Saved Searches 8

Tests the pre-save hook for new saved searches.

@covers ::preCreate @covers ::preSave

@dataProvider preSaveDataProvider

Parameters

int $notify_interval: The notification interval to set.

int $last_executed: The "Last executed" timestamp to set.

string|null $index_id: The index ID to set.

int|null $expected_next_execution: The expected "next_execution" field value for the saved search.

string $expected_index_id: The expected "index_id" field value for the saved search.

File

tests/src/Kernel/SavedSearchCrudTest.php, line 164

Class

SavedSearchCrudTest
Tests CRUD functionality for saved searches.

Namespace

Drupal\Tests\search_api_saved_searches\Kernel

Code

public function testPreSave($notify_interval, $last_executed, $index_id, $expected_next_execution, $expected_index_id) {
  $query = Index::create([
    'id' => 'test',
  ])
    ->query();
  $values = [
    'type' => 'default',
    'query' => $query,
    'notify_interval' => $notify_interval,
    'last_executed' => $last_executed,
  ];
  if ($index_id !== NULL) {
    $values['index_id'] = $index_id;
  }
  $search = SavedSearch::create($values);
  $search
    ->save();
  $this
    ->assertNotNull($search
    ->id());
  $this
    ->assertEquals($expected_next_execution, $search
    ->get('next_execution')->value);
  $this
    ->assertEquals($expected_index_id, $search
    ->get('index_id')->value);
  $search = SavedSearch::load($search
    ->id());
  $this
    ->assertEquals($expected_next_execution, $search
    ->get('next_execution')->value);
  $this
    ->assertEquals($expected_index_id, $search
    ->get('index_id')->value);

  // Test that saving again leads to expected results.
  $last_executed += 10;
  if ($expected_next_execution !== NULL) {
    $expected_next_execution += 10;
  }
  $search
    ->set('last_executed', $last_executed);
  $search
    ->save();
  $this
    ->assertEquals($expected_next_execution, $search
    ->get('next_execution')->value);
}