RedirectContext::shouldBeRedirected()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
ccs 0
cts 24
cp 0
rs 8.439
cc 6
eloc 20
nc 6
nop 1
crap 42
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Redirect;
6
7
// Helpers.
8
use Behat\DebugExtension\Message;
9
use Behat\Gherkin\Node\TableNode;
10
11
class RedirectContext extends RawRedirectContext
12
{
13
    /**
14
     * @param string $page
15
     *   Expected page URL.
16
     *
17
     * @throws \Exception
18
     * @throws \OverflowException
19
     *
20
     * @Then /^(?:|I )should be redirected(?:| on "([^"]*)")$/
21
     */
22
    public function shouldBeRedirected($page = null)
23
    {
24
        $wait = $this->getTqParameter('wait_for_redirect');
25
        $pages = [];
26
        $seconds = 0;
27
28
        new Message('comment', 4, ['Waiting %s seconds for redirect...'], [$wait]);
29
30
        if (isset($page)) {
31
            $page = trim($page, '/');
32
            $pages = [$page, $this->locatePath($page)];
33
        }
34
35
        while ($wait >= $seconds++) {
36
            $url = $this->getCurrentUrl();
37
            $raw = explode('?', $url)[0];
38
39
            self::debug([
40
                'Expected URLs: %s',
41
                'Current URL: %s',
42
            ], [
43
                implode(', ', $pages),
44
                $raw,
45
            ]);
46
47
            if ((!empty($pages) && in_array($raw, $pages)) || $url === self::$pageUrl) {
48
                return;
49
            }
50
51
            sleep(1);
52
        }
53
54
        throw new \OverflowException('The waiting time is over.');
55
    }
56
57
    /**
58
     * @example
59
     * Given user should have an access to the following pages
60
     *   | page/url |
61
     *
62
     * @param string $not
63
     * @param TableNode $paths
64
     *
65
     * @throws \Exception
66
     *
67
     * @Given /^user should(| not) have an access to the following pages:$/
68
     */
69
    public function checkUserAccessToPages($not, TableNode $paths)
70
    {
71
        $code = empty($not) ? 200 : 403;
72
        $fails = [];
73
74
        foreach (array_keys($paths->getRowsHash()) as $path) {
75
            if (!$this->assertStatusCode($path, $code)) {
76
                $fails[] = $path;
77
            }
78
        }
79
80
        if (!empty($fails)) {
81
            throw new \Exception(sprintf(
82
                'The following paths: "%s" are %s accessible!',
83
                implode(', ', $fails),
84
                $not ? '' : 'not'
85
            ));
86
        }
87
    }
88
89
    /**
90
     * This step should be used instead of "I am at" if page should be checked
91
     * for accessibility before visiting.
92
     *
93
     * Also, this step can be replaced by:
94
     *   Then I am at "page/url"
95
     *
96
     * @param string $path
97
     *   Path to visit.
98
     * @param string|int $code
99
     *   Expected HTTP status code.
100
     *
101
     * @throws \Exception
102
     *
103
     * @Given /^I am on the "([^"]*)" page(?:| and HTTP code is "([^"]*)")$/
104
     * @Given /^(?:|I )visit the "([^"]*)" page(?:| and HTTP code is "([^"]*)")$/
105
     */
106
    public function visitPage($path, $code = 200)
107
    {
108
        if (!$this->assertStatusCode($path, $code)) {
109
            throw new \Exception(sprintf('The page "%s" is not accessible!', $path));
110
        }
111
112
        self::debug(['Visited page: %s'], [$path]);
113
114
        $this->visitPath($path);
115
    }
116
}
117