Completed
Pull Request — develop (#190)
by Michiel
04:13 queued 02:31
created

MinkContext::iShouldNotSeeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2020 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\Behat;
20
21
use Behat\Mink\Exception\ExpectationException;
22
use Behat\MinkExtension\Context\MinkContext as BaseMinkContext;
23
use DOMDocument;
24
use DOMXPath;
25
use RobRichards\XMLSecLibs\XMLSecurityDSig;
26
use RuntimeException;
27
use SAML2\XML\mdui\Common;
28
use SAML2\XML\shibmd\Scope;
29
30
/**
31
 * Mink-enabled context.
32
 */
33
class MinkContext extends BaseMinkContext
34
{
35
    /**
36
     * @var array a list of window names identified by the name the tester refers to them in the step definitions.
37
     * @example ['My tab' => 'WindowNameGivenByBrowser', 'My other tab' => 'WindowNameGivenByBrowser']
38
     */
39
    private $windows = [];
40
41
    /**
42
     * @Then /^the response should contain \'([^\']*)\'$/
43
     */
44
    public function theResponseShouldContain($string)
45
    {
46
        $this->assertSession()->responseContains($string);
47
    }
48
49
    /**
50
     * @Then /^the response should match xpath \'([^\']*)\'$/
51
     */
52
    public function theResponseShouldMatchXpath($xpath)
53
    {
54
        $document = new DOMDocument();
55
        $document->loadXML($this->getSession()->getPage()->getContent());
56
57
        $xpathObj = new DOMXPath($document);
58
        $xpathObj->registerNamespace('ds', XMLSecurityDSig::XMLDSIGNS);
59
        $xpathObj->registerNamespace('mdui', Common::NS);
60
        $xpathObj->registerNamespace('mdash', Common::NS);
61
        $xpathObj->registerNamespace('shibmd', Scope::NS);
62
        $nodeList = $xpathObj->query($xpath);
63
64 View Code Duplication
        if (!$nodeList || $nodeList->length === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            $message = sprintf('The xpath "%s" did not result in at least one match.', $xpath);
66
            throw new ExpectationException($message, $this->getSession());
67
        }
68
    }
69
70
    /**
71
     * @Then /^the response should not match xpath \'([^\']*)\'$/
72
     */
73
    public function theResponseShouldNotMatchXpath($xpath)
74
    {
75
        $document = new DOMDocument();
76
        $document->loadXML($this->getSession()->getPage()->getContent());
77
78
        $xpathObj = new DOMXPath($document);
79
        $xpathObj->registerNamespace('ds', XMLSecurityDSig::XMLDSIGNS);
80
        $xpathObj->registerNamespace('mdui', Common::NS);
81
        $nodeList = $xpathObj->query($xpath);
82
83 View Code Duplication
        if ($nodeList && $nodeList->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            $message = sprintf(
85
                'The xpath "%s" resulted in "%d" matches, where it should result in no matches"',
86
                $xpath,
87
                $nodeList->length
88
            );
89
            throw new ExpectationException($message, $this->getSession());
90
        }
91
    }
92
93
    /**
94
     * @Given /^I should see URL "([^"]*)"$/
95
     */
96
    public function iShouldSeeUrl($url)
97
    {
98
        $this->assertSession()->responseContains($url);
99
    }
100
101
    /**
102
     * @Given /^I should not see URL "([^"]*)"$/
103
     */
104
    public function iShouldNotSeeUrl($url)
105
    {
106
        $this->assertSession()->responseNotContains($url);
107
    }
108
109
    /**
110
     * @Given /^I open (\d+) browser tabs identified by "([^"]*)"$/
111
     */
112
    public function iOpenTwoBrowserTabsIdentifiedBy($numberOfTabs, $tabNames)
113
    {
114
        $this->getMink()->setDefaultSessionName(AbstractSubContext::SESSION_CHROME);
115
        $tabs = explode(',', $tabNames);
116
        if (count($tabs) != $numberOfTabs) {
117
            throw new RuntimeException(
118
                'Please identify all tabs you are opening in order to refer to them at a later stage'
119
            );
120
        }
121
122
        foreach ($tabs as $tab) {
123
            $this->getMink()
124
                ->getSession()
125
                ->executeScript("window.open('/','_blank');");
126
127
            $windowsNames = $this->getSession()->getWindowNames();
128
129
            if (!$windowsNames) {
130
                throw new RuntimeException('The windows where not opened correctly.');
131
            }
132
            // Grab the window name (which is the last one added to the window list)
133
            $windowName = array_pop($windowsNames);
134
            // Keep track of the opened windows in order allow switching between them
135
            $this->windows[trim($tab)] = $windowName;
136
        }
137
    }
138
139
    /**
140
     * @Given /^I switch to "([^"]*)"$/
141
     */
142
    public function iSwitchToWindow($windowName)
143
    {
144
        // (re) set the default session to the chrome session.
145
        $this->getMink()->setDefaultSessionName(AbstractSubContext::SESSION_CHROME);
146
        $this->switchToWindow($windowName);
147
    }
148
149
    public function switchToWindow($windowName)
150
    {
151
        if (!isset($this->windows[$windowName])) {
152
            throw new RuntimeException(sprintf('Unknown window/tab name "%s"', $windowName));
153
        }
154
        $this->getSession()->switchToWindow($this->windows[$windowName]);
155
    }
156
}
157