| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\VersionedAdmin\Tests\Behat\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Behat\Mink\Element\NodeElement; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\BehatExtension\Context\SilverStripeContext; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | if (!class_exists(SilverStripeContext::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class FeatureContext extends SilverStripeContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @Then I should see a list of versions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function iShouldSeeAListOfVersions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->getVersions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @Then I should see a list of versions in descending order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function iShouldSeeAListOfVersionsInDescendingOrder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $versions = $this->getVersions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         assertNotEmpty($versions, 'I see a list of versions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $previous = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         foreach ($versions as $version) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             /** @var NodeElement $version */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             if ($previous) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 assert($version->getValue() < $previous); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $previous = $version->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @When I click on the first version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function iClickOnTheFirstVersion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         assertNotNull($this->getLatestVersion(), 'I should see a list of versions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->getLatestVersion()->click(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @When I click on version :versionNo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function iClickOnVersion($versionNo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $versions = $this->getVersions(' td:first-child'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $desiredVersion = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($versions as $version) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             /** @var NodeElement $version */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if ($version->getText() == $versionNo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $desiredVersion = $version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         assertNotNull($desiredVersion, 'Desired version ' . $versionNo . ' was not found in the page.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->clickVersion($desiredVersion); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Click on the given version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param NodeElement $version | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     protected function clickVersion(NodeElement $version) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $version->click(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         // Wait for the form builder to load | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $this->getSession()->wait(3000, 'window.jQuery("#Form_versionForm").length > 0'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Returns the versions from the history viewer list (table rows) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param string $modifier Optional CSS selector modifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return NodeElement[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     protected function getVersions($modifier = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // Wait for the table to be visible | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->getSession()->wait(3000, 'window.jQuery(".history-viewer .table").length > 0'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $versions = $this->getSession() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->getPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ->findAll('css', '.history-viewer .table tbody tr' . $modifier); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $versions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Example: I should see the "Live" badge | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Example: I should not see the "Live" badge | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @Then /^I should (not |)see the "([\w\s]+)" badge$/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $negative | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function iShouldSeeTheBadge($negative, $text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if ($negative) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->assertElementNotOnPage('.history-viewer .badge'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $this->assertElementContains('.history-viewer .badge', $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Example: I should see "ADMIN User" in the author column in version 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @Then I should see :text in the author column in version :versionNumber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function iShouldSeeInTheAuthorColumn($text, $versionNumber) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $version = $this->getSpecificVersion($versionNumber); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $authorColumn = $version->find('css', 'td:nth-of-type(3)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $exists = strpos($authorColumn->getText(), $text) !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         assertTrue($exists, 'Author column contains ' . $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Example: I should see "Saved" in the record column in version 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @Then I should see :text in the record column in version :versionNumber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function iShouldSeeInTheRecordColumn($text, $versionNumber) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $version = $this->getSpecificVersion($versionNumber); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $recordColumn = $version->find('css', 'td:nth-of-type(2)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $exists = strpos($recordColumn->getText(), $text) !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         assertTrue($exists, 'Record column contains ' . $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @Then I should see :text in the version column in version :versionNumber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     public function iShouldSeeInTheVersionColumn($text, $versionNumber) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $version = $this->getSpecificVersion($versionNumber); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $versionColumn = $version->find('css', 'td'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $exists = strpos($versionColumn->getText(), $text) !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         assertTrue($exists, 'Version column contains ' . $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * Returns the table row that holds information on the most recent version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     protected function getLatestVersion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $versions = $this->getVersions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return current($versions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Returns the table row that holds information on the selected version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param int $versionNumber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @return NodeElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     protected function getSpecificVersion($versionNumber) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         $versions = $this->getVersions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         foreach ($versions as $version) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             /** @var NodeElement $version */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             if (strpos($version->getText(), $versionNumber) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 return $version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.