| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace HtaccessCapabilityTester\Testers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \HtaccessCapabilityTester\HtaccessCapabilityTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \HtaccessCapabilityTester\HttpRequesterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \HtaccessCapabilityTester\HttpResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use \HtaccessCapabilityTester\SimpleHttpRequester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use \HtaccessCapabilityTester\TestResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use \HtaccessCapabilityTester\Testers\Helpers\ResponseInterpreter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CustomTester extends AbstractTester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var array  A definition defining the test */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var array  For convenience, all tests */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param  array   $test     The test (may contain subtests) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 80 |  |     public function __construct($test) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 80 |  |         $this->test = $test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 80 |  |         if (isset($test['subtests'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 38 |  |             $this->tests = $test['subtests']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             // Add main subdir to subdir for all subtests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 38 |  |             foreach ($this->tests as &$subtest) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 38 |  |                 if (isset($subtest['subdir'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 38 |  |                     $subtest['subdir'] = $test['subdir'] . '/' . $subtest['subdir']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 65 |  |             $this->tests = [$test]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         //echo '<pre>' . print_r($this->tests, true) . '</pre>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         //echo json_encode($this->tests) . '<br>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 80 |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 80 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Register the test files using the "registerTestFile" method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return  void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 80 |  |     protected function registerTestFiles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 80 |  |         foreach ($this->tests as $test) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 80 |  |             if (isset($test['files'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 80 |  |                 foreach ($test['files'] as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     // Two syntaxes are allowed: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     // - Simple array (ie: ['0.txt', '0'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     // - Named, ie:  ['filename' => '0.txt', 'content' => '0'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     // The second makes more readable YAML definitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 80 |  |                     if (isset($file['filename'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                         $filename = $file['filename']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                         $content = $file['content']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 80 |  |                         list ($filename, $content) = $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 80 |  |                     $this->registerTestFile($test['subdir'] . '/' . $filename, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 80 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 75 |  |     public function getSubDir() | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 75 |  |         return $this->test['subdir']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      *  Error handling | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param  array         $test      the subtest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param  HttpResponse  $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return TestResult|null  If no errors, null is returned, otherwise a TestResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 80 |  |     private function standardErrorHandling($test, $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 80 |  |         $bypassErrors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 80 |  |         $byPass = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 80 |  |         if (isset($test['request']['bypass-standard-error-handling'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 35 |  |             $bypassErrors = $test['request']['bypass-standard-error-handling']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 80 |  |         if (in_array($response->statusCode, $bypassErrors) || in_array('all', $bypassErrors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 30 |  |             $byPass = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 80 |  |         if ($byPass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 30 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 71 |  |         switch ($response->statusCode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 71 |  |             case '403': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 9 |  |                 return new TestResult(null, '403 Forbidden'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 62 |  |             case '404': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 return new TestResult(null, '404 Not Found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 62 |  |             case '500': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 10 |  |                 $hct = $this->getHtaccessCapabilityTester(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 // Run innocent request / get it from cache. This sets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 // $statusCodeOfLastRequest, which we need now | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 10 |  |                 $hct->innocentRequestWorks(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 10 |  |                 if ($hct->statusCodeOfLastRequest == '500') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |                     return new TestResult(null, 'Errored with 500. Everything errors with 500.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 8 |  |                     return new TestResult( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 8 |  |                         false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         'Errored with 500. ' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 8 |  |                         'Not all goes 500, so it must be a forbidden directive in the .htaccess' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 52 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      *  Run single test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param  array  $test  the subtest to run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return TestResult  Returns a test result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 80 |  |     private function realRunSubTest($test) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 80 |  |         $requestUrl = $this->baseUrl . '/' . $test['subdir'] . '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 80 |  |         if (isset($test['request']['url'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 46 |  |             $requestUrl .= $test['request']['url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 65 |  |             $requestUrl .= $test['request']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         //echo $requestUrl . '<br>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 80 |  |         $response = $this->makeHttpRequest($requestUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         // Standard error handling | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 80 |  |         $errorResult = $this->standardErrorHandling($test, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 80 |  |         if (!is_null($errorResult)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 19 |  |             return $errorResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 71 |  |         return ResponseInterpreter::interpret($response, $test['interpretation']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      *  Run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @param  string  $baseDir  Directory on the server where the test files can be put | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @param  string  $baseUrl  The base URL of the test files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @return TestResult  Returns a test result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @throws \Exception  In case the test cannot be run due to serious issues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 80 |  |     private function realRun($baseDir, $baseUrl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 80 |  |         $this->prepareForRun($baseDir, $baseUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 80 |  |         $result = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 80 |  |         foreach ($this->tests as $i => $test) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             Disabled, as I'm no longer sure if it is that useful | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             if (isset($test['requirements'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             $hct = $this->getHtaccessCapabilityTester(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             foreach ($test['requirements'] as $requirement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $requirementResult = $hct->callMethod($requirement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 if (!$requirementResult) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                     // Skip test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                     continue 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             }*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 80 |  |             if (isset($test['request'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 80 |  |                 $result = $this->realRunSubTest($test); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 80 |  |                 if ($result->info != 'no-match') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 79 |  |                     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 13 |  |         if (is_null($result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             $result = new TestResult(null, 'Nothing to test!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 13 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      *  Run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * @param  string  $baseDir  Directory on the server where the test files can be put | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param  string  $baseUrl  The base URL of the test files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @return TestResult  Returns a test result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * @throws \Exception  In case the test cannot be run due to serious issues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 80 |  |     public function run($baseDir, $baseUrl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 80 |  |         $testResult = $this->realRun($baseDir, $baseUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         // A test might not create a request if it has an unfulfilled requirement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 80 |  |         if (isset($this->lastHttpResponse)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 80 |  |             $testResult->statusCodeOfLastRequest = $this->lastHttpResponse->statusCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 80 |  |         return $testResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 213 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 214 |  |  |  |