PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/tpl/tests/expressions_parsing.php

https://bitbucket.org/doubleface/jelix-jpu
PHP | 303 lines | 252 code | 42 blank | 9 comment | 5 complexity | 154a52f35f86514a9e4ac7aed48bf739 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package testapp
  4. * @subpackage jelix_tests module
  5. * @author Jouanneau Laurent
  6. * @contributor
  7. * @copyright 2007-2008 Jouanneau laurent
  8. * @link http://www.jelix.org
  9. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11. define('TEST_JTPL_COMPILER_ASSIGN',1);
  12. class testJtplCompiler extends jTplCompiler {
  13. public function setUserPlugins($userModifiers, $userFunctions) {
  14. $this->_modifier = array_merge($this->_modifier, $userModifiers);
  15. $this->_userFunctions = $userFunctions;
  16. }
  17. public function testParseExpr($string, $allowed=array(), $exceptchar=array(';'), $splitArgIntoArray=false){
  18. return $this->_parseFinal($string, $allowed, $exceptchar, $splitArgIntoArray);
  19. }
  20. public function testParseVarExpr($string){
  21. return $this->_parseFinal($string,$this->_allowedInVar, $this->_excludedInVar);
  22. }
  23. public function testParseForeachExpr($string){
  24. return $this->_parseFinal($string,$this->_allowedInForeach, array(';','!'));
  25. }
  26. public function testParseAnyExpr($string){
  27. return $this->_parseFinal($string, $this->_allowedInExpr, array());
  28. }
  29. public function testParseAssignExpr($string){
  30. return $this->_parseFinal($string,$this->_allowedAssign);
  31. }
  32. public function testParseAssignExpr2($string){
  33. return $this->_parseFinal($string,$this->_allowedAssign, array(';'),true);
  34. }
  35. public function testParseVariable($string){
  36. return $this->_parseVariable($string);
  37. }
  38. }
  39. function testjtplcontentUserModifier($s){}
  40. class UTjtplexpr extends jUnitTestCase {
  41. protected $varexpr = array(
  42. 'a'=>'a',
  43. '"aaa"'=>'"aaa"',
  44. '123'=>'123',
  45. ' '=>' ',
  46. '123.456'=>'123.456',
  47. '->'=>'->',
  48. "'aze'"=>"'aze'",
  49. '$aa'=>'$t->_vars[\'aa\']',
  50. '$aa.$bb'=>'$t->_vars[\'aa\'].$t->_vars[\'bb\']',
  51. '$aa."bbb"'=>'$t->_vars[\'aa\']."bbb"',
  52. '$aa+234'=>'$t->_vars[\'aa\']+234',
  53. '$aa-234'=>'$t->_vars[\'aa\']-234',
  54. '$aa*234'=>'$t->_vars[\'aa\']*234',
  55. '$aa/234'=>'$t->_vars[\'aa\']/234',
  56. '!$aa'=>'!$t->_vars[\'aa\']',
  57. 'array($aa)'=>'array($t->_vars[\'aa\'])',
  58. ' array( $aa ) '=>' array( $t->_vars[\'aa\'] ) ',
  59. ' array("e"=>$aa, "tt"=>987 )'=>' array("e"=>$t->_vars[\'aa\'], "tt"=>987 )',
  60. '@aa@'=>'${jTplConfig::$localesGetter}(\'aa\')',
  61. '@aa.$ooo@'=>'${jTplConfig::$localesGetter}(\'aa.\'.$t->_vars[\'ooo\'].\'\')',
  62. '@aa~bbb@'=>'${jTplConfig::$localesGetter}(\'aa~bbb\')',
  63. '@aa~trc.$abcd.popo@'=>'${jTplConfig::$localesGetter}(\'aa~trc.\'.$t->_vars[\'abcd\'].\'.popo\')',
  64. '@$aa~trc.$abcd.popo@'=>'${jTplConfig::$localesGetter}(\'\'.$t->_vars[\'aa\'].\'~trc.\'.$t->_vars[\'abcd\'].\'.popo\')',
  65. '$aa.@trc.$abcd.popo@'=>'$t->_vars[\'aa\'].${jTplConfig::$localesGetter}(\'trc.\'.$t->_vars[\'abcd\'].\'.popo\')',
  66. '@aa~trc.234.popo@'=>'${jTplConfig::$localesGetter}(\'aa~trc.234.popo\')',
  67. '@aa~trc.23.4.popo@'=>'${jTplConfig::$localesGetter}(\'aa~trc.23.4.popo\')',
  68. '@aa~trc.23.4.list@'=>'${jTplConfig::$localesGetter}(\'aa~trc.23.4.list\')',
  69. '$aa*count($bb)'=>'$t->_vars[\'aa\']*count($t->_vars[\'bb\'])',
  70. '$aa & $bb'=>'$t->_vars[\'aa\'] & $t->_vars[\'bb\']',
  71. '$aa | $bb'=>'$t->_vars[\'aa\'] | $t->_vars[\'bb\']',
  72. '$aa++'=>'$t->_vars[\'aa\']++',
  73. '$aa--'=>'$t->_vars[\'aa\']--',
  74. '$bb->bar'=>'$t->_vars[\'bb\']->bar',
  75. '$bb->$bar'=>'$t->_vars[\'bb\']->{$t->_vars[\'bar\']}',
  76. '$bb->$bar->yo'=>'$t->_vars[\'bb\']->{$t->_vars[\'bar\']}->yo',
  77. '@abstract.as.break.case.catch.class.clone@'=>'${jTplConfig::$localesGetter}(\'abstract.as.break.case.catch.class.clone\')',
  78. '@const.continue.declare.default.do.echo.else.elseif.empty@'=>'${jTplConfig::$localesGetter}(\'const.continue.declare.default.do.echo.else.elseif.empty\')',
  79. '@exit.final.for.foreach.function.global.if.implements.instanceof@'=>'${jTplConfig::$localesGetter}(\'exit.final.for.foreach.function.global.if.implements.instanceof\')',
  80. '@interface.and.or.xor.new.private.public@'=>'${jTplConfig::$localesGetter}(\'interface.and.or.xor.new.private.public\')',
  81. '@protected.return.static.switch.throw.try.use.var.eval.while@'=>'${jTplConfig::$localesGetter}(\'protected.return.static.switch.throw.try.use.var.eval.while\')',
  82. '$aa*(234+$b)'=>'$t->_vars[\'aa\']*(234+$t->_vars[\'b\'])',
  83. '$aa[$bb[4]]'=>'$t->_vars[\'aa\'][$t->_vars[\'bb\'][4]]',
  84. );
  85. protected $varexprTrustedMode = array(
  86. '$aaa.PHP_VERSION'=>'$t->_vars[\'aaa\'].PHP_VERSION',
  87. );
  88. protected $varexprUnTrustedMode = array(
  89. );
  90. function testVarExprTrustedMode() {
  91. $compil = new testJtplCompiler();
  92. $compil->trusted = true;
  93. foreach($this->varexpr as $k=>$t){
  94. try{
  95. $res = $compil->testParseVarExpr($k);
  96. $this->assertEqualOrDiff($t, $res);
  97. }catch(Exception $e){
  98. $this->fail("Test '$k', Unknown Exception: ".$e->getMessage());
  99. }
  100. }
  101. foreach($this->varexprTrustedMode as $k=>$t){
  102. try{
  103. $res = $compil->testParseVarExpr($k);
  104. $this->assertEqualOrDiff($t, $res);
  105. }catch(Exception $e){
  106. $this->fail("Test '$k', Unknown Exception: ".$e->getMessage());
  107. }
  108. }
  109. }
  110. function testVarExprUnTrustedMode() {
  111. $compil = new testJtplCompiler();
  112. $compil->trusted = false;
  113. foreach($this->varexpr as $k=>$t){
  114. try{
  115. $res = $compil->testParseVarExpr($k);
  116. $this->assertEqualOrDiff($t, $res);
  117. }catch(Exception $e){
  118. $this->fail("Test '$k', Unknown Exception: ".$e->getMessage());
  119. }
  120. }
  121. foreach($this->varexprUnTrustedMode as $k=>$t){
  122. try{
  123. $res = $compil->testParseVarExpr($k);
  124. $this->assertEqualOrDiff($t, $res);
  125. }catch(Exception $e){
  126. $this->fail("Test '$k', Unknown Exception: ".$e->getMessage());
  127. }
  128. }
  129. }
  130. protected $badvarexpr = array(
  131. '$' =>array('Dans le tag du template , le caractère $ n\'est pas autorisé'),
  132. 'foreach($a)' =>array('Dans le tag du template , le code php foreach n\'est pas autorisé'),
  133. '@aaa.bbb' =>array('Dans le tag du template , il manque la fin de la clef de localisation'),
  134. '@aaa.b,bb@' =>array('Dans le tag du template , le caractère , n\'est pas autorisé'),
  135. '@@' =>array('Dans le tag du template , clef de localisation vide'),
  136. '[$aa/234]' =>array('Dans le tag du template , le caractère [ n\'est pas autorisé'),
  137. '$b+($aa/234' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  138. '$b+(($aa/234)' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  139. '$aa/234)' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  140. '$aa/234))' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  141. '$aa[234' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  142. '$aa[[234]' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  143. '$aa234]' =>array('Dans le tag du template , il y a des erreurs au niveau des parenthèses'),
  144. 'isset($t[5])' =>array('Dans le tag du template , le code php isset n\'est pas autorisé'),
  145. 'empty($aa)' =>array('Dans le tag du template , le code php empty n\'est pas autorisé'),
  146. '$aa == 123' =>array('Dans le tag du template , le code php == n\'est pas autorisé'),
  147. '$aa != 123' =>array('Dans le tag du template , le code php != n\'est pas autorisé'),
  148. '$aa >= 123' =>array('Dans le tag du template , le code php >= n\'est pas autorisé'),
  149. '$aa !== 123' =>array('Dans le tag du template , le code php !== n\'est pas autorisé'),
  150. '$aa <= 123' =>array('Dans le tag du template , le code php <= n\'est pas autorisé'),
  151. '$aa << 123' =>array('Dans le tag du template , le code php << n\'est pas autorisé'),
  152. '$aa >> 123' =>array('Dans le tag du template , le code php >> n\'est pas autorisé'),
  153. '$aa == false' =>array('Dans le tag du template , le code php == n\'est pas autorisé'),
  154. '$aa == true' =>array('Dans le tag du template , le code php == n\'est pas autorisé'),
  155. '$aa == null' =>array('Dans le tag du template , le code php == n\'est pas autorisé'),
  156. '$aa && $bb' =>array('Dans le tag du template , le code php && n\'est pas autorisé'),
  157. '$aa || $bb' =>array('Dans le tag du template , le code php || n\'est pas autorisé'),
  158. '$aa and $bb' =>array('Dans le tag du template , le code php and n\'est pas autorisé'),
  159. '$aa or $bb' =>array('Dans le tag du template , le code php or n\'est pas autorisé'),
  160. '$aa xor $bb' =>array('Dans le tag du template , le code php xor n\'est pas autorisé'),
  161. '$aa=$bb' =>array('Dans le tag du template , le caractère = n\'est pas autorisé'),
  162. '$aa+=$bb' =>array('Dans le tag du template , le code php += n\'est pas autorisé'),
  163. '$aa-=$bb' =>array('Dans le tag du template , le code php -= n\'est pas autorisé'),
  164. '$aa/=$bb' =>array('Dans le tag du template , le code php /= n\'est pas autorisé'),
  165. '$aa*=$bb' =>array('Dans le tag du template , le code php *= n\'est pas autorisé'),
  166. );
  167. protected $badvarexprTrustedMode = array(
  168. );
  169. protected $badvarexprUnTrustedMode = array(
  170. '$aaa.PHP_VERSION'=>array('Dans le tag du template , les constantes (PHP_VERSION) sont interdites'),
  171. );
  172. function testBadVarExprTrustedMode() {
  173. $compil = new testJtplCompiler();
  174. $compil->trusted = true;
  175. foreach($this->badvarexpr as $k=>$t){
  176. try{
  177. $res = $compil->testParseVarExpr($k);
  178. $this->fail("No Exception for this test '$k' ");
  179. }catch(Exception $e){
  180. $this->assertEqualOrDiff($t[0], $e->getMessage());
  181. }
  182. }
  183. foreach($this->badvarexprTrustedMode as $k=>$t){
  184. try{
  185. $res = $compil->testParseVarExpr($k);
  186. $this->fail("No Exception for this test '$k' ");
  187. }catch(Exception $e){
  188. $this->assertEqualOrDiff($t[0], $e->getMessage());
  189. }
  190. }
  191. }
  192. function testBadVarExprUnTrustedMode() {
  193. $compil = new testJtplCompiler();
  194. $compil->trusted = false;
  195. foreach($this->badvarexpr as $k=>$t){
  196. try{
  197. $res = $compil->testParseVarExpr($k);
  198. $this->fail("No Exception for this test '$k' ");
  199. }catch(Exception $e){
  200. $this->assertEqualOrDiff($t[0], $e->getMessage());
  201. }
  202. }
  203. foreach($this->badvarexprUnTrustedMode as $k=>$t){
  204. try{
  205. $res = $compil->testParseVarExpr($k);
  206. $this->fail("No Exception for this test '$k' ");
  207. }catch(Exception $e){
  208. $this->assertEqualOrDiff($t[0], $e->getMessage());
  209. }
  210. }
  211. }
  212. protected $varTag = array(
  213. '$aaa|escxml' => 'htmlspecialchars($t->_vars[\'aaa\'])',
  214. '$aaa|bla'=>'testjtplcontentUserModifier($t->_vars[\'aaa\'])',
  215. );
  216. function testVarTag() {
  217. $compil = new testJtplCompiler();
  218. $compil->trusted = true;
  219. $compil->setUserPlugins(array('bla'=>'testjtplcontentUserModifier'),array());
  220. foreach($this->varTag as $k=>$t){
  221. try{
  222. $res = $compil->testParseVariable($k);
  223. $this->assertEqualOrDiff($t, $res);
  224. }catch(Exception $e){
  225. $this->fail("Test '$k', Unknown Exception: ".$e->getMessage());
  226. }
  227. }
  228. }
  229. protected $varAssign = array(
  230. '$aa=$bb'=>'$t->_vars[\'aa\']=$t->_vars[\'bb\']',
  231. '$aa+=$bb'=>'$t->_vars[\'aa\']+=$t->_vars[\'bb\']',
  232. '$aa-=$bb'=>'$t->_vars[\'aa\']-=$t->_vars[\'bb\']',
  233. '$aa/=$bb'=>'$t->_vars[\'aa\']/=$t->_vars[\'bb\']',
  234. '$aa*=$bb'=>'$t->_vars[\'aa\']*=$t->_vars[\'bb\']',
  235. 'TEST_JTPL_COMPILER_ASSIGN'=>'TEST_JTPL_COMPILER_ASSIGN'
  236. );
  237. protected $varAssignUnTrustedMode = array(
  238. 'TEST_JTPL_COMPILER_ASSIGN'=>array('Dans le tag du template , les constantes (TEST_JTPL_COMPILER_ASSIGN) sont interdites'),
  239. );
  240. function testAssign() {
  241. $compil = new testJtplCompiler();
  242. $compil->trusted = true;
  243. foreach($this->varAssign as $k=>$t){
  244. try{
  245. $res = $compil->testParseAssignExpr($k);
  246. $this->assertEqualOrDiff($t, $res);
  247. }catch(Exception $e){
  248. $this->fail("Test '$k', Unknown Exception: ".$e->getMessage());
  249. }
  250. }
  251. $compil->trusted = false;
  252. foreach($this->varAssignUnTrustedMode as $k=>$t){
  253. try{
  254. $res = $compil->testParseAssignExpr($k);
  255. $this->fail("No Exception for this test '$k' ");
  256. }catch(Exception $e){
  257. $this->assertEqualOrDiff($t[0], $e->getMessage());
  258. }
  259. }
  260. }
  261. }