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