AssertRedirectTrait.php in Redirect 8
File
tests/src/Functional/AssertRedirectTrait.php
View source
<?php
namespace Drupal\Tests\redirect\Functional;
use Drupal\Core\Url;
use GuzzleHttp\Exception\ClientException;
trait AssertRedirectTrait {
public function assertRedirect($path, $expected_ending_url, $expected_ending_status = 301, $method = 'GET') {
$client = $this
->getHttpClient();
$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)) {
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;
}
}