You are here

protected function IntegrationTest::checkAutocompleteAccess in Search API Autocomplete 8

Verifies that autocomplete is only applied after access checks.

1 call to IntegrationTest::checkAutocompleteAccess()
IntegrationTest::testModule in tests/src/FunctionalJavascript/IntegrationTest.php
Tests the complete functionality of the module via the UI.

File

tests/src/FunctionalJavascript/IntegrationTest.php, line 465

Class

IntegrationTest
Tests the functionality of the whole module from a user's perspective.

Namespace

Drupal\Tests\search_api_autocomplete\FunctionalJavascript

Code

protected function checkAutocompleteAccess() {
  $assert_session = $this
    ->assertSession();

  // Make sure autocomplete functionality is only available for users with the
  // right permission.
  $users = [
    'non-admin' => $this->normalUser,
    'anonymous' => NULL,
  ];
  $permission = "use search_api_autocomplete for {$this->searchId}";
  $autocomplete_path = "search_api_autocomplete/{$this->searchId}";
  foreach ($users as $user_type => $account) {
    $this
      ->drupalLogout();
    if ($account) {
      $this
        ->drupalLogin($account);
    }
    $this
      ->drupalGet('search-api-autocomplete-test');
    $element = $assert_session
      ->elementExists('css', 'input[data-drupal-selector="edit-keys"]');
    $this
      ->assertFalse($element
      ->hasAttribute('data-search-api-autocomplete-search'), "Autocomplete should not be enabled for {$user_type} user without the necessary permission.");
    $this
      ->assertFalse($element
      ->hasClass('form-autocomplete'), "Autocomplete should not be enabled for {$user_type} user without the necessary permission.");
    $this
      ->drupalGet($autocomplete_path, [
      'query' => [
        'q' => 'test',
      ],
    ]);
    $assert_session
      ->pageTextContains('Access denied');
    $assert_session
      ->pageTextContains('You are not authorized to access this page.');
    $rid = $account ? 'authenticated' : 'anonymous';
    $role = Role::load($rid);
    $role
      ->grantPermission($permission);
    $role
      ->save();
    $this
      ->drupalGet('search-api-autocomplete-test');
    $element = $assert_session
      ->elementExists('css', 'input[data-drupal-selector="edit-keys"]');
    $this
      ->assertTrue($element
      ->hasAttribute('data-search-api-autocomplete-search'), "Autocomplete should not be enabled for {$user_type} user without the necessary permission.");

    // @todo Remove check once we depend on Drupal 9.0+.
    if (method_exists($this, 'assertStringContainsString')) {
      $this
        ->assertStringContainsString($this->searchId, $element
        ->getAttribute('data-search-api-autocomplete-search'), "Autocomplete should not be enabled for {$user_type} user without the necessary permission.");
    }
    else {
      $this
        ->assertContains($this->searchId, $element
        ->getAttribute('data-search-api-autocomplete-search'), "Autocomplete should not be enabled for {$user_type} user without the necessary permission.");
    }
    $this
      ->assertTrue($element
      ->hasClass('form-autocomplete'), "Autocomplete should not be enabled for {$user_type} user without the necessary permission.");
    $this
      ->drupalGet($autocomplete_path, [
      'query' => [
        'q' => 'test',
      ],
    ]);
  }
  $this
    ->drupalLogin($this->adminUser);
}