PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Smarty/SmartyTest.php

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