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\user\Entity\User;
class UserCrudReactionTest extends KernelTestBase {
protected static $modules = [
'options',
'search_api',
'search_api_saved_searches',
'system',
'user',
];
protected $testUser;
protected $savedSearches = [];
protected function setUp() {
parent::setUp();
$this
->installConfig('search_api_saved_searches');
$this
->installEntitySchema('user');
$this
->installEntitySchema('search_api_saved_search');
$this
->installEntitySchema('search_api_task');
$this
->installSchema('system', [
'key_value_expire',
'sequences',
]);
$this
->installSchema('user', [
'users_data',
]);
$this
->installSchema('search_api_saved_searches', 'search_api_saved_searches_old_results');
$this->testUser = User::create([
'uid' => 5,
'name' => 'test',
'status' => TRUE,
'mail' => 'test@example.com',
]);
$this->testUser
->save();
$this->savedSearches[] = SavedSearch::create([
'type' => 'default',
'uid' => $this->testUser
->id(),
'status' => TRUE,
'notify_interval' => 3600 * 24,
'mail' => $this->testUser
->getEmail(),
]);
$this->savedSearches[] = SavedSearch::create([
'type' => 'default',
'uid' => 0,
'status' => TRUE,
'notify_interval' => 3600 * 24,
'mail' => 'foo@example.com',
]);
$this->savedSearches[] = SavedSearch::create([
'type' => 'default',
'uid' => 0,
'status' => TRUE,
'notify_interval' => 3600 * 24,
'mail' => 'foo@example.com',
]);
$this->savedSearches[] = SavedSearch::create([
'type' => 'default',
'uid' => 0,
'status' => TRUE,
'notify_interval' => 3600 * 24,
'mail' => 'bar@example.com',
]);
foreach ($this->savedSearches as $search) {
$search
->save();
}
}
public function testUserInsert() {
$account = User::create([
'name' => 'foo',
'status' => TRUE,
'mail' => 'foo@example.com',
]);
$account
->save();
$this
->reloadSavedSearches();
$this
->assertEquals($account
->id(), $this->savedSearches[1]
->getOwnerId());
$this
->assertEquals($account
->id(), $this->savedSearches[2]
->getOwnerId());
$this
->assertEquals(0, $this->savedSearches[3]
->getOwnerId());
User::create([
'name' => 'bar',
'status' => FALSE,
'mail' => 'bar@example.com',
])
->save();
$this
->reloadSavedSearches();
$this
->assertEquals($account
->id(), $this->savedSearches[1]
->getOwnerId());
$this
->assertEquals($account
->id(), $this->savedSearches[2]
->getOwnerId());
$this
->assertEquals(0, $this->savedSearches[3]
->getOwnerId());
}
public function testUserActivate() {
$account = User::create([
'name' => 'foo',
'status' => FALSE,
'mail' => 'foo@example.com',
]);
$account
->save();
$this
->reloadSavedSearches();
$this
->assertEquals(0, $this->savedSearches[1]
->getOwnerId());
$this
->assertEquals(0, $this->savedSearches[2]
->getOwnerId());
$this
->assertEquals(0, $this->savedSearches[3]
->getOwnerId());
$account
->activate()
->save();
$this
->reloadSavedSearches();
$this
->assertEquals($account
->id(), $this->savedSearches[1]
->getOwnerId());
$this
->assertEquals($account
->id(), $this->savedSearches[2]
->getOwnerId());
$this
->assertEquals(0, $this->savedSearches[3]
->getOwnerId());
}
public function testUserDeactivate() {
$this->testUser
->block()
->save();
$this
->reloadSavedSearches();
$search = array_shift($this->savedSearches);
$this
->assertEquals(-1, $search
->get('notify_interval')->value);
foreach ($this->savedSearches as $search) {
$this
->assertEquals(3600 * 24, $search
->get('notify_interval')->value);
}
}
public function testUserDelete() {
$this->testUser
->delete();
$this
->reloadSavedSearches();
$search = array_shift($this->savedSearches);
$this
->assertEmpty($search);
$this
->reloadSavedSearches();
foreach ($this->savedSearches as $search) {
$this
->assertNotEmpty($search);
}
}
public function testIndexDelete() {
$this
->installConfig([
'search_api',
]);
$index = Index::create([
'id' => 'test',
]);
$index
->save();
$this->savedSearches[0]
->set('index_id', 'test')
->save();
$index
->delete();
$this
->reloadSavedSearches();
$this
->assertEmpty($this->savedSearches[0]);
$this
->assertNotEmpty($this->savedSearches[1]);
}
public function testUserMailChange() {
SavedSearchType::create([
'id' => 'non_default',
'status' => TRUE,
'label' => 'Non-default',
])
->save();
$this->savedSearches[] = SavedSearch::create([
'type' => 'default',
'uid' => $this->testUser
->id(),
'status' => TRUE,
'notify_interval' => 3600 * 24,
'mail' => 'foobar@example.com',
]);
end($this->savedSearches)
->save();
$this->savedSearches[] = SavedSearch::create([
'type' => 'non_default',
'uid' => $this->testUser
->id(),
'status' => TRUE,
'notify_interval' => 3600 * 24,
]);
end($this->savedSearches)
->save();
$this->testUser
->setEmail('test@example.net')
->save();
$this
->reloadSavedSearches();
$this
->assertEquals('test@example.net', $this->savedSearches[0]
->get('mail')->value);
$this
->assertEquals('foobar@example.com', $this->savedSearches[4]
->get('mail')->value);
}
protected function reloadSavedSearches() {
foreach ($this->savedSearches as $i => $search) {
$this->savedSearches[$i] = SavedSearch::load($search
->id());
}
}
}