This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests_html.php
324 lines (284 loc) · 6.53 KB
/
tests_html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* Tests for testing/auditing the html code of the MOD
*
* @package mpv
* @version $Id$
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*
*/
/**
* Collection of tests which are ran on the MOD's code
*
* @package mpv
* @subpackage tests
*/
class mpv_tests_html
{
/**
* mpv (validator) object
*
* @access private
* @var object
*/
private $validator;
/**
* Terminate the testing process
*
* @access private
* @var bool
*/
private $terminate;
/**
* Array containing the names of the tests which failed
*
* @access private
* @var array
*/
private $failed_tests;
/**
* The filename of the file we're currently testing
*
* @access private
* @var string
*/
private $file_name;
/**
* The contents of the file we're currently testing
*
* @access private
* @var string
*/
private $file_contents;
/**
* The content of the file we're testing, but as array
*
* @access private
* @var array
*/
private $file_contents_file;
/**
* Constructor
*
* @access public
* @param object mpv object
* @return void
*/
public function __construct(mpv $validator)
{
$this->validator = $validator;
$this->terminate = false;
$this->failed_tests = array();
}
/**
* Run the tests in this collection
*
* @access public
* @return void
*/
public function run()
{
$test_methods = get_class_methods($this);
$files = array();
if (is_array($this->validator->package_files))
{
$files = array_merge($files, $this->validator->package_files);
}
if (is_array($this->validator->modx_files))
{
$files = array_merge($files, $this->validator->modx_files);
}
foreach ($files as $package_file)
{
if (mpv::check_unwanted($package_file))
{
continue;
}
// Only test html, php, xml files
if (!in_array(strrchr($package_file, '.'), array('.php', '.xml', '.html')))
{
continue;
}
$this->failed_tests = array();
$this->file_name = $package_file;
$this->file_contents = file_get_contents($this->validator->temp_dir . $package_file);
$this->file_contents_file = file($this->validator->temp_dir . $package_file);
foreach ($test_methods as $method)
{
if (substr($method, 0, 5) == 'test_')
{
if (!$this->$method() || $this->terminate)
{
$this->failed_tests[] = substr($method, 5);
}
if ($this->terminate)
{
unset($this->file_contents, $this->file_contents_file);
return;
}
}
}
unset($this->file_contents, $this->file_contents_file);
}
}
/**
* Test for non closed BR tags
*
* @return bool
*/
private function test_br()
{
$return = true;
if (preg_match('#<br(\s+)?>#', $this->file_contents))
{
$return = $this->display_line_code(mpv::ERROR_FAIL, 'USAGE_BR_NON_CLOSED', false, '#<br(\s+)?>#');
}
return $return;
}
/**
* Test for non closed IMG tags
*
* @return bool
*/
private function test_img()
{
$return = true;
/*
* Disable this check for now, it doesnt detect it correctly atm.
if (preg_match('#<img\s.+(?<!/)>#', $this->file_contents))
{
$return = $this->display_line_code(mpv::ERROR_FAIL, 'USAGE_IMG_NON_CLOSED', false, '#<img\s.+(?<!/)>#');
}
*/
return $return;
}
/**
* Wrapper around $this->validator->push_error
*
* @access private
* @param int Error type
* @param string Message
* @param mixed Optional array of sprintf() values, or a non-array for passing one single value
* @return void
*/
private function push_error($type, $message, $sprintf_args = null)
{
$this->validator->push_error($type, $message, $this->file_name, $sprintf_args);
}
/**
* Displays a inline code block with the line of wrong code.
*
* @access private
* @param int Error type
* @param string Message
* @param string String to use in strpos
* @param string String to use in preg_match
* @param array array that contains words that should not be in the line
* @return bool
*/
private function display_line_code($type, $message, $strpos, $preg_match = false, $ignore_in_line = array())
{
$found = false;
$in_comment = false;
if (!is_array($ignore_in_line))
{
if (is_string($ignore_in_line))
{
$ignore_in_line = array(0 => $ignore_in_line);
}
else
{
$ignore_in_line = array();
}
}
foreach ($this->file_contents_file as $line => $content)
{
$content_new = $content;
$loc = strpos($content, '*/');
if ($in_comment && $loc === false)
{
$content_new = '';
}
else if ($in_comment && $loc !== false)
{
// Need to replace everything till */
$total = strlen($content_new);
$negative = $total - $loc;
$total = $total - $negative;
$content_new = substr($content_new, ($loc + 2));
$in_comment = false;
}
else if(!$in_comment && strpos($content, '/*') !== false)
{
if ($loc !== false) // Used as inline
{
$content_new = preg_replace('#/\*(.*)\*/#si', '', $content_new);
}
else
{
$in_comment = true;
$content_new = substr($content_new, 0, strpos($content, '/*'));
}
}
$loc = strpos($content_new, '//');
if ($loc !== false)
{
$content_new = substr($content_new, 0, $loc + 2);
}
foreach ($ignore_in_line as $value)
{
if (strpos($content_new, $value) !== false)
{
$content_new = '';
}
}
// Yes, $content_new in if, and $content in code.
// This is because we want comments still being displayed ;)
if (!$preg_match && strpos($content_new, $strpos) !== false)
{
$this->push_error($type, $message, array((string) ($line + 1) , '[code]' . trim($content) . '[/code]'));
$found = true;
}
else if ($preg_match && preg_match($preg_match, $content))
{
$this->push_error($type, $message, array((string) ($line + 1) , '[code]' . trim($content) . '[/code]'));
$found = true;
}
}
return $found;
}
/**
* Terminate all further testing; used if failing one test could result in other tests malfunctioning
*
* @access private
* @return bool
*/
private function terminate()
{
$this->terminate = true;
$this->push_error(mpv::ERROR_WARNING, 'TESTING_TERMINATED');
return false;
}
/**
* Check whether a given test failed
*
* @access private
* @param string Test name
* @return bool
*/
private function failed_test($test_name)
{
return in_array($test_name, $this->failed_tests);
}
/**
* Returns a array with failed tests
*
* @access public
* @return array
*/
public function return_failed_tests()
{
return $this->failed_tests;
}
}