Completed
Pull Request — master (#16)
by Sergii
04:36
created

RedirectContext::visitPage()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
ccs 0
cts 6
cp 0
rs 8.5806
cc 4
eloc 17
nc 4
nop 2
crap 20
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(['Expected URLs: %s', 'Current URL: %s'], [implode(', ', $pages), $raw]);
40
41
            if ((!empty($pages) && in_array($raw, $pages)) || $url === self::$pageUrl) {
42
                return;
43
            }
44
45
            sleep(1);
46
        }
47
48
        throw new \OverflowException('Waiting time is over.');
49
    }
50
51
    /**
52
     * @example
53
     * Given user should have an access to the following pages
54
     *   | page/url |
55
     *
56
     * @param string $not
57
     * @param TableNode $paths
58
     *
59
     * @throws \Exception
60
     *
61
     * @Given /^user should(| not) have an access to the following pages:$/
62
     */
63
    public function checkUserAccessToPages($not, TableNode $paths)
64
    {
65
        $code = empty($not) ? 200 : 403;
66
        $fails = [];
67
68
        foreach (array_keys($paths->getRowsHash()) as $path) {
69
            if (!$this->assertStatusCode($path, $code)) {
70
                $fails[] = $path;
71
            }
72
        }
73
74
        if (!empty($fails)) {
75
            throw new \Exception(sprintf(
76
                'The following paths: "%s" are %s accessible!',
77
                implode(', ', $fails),
78
                $not ? '' : 'not'
79
            ));
80
        }
81
    }
82
83
    /**
84
     * This step should be used instead of "I am at" if page should be checked
85
     * for accessibility before visiting.
86
     *
87
     * Also, this step can be replaced by:
88
     *   Then I am at "page/url"
89
     *
90
     * @param string $path
91
     *   Path to visit.
92
     * @param string|int $code
93
     *   Expected HTTP status code.
94
     *
95
     * @throws \Exception
96
     *
97
     * @Given /^I am on the "([^"]*)" page(?:| and HTTP code is "([^"]*)")$/
98
     * @Given /^(?:|I )visit the "([^"]*)" page(?:| and HTTP code is "([^"]*)")$/
99
     */
100
    public function visitPage($path, $code = 200)
101
    {
102
        $this->visitPath($path);
103
104
        if (DRUPAL_CORE > 7) {
105
            $entries = \Drupal::database()
106
                ->select('watchdog', 'w')
107
                ->fields('w', ['message', 'variables'])
108
                ->condition('type', ['php'], 'IN')
109
                ->range(0, 10)
110
                ->execute()
111
                ->fetchAll();
112
113
            foreach ($entries as $entry) {
114
                $entry->variables = unserialize($entry->variables);
115
116
                var_dump((string) format_string($entry->message, $entry->variables));
0 ignored issues
show
Security Debugging Code introduced by
var_dump((string) format...e, $entry->variables)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
117
                var_dump($entry->variables);
118
            }
119
        }
120
121
        self::debug(['Visited page: %s'], [$path]);
122
123
        if (!$this->assertStatusCode($path, $code)) {
124
            throw new \Exception(sprintf('The page "%s" is not accessible!', $path));
125
        }
126
    }
127
}
128