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