You are here

public function AssertRedirectTrait::assertRedirect in Redirect 8

Asserts the redirect from $path to the $expected_ending_url.

Parameters

string $path: The request path.

$expected_ending_url: The path where we expect it to redirect. If NULL value provided, no redirect is expected.

int $expected_ending_status: The status we expect to get with the first request.

string $method: The HTTP METHOD to use.

Return value

\Psr\Http\Message\ResponseInterface The HTTP response.

7 calls to AssertRedirectTrait::assertRedirect()
Fix404RedirectUILanguageTest::testFix404RedirectList in modules/redirect_404/tests/src/Functional/Fix404RedirectUILanguageTest.php
Tests the fix 404 pages workflow with language and content translation.
RedirectUILanguageTest::testEditRedirectLanguage in tests/src/Functional/RedirectUILanguageTest.php
Test editing the redirect language.
RedirectUILanguageTest::testLanguageSpecificRedirects in tests/src/Functional/RedirectUILanguageTest.php
Test multilingual scenarios.
RedirectUILanguageTest::testUndefinedLangugageRedirects in tests/src/Functional/RedirectUILanguageTest.php
Test non-language specific redirect.
RedirectUITest::testAutomaticRedirects in tests/src/Functional/RedirectUITest.php
Tests redirects being automatically created upon path alias change.

... See full list

File

tests/src/Functional/AssertRedirectTrait.php, line 29

Class

AssertRedirectTrait
Asserts the redirect from a given path to the expected destination path.

Namespace

Drupal\Tests\redirect\Functional

Code

public function assertRedirect($path, $expected_ending_url, $expected_ending_status = 301, $method = 'GET') {
  $client = $this
    ->getHttpClient();

  /** @var \Psr\Http\Message\ResponseInterface $response */
  $url = $this
    ->getAbsoluteUrl($path);
  try {
    $response = $client
      ->request($method, $url, [
      'allow_redirects' => false,
    ]);
  } catch (ClientException $e) {
    $this
      ->assertEquals($expected_ending_status, $e
      ->getResponse()
      ->getStatusCode());
    return $e
      ->getResponse();
  }
  $this
    ->assertEquals($expected_ending_status, $response
    ->getStatusCode());
  $ending_url = $response
    ->getHeader('location');
  $ending_url = $ending_url ? $ending_url[0] : NULL;
  $message = "Testing redirect from {$path} to {$expected_ending_url}. Ending url: {$ending_url}";
  if ($expected_ending_url == '<front>') {
    $expected_ending_url = Url::fromUri('base:')
      ->setAbsolute()
      ->toString();
  }
  elseif (!empty($expected_ending_url)) {

    // Check for absolute/external urls.
    if (!parse_url($expected_ending_url, PHP_URL_SCHEME)) {
      $expected_ending_url = Url::fromUri('base:' . $expected_ending_url)
        ->setAbsolute()
        ->toString();
    }
  }
  else {
    $expected_ending_url = NULL;
  }
  $this
    ->assertEqual($expected_ending_url, $ending_url, $message);
  return $response;
}