This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Openbuildings\PHPUnitSpiderling; |
||
4 | |||
5 | use PHPUnit\Framework\AssertionFailedError; |
||
6 | use PHPUnit\Framework\Test; |
||
7 | use PHPUnit\Framework\TestListener; |
||
8 | use PHPUnit\Framework\TestSuite; |
||
9 | use PHPUnit\Framework\Warning; |
||
10 | use Throwable; |
||
11 | |||
12 | class SaveOnFailure implements TestListener |
||
0 ignored issues
–
show
|
|||
13 | { |
||
14 | /** |
||
15 | * Convert an attribute strigng from a relative to absolute, by providing a base_url. |
||
16 | * |
||
17 | * @param string $attribute name of the attribute, e.g. src, href |
||
18 | * @param string $content the string where to do the change |
||
19 | * @param string $base_url |
||
20 | * |
||
21 | * @return string |
||
22 | */ |
||
23 | 8 | public static function to_absolute_attribute($attribute, $content, $base_url = null) |
|
24 | { |
||
25 | 8 | return preg_replace('/('.$attribute.'=[\'"])\//', '$1'.rtrim($base_url, '/').'/', $content); |
|
26 | } |
||
27 | |||
28 | /** |
||
29 | * Check if a directory does not exist, create it, otherwise check if it is writable. |
||
30 | * |
||
31 | * @param string $directory |
||
32 | * |
||
33 | * @throws Exception If directory is not writable |
||
34 | */ |
||
35 | 2 | public static function autocreate_directory($directory) |
|
36 | { |
||
37 | 2 | if (!file_exists($directory)) { |
|
38 | 2 | mkdir($directory, 0777, true); |
|
39 | } |
||
40 | |||
41 | 2 | if (!is_writable($directory)) { |
|
42 | throw new \Exception("Directory \"{$directory}\" is not writable"); |
||
43 | } |
||
44 | 2 | } |
|
45 | |||
46 | /** |
||
47 | * Delete all the files from a directory. |
||
48 | * |
||
49 | * @param string $directory |
||
50 | */ |
||
51 | 2 | public static function clear_directory($directory) |
|
52 | { |
||
53 | 2 | foreach (scandir($directory) as $file) { |
|
54 | 2 | if ($file !== '.' and $file !== '..') { |
|
55 | 1 | unlink($directory.$file); |
|
56 | } |
||
57 | } |
||
58 | 2 | } |
|
59 | |||
60 | /** |
||
61 | * Execute a php script and get the output of that script as a string, optionally pass variables as an associative array to be converted to local variables inside of the file. |
||
62 | * |
||
63 | * @param string $filename |
||
64 | * @param array $data |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 1 | public static function render_file($filename, array $data = []) |
|
69 | { |
||
70 | 1 | extract($data, EXTR_SKIP); |
|
71 | |||
72 | 1 | ob_start(); |
|
73 | 1 | include $filename; |
|
74 | |||
75 | 1 | return ob_get_clean(); |
|
76 | } |
||
77 | |||
78 | protected $_directory; |
||
79 | protected $_base_url; |
||
80 | |||
81 | 1 | public function __construct($directory, $base_url) |
|
82 | { |
||
83 | 1 | if (!$directory) { |
|
84 | throw new \Exception('You must set a directory to output errors to'); |
||
85 | } |
||
86 | |||
87 | 1 | $this->_directory = rtrim($directory, \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR; |
|
88 | 1 | $this->_base_url = $base_url; |
|
89 | |||
90 | 1 | self::autocreate_directory($directory); |
|
91 | 1 | self::clear_directory($directory); |
|
92 | 1 | } |
|
93 | |||
94 | /** |
||
95 | * Save the current content of the driver into an html file. Add javascript errors, messages and a title to the html content. |
||
96 | * |
||
97 | * @param \Openbuildings\Spiderling\Driver $driver |
||
98 | * @param string $filename |
||
99 | * @param string $title |
||
100 | */ |
||
101 | 1 | public function save_driver_content(\Openbuildings\Spiderling\Driver $driver, $filename, $title) |
|
102 | { |
||
103 | 1 | $content = $driver->content(); |
|
104 | |||
105 | 1 | foreach (['href', 'action', 'src'] as $attribute) { |
|
106 | 1 | $content = self::to_absolute_attribute($attribute, $content, $this->_base_url); |
|
107 | } |
||
108 | |||
109 | 1 | $testview = self::render_file(__DIR__.'/../assets/error-page.php', [ |
|
110 | 1 | 'url' => $driver->current_url(), |
|
111 | 1 | 'title' => $title, |
|
112 | 1 | 'javascript_errors' => $driver->javascript_errors(), |
|
113 | 1 | 'javascript_messages' => $driver->javascript_messages(), |
|
114 | ]); |
||
115 | |||
116 | 1 | $page_content = str_replace('</body>', $testview.'</body>', $content); |
|
117 | |||
118 | 1 | file_put_contents($this->_directory."/$filename.html", $page_content); |
|
119 | |||
120 | try { |
||
121 | 1 | $driver->screenshot($this->_directory."/$filename.png"); |
|
122 | } catch (\Openbuildings\Spiderling\Exception_Notimplemented $e) { |
||
123 | // Ignore not generating the screenshot if the driver doesn't support it |
||
124 | } |
||
125 | 1 | } |
|
126 | |||
127 | /** |
||
128 | * Implement PHPUnit\Framework\TestListener, save driver content if there was an error. |
||
129 | * |
||
130 | * @param Test $test |
||
131 | * @param Exception $exception |
||
132 | * @param int $time |
||
133 | */ |
||
134 | 1 | public function addError(Test $test, Throwable $exception, float $time): void |
|
135 | { |
||
136 | 1 | if ($test instanceof TestCase and $test->is_driver_active() and $test->driver()->is_page_active()) { |
|
137 | 1 | $this->save_driver_content( |
|
138 | 1 | $test->driver(), |
|
139 | 1 | \get_class($test).'_'.$test->getName(false), |
|
140 | 1 | $exception->getMessage() |
|
141 | ); |
||
142 | } |
||
143 | 1 | } |
|
144 | |||
145 | /** |
||
146 | * Implement PHPUnit\Framework\TestListener, save driver content if there was an error. |
||
147 | * |
||
148 | * @param Test $test |
||
149 | * @param \PHPUnit\Framework\AssertionFailedError $failure |
||
150 | * @param int $time |
||
151 | */ |
||
152 | 1 | public function addFailure(Test $test, AssertionFailedError $failure, float $time): void |
|
153 | { |
||
154 | 1 | if ($test instanceof TestCase and $test->is_driver_active() and $test->driver()->is_page_active()) { |
|
155 | 1 | $this->save_driver_content( |
|
156 | 1 | $test->driver(), |
|
157 | 1 | \get_class($test).'_'.$test->getName(false), |
|
158 | 1 | $failure->getMessage() |
|
159 | ); |
||
160 | } |
||
161 | 1 | } |
|
162 | |||
163 | // @codeCoverageIgnoreStart |
||
164 | public function addWarning(Test $test, Warning $e, float $time): void |
||
165 | { |
||
166 | } |
||
167 | |||
168 | public function addRiskyTest(Test $test, Throwable $e, float $time): void |
||
169 | { |
||
170 | } |
||
171 | |||
172 | public function addIncompleteTest(Test $test, Throwable $e, float $time): void |
||
173 | { |
||
174 | } |
||
175 | |||
176 | public function addSkippedTest(Test $test, Throwable $e, float $time): void |
||
177 | { |
||
178 | } |
||
179 | |||
180 | public function startTest(Test $test): void |
||
181 | { |
||
182 | } |
||
183 | |||
184 | public function endTest(Test $test, float $time): void |
||
185 | { |
||
186 | } |
||
187 | |||
188 | public function startTestSuite(TestSuite $suite): void |
||
189 | { |
||
190 | } |
||
191 | |||
192 | public function endTestSuite(TestSuite $suite): void |
||
193 | { |
||
194 | } |
||
195 | |||
196 | // @codeCoverageIgnoreEnd |
||
197 | } |
||
198 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.