3 require_once './config.php';
4 require_once SMARTY_DIR
. 'Smarty.class.php';
5 require_once 'PHPUnit.php';
9 var $arr = array('one' => 'one', 'two' => 2);
12 function meth($a="a", $b="b") {
22 class SmartyTest
extends PHPUnit_TestCase
{
23 // contains the object handle of the string class
25 // contains the last triggered error's errorlevel
28 // constructor of the test suite
29 function SmartyTest($name) {
30 $this->PHPUnit_TestCase($name);
33 // called before the test functions will be executed
34 // this function is defined in PHPUnit_TestCase and overwritten
37 // create a new instance of String with the
39 $this->smarty
= new Smarty
;
41 // called after the test functions are executed
42 // this function is defined in PHPUnit_TestCase and overwritten
45 // delete your instance
49 // dummy errorhandler for functions that are supposed to call trigger_error()
50 function error_handler($errorlevel) {
51 if ($errorlevel) $this->errorlevel
= $errorlevel;
56 // test that template_dir exists
57 function test_template_dir_exists() {
58 $this->assertTrue(file_exists($this->smarty
->template_dir
));
60 // test that template_dir is a directory
61 function test_template_dir_is_dir() {
62 $this->assertTrue(is_dir($this->smarty
->template_dir
));
64 // test that template_dir is readable
65 function test_template_dir_is_readable() {
66 $this->assertTrue(is_readable($this->smarty
->template_dir
));
68 // test that config_dir exists
69 function test_config_dir_exists() {
70 $this->assertTrue(file_exists($this->smarty
->config_dir
));
72 // test that config_dir is a directory
73 function test_config_dir_is_dir() {
74 $this->assertTrue(is_dir($this->smarty
->config_dir
));
76 // test that config_dir is readable
77 function test_config_dir_is_readable() {
78 $this->assertTrue(is_readable($this->smarty
->config_dir
));
80 // test that compile_dir exists
81 function test_compile_dir_exists() {
82 $this->assertTrue(file_exists($this->smarty
->compile_dir
));
84 // test that compile_dir is a directory
85 function test_compile_dir_is_dir() {
86 $this->assertTrue(is_dir($this->smarty
->compile_dir
));
88 // test that compile_dir is readable
89 function test_compile_dir_is_readable() {
90 $this->assertTrue(is_readable($this->smarty
->compile_dir
));
92 // test that compile_dir is writable
93 function test_compile_dir_is_writable() {
94 $this->assertTrue(is_writable($this->smarty
->compile_dir
));
96 // test that cache_dir exists
97 function test_cache_dir_exists() {
98 $this->assertTrue(file_exists($this->smarty
->cache_dir
));
100 // test that cache_dir is a directory
101 function test_cache_dir_is_dir() {
102 $this->assertTrue(is_dir($this->smarty
->cache_dir
));
104 // test that cache_dir is readable
105 function test_cache_dir_is_readable() {
106 $this->assertTrue(is_readable($this->smarty
->cache_dir
));
108 // test that cache_dir is writable
109 function test_cache_dir_is_writable() {
110 $this->assertTrue(is_writable($this->smarty
->cache_dir
));
113 /* METHOD EXISTS TESTS */
114 function test_assign_method_exists() {
115 $this->assertTrue(method_exists($this->smarty
, 'assign'));
117 function test_assign_by_ref_method_exists() {
118 $this->assertTrue(method_exists($this->smarty
, 'assign_by_ref'));
120 function test_append_method_exists() {
121 $this->assertTrue(method_exists($this->smarty
, 'append'));
123 function test_append_by_ref_method_exists() {
124 $this->assertTrue(method_exists($this->smarty
, 'append_by_ref'));
126 function test_clear_assign_method_exists() {
127 $this->assertTrue(method_exists($this->smarty
, 'clear_assign'));
129 function test_register_function_method_exists() {
130 $this->assertTrue(method_exists($this->smarty
, 'register_function'));
132 function test_unregister_function_method_exists() {
133 $this->assertTrue(method_exists($this->smarty
, 'unregister_function'));
135 function test_register_object_method_exists() {
136 $this->assertTrue(method_exists($this->smarty
, 'register_object'));
138 function test_unregister_object_method_exists() {
139 $this->assertTrue(method_exists($this->smarty
, 'unregister_object'));
141 function test_register_block_method_exists() {
142 $this->assertTrue(method_exists($this->smarty
, 'register_block'));
144 function test_unregister_block_method_exists() {
145 $this->assertTrue(method_exists($this->smarty
, 'unregister_block'));
147 function test_register_compiler_function_method_exists() {
148 $this->assertTrue(method_exists($this->smarty
, 'register_compiler_function'));
150 function test_unregister_compiler_function_method_exists() {
151 $this->assertTrue(method_exists($this->smarty
, 'unregister_compiler_function'));
153 function test_register_modifier_method_exists() {
154 $this->assertTrue(method_exists($this->smarty
, 'register_modifier'));
156 function test_unregister_modifier_method_exists() {
157 $this->assertTrue(method_exists($this->smarty
, 'unregister_modifier'));
159 function test_register_resource_method_exists() {
160 $this->assertTrue(method_exists($this->smarty
, 'register_resource'));
162 function test_unregister_resource_method_exists() {
163 $this->assertTrue(method_exists($this->smarty
, 'unregister_resource'));
165 function test_register_prefilter_method_exists() {
166 $this->assertTrue(method_exists($this->smarty
, 'register_prefilter'));
168 function test_unregister_prefilter_method_exists() {
169 $this->assertTrue(method_exists($this->smarty
, 'unregister_prefilter'));
171 function test_register_postfilter_method_exists() {
172 $this->assertTrue(method_exists($this->smarty
, 'register_postfilter'));
174 function test_unregister_postfilter_method_exists() {
175 $this->assertTrue(method_exists($this->smarty
, 'unregister_postfilter'));
177 function test_register_outputfilter_method_exists() {
178 $this->assertTrue(method_exists($this->smarty
, 'register_outputfilter'));
180 function test_unregister_outputfilter_method_exists() {
181 $this->assertTrue(method_exists($this->smarty
, 'unregister_outputfilter'));
183 function test_load_filter_method_exists() {
184 $this->assertTrue(method_exists($this->smarty
, 'load_filter'));
186 function test_clear_cache_method_exists() {
187 $this->assertTrue(method_exists($this->smarty
, 'clear_cache'));
189 function test_clear_all_cache_method_exists() {
190 $this->assertTrue(method_exists($this->smarty
, 'clear_all_cache'));
192 function test_is_cached_method_exists() {
193 $this->assertTrue(method_exists($this->smarty
, 'is_cached'));
195 function test_clear_all_assign_method_exists() {
196 $this->assertTrue(method_exists($this->smarty
, 'clear_all_assign'));
198 function test_clear_compiled_tpl_method_exists() {
199 $this->assertTrue(method_exists($this->smarty
, 'clear_compiled_tpl'));
201 function test_template_exists_method_exists() {
202 $this->assertTrue(method_exists($this->smarty
, 'template_exists'));
204 function test_get_template_vars_method_exists() {
205 $this->assertTrue(method_exists($this->smarty
, 'get_template_vars'));
207 function test_get_config_vars_method_exists() {
208 $this->assertTrue(method_exists($this->smarty
, 'get_config_vars'));
210 function test_trigger_error_method_exists() {
211 $this->assertTrue(method_exists($this->smarty
, 'trigger_error'));
213 function test_display_method_exists() {
214 $this->assertTrue(method_exists($this->smarty
, 'display'));
216 function test_fetch_method_exists() {
217 $this->assertTrue(method_exists($this->smarty
, 'fetch'));
219 function test_config_load_method_exists() {
220 $this->assertTrue(method_exists($this->smarty
, 'config_load'));
222 function test_get_registered_object_method_exists() {
223 $this->assertTrue(method_exists($this->smarty
, 'get_registered_object'));
225 function test_clear_config_method_exists() {
226 $this->assertTrue(method_exists($this->smarty
, 'clear_config'));
228 function test_get_plugin_filepath() {
229 $this->assertTrue(method_exists($this->smarty
, '_get_plugin_filepath'));
233 function test_clear_compiled_tpl() {
234 $this->assertTrue($this->smarty
->clear_compiled_tpl());
239 // test that display() executes properly
240 function test_call_to_display() {
242 $this->smarty
->display('index.tpl');
243 $output = ob_get_contents();
245 $this->assertEquals($output, 'TEST STRING');
250 // test that fetch() executes properly
251 function test_call_to_fetch() {
252 $this->assertEquals($this->smarty
->fetch('index.tpl'), 'TEST STRING');
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');
265 // test assigning and calling an object
266 function test_parse_obj_meth() {
268 $this->smarty
->assign('obj', $obj);
269 $this->smarty
->assign('foo', 'foo');
270 $this->assertEquals('foo:2.5
277 foo:foo:b', $this->smarty
->fetch('parse_obj_meth.tpl'));
280 // test assigning and calling an object
281 function test_parse_math() {
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
299 7', $this->smarty
->fetch('parse_math.tpl'));
302 /* CONFIG FILE TESTS */
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');
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');
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);
326 function test_escape_modifier_include_file() {
327 $filepath = $this->smarty
->_get_plugin_filepath('modifier', 'escape');
328 $this->assertTrue(include($filepath));
331 function test_escape_modifier_function_exists() {
332 $this->assertTrue(function_exists('smarty_modifier_escape'));
335 function test_escape_modifier_escape_default() {
336 $string = smarty_modifier_escape("<html><body></body></html>");
337 $this->assertEquals('<html><body></body></html>',
341 function test_escape_modifier_escape_html() {
342 $string = smarty_modifier_escape("<html><body></body></html>", 'html');
343 $this->assertEquals('<html><body></body></html>',
347 function test_escape_modifier_escape_htmlall() {
348 $string = smarty_modifier_escape("<html><body></body></html>", 'htmlall');
349 $this->assertEquals('<html><body></body></html>',
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);
358 function test_escape_modifier_escape_quotes() {
359 $string = smarty_modifier_escape("'\\'\\''", 'quotes');
360 $this->assertEquals("\\'\\'\\'\\'", $string);
363 function test_escape_modifier_escape_hex() {
364 $string = smarty_modifier_escape("abcd", 'hex');
365 $this->assertEquals('%61%62%63%64', $string);
368 function test_escape_modifier_escape_hexentity() {
369 $string = smarty_modifier_escape("ABCD", 'hexentity');
370 $this->assertEquals('ABCD', $string);
373 function test_escape_modifier_escape_javascript() {
374 $string = smarty_modifier_escape("\r\n\\", 'javascript');
375 $this->assertEquals('\\r\\n\\\\', $string);
379 function test_core_is_secure_file_exists() {
380 $file = SMARTY_CORE_DIR
. 'core.is_secure.php';
381 $this->assertTrue(file_exists($file));
384 function test_core_is_secure_file_include() {
385 $file = SMARTY_CORE_DIR
. 'core.is_secure.php';
386 $this->assertTrue(include($file));
389 function test_core_is_secure_function_exists() {
390 $this->assertTrue(function_exists('smarty_core_is_secure'));
393 function test_core_is_secure_function_is_secure_true() {
394 $security = $this->smarty
->security
;
395 $this->smarty
->security
= true;
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;
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;
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',
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;
435 function test_core_is_secure_function_smarty_var_const_not_allowed() {
436 $security = $this->smarty
->security
;
437 $this->smarty
->security
= true;
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();
444 $this->assertEquals( $this->errorlevel
, E_USER_WARNING
);
445 $this->smarty
->security
= $security;
This page took 0.535228 seconds and 4 git commands to generate.