You are here

public function Domain301RedirectTest::testDomainRedirect in Domain 301 Redirect 8

Test redirecting to various domains.

File

tests/src/Functional/Domain301RedirectTest.php, line 57

Class

Domain301RedirectTest
Test the redirection of a domain using the DomainRedirectEventSubscriber.

Namespace

Drupal\Tests\domain_301_redirect\Functional

Code

public function testDomainRedirect() {
  list($statusCode, $headers) = $this
    ->getRedirect($this->domain);
  $this
    ->assertNotEquals(301, $statusCode, 'No redirect is performed.');

  // Set a redirect domain.
  $this
    ->enableRedirect($this->redirectDomain);
  list($statusCode, $headers) = $this
    ->getRedirect($this->domain);
  $this
    ->assertEquals(301, $statusCode, 'Domain redirect is performed.');
  $location = $headers['Location'][0] ?? NULL;
  $this
    ->assertEquals($this->redirectDomain, $location, 'Domain redirect url is correct.');

  // A 404 request should still redirect.
  list($statusCode, $headers) = $this
    ->getRedirect($this->domain . 'not-found');
  $this
    ->assertEquals(301, $statusCode, 'Domain redirect is performed for 404 urls.');
  $location = $headers['Location'][0] ?? NULL;
  $this
    ->assertEquals($this->redirectDomain . 'not-found', $location, 'Domain redirect url is correct for 404 urls.');

  // A 403 request should still redirect.
  list($statusCode, $headers) = $this
    ->getRedirect($this->domain . 'admin/config');
  $this
    ->assertEquals(301, $statusCode, 'Domain redirect is performed for 403 urls.');
  $location = $headers['Location'][0] ?? NULL;
  $this
    ->assertEquals($this->redirectDomain . 'admin/config', $location, 'Domain redirect url is correct for 403 urls.');

  // An http request should redirect when we're redirecting to https.
  $this
    ->enableRedirect($this->secureDomain);
  list($statusCode, $headers) = $this
    ->getRedirect($this->domain);
  $this
    ->assertEquals(301, $statusCode, 'Domain redirect is performed on an https domain.');
  $location = $headers['Location'][0] ?? NULL;
  $this
    ->assertEquals($this->secureDomain, $location, 'Domain redirect url is correct on an https domain.');
  $this
    ->disableRedirect();
  list($statusCode, $headers) = $this
    ->getRedirect($this->domain);
  $this
    ->assertNotEquals(301, $statusCode, 'The site is no longer redirecting.');
}