Interim commit.
[yaffs-website] / web / modules / contrib / redirect / src / Tests / AssertRedirectTrait.php
1 <?php
2
3 namespace Drupal\redirect\Tests;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Url;
7
8 /**
9  * Asserts the redirect from a given path to the expected destination path.
10  */
11 trait AssertRedirectTrait {
12
13   /**
14    * Asserts the redirect from $path to the $expected_ending_url.
15    *
16    * @param string $path
17    *   The request path.
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.
23    */
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);
27
28     $ending_url = isset($headers[0]['location']) ? $headers[0]['location'] : NULL;
29     $message = SafeMarkup::format('Testing redirect from %from to %to. Ending url: %url', [
30       '%from' => $path,
31       '%to' => $expected_ending_url,
32       '%url' => $ending_url,
33     ]);
34
35     if ($expected_ending_url == '<front>') {
36       $expected_ending_url = Url::fromUri('base:')->setAbsolute()->toString();
37     }
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();
42       }
43     }
44     else {
45       $expected_ending_url = NULL;
46     }
47
48     $this->assertEqual($expected_ending_url, $ending_url, $message);
49
50     $this->assertEqual($headers[0][':status'], $expected_ending_status);
51   }
52
53 }