You are here

function RedirectUITest::testRedirectLoop in Redirect 8

Test the redirect loop protection and logging.

File

tests/src/Functional/RedirectUITest.php, line 156

Class

RedirectUITest
UI tests for redirect module.

Namespace

Drupal\Tests\redirect\Functional

Code

function testRedirectLoop() {

  // Redirect loop redirection only works when page caching is disabled.
  \Drupal::service('module_installer')
    ->uninstall([
    'page_cache',
  ]);

  /** @var \Drupal\redirect\Entity\Redirect $redirect1 */
  $redirect1 = $this->storage
    ->create();
  $redirect1
    ->setSource('node');
  $redirect1
    ->setRedirect('admin');
  $redirect1
    ->setStatusCode(301);
  $redirect1
    ->save();

  /** @var \Drupal\redirect\Entity\Redirect $redirect2 */
  $redirect2 = $this->storage
    ->create();
  $redirect2
    ->setSource('admin');
  $redirect2
    ->setRedirect('node');
  $redirect2
    ->setStatusCode(301);
  $redirect2
    ->save();
  $this->maximumRedirects = 10;
  $this
    ->drupalGet('node');
  $this
    ->assertText('Service unavailable');
  $this
    ->assertResponse(503);
  $log = \Drupal::database()
    ->select('watchdog')
    ->fields('watchdog')
    ->condition('type', 'redirect')
    ->execute()
    ->fetchAll();
  if (count($log) == 0) {
    $this
      ->fail('Redirect loop has not been logged');
  }
  else {
    $log = reset($log);
    $this
      ->assertEquals(RfcLogLevel::WARNING, $log->severity);
    $this
      ->assertEquals('Redirect loop identified at %path for redirect %rid', $log->message);
    $this
      ->assertEquals([
      '%path' => '/node',
      '%rid' => $redirect1
        ->id(),
    ], unserialize($log->variables));
  }
}