3 namespace Drupal\redirect\Tests;
5 use Drupal\Component\Utility\SafeMarkup;
9 * Asserts the redirect from a given path to the expected destination path.
11 trait AssertRedirectTrait {
14 * Asserts the redirect from $path to the $expected_ending_url.
18 * @param $expected_ending_url
19 * The path where we expect it to redirect. If NULL value provided, no
20 * redirect is expected.
21 * @param string $expected_ending_status
22 * The status we expect to get with the first request.
24 public function assertRedirect($path, $expected_ending_url, $expected_ending_status = 'HTTP/1.1 301 Moved Permanently') {
25 $this->drupalHead($path);
26 $headers = $this->drupalGetHeaders(TRUE);
28 $ending_url = isset($headers[0]['location']) ? $headers[0]['location'] : NULL;
29 $message = SafeMarkup::format('Testing redirect from %from to %to. Ending url: %url', [
31 '%to' => $expected_ending_url,
32 '%url' => $ending_url,
35 if ($expected_ending_url == '<front>') {
36 $expected_ending_url = Url::fromUri('base:')->setAbsolute()->toString();
38 elseif (!empty($expected_ending_url)) {
39 // Check for absolute/external urls.
40 if (!parse_url($expected_ending_url, PHP_URL_SCHEME)) {
41 $expected_ending_url = Url::fromUri('base:' . $expected_ending_url)->setAbsolute()->toString();
45 $expected_ending_url = NULL;
48 $this->assertEqual($expected_ending_url, $ending_url, $message);
50 $this->assertEqual($headers[0][':status'], $expected_ending_status);