PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty/unit_test/test_cases.php

http://github.com/gabrys/wikidot
PHP | 450 lines | 367 code | 37 blank | 46 comment | 1 complexity | 824dbd2706cdb143395df4f36920cc97 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once './config.php';
  3. require_once SMARTY_DIR . 'Smarty.class.php';
  4. require_once 'PHPUnit.php';
  5. class Obj {
  6. public $val = 'val';
  7. public $arr = array('one' => 'one', 'two' => 2);
  8. public $ten = 10;
  9. function meth($a="a", $b="b") {
  10. return "$a:$b";
  11. }
  12. function six() {
  13. return 6;
  14. }
  15. }
  16. class SmartyTest extends PHPUnit_TestCase {
  17. // contains the object handle of the string class
  18. public $abc;
  19. // contains the last triggered error's errorlevel
  20. public $errorlevel;
  21. // constructor of the test suite
  22. function SmartyTest($name) {
  23. $this->PHPUnit_TestCase($name);
  24. }
  25. // called before the test functions will be executed
  26. // this function is defined in PHPUnit_TestCase and overwritten
  27. // here
  28. function setUp() {
  29. // create a new instance of String with the
  30. // string 'abc'
  31. $this->smarty = new Smarty;
  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. function test_clear_compiled_tpl() {
  220. $this->assertTrue($this->smarty->clear_compiled_tpl());
  221. }
  222. /* DISPLAY TESTS */
  223. // test that display() executes properly
  224. function test_call_to_display() {
  225. ob_start();
  226. $this->smarty->display('index.tpl');
  227. $output = ob_get_contents();
  228. ob_end_clean();
  229. $this->assertEquals($output, 'TEST STRING');
  230. }
  231. /* FETCH TESTS */
  232. // test that fetch() executes properly
  233. function test_call_to_fetch() {
  234. $this->assertEquals($this->smarty->fetch('index.tpl'), 'TEST STRING');
  235. }
  236. /* ASSIGN TESTS */
  237. // test assigning a simple template variable
  238. function test_assign_var() {
  239. $this->smarty->assign('foo', 'bar');
  240. $this->assertEquals($this->smarty->fetch('assign_var.tpl'), 'bar');
  241. }
  242. /* PARSING TESTS */
  243. // test assigning and calling an object
  244. function test_parse_obj_meth() {
  245. $obj = new Obj();
  246. $this->smarty->assign('obj', $obj);
  247. $this->smarty->assign('foo', 'foo');
  248. $this->assertEquals('foo:2.5
  249. 2.5:foo
  250. 2.5:b
  251. val:foo
  252. foo:val
  253. foo:foo
  254. one:2
  255. foo:foo:b', $this->smarty->fetch('parse_obj_meth.tpl'));
  256. }
  257. // test assigning and calling an object
  258. function test_parse_math() {
  259. $obj = new Obj();
  260. $this->smarty->assign('obj', $obj);
  261. $this->smarty->assign('flt', 2.5);
  262. $this->smarty->assign('items', array(1, 2));
  263. $this->assertEquals('3
  264. 3.5
  265. 7
  266. 11
  267. 4
  268. 4.5
  269. 8
  270. 12
  271. 12.5
  272. 25
  273. 16
  274. 20
  275. 8.5
  276. 7', $this->smarty->fetch('parse_math.tpl'));
  277. }
  278. /* CONFIG FILE TESTS */
  279. // test assigning a double quoted global variable
  280. function test_config_load_globals_double_quotes() {
  281. // load the global var
  282. $this->smarty->config_load('globals_double_quotes.conf');
  283. // test that it is assigned
  284. $this->assertEquals($this->smarty->_config[0]['vars']['foo'], 'bar');
  285. }
  286. // test assigning a single quoted global variable
  287. function test_config_load_globals_single_quotes() {
  288. // load the global var
  289. $this->smarty->config_load('globals_single_quotes.conf');
  290. // test that it is assigned
  291. $this->assertEquals($this->smarty->_config[0]['vars']['foo'], 'bar');
  292. }
  293. // test loading and running modifier.escape.php
  294. function test_escape_modifier_get_plugins_filepath() {
  295. $filepath = $this->smarty->_get_plugin_filepath('modifier', 'escape');
  296. $this->assertTrue($filepath);
  297. }
  298. function test_escape_modifier_include_file() {
  299. $filepath = $this->smarty->_get_plugin_filepath('modifier', 'escape');
  300. $this->assertTrue(include($filepath));
  301. }
  302. function test_escape_modifier_function_exists() {
  303. $this->assertTrue(function_exists('smarty_modifier_escape'));
  304. }
  305. function test_escape_modifier_escape_default() {
  306. $string = smarty_modifier_escape("<html><body></body></html>");
  307. $this->assertEquals('&lt;html&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;',
  308. $string);
  309. }
  310. function test_escape_modifier_escape_html() {
  311. $string = smarty_modifier_escape("<html><body></body></html>", 'html');
  312. $this->assertEquals('&lt;html&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;',
  313. $string);
  314. }
  315. function test_escape_modifier_escape_htmlall() {
  316. $string = smarty_modifier_escape("<html><body></body></html>", 'htmlall');
  317. $this->assertEquals('&lt;html&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;',
  318. $string);
  319. }
  320. function test_escape_modifier_escape_url() {
  321. $string = smarty_modifier_escape("http://test.com?foo=bar", 'url');
  322. $this->assertEquals('http%3A%2F%2Ftest.com%3Ffoo%3Dbar', $string);
  323. }
  324. function test_escape_modifier_escape_quotes() {
  325. $string = smarty_modifier_escape("'\\'\\''", 'quotes');
  326. $this->assertEquals("\\'\\'\\'\\'", $string);
  327. }
  328. function test_escape_modifier_escape_hex() {
  329. $string = smarty_modifier_escape("abcd", 'hex');
  330. $this->assertEquals('%61%62%63%64', $string);
  331. }
  332. function test_escape_modifier_escape_hexentity() {
  333. $string = smarty_modifier_escape("ABCD", 'hexentity');
  334. $this->assertEquals('&#x41;&#x42;&#x43;&#x44;', $string);
  335. }
  336. function test_escape_modifier_escape_javascript() {
  337. $string = smarty_modifier_escape("\r\n\\", 'javascript');
  338. $this->assertEquals('\\r\\n\\\\', $string);
  339. }
  340. function test_core_is_secure_file_exists() {
  341. $file = SMARTY_CORE_DIR . 'core.is_secure.php';
  342. $this->assertTrue(file_exists($file));
  343. }
  344. function test_core_is_secure_file_include() {
  345. $file = SMARTY_CORE_DIR . 'core.is_secure.php';
  346. $this->assertTrue(include($file));
  347. }
  348. function test_core_is_secure_function_exists() {
  349. $this->assertTrue(function_exists('smarty_core_is_secure'));
  350. }
  351. function test_core_is_secure_function_is_secure_true() {
  352. $security = $this->smarty->security;
  353. $this->smarty->security = true;
  354. /* check if index.tpl is secure (should be true) */
  355. $params = array('resource_type' => 'file',
  356. 'resource_base_path' => dirname(__FILE__) . '/templates',
  357. 'resource_name' => dirname(__FILE__) . '/templates/index.tpl');
  358. $this->assertTrue(smarty_core_is_secure($params, $this->smarty));
  359. $this->smarty->security = $security;
  360. }
  361. function test_core_is_secure_function_is_secure_false() {
  362. $security = $this->smarty->security;
  363. $this->smarty->security = true;
  364. /* check if test_cases.php is secure (should be false) */
  365. $params = array('resource_type' => 'file',
  366. 'resource_base_path' => dirname(__FILE__) . '/templates',
  367. 'resource_name' => __FILE__);
  368. $this->assertFalse(smarty_core_is_secure($params, $this->smarty));
  369. $this->smarty->security = $security;
  370. }
  371. // test constants and security
  372. function test_core_is_secure_function_smarty_var_const() {
  373. define('TEST_CONSTANT', 'test constant');
  374. $this->assertEquals('test constant', $this->smarty->fetch('constant.tpl',
  375. null, 'var_const'));
  376. }
  377. function test_core_is_secure_function_smarty_var_const_allowed() {
  378. $security = $this->smarty->security;
  379. $security_settings = $this->smarty->security_settings;
  380. $this->smarty->security_settings['ALLOW_CONSTANTS'] = true;
  381. $this->smarty->security = true;
  382. $this->assertEquals('test constant', $this->smarty->fetch('constant.tpl',
  383. null, 'var_const_allowed'));
  384. $this->smarty->security_settings = $security_settings;
  385. $this->smarty->security = $security;
  386. }
  387. function test_core_is_secure_function_smarty_var_const_not_allowed() {
  388. $security = $this->smarty->security;
  389. $this->smarty->security = true;
  390. /* catch errors: */
  391. $this->errorlevel = null;
  392. set_error_handler(array(&$this, 'error_handler'));
  393. $this->smarty->fetch('constant.tpl', null, 'var_const_not_allowed');
  394. restore_error_handler();
  395. $this->assertEquals( $this->errorlevel, E_USER_WARNING);
  396. $this->smarty->security = $security;
  397. }
  398. }
  399. ?>