| 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('shibmd', Scope::NS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $nodeList = $xpathObj->query($xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |         if (!$nodeList || $nodeList->length === 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $message = sprintf('The xpath "%s" did not result in at least one match.', $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             throw new ExpectationException($message, $this->getSession()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @Then /^the response should not match xpath \'([^\']*)\'$/ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function theResponseShouldNotMatchXpath($xpath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $document = new DOMDocument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $document->loadXML($this->getSession()->getPage()->getContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $xpathObj = new DOMXPath($document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $xpathObj->registerNamespace('ds', XMLSecurityDSig::XMLDSIGNS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $xpathObj->registerNamespace('mdui', Common::NS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $nodeList = $xpathObj->query($xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 82 |  | View Code Duplication |         if ($nodeList && $nodeList->length > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $message = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 'The xpath "%s" resulted in "%d" matches, where it should result in no matches"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $xpath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $nodeList->length | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             throw new ExpectationException($message, $this->getSession()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @Given /^I should see URL "([^"]*)"$/ | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function iShouldSeeUrl($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->assertSession()->responseContains($url); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @Given /^I should not see URL "([^"]*)"$/ | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function iShouldNotSeeUrl($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->assertSession()->responseNotContains($url); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @Given /^I open (\d+) browser tabs identified by "([^"]*)"$/ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function iOpenTwoBrowserTabsIdentifiedBy($numberOfTabs, $tabNames) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->getMink()->setDefaultSessionName(AbstractSubContext::SESSION_CHROME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $tabs = explode(',', $tabNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         if (count($tabs) != $numberOfTabs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 'Please identify all tabs you are opening in order to refer to them at a later stage' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         foreach ($tabs as $tab) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $this->getMink() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 ->getSession() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 ->executeScript("window.open('/','_blank');"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $windowsNames = $this->getSession()->getWindowNames(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             if (!$windowsNames) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 throw new RuntimeException('The windows where not opened correctly.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             // Grab the window name (which is the last one added to the window list) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $windowName = array_pop($windowsNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             // Keep track of the opened windows in order allow switching between them | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $this->windows[trim($tab)] = $windowName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * @Given /^I switch to "([^"]*)"$/ | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public function iSwitchToWindow($windowName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         // (re) set the default session to the chrome session. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->getMink()->setDefaultSessionName(AbstractSubContext::SESSION_CHROME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->switchToWindow($windowName); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     public function switchToWindow($windowName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         if (!isset($this->windows[$windowName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             throw new RuntimeException(sprintf('Unknown window/tab name "%s"', $windowName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->getSession()->switchToWindow($this->windows[$windowName]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  | 
            
                        
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.