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

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

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