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_saved_searches\Entity\SavedSearch;
use Drupal\search_api_saved_searches\Entity\SavedSearchType;
use Drupal\search_api_saved_searches\Service\NewResultsCheck;
use Drupal\search_api_saved_searches\SavedSearchInterface;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;
class SavedSearchCrudTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'options',
'search_api',
'search_api_saved_searches',
'system',
'user',
];
protected $newResultsCheck;
protected $newResultsCheckMethodCalls;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('search_api_saved_search');
$this
->installEntitySchema('search_api_task');
$this
->installConfig('search_api_saved_searches');
$this
->installSchema('system', [
'key_value_expire',
'sequences',
]);
$this
->installSchema('search_api_saved_searches', 'search_api_saved_searches_old_results');
$this->newResultsCheck = $this
->createMock(NewResultsCheck::class);
$method_log = (object) [];
$this->newResultsCheck
->method('getNewResults')
->willReturnCallback(function (SavedSearchInterface $search) use ($method_log) {
$method_log->getNewResults[] = [
$search
->id(),
];
});
$this->newResultsCheck
->method('saveKnownResults')
->willReturnCallback(function (SavedSearchInterface $search) use ($method_log) {
$method_log->saveKnownResults[] = [
$search
->id(),
];
});
$this->newResultsCheckMethodCalls = $method_log;
$this->container
->set('search_api_saved_searches.new_results_check', $this->newResultsCheck);
}
public function testPostCreate($set_label, $keys, $expected_label) {
$query = Index::create()
->query();
$query
->keys($keys);
$values = [
'type' => 'default',
'query' => $query,
];
if ($set_label !== NULL) {
$values['label'] = $set_label;
}
$search = SavedSearch::create($values);
$this
->assertEquals($expected_label, $search
->label());
}
public function postCreateDataProvider() {
return [
'existing label' => [
'Foobar',
'lorem',
'Foobar',
],
'with keys' => [
NULL,
'lorem',
'lorem',
],
'without keys' => [
NULL,
NULL,
'Saved search',
],
'with complex keys' => [
NULL,
[
'#conjunction' => 'AND',
'foo',
'bar',
],
'Saved search',
],
];
}
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);
$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);
}
public function preSaveDataProvider() {
return [
'with notifications, index_id set' => [
10,
1234567890,
'foobar',
1234567890 + 10,
'foobar',
],
'with instant notifications' => [
0,
1234567890,
'foobar',
1234567890,
'foobar',
],
'without notifications, index_id not set' => [
-1,
1234567890,
NULL,
NULL,
'test',
],
];
}
public function testPostSave($set_date_field, $set_query, $expect_check) {
$index_id = 'test';
if ($set_date_field) {
$options['date_field'][$index_id] = 'created';
SavedSearchType::load('default')
->set('options', $options)
->save();
}
$values = [
'type' => 'default',
];
if ($set_query) {
$query = Index::create([
'id' => $index_id,
])
->query();
$values['query'] = $query;
}
$search = SavedSearch::create($values);
$search
->save();
$method_log = $this->newResultsCheckMethodCalls;
if ($expect_check) {
$this
->assertEquals([
[
$search
->id(),
],
], $method_log->saveKnownResults);
$this
->assertObjectNotHasAttribute('getNewResults', $method_log);
}
else {
$this
->assertObjectNotHasAttribute('saveKnownResults', $method_log);
$this
->assertObjectNotHasAttribute('getNewResults', $method_log);
}
unset($method_log->getNewResults);
$search
->save();
$this
->assertObjectNotHasAttribute('getNewResults', $method_log);
}
public function postSaveDataProvider() {
return [
'with date field' => [
TRUE,
TRUE,
FALSE,
],
'without query' => [
FALSE,
FALSE,
FALSE,
],
'expect check' => [
FALSE,
TRUE,
TRUE,
],
];
}
public function testPostDelete() {
$search = SavedSearch::create([
'type' => 'default',
]);
$search
->save();
\Drupal::database()
->insert('search_api_saved_searches_old_results')
->fields([
'search_id' => $search
->id(),
'search_type' => 'default',
'item_id' => '1',
])
->execute();
$count = \Drupal::database()
->select('search_api_saved_searches_old_results', 't')
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(1, $count);
$search
->delete();
$search = SavedSearch::load($search
->id());
$this
->assertNull($search);
$count = \Drupal::database()
->select('search_api_saved_searches_old_results', 't')
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(0, $count);
}
public function testIndexDelete() {
$index_id = 'test';
$index = Index::create([
'id' => $index_id,
]);
$index
->enforceIsNew(FALSE);
$search = SavedSearch::create([
'type' => 'default',
'index_id' => $index_id,
]);
$search
->save();
$search = SavedSearch::load($search
->id());
$this
->assertNotNull($search);
$index
->delete();
$search = SavedSearch::load($search
->id());
$this
->assertNull($search);
}
public function testDefaultOwner() {
$anonymous = User::create([
'uid' => 0,
'name' => '',
]);
$anonymous
->save();
$values = [
'type' => 'default',
];
$search = SavedSearch::create($values);
$owner = $search
->getOwner();
$this
->assertNotNull($owner);
$this
->assertEquals(0, $owner
->id());
$this
->assertEquals(0, $search
->getOwnerId());
$user = $this
->createUser();
$uid = $user
->id();
$this
->setCurrentUser($user);
$search = SavedSearch::create($values);
$owner = $search
->getOwner();
$this
->assertNotNull($owner);
$this
->assertEquals($uid, $owner
->id());
$this
->assertEquals($uid, $search
->getOwnerId());
}
}