View source
<?php
namespace Drupal\Tests\search_api_saved_searches\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api_saved_searches\Entity\SavedSearch;
use Drupal\search_api_saved_searches\Entity\SavedSearchType;
use Drupal\Tests\search_api\Functional\ExampleContentTrait;
use Drupal\Tests\search_api\Kernel\TestTimeService;
use Drupal\user\Entity\User;
class NewResultsCheckTest extends KernelTestBase {
use ExampleContentTrait;
protected static $modules = [
'entity_test',
'options',
'search_api',
'search_api_db',
'search_api_saved_searches',
'search_api_test',
'search_api_test_db',
'system',
'user',
];
protected $index;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('search_api_task');
$this
->installEntitySchema('search_api_saved_search');
$this
->installEntitySchema('entity_test_mulrev_changed');
$this
->installSchema('search_api', [
'search_api_item',
]);
$this
->installSchema('search_api_saved_searches', [
'search_api_saved_searches_old_results',
]);
$this
->installConfig([
'search_api',
'search_api_test_db',
'search_api_saved_searches',
]);
$logger = new TestLogger('');
$this->container
->set('logger.factory', $logger);
$this->container
->set('logger.channel.search_api', $logger);
$this->container
->set('logger.channel.search_api_saved_searches', $logger);
User::create([
'uid' => 0,
'name' => '',
])
->save();
$this
->setUpExampleStructure();
$this
->insertExampleContent();
$this->index = Index::load('database_search_index');
$this->index
->indexItems();
}
public function testGetNewResults($date_field, array $expected_new_results = NULL, array $type_options = [], int $expected_result_count = NULL) {
$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);
$results = $query
->execute();
$this
->assertEquals(2, $results
->getResultCount());
$this
->assertEquals($this
->getItemIds([
4,
5,
]), array_keys($results
->getResultItems()));
$this
->addTestEntity(6, [
'name' => 'test 6',
'type' => 'article',
]);
$this->index
->indexItems();
$search = SavedSearch::create([
'type' => 'default',
'query' => $query,
]);
$search
->save();
$time
->advanceTime(10);
$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()));
}
}
public function getNewResultsDataProvider() {
return [
'id method' => [
NULL,
[
6,
7,
8,
10,
],
],
'date field method' => [
'created',
[
7,
10,
],
],
'max_results' => [
NULL,
[
6,
7,
],
[
'max_results' => 2,
],
4,
],
'date field with max_results' => [
'created',
[
7,
],
[
'max_results' => 1,
],
2,
],
'query_limit' => [
NULL,
NULL,
[
'query_limit' => 2,
],
],
'date field with query_limit' => [
'created',
[
7,
10,
],
[
'query_limit' => 2,
],
],
];
}
}