You are here

function SecurepagesTest::_testAnonymous in Secure Pages 8

Tests for anonymous browsing with securepages.

1 call to SecurepagesTest::_testAnonymous()
SecurepagesTest::testSecurePages in src/Tests/SecurepagesTest.php
Runs all the tests in a sequence to avoid multiple re-installs.

File

src/Tests/SecurepagesTest.php, line 121
Contains \Drupal\securepages\Tests\SecurepagesTest.

Class

SecurepagesTest
Test Secure Pages redirects.

Namespace

Drupal\securepages\Tests

Code

function _testAnonymous() {

  // Visit the home page (same as user page) with plain HTTP.
  $this
    ->drupalGet('', [
    'https' => FALSE,
  ]);
  $this
    ->assertResponse(302);
  $this
    ->assertUrl(Url::fromRoute('user.login', [], [
    'https' => TRUE,
    'absolute' => TRUE,
  ]));

  // Visit the login page (same as front page in tests) with plain HTTP.
  $this
    ->drupalGet('user/login', [
    'https' => FALSE,
  ]);
  $this
    ->assertResponse(302);
  $this
    ->assertUrl(Url::fromRoute('user.login', [], [
    'https' => TRUE,
    'absolute' => TRUE,
  ]));

  // Visit the home page and login with HTTPS.
  $this
    ->drupalGet('', [
    'https' => TRUE,
  ]);
  $this
    ->assertResponse(302);
  $this
    ->assertUrl(Url::fromRoute('user.login', [], [
    'https' => TRUE,
    'absolute' => TRUE,
  ]));
  $this
    ->drupalGet('user/login', [
    'https' => TRUE,
  ]);
  $this
    ->assertResponse(200);
  $this
    ->assertUrl(Url::fromRoute('user.login', [], [
    'https' => TRUE,
    'absolute' => TRUE,
  ]));

  // Enable "Switch back to http pages when there are no matches".
  \Drupal::configFactory()
    ->getEditable('securepages.settings')
    ->set('switch', TRUE)
    ->save();

  // Visit the home page and user with HTTPS and confirm the switch-back.
  $this
    ->drupalGet('', [
    'https' => TRUE,
  ]);
  $this
    ->assertResponse(302);
  $this
    ->assertUrl(Url::fromRoute('user.login', [], [
    'https' => FALSE,
    'absolute' => TRUE,
  ]));
  $this
    ->drupalGet('user/login', [
    'https' => TRUE,
  ]);
  $this
    ->assertResponse(302);
  $this
    ->assertUrl(Url::fromRoute('user.login', [], [
    'https' => FALSE,
    'absolute' => TRUE,
  ]));

  // Clean up.
  \Drupal::configFactory()
    ->getEditable('securepages.settings')
    ->set('switch', FALSE)
    ->save();
}