You are here

public function NameAutocompleteTest::testLogNameAutocomplete in Log entity 2.x

Tests name autocomplete route.

File

tests/src/Kernel/NameAutocompleteTest.php, line 141

Class

NameAutocompleteTest
Tests name autocomplete for logs.

Namespace

Drupal\Tests\log\Kernel

Code

public function testLogNameAutocomplete() {

  // Tests admin account with an autocomplete query that shouldn't return any
  // logs.
  $this->container
    ->get('current_user')
    ->setAccount($this->adminAccount);
  $result = $this
    ->getAutocompleteResult('nonsense');
  $this
    ->assertEmpty($result, 'No results for non matching search query.');

  // Tests admin account so it returns the complete set of logs.
  $result = $this
    ->getAutocompleteResult('log');
  $this
    ->assertEqual(count($this->logs), count($result), 'Number of results for matching query and admin user is as expected.');

  // With an account that has 'view any default log' permission, the result
  // should be the complete set of logs.
  $this->container
    ->get('current_user')
    ->setAccount($this->anyAccount);
  $result = $this
    ->getAutocompleteResult('log');
  $this
    ->assertEqual(3, count($result), 'Number of results for matching query and user with view any permission is as expected.');

  // With an account that has 'view own default log' permission, the result
  // should be the logs belonging to that user.
  $this->container
    ->get('current_user')
    ->setAccount($this->ownAccount);
  $result = $this
    ->getAutocompleteResult('log');
  $this
    ->assertEqual(1, count($result), 'Number of results for matching query and user with view own permission is as expected.');
  $own_log = array_filter($this->logs, function ($log) {

    /** @var \Drupal\log\Entity\LogInterface $log */
    return $log
      ->id() == $this->ownAccount
      ->id();
  });
  $own_log = reset($own_log);
  $this
    ->assertEqual($result[0], $own_log
    ->label(), 'The right log for the user is returned.');

  // With an account with no permissions and the right query, there should be
  // no results anyway.
  $this->container
    ->get('current_user')
    ->setAccount($this->noneAccount);
  $result = $this
    ->getAutocompleteResult('log');
  $this
    ->assertEmpty($result, 'No results for user without permissions.');
}