Commit | Line | Data |
---|---|---|
b42b2bf9 H |
1 | <?php |
2 | ||
3 | require_once './config.php'; | |
4 | require_once SMARTY_DIR . 'Smarty.class.php'; | |
5 | require_once 'PHPUnit.php'; | |
6 | ||
7 | class Obj { | |
8 | var $val = 'val'; | |
9 | var $arr = array('one' => 'one', 'two' => 2); | |
10 | var $ten = 10; | |
11 | ||
12 | function meth($a="a", $b="b") { | |
13 | return "$a:$b"; | |
14 | } | |
15 | ||
16 | function six() { | |
17 | return 6; | |
18 | } | |
19 | } | |
20 | ||
21 | ||
22 | class SmartyTest extends PHPUnit_TestCase { | |
23 | // contains the object handle of the string class | |
24 | var $abc; | |
25 | // contains the last triggered error's errorlevel | |
26 | var $errorlevel; | |
27 | ||
28 | // constructor of the test suite | |
29 | function SmartyTest($name) { | |
30 | $this->PHPUnit_TestCase($name); | |
31 | } | |
32 | ||
33 | // called before the test functions will be executed | |
34 | // this function is defined in PHPUnit_TestCase and overwritten | |
35 | // here | |
36 | function setUp() { | |
37 | // create a new instance of String with the | |
38 | // string 'abc' | |
39 | $this->smarty = new Smarty; | |
40 | } | |
41 | // called after the test functions are executed | |
42 | // this function is defined in PHPUnit_TestCase and overwritten | |
43 | // here | |
44 | function tearDown() { | |
45 | // delete your instance | |
46 | unset($this->smarty); | |
47 | } | |
48 | ||
49 | // dummy errorhandler for functions that are supposed to call trigger_error() | |
50 | function error_handler($errorlevel) { | |
51 | if ($errorlevel) $this->errorlevel = $errorlevel; | |
52 | } | |
53 | ||
54 | /* DIRECTORY TESTS */ | |
55 | ||
56 | // test that template_dir exists | |
57 | function test_template_dir_exists() { | |
58 | $this->assertTrue(file_exists($this->smarty->template_dir)); | |
59 | } | |
60 | // test that template_dir is a directory | |
61 | function test_template_dir_is_dir() { | |
62 | $this->assertTrue(is_dir($this->smarty->template_dir)); | |
63 | } | |
64 | // test that template_dir is readable | |
65 | function test_template_dir_is_readable() { | |
66 | $this->assertTrue(is_readable($this->smarty->template_dir)); | |
67 | } | |
68 | // test that config_dir exists | |
69 | function test_config_dir_exists() { | |
70 | $this->assertTrue(file_exists($this->smarty->config_dir)); | |
71 | } | |
72 | // test that config_dir is a directory | |
73 | function test_config_dir_is_dir() { | |
74 | $this->assertTrue(is_dir($this->smarty->config_dir)); | |
75 | } | |
76 | // test that config_dir is readable | |
77 | function test_config_dir_is_readable() { | |
78 | $this->assertTrue(is_readable($this->smarty->config_dir)); | |
79 | } | |
80 | // test that compile_dir exists | |
81 | function test_compile_dir_exists() { | |
82 | $this->assertTrue(file_exists($this->smarty->compile_dir)); | |
83 | } | |
84 | // test that compile_dir is a directory | |
85 | function test_compile_dir_is_dir() { | |
86 | $this->assertTrue(is_dir($this->smarty->compile_dir)); | |
87 | } | |
88 | // test that compile_dir is readable | |
89 | function test_compile_dir_is_readable() { | |
90 | $this->assertTrue(is_readable($this->smarty->compile_dir)); | |
91 | } | |
92 | // test that compile_dir is writable | |
93 | function test_compile_dir_is_writable() { | |
94 | $this->assertTrue(is_writable($this->smarty->compile_dir)); | |
95 | } | |
96 | // test that cache_dir exists | |
97 | function test_cache_dir_exists() { | |
98 | $this->assertTrue(file_exists($this->smarty->cache_dir)); | |
99 | } | |
100 | // test that cache_dir is a directory | |
101 | function test_cache_dir_is_dir() { | |
102 | $this->assertTrue(is_dir($this->smarty->cache_dir)); | |
103 | } | |
104 | // test that cache_dir is readable | |
105 | function test_cache_dir_is_readable() { | |
106 | $this->assertTrue(is_readable($this->smarty->cache_dir)); | |
107 | } | |
108 | // test that cache_dir is writable | |
109 | function test_cache_dir_is_writable() { | |
110 | $this->assertTrue(is_writable($this->smarty->cache_dir)); | |
111 | } | |
112 | ||
113 | /* METHOD EXISTS TESTS */ | |
114 | function test_assign_method_exists() { | |
115 | $this->assertTrue(method_exists($this->smarty, 'assign')); | |
116 | } | |
117 | function test_assign_by_ref_method_exists() { | |
118 | $this->assertTrue(method_exists($this->smarty, 'assign_by_ref')); | |
119 | } | |
120 | function test_append_method_exists() { | |
121 | $this->assertTrue(method_exists($this->smarty, 'append')); | |
122 | } | |
123 | function test_append_by_ref_method_exists() { | |
124 | $this->assertTrue(method_exists($this->smarty, 'append_by_ref')); | |
125 | } | |
126 | function test_clear_assign_method_exists() { | |
127 | $this->assertTrue(method_exists($this->smarty, 'clear_assign')); | |
128 | } | |
129 | function test_register_function_method_exists() { | |
130 | $this->assertTrue(method_exists($this->smarty, 'register_function')); | |
131 | } | |
132 | function test_unregister_function_method_exists() { | |
133 | $this->assertTrue(method_exists($this->smarty, 'unregister_function')); | |
134 | } | |
135 | function test_register_object_method_exists() { | |
136 | $this->assertTrue(method_exists($this->smarty, 'register_object')); | |
137 | } | |
138 | function test_unregister_object_method_exists() { | |
139 | $this->assertTrue(method_exists($this->smarty, 'unregister_object')); | |
140 | } | |
141 | function test_register_block_method_exists() { | |
142 | $this->assertTrue(method_exists($this->smarty, 'register_block')); | |
143 | } | |
144 | function test_unregister_block_method_exists() { | |
145 | $this->assertTrue(method_exists($this->smarty, 'unregister_block')); | |
146 | } | |
147 | function test_register_compiler_function_method_exists() { | |
148 | $this->assertTrue(method_exists($this->smarty, 'register_compiler_function')); | |
149 | } | |
150 | function test_unregister_compiler_function_method_exists() { | |
151 | $this->assertTrue(method_exists($this->smarty, 'unregister_compiler_function')); | |
152 | } | |
153 | function test_register_modifier_method_exists() { | |
154 | $this->assertTrue(method_exists($this->smarty, 'register_modifier')); | |
155 | } | |
156 | function test_unregister_modifier_method_exists() { | |
157 | $this->assertTrue(method_exists($this->smarty, 'unregister_modifier')); | |
158 | } | |
159 | function test_register_resource_method_exists() { | |
160 | $this->assertTrue(method_exists($this->smarty, 'register_resource')); | |
161 | } | |
162 | function test_unregister_resource_method_exists() { | |
163 | $this->assertTrue(method_exists($this->smarty, 'unregister_resource')); | |
164 | } | |
165 | function test_register_prefilter_method_exists() { | |
166 | $this->assertTrue(method_exists($this->smarty, 'register_prefilter')); | |
167 | } | |
168 | function test_unregister_prefilter_method_exists() { | |
169 | $this->assertTrue(method_exists($this->smarty, 'unregister_prefilter')); | |
170 | } | |
171 | function test_register_postfilter_method_exists() { | |
172 | $this->assertTrue(method_exists($this->smarty, 'register_postfilter')); | |
173 | } | |
174 | function test_unregister_postfilter_method_exists() { | |
175 | $this->assertTrue(method_exists($this->smarty, 'unregister_postfilter')); | |
176 | } | |
177 | function test_register_outputfilter_method_exists() { | |
178 | $this->assertTrue(method_exists($this->smarty, 'register_outputfilter')); | |
179 | } | |
180 | function test_unregister_outputfilter_method_exists() { | |
181 | $this->assertTrue(method_exists($this->smarty, 'unregister_outputfilter')); | |
182 | } | |
183 | function test_load_filter_method_exists() { | |
184 | $this->assertTrue(method_exists($this->smarty, 'load_filter')); | |
185 | } | |
186 | function test_clear_cache_method_exists() { | |
187 | $this->assertTrue(method_exists($this->smarty, 'clear_cache')); | |
188 | } | |
189 | function test_clear_all_cache_method_exists() { | |
190 | $this->assertTrue(method_exists($this->smarty, 'clear_all_cache')); | |
191 | } | |
192 | function test_is_cached_method_exists() { | |
193 | $this->assertTrue(method_exists($this->smarty, 'is_cached')); | |
194 | } | |
195 | function test_clear_all_assign_method_exists() { | |
196 | $this->assertTrue(method_exists($this->smarty, 'clear_all_assign')); | |
197 | } | |
198 | function test_clear_compiled_tpl_method_exists() { | |
199 | $this->assertTrue(method_exists($this->smarty, 'clear_compiled_tpl')); | |
200 | } | |
201 | function test_template_exists_method_exists() { | |
202 | $this->assertTrue(method_exists($this->smarty, 'template_exists')); | |
203 | } | |
204 | function test_get_template_vars_method_exists() { | |
205 | $this->assertTrue(method_exists($this->smarty, 'get_template_vars')); | |
206 | } | |
207 | function test_get_config_vars_method_exists() { | |
208 | $this->assertTrue(method_exists($this->smarty, 'get_config_vars')); | |
209 | } | |
210 | function test_trigger_error_method_exists() { | |
211 | $this->assertTrue(method_exists($this->smarty, 'trigger_error')); | |
212 | } | |
213 | function test_display_method_exists() { | |
214 | $this->assertTrue(method_exists($this->smarty, 'display')); | |
215 | } | |
216 | function test_fetch_method_exists() { | |
217 | $this->assertTrue(method_exists($this->smarty, 'fetch')); | |
218 | } | |
219 | function test_config_load_method_exists() { | |
220 | $this->assertTrue(method_exists($this->smarty, 'config_load')); | |
221 | } | |
222 | function test_get_registered_object_method_exists() { | |
223 | $this->assertTrue(method_exists($this->smarty, 'get_registered_object')); | |
224 | } | |
225 | function test_clear_config_method_exists() { | |
226 | $this->assertTrue(method_exists($this->smarty, 'clear_config')); | |
227 | } | |
228 | function test_get_plugin_filepath() { | |
229 | $this->assertTrue(method_exists($this->smarty, '_get_plugin_filepath')); | |
230 | } | |
231 | ||
232 | ||
233 | function test_clear_compiled_tpl() { | |
234 | $this->assertTrue($this->smarty->clear_compiled_tpl()); | |
235 | } | |
236 | ||
237 | /* DISPLAY TESTS */ | |
238 | ||
239 | // test that display() executes properly | |
240 | function test_call_to_display() { | |
241 | ob_start(); | |
242 | $this->smarty->display('index.tpl'); | |
243 | $output = ob_get_contents(); | |
244 | ob_end_clean(); | |
245 | $this->assertEquals($output, 'TEST STRING'); | |
246 | } | |
247 | ||
248 | /* FETCH TESTS */ | |
249 | ||
250 | // test that fetch() executes properly | |
251 | function test_call_to_fetch() { | |
252 | $this->assertEquals($this->smarty->fetch('index.tpl'), 'TEST STRING'); | |
253 | } | |
254 | ||
255 | /* ASSIGN TESTS */ | |
256 | ||
257 | // test assigning a simple template variable | |
258 | function test_assign_var() { | |
259 | $this->smarty->assign('foo', 'bar'); | |
260 | $this->assertEquals($this->smarty->fetch('assign_var.tpl'), 'bar'); | |
261 | } | |
262 | ||
263 | /* PARSING TESTS */ | |
264 | ||
265 | // test assigning and calling an object | |
266 | function test_parse_obj_meth() { | |
267 | $obj = new Obj(); | |
268 | $this->smarty->assign('obj', $obj); | |
269 | $this->smarty->assign('foo', 'foo'); | |
270 | $this->assertEquals('foo:2.5 | |
271 | 2.5:foo | |
272 | 2.5:b | |
273 | val:foo | |
274 | foo:val | |
275 | foo:foo | |
276 | one:2 | |
277 | foo:foo:b', $this->smarty->fetch('parse_obj_meth.tpl')); | |
278 | } | |
279 | ||
280 | // test assigning and calling an object | |
281 | function test_parse_math() { | |
282 | $obj = new Obj(); | |
283 | $this->smarty->assign('obj', $obj); | |
284 | $this->smarty->assign('flt', 2.5); | |
285 | $this->smarty->assign('items', array(1, 2)); | |
286 | $this->assertEquals('3 | |
287 | 3.5 | |
288 | 7 | |
289 | 11 | |
290 | 4 | |
291 | 4.5 | |
292 | 8 | |
293 | 12 | |
294 | 12.5 | |
295 | 25 | |
296 | 16 | |
297 | 20 | |
298 | 8.5 | |
299 | 7', $this->smarty->fetch('parse_math.tpl')); | |
300 | } | |
301 | ||
302 | /* CONFIG FILE TESTS */ | |
303 | ||
304 | // test assigning a double quoted global variable | |
305 | function test_config_load_globals_double_quotes() { | |
306 | // load the global var | |
307 | $this->smarty->config_load('globals_double_quotes.conf'); | |
308 | // test that it is assigned | |
309 | $this->assertEquals($this->smarty->_config[0]['vars']['foo'], 'bar'); | |
310 | } | |
311 | ||
312 | // test assigning a single quoted global variable | |
313 | function test_config_load_globals_single_quotes() { | |
314 | // load the global var | |
315 | $this->smarty->config_load('globals_single_quotes.conf'); | |
316 | // test that it is assigned | |
317 | $this->assertEquals($this->smarty->_config[0]['vars']['foo'], 'bar'); | |
318 | } | |
319 | ||
320 | // test loading and running modifier.escape.php | |
321 | function test_escape_modifier_get_plugins_filepath() { | |
322 | $filepath = $this->smarty->_get_plugin_filepath('modifier', 'escape'); | |
323 | $this->assertTrue($filepath); | |
324 | } | |
325 | ||
326 | function test_escape_modifier_include_file() { | |
327 | $filepath = $this->smarty->_get_plugin_filepath('modifier', 'escape'); | |
328 | $this->assertTrue(include($filepath)); | |
329 | } | |
330 | ||
331 | function test_escape_modifier_function_exists() { | |
332 | $this->assertTrue(function_exists('smarty_modifier_escape')); | |
333 | } | |
334 | ||
335 | function test_escape_modifier_escape_default() { | |
336 | $string = smarty_modifier_escape("<html><body></body></html>"); | |
337 | $this->assertEquals('<html><body></body></html>', | |
338 | $string); | |
339 | } | |
340 | ||
341 | function test_escape_modifier_escape_html() { | |
342 | $string = smarty_modifier_escape("<html><body></body></html>", 'html'); | |
343 | $this->assertEquals('<html><body></body></html>', | |
344 | $string); | |
345 | } | |
346 | ||
347 | function test_escape_modifier_escape_htmlall() { | |
348 | $string = smarty_modifier_escape("<html><body></body></html>", 'htmlall'); | |
349 | $this->assertEquals('<html><body></body></html>', | |
350 | $string); | |
351 | } | |
352 | ||
353 | function test_escape_modifier_escape_url() { | |
354 | $string = smarty_modifier_escape("http://test.com?foo=bar", 'url'); | |
355 | $this->assertEquals('http%3A%2F%2Ftest.com%3Ffoo%3Dbar', $string); | |
356 | } | |
357 | ||
358 | function test_escape_modifier_escape_quotes() { | |
359 | $string = smarty_modifier_escape("'\\'\\''", 'quotes'); | |
360 | $this->assertEquals("\\'\\'\\'\\'", $string); | |
361 | } | |
362 | ||
363 | function test_escape_modifier_escape_hex() { | |
364 | $string = smarty_modifier_escape("abcd", 'hex'); | |
365 | $this->assertEquals('%61%62%63%64', $string); | |
366 | } | |
367 | ||
368 | function test_escape_modifier_escape_hexentity() { | |
369 | $string = smarty_modifier_escape("ABCD", 'hexentity'); | |
370 | $this->assertEquals('ABCD', $string); | |
371 | } | |
372 | ||
373 | function test_escape_modifier_escape_javascript() { | |
374 | $string = smarty_modifier_escape("\r\n\\", 'javascript'); | |
375 | $this->assertEquals('\\r\\n\\\\', $string); | |
376 | } | |
377 | ||
378 | ||
379 | function test_core_is_secure_file_exists() { | |
380 | $file = SMARTY_CORE_DIR . 'core.is_secure.php'; | |
381 | $this->assertTrue(file_exists($file)); | |
382 | } | |
383 | ||
384 | function test_core_is_secure_file_include() { | |
385 | $file = SMARTY_CORE_DIR . 'core.is_secure.php'; | |
386 | $this->assertTrue(include($file)); | |
387 | } | |
388 | ||
389 | function test_core_is_secure_function_exists() { | |
390 | $this->assertTrue(function_exists('smarty_core_is_secure')); | |
391 | } | |
392 | ||
393 | function test_core_is_secure_function_is_secure_true() { | |
394 | $security = $this->smarty->security; | |
395 | $this->smarty->security = true; | |
396 | ||
397 | /* check if index.tpl is secure (should be true) */ | |
398 | $params = array('resource_type' => 'file', | |
399 | 'resource_base_path' => dirname(__FILE__) . '/templates', | |
400 | 'resource_name' => dirname(__FILE__) . '/templates/index.tpl'); | |
401 | $this->assertTrue(smarty_core_is_secure($params, $this->smarty)); | |
402 | $this->smarty->security = $security; | |
403 | } | |
404 | ||
405 | function test_core_is_secure_function_is_secure_false() { | |
406 | $security = $this->smarty->security; | |
407 | $this->smarty->security = true; | |
408 | /* check if test_cases.php is secure (should be false) */ | |
409 | $params = array('resource_type' => 'file', | |
410 | 'resource_base_path' => dirname(__FILE__) . '/templates', | |
411 | 'resource_name' => __FILE__); | |
412 | $this->assertFalse(smarty_core_is_secure($params, $this->smarty)); | |
413 | $this->smarty->security = $security; | |
414 | ||
415 | } | |
416 | ||
417 | // test constants and security | |
418 | function test_core_is_secure_function_smarty_var_const() { | |
419 | define('TEST_CONSTANT', 'test constant'); | |
420 | $this->assertEquals('test constant', $this->smarty->fetch('constant.tpl', | |
421 | null, 'var_const')); | |
422 | } | |
423 | ||
424 | function test_core_is_secure_function_smarty_var_const_allowed() { | |
425 | $security = $this->smarty->security; | |
426 | $security_settings = $this->smarty->security_settings; | |
427 | $this->smarty->security_settings['ALLOW_CONSTANTS'] = true; | |
428 | $this->smarty->security = true; | |
429 | $this->assertEquals('test constant', $this->smarty->fetch('constant.tpl', | |
430 | null, 'var_const_allowed')); | |
431 | $this->smarty->security_settings = $security_settings; | |
432 | $this->smarty->security = $security; | |
433 | } | |
434 | ||
435 | function test_core_is_secure_function_smarty_var_const_not_allowed() { | |
436 | $security = $this->smarty->security; | |
437 | $this->smarty->security = true; | |
438 | /* catch errors: */ | |
439 | $this->errorlevel = null; | |
440 | set_error_handler(array(&$this, 'error_handler')); | |
441 | $this->smarty->fetch('constant.tpl', null, 'var_const_not_allowed'); | |
442 | restore_error_handler(); | |
443 | ||
444 | $this->assertEquals( $this->errorlevel, E_USER_WARNING); | |
445 | $this->smarty->security = $security; | |
446 | } | |
447 | ||
448 | } | |
449 | ||
450 | ?> |