* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: ContentInterpolationTest.php 888 2010-06-08 09:48:33Z kornel $ * @link http://phptal.org/ */ class ContentInterpolationTest extends PHPTAL_TestCase { public function testInterpol() { $src = <<\${foo} EOT; $exp = <<foo value EOT; $tpl = $this->newPHPTAL(); $tpl->setSource($src); $tpl->foo = 'foo value'; $res = $tpl->execute(); $this->assertEquals($exp, $res); } public function testInterpol2() { $src = <<\${foo2} x \${structure foo} y \${foo}\${structure foo2} EOT; $exp = <<{foo2 <img />} x foo value y foo value{foo2 } EOT; $tpl = $this->newPHPTAL(); $tpl->setSource($src); $tpl->foo = 'foo value'; $tpl->foo2 = '{foo2 }'; $res = $tpl->execute(); $this->assertEquals($exp, $res); } public function testInterpol3() { $src = <<\${foo}\${foo} EOT; $exp = <<foo valuefoo value EOT; $tpl = $this->newPHPTAL(); $tpl->setSource($src); $tpl->foo = 'foo value'; $res = $tpl->execute(); $this->assertEquals($exp, $res); } public function testNoInterpol() { $src = <<$\${foo} EOT; $exp = <<\${foo} EOT; $tpl = $this->newPHPTAL(); $tpl->setSource($src); $tpl->foo = 'foo value'; $res = $tpl->execute(); $this->assertEquals($exp, $res); } public function testInterpolAdv() { $src = <<$$\${foo} EOT; $exp = <<\$foo value EOT; $tpl = $this->newPHPTAL(); $tpl->setSource($src); $tpl->foo = 'foo value'; $res = $tpl->execute(); $this->assertEquals($exp, $res); } function testUnescape() { $tpl = $this->newPHPTAL(); $tpl->var = 'val<'; $tpl->setSource('

${var} $${var} $$${var} $$$${var}

'); $this->assertEquals(normalize_html('

val< ${var} $val< $${var}

'), normalize_html($tpl->execute())); } function testUnescapeString() { $tpl = $this->newPHPTAL(); $tpl->var = 'val<'; $tpl->setSource('

'); $this->assertEquals(normalize_html('

val< ${var} $val< $${var}

'), normalize_html($tpl->execute())); } function testUnescapeStructure() { $tpl = $this->newPHPTAL(); $tpl->var = 'val'; $tpl->setSource('

${structure var} $${structure var} $$${structure var} $$$${structure var}

'); $this->assertEquals(normalize_html('

val ${structure var} $val $${structure var}

'), normalize_html($tpl->execute())); } function testUnescapeCDATA() { $tpl = $this->newPHPTAL(); $tpl->var = 'val<'; $tpl->setSource(''); $this->assertEquals(normalize_html(''), normalize_html($tpl->execute())); } function testUnescapeCDATAStructure() { $tpl = $this->newPHPTAL(); $tpl->var = 'val<'; $tpl->setSource(''); $this->assertEquals(normalize_html(''), normalize_html($tpl->execute())); } function testUnescapePHPTales() { $tpl = $this->newPHPTAL(); $tpl->var = '1'; $tpl->setSource('

${var+1} $${var+1} $$${var+1} $$$${var+1}

'); $this->assertEquals(normalize_html('

2 ${var+1} $2 $${var+1}

'), normalize_html($tpl->execute())); } public function testPHPBlock() { $tpl = $this->newPHPTAL(); $tpl->setSource('

test"; ?>testtest

'); $this->assertEquals('

testtest&test

', $tpl->execute()); } public function testPHPBlockShort() { ini_set('short_open_tag', 1); if (!ini_get('short_open_tag')) $this->markTestSkipped("PHP is buggy"); $tpl = $this->newPHPTAL(); $tpl->setSource('

test"); ?>testtest

'); $this->assertEquals('

testtest&test

', $tpl->execute()); ini_restore('short_open_tag'); } public function testPHPBlockNoShort() { ini_set('short_open_tag', 0); if (ini_get('short_open_tag')) $this->markTestSkipped("PHP is buggy"); $tpl = $this->newPHPTAL(); $tpl->setSource('

test"); ?>testtest

'); try { // unlike attributes, this isn't going to be escaped, because it gets parsed as a real processing instruction $this->assertEquals('

test"); ?>testtest

', $tpl->execute()); } catch(PHPTAL_ParserException $e) {/* xml ill-formedness error is ok too */} ini_restore('short_open_tag'); } /** * @expectedException PHPTAL_VariableNotFoundException */ function testErrorsThrow() { $tpl = $this->newPHPTAL(); $tpl->setSource('

${error}

'); $tpl->execute(); } /** * @expectedException PHPTAL_VariableNotFoundException */ function testErrorsThrow2() { $tpl = $this->newPHPTAL(); $tpl->setSource('

${error | error}

'); $tpl->execute(); } function testErrorsSilenced() { $tpl = $this->newPHPTAL(); $tpl->setSource('

${error | nothing}

'); $this->assertEquals('

', $tpl->execute()); } function testZeroIsNotEmpty() { $tpl = $this->newPHPTAL(); $tpl->zero = '0'; $tpl->setSource('

${zero | error}

'); $this->assertEquals('

0

', $tpl->execute()); } function testPreservesNewline() { $tpl = $this->newPHPTAL()->setSource(' ${variable1 | string:Line 1} Line 3 '); $this->assertEquals(' Line 1 Line 2 Line 3 ', $tpl->execute(), $tpl->getCodePath()); } function testMultilineInterpolationPHP() { $res = $this->newPHPTAL()->setSource('

${php:\'foo bar\'}

')->execute(); $this->assertEquals('

foo bar

', $res); $res = $this->newPHPTAL()->setSource('

${php:\'foo\' . substr(\'barz\' , 0,3)}

')->execute(); $this->assertEquals('

foobar

', $res); } function testMultilineInterpolation() { $res = $this->newPHPTAL()->setSource('

${string:foo bar}

')->execute(); $this->assertEquals('

foo bar

', $res); $res = $this->newPHPTAL()->setSource('

${structure string:foo bar}

')->execute(); $this->assertEquals('

foo bar

', $res); } function testTagsBreakTALES() { $res = $this->newPHPTAL()->setSource('

${foo
bar}

')->execute(); $this->assertEquals('

${foo
bar}

', $res); } function testEscapedTagsDontBreakTALES() { $res = $this->newPHPTAL()->setSource('

${structure string:foo<br />bar}

')->execute(); $this->assertEquals('

foo
bar

', $res); } }