* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: EscapeCDATATest.php 888 2010-06-08 09:48:33Z kornel $ * @link http://phptal.org/ */ class EscapeCDATATest extends PHPTAL_TestCase { private $last_code_path; private function executeString($str, $params = array()) { $tpl = $this->newPHPTAL(); foreach ($params as $k => $v) $tpl->set($k, $v); $tpl->setSource($str); $this->last_code_path = $tpl->getCodePath(); return $tpl->execute(); } function testTrimString() { $this->assertEquals( normalize_html(']]> foo ]> bar'), normalize_html(' ]]> foo ]> bar ') ); $this->assertNotEquals( normalize_html('foo]]>bar'), normalize_html('foo]]>bar') ); } function testDoesEscapeHTMLContent(){ $tpl = $this->newPHPTAL('input/escape.html'); $exp = normalize_html_file('output/escape.html'); $res = normalize_html($tpl->execute()); $this->assertEquals($exp, $res); } function testEntityTextInPath() { $res = $this->executeString('
'); // either way is good if (false !== strpos($res, 'assertEquals('
', $res); } else { $this->assertEquals('
"< & &amp; &quot; &lt;,"< & &amp; &quot; &lt;
', $res); } } function testEntityStructureInPath() { $res = $this->executeString('
'); $this->assertEquals('
', $res); } function testEntityInContentPHP() { $res = $this->executeString('
'); $this->assertEquals('
15,3
', $res); } function testEntityInScriptPHP() { $res = $this->executeString(''); $this->assertEquals('', $res); } function testEntityInPHP2() { $res = $this->executeString('
'); $this->assertEquals('
', $res); } function testEntityInPHP3() { $res = $this->executeString('
,]]>
'); $this->assertEquals('
', $res); } function testNoEncodingAfterPHP() { $res = $this->executeString('
]]>
'); $this->assertEquals('
', $res); } /** * normal XML behavior expected */ function testEscapeCDATAXML() { $tpl = $this->newPHPTAL(); $tpl->setOutputMode(PHPTAL::XML); $tpl->setSource(' '); $tpl->cdata = ']]>'; $res = $tpl->execute(); $this->assertEquals(']]></x>; ]]></x>; ]]>', $res); } /** * ugly hybrid between HTML (XHTML as text/html) and XML */ function testEscapeCDATAXHTML() { $tpl = $this->newPHPTAL(); $tpl->setOutputMode(PHPTAL::XHTML); $tpl->setSource(' '); $tpl->cdata = ']]>'; $res = $tpl->execute(); $this->assertEquals(' ]]>', $res); } function testEscapeCDATAHTML() { $tpl = $this->newPHPTAL(); $tpl->setOutputMode(PHPTAL::HTML5); $tpl->setSource(' '); $tpl->cdata = ']]>'; $res = $tpl->execute(); $this->assertEquals(']]></x>; ]]></x>; ]]>', $res); } function testAutoCDATA() { $res = $this->executeString(''); $this->assertEquals('', $res, $this->last_code_path); } function testAutoCDATA2() { $res = $this->executeString(' 1 < 2 ${php:\'&\' . \'&\'} '); $this->assertEquals('/**/', $res, $this->last_code_path); } function testNoAutoCDATA() { $res = $this->executeString(''); $this->assertEquals('', $res, $this->last_code_path); } function testNoAutoCDATA2() { $res = $this->executeString('', array('foo'=>'')); $this->assertEquals('', $res, $this->last_code_path); } function testNoAutoCDATA3() { $res = $this->executeString(''); $this->assertEquals('', $res, $this->last_code_path); } }