You are here

protected function IntegrationTest::checkFunctionalityAnonymous in Search API Saved Searches 8

Checks functionality for anonymous users.

1 call to IntegrationTest::checkFunctionalityAnonymous()
IntegrationTest::testModule in tests/src/Functional/IntegrationTest.php
Tests overall functionality of the module.

File

tests/src/Functional/IntegrationTest.php, line 242

Class

IntegrationTest
Tests overall functionality of the module.

Namespace

Drupal\Tests\search_api_saved_searches\Functional

Code

protected function checkFunctionalityAnonymous() {
  $assert_session = $this
    ->assertSession();
  if ($this->loggedInUser) {
    $this
      ->drupalLogout();
  }

  // Anonymous users don't have permission yet to use saved searches.
  $this
    ->drupalGet('search-api-test');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains('Description for the');
  $this
    ->drupalGet('search-api-test-search-view-caching-none');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains('Description for the');
  $this
    ->drupalGet('search-api-test-sorts');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains('Description for the');

  // Grant the permissions.
  $role = Role::load(Role::ANONYMOUS_ID);
  $this
    ->grantPermissions($role, [
    'use default search_api_saved_searches',
    'use foobar search_api_saved_searches',
  ]);

  // Now check that there are the expected blocks on all three search pages.
  $this
    ->drupalGet('search-api-test', [
    'query' => [
      'search_api_fulltext' => 'foo',
    ],
  ]);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Default saved searches block');
  $assert_session
    ->pageTextContains('Description for the default type.');
  $assert_session
    ->pageTextNotContains('Foo & Bar saved searches block');
  $assert_session
    ->pageTextNotContains('Description for the foobar type.');
  $edit = [
    'label[0][value]' => 'First saved search',
    'notify_interval' => '3600',
    'mail[0][value]' => 'test@example.net',
  ];
  $this
    ->submitForm($edit, 'Save search');
  $assert_session
    ->pageTextContains('Your saved search was successfully created.');
  $assert_session
    ->pageTextContains('You will soon receive an e-mail with a confirmation link to activate it.');
  $this
    ->drupalGet('search-api-test-search-view-caching-none', [
    'query' => [
      'search_api_fulltext' => 'bar',
    ],
  ]);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Foo & Bar saved searches block');
  $assert_session
    ->pageTextContains('Description for the foobar type.');
  $assert_session
    ->pageTextNotContains('Default saved searches block');
  $assert_session
    ->pageTextNotContains('Description for the default type.');
  $edit = [
    'label[0][value]' => 'Second saved search',
    'notify_interval' => '86400',
    'mail[0][value]' => 'foobar@example.net',
  ];
  $this
    ->submitForm($edit, 'Save search');
  $assert_session
    ->pageTextContains('Your saved search was successfully created.');
  $assert_session
    ->pageTextContains('You will soon receive an e-mail with a confirmation link to activate it.');
  $this
    ->drupalGet('search-api-test-sorts');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextNotContains('Description for the');
  $captured_emails = \Drupal::state()
    ->get('system.test_mail_collector');
  $second_mail = array_pop($captured_emails);
  $first_mail = array_pop($captured_emails);
  $this
    ->assertEquals('test@example.net', $first_mail['to']);
  $this
    ->assertEquals('foobar@example.net', $second_mail['to']);
  $regex = '#- (\\w+):\\s+(\\S+)#';
  $this
    ->assertEquals(4, preg_match_all($regex, $first_mail['body'], $matches, PREG_SET_ORDER));
  $first_mail_urls = [];
  foreach ($matches as $match) {
    $this
      ->assertStringContainsString('token=', $match[2], "{$match[1]} URL for first saved search doesn't include a token.");
    $first_mail_urls[$match[1]] = $match[2];
  }
  $this
    ->assertEquals(4, preg_match_all($regex, $second_mail['body'], $matches, PREG_SET_ORDER));
  $second_mail_urls = [];
  foreach ($matches as $match) {
    $this
      ->assertStringContainsString('token=', $match[2], "{$match[1]} URL for second saved search doesn't include a token.");
    $second_mail_urls[$match[1]] = $match[2];
  }

  // Check URLs for first saved search.
  $this
    ->drupalGet($first_mail_urls['Activate']);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Your saved search was successfully activated.');
  $this
    ->drupalGet($first_mail_urls['Activate']);
  $assert_session
    ->statusCodeEquals(404);
  $this
    ->drupalGet($first_mail_urls['View']);
  $assert_session
    ->statusCodeEquals(200);
  $search_url = $this
    ->buildUrl('search-api-test', [
    'query' => [
      'search_api_fulltext' => 'foo',
    ],
  ]);
  $assert_session
    ->addressEquals($search_url);
  $this
    ->drupalGet($first_mail_urls['Edit']);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Edit saved search First saved search');
  $assert_session
    ->pageTextContains('Fulltext keywords');
  $assert_session
    ->pageTextContains('Notification interval');
  $assert_session
    ->pageTextContains('E-mail');
  $this
    ->drupalGet($first_mail_urls['Delete']);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Do you really want to delete this saved search?');

  // Check URLs for second saved search.
  $this
    ->drupalGet($second_mail_urls['Activate']);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Your saved search was successfully activated.');
  $this
    ->drupalGet($second_mail_urls['Activate']);
  $assert_session
    ->statusCodeEquals(404);
  $this
    ->drupalGet($second_mail_urls['View']);
  $assert_session
    ->statusCodeEquals(200);
  $search_url = $this
    ->buildUrl('search-api-test-search-view-caching-none', [
    'query' => [
      'search_api_fulltext' => 'bar',
    ],
  ]);
  $assert_session
    ->addressEquals($search_url);
  $this
    ->drupalGet($second_mail_urls['Edit']);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Edit saved search Second saved search');
  $assert_session
    ->pageTextNotContains('Fulltext keywords');
  $assert_session
    ->pageTextContains('Notification interval');
  $assert_session
    ->pageTextContains('E-mail');
  $this
    ->drupalGet($second_mail_urls['Delete']);
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Do you really want to delete this saved search?');
}