You are here

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

Tests whether the correct owner is set by default for a new saved search.

File

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

Class

SavedSearchCrudTest
Tests CRUD functionality for saved searches.

Namespace

Drupal\Tests\search_api_saved_searches\Kernel

Code

public function testDefaultOwner() {

  // Create the anonymous user. For that, we need the default roles.
  $anonymous = User::create([
    'uid' => 0,
    'name' => '',
  ]);
  $anonymous
    ->save();

  // Create a saved search as anonymous.
  $values = [
    'type' => 'default',
  ];
  $search = SavedSearch::create($values);
  $owner = $search
    ->getOwner();
  $this
    ->assertNotNull($owner);
  $this
    ->assertEquals(0, $owner
    ->id());
  $this
    ->assertEquals(0, $search
    ->getOwnerId());

  // Log in new user.
  $user = $this
    ->createUser();
  $uid = $user
    ->id();
  $this
    ->setCurrentUser($user);

  // Create a saved search as a registered user.
  $search = SavedSearch::create($values);
  $owner = $search
    ->getOwner();
  $this
    ->assertNotNull($owner);
  $this
    ->assertEquals($uid, $owner
    ->id());
  $this
    ->assertEquals($uid, $search
    ->getOwnerId());
}