You are here

public function GlobalRedirectTest::testRedirects in Redirect 8

Will test the redirects.

File

tests/src/Functional/GlobalRedirectTest.php, line 165

Class

GlobalRedirectTest
Global redirect test cases.

Namespace

Drupal\Tests\redirect\Functional

Code

public function testRedirects() {

  // First, test that redirects can be disabled.
  $this->config
    ->set('route_normalizer_enabled', FALSE)
    ->save();
  $this
    ->assertNoRedirect('index.php/node/' . $this->node
    ->id());
  $this
    ->assertNoRedirect('index.php/test-node');
  $this
    ->assertNoRedirect('test-node/');
  $this
    ->assertNoRedirect('Test-node/');
  $this->config
    ->set('route_normalizer_enabled', TRUE)
    ->save();

  // Test alias normalization.
  $this
    ->assertRedirect('node/' . $this->node
    ->id(), 'test-node');
  $this
    ->assertRedirect('Test-node', 'test-node');

  // Test redirects for non-clean urls.
  $this
    ->assertRedirect('index.php/node/' . $this->node
    ->id(), 'test-node');
  $this
    ->assertRedirect('index.php/test-node', 'test-node');

  // Test deslashing.
  $this
    ->assertRedirect('test-node/', 'test-node');

  // Test front page redirects.
  $this
    ->config('system.site')
    ->set('page.front', '/node')
    ->save();
  $this
    ->assertRedirect('node', '/');

  // Test front page redirects with an alias.
  $this
    ->createPathAlias('/node', '/node-alias');
  $this
    ->assertRedirect('node-alias', '/');

  // Test a POST request. It should stay on the same path and not try to
  // redirect. Because Mink does not provide methods to do plain POSTs, we
  // need to use the underlying Guzzle HTTP client directly.

  /** @var \Behat\Mink\Driver\GoutteDriver $driver */
  $driver = $this
    ->getSession()
    ->getDriver();
  $response = $driver
    ->getClient()
    ->getClient()
    ->post($this
    ->getAbsoluteUrl('Test-node'), [
    // Do not follow redirects. This way, we can assert that the server did
    // not even _try_ to redirect us
    'allow_redirects' => FALSE,
    'headers' => [
      'Accept' => 'application/json',
    ],
  ]);

  // Does not do a redirect, stays in the same path.
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->assertEmpty($response
    ->getHeader('Location'));
  $this
    ->assertStringNotContainsString('http-equiv="refresh', (string) $response
    ->getBody());

  // Test the access checking.
  $this->config
    ->set('access_check', TRUE)
    ->save();
  $this
    ->assertNoRedirect('admin/config/system/site-information', 403);
  $this->config
    ->set('access_check', FALSE)
    ->save();

  // @todo - here it seems that the access check runs prior to our redirecting
  //   check why so and enable the test.

  //$this->assertRedirect('admin/config/system/site-information', 'site-info');

  // Test original query string is preserved with alias normalization.
  $this
    ->assertRedirect('Test-node?&foo&.bar=baz', 'test-node?&foo&.bar=baz');

  // Test alias normalization with trailing ?.
  // @todo \GuzzleHttp\Psr7\Uri strips away the trailing ?, this should
  //   actually be a redirect but can't be tested with Guzzle. Improve in
  //   https://www.drupal.org/project/redirect/issues/3119503.
  $this
    ->assertNoRedirect('test-node?');
  $this
    ->assertRedirect('Test-node?', 'test-node');

  // Test alias normalization still works without trailing ?.
  $this
    ->assertNoRedirect('test-node');
  $this
    ->assertRedirect('Test-node', 'test-node');

  // Login as user with admin privileges.
  $this
    ->drupalLogin($this->adminUser);

  // Test ignoring admin paths.
  $this->config
    ->set('ignore_admin_path', FALSE)
    ->save();
  $this
    ->assertRedirect('admin/config/system/site-information', 'site-info');

  // Test alias normalization again with ignore_admin_path false.
  $this
    ->assertRedirect('Test-node', 'test-node');
  $this->config
    ->set('ignore_admin_path', TRUE)
    ->save();
  $this
    ->assertNoRedirect('admin/config/system/site-information');

  // Test alias normalization again with ignore_admin_path true.
  $this
    ->assertRedirect('Test-node', 'test-node');
}