Move unit test for TemplateParser v1

This commit is contained in:
Kijin Sung 2023-10-15 00:50:26 +09:00
parent f13e3d633b
commit a640306123
5 changed files with 32 additions and 28 deletions

View file

@ -784,7 +784,9 @@ class TemplateParser_v2
$escape_option = $filter; $escape_option = $filter;
break; break;
case 'escapejs': case 'escapejs':
case 'js':
$str = "escape_js({$str})"; $str = "escape_js({$str})";
$escape_option = 'noescape';
break; break;
case 'json': case 'json':
$str = "json_encode({$str}, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)"; $str = "json_encode({$str}, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)";

View file

@ -361,6 +361,8 @@ class Cleanup extends Base
'modules/spamfilter/spamfilter.lib.php' => 'deleted', 'modules/spamfilter/spamfilter.lib.php' => 'deleted',
'modules/spamfilter/ruleset/' => 'deleted', 'modules/spamfilter/ruleset/' => 'deleted',
'phpDoc/' => 'deleted:xe', 'phpDoc/' => 'deleted:xe',
'tests/unit/classes/template/' => 'deleted',
'tests/unit/classes/TemplateHandlerTest.php' => 'deleted',
'tools/dbxml_validator/' => 'deleted:xe', 'tools/dbxml_validator/' => 'deleted:xe',
'tools/korea_ip_ranges/' => 'deleted', 'tools/korea_ip_ranges/' => 'deleted',
'tools/phpDoc/' => 'deleted', 'tools/phpDoc/' => 'deleted',

View file

@ -1,13 +1,13 @@
<?php <?php
class TemplateHandlerTest extends \Codeception\TestCase\Test class TemplateParserV1Test extends \Codeception\TestCase\Test
{ {
private $baseurl; private $baseurl;
private $prefix = '<?php if (!defined("RX_VERSION")) exit();'; private $prefix = '<?php if (!defined("RX_VERSION")) exit();';
public function _before() public function _before()
{ {
$this->baseurl = '/' . basename(dirname(dirname(dirname(__DIR__)))) . '/'; $this->baseurl = '/' . basename(dirname(dirname(dirname(dirname(__DIR__))))) . '/';
} }
public function testParse() public function testParse()
@ -101,42 +101,42 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// #include // #include
array( array(
'<dummy /><!--#include("sample.html")--><div>This is another dummy</div>', '<dummy /><!--#include("sample.html")--><div>This is another dummy</div>',
'?><dummy /><?php $__tpl=TemplateHandler::getInstance();echo $__tpl->compile(\'tests/unit/classes/template\',\'sample.html\') ?><div>This is another dummy</div>' '?><dummy /><?php $__tpl=TemplateHandler::getInstance();echo $__tpl->compile(\'tests/_data/template\',\'sample.html\') ?><div>This is another dummy</div>'
), ),
// <include target="file"> // <include target="file">
array( array(
'<dummy /><include target="../sample.html" /><div>This is another dummy</div>', '<dummy /><include target="../sample.html" /><div>This is another dummy</div>',
'?><dummy /><?php $__tpl=TemplateHandler::getInstance();echo $__tpl->compile(\'tests/unit/classes\',\'sample.html\') ?><div>This is another dummy</div>' '?><dummy /><?php $__tpl=TemplateHandler::getInstance();echo $__tpl->compile(\'tests/_data\',\'sample.html\') ?><div>This is another dummy</div>'
), ),
// <load target="../../modules/page/lang/lang.xml"> // <load target="../../modules/page/lang/lang.xml">
array( array(
'<dummy /><load target="../../../../modules/page/lang/lang.xml" /><dummy />', '<dummy /><load target="../../../modules/page/lang/lang.xml" /><dummy />',
'?><dummy /><?php Context::loadLang(\'modules/page/lang\'); ?><dummy />' '?><dummy /><?php Context::loadLang(\'modules/page/lang\'); ?><dummy />'
), ),
// <load target="style.css"> // <load target="style.css">
array( array(
'<dummy /><load target="css/style.css" /><dummy />', '<dummy /><load target="css/style.css" /><dummy />',
'?><dummy /><!--#Meta:tests/unit/classes/template/css/style.css--><?php Context::loadFile([\'tests/unit/classes/template/css/style.css\', \'\', \'\', \'\', []]); ?><dummy />' '?><dummy /><!--#Meta:tests/_data/template/css/style.css--><?php Context::loadFile([\'tests/_data/template/css/style.css\', \'\', \'\', \'\', []]); ?><dummy />'
), ),
// <unload target="style.css"> // <unload target="style.css">
array( array(
'<dummy /><unload target="css/style.css" /><dummy />', '<dummy /><unload target="css/style.css" /><dummy />',
'?><dummy /><?php Context::unloadFile(\'tests/unit/classes/template/css/style.css\', \'\', \'\'); ?><dummy />' '?><dummy /><?php Context::unloadFile(\'tests/_data/template/css/style.css\', \'\', \'\'); ?><dummy />'
), ),
// <!--%import("../../modules/page/tpl/filter/insert_config.xml")--> // <!--%import("../../modules/page/tpl/filter/insert_config.xml")-->
array( array(
'<dummy /><!--%import("../../../../modules/page/tpl/filter/insert_config.xml")--><dummy />', '<dummy /><!--%import("../../../modules/page/tpl/filter/insert_config.xml")--><dummy />',
'?><dummy /><?php require_once(\'./classes/xml/XmlJsFilter.class.php\');$__xmlFilter=new XmlJsFilter(\'modules/page/tpl/filter\',\'insert_config.xml\');$__xmlFilter->compile(); ?><dummy />' '?><dummy /><?php require_once(\'./classes/xml/XmlJsFilter.class.php\');$__xmlFilter=new XmlJsFilter(\'modules/page/tpl/filter\',\'insert_config.xml\');$__xmlFilter->compile(); ?><dummy />'
), ),
// <!--%import("../script.js",type="body")--> // <!--%import("../script.js",type="body")-->
array( array(
'<dummy /><!--%import("../script.js",type="body")--><dummy />', '<dummy /><!--%import("../script.js",type="body")--><dummy />',
'?><dummy /><!--#Meta:tests/unit/classes/script.js--><?php Context::loadFile([\'tests/unit/classes/script.js\', \'body\', \'\', \'\']); ?><dummy />' '?><dummy /><!--#Meta:tests/_data/script.js--><?php Context::loadFile([\'tests/_data/script.js\', \'body\', \'\', \'\']); ?><dummy />'
), ),
// <!--%unload("../script.js",type="body")--> // <!--%unload("../script.js",type="body")-->
array( array(
'<dummy /><!--%unload("../script.js",type="body")--><dummy />', '<dummy /><!--%unload("../script.js",type="body")--><dummy />',
'?><dummy /><?php Context::unloadFile(\'tests/unit/classes/script.js\', \'\'); ?><dummy />' '?><dummy /><?php Context::unloadFile(\'tests/_data/script.js\', \'\'); ?><dummy />'
), ),
// comment // comment
array( array(
@ -151,11 +151,11 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// relative path1 // relative path1
array( array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../local.gif" />', '<img src="http://naver.com/naver.gif"><input type="image" src="../local.gif" />',
'?><img src="http://naver.com/naver.gif"><input type="image" src="' . $this->baseurl . 'tests/unit/classes/local.gif" />' '?><img src="http://naver.com/naver.gif"><input type="image" src="' . $this->baseurl . 'tests/_data/local.gif" />'
), ),
// relative path2 // relative path2
array( array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../../../dir/local.gif" />', '<img src="http://naver.com/naver.gif"><input type="image" src="../../dir/local.gif" />',
'?><img src="http://naver.com/naver.gif"><input type="image" src="' . $this->baseurl . 'tests/dir/local.gif" />' '?><img src="http://naver.com/naver.gif"><input type="image" src="' . $this->baseurl . 'tests/dir/local.gif" />'
), ),
// error case // error case
@ -211,12 +211,12 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// issue 512 - ignores <marquee> // issue 512 - ignores <marquee>
array( array(
'<div class="topimgContex"><marquee direction="up" scrollamount="1" height="130" loop="infinity" behavior="lscro">{$lang->sl_show_topimgtext}</marquee></div>', '<div class="topimgContex"><marquee direction="up" scrollamount="1" height="130" loop="infinity" behavior="lscro">{$lang->sl_show_topimgtext}</marquee></div>',
'?><div class="topimgContex"><marquee direction="up" scrollamount="1" height="130" loop="infinity" behavior="lscro"><?php echo $lang->sl_show_topimgtext ?></marquee></div>' '?><div class="topimgContex"><marquee direction="up" scrollamount="1" height="130" loop="infinity" behavior="lscro"><?php echo $__Context->lang->sl_show_topimgtext ?? \'\' ?></marquee></div>'
), ),
// issue 584 // issue 584
array( array(
'<img cond="$oBodex->display_extra_images[\'mobile\'] && $arr_extra && $arr_extra->bodex->mobile" src="./images/common/mobile.gif" title="mobile" alt="mobile" />', '<img cond="$oBodex->display_extra_images[\'mobile\'] && $arr_extra && $arr_extra->bodex->mobile" src="./images/common/mobile.gif" title="mobile" alt="mobile" />',
'if($__Context->oBodex->display_extra_images[\'mobile\'] && $__Context->arr_extra && $__Context->arr_extra->bodex->mobile){ ?><img src="' . $this->baseurl . 'tests/unit/classes/template/images/common/mobile.gif" title="mobile" alt="mobile" /><?php } ?>' 'if($__Context->oBodex->display_extra_images[\'mobile\'] && $__Context->arr_extra && $__Context->arr_extra->bodex->mobile){ ?><img src="' . $this->baseurl . 'tests/_data/template/images/common/mobile.gif" title="mobile" alt="mobile" /><?php } ?>'
), ),
// issue 831 // issue 831
array( array(
@ -226,7 +226,7 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// issue 746 // issue 746
array( array(
'<img src="../whatever/img.png" />', '<img src="../whatever/img.png" />',
'?><img src="' . $this->baseurl . 'tests/unit/classes/whatever/img.png" />' '?><img src="' . $this->baseurl . 'tests/_data/whatever/img.png" />'
), ),
// issue 696 // issue 696
array( array(
@ -236,35 +236,35 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img cond="$foo->bar" src="../common/mobile.gif" />', '<img cond="$foo->bar" src="../common/mobile.gif" />',
'if($__Context->foo->bar ?? false){ ?><img src="' . $this->baseurl . 'tests/unit/classes/common/mobile.gif" /><?php } ?>' 'if($__Context->foo->bar ?? false){ ?><img src="' . $this->baseurl . 'tests/_data/common/mobile.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img cond="$foo->bar > 100" alt="a!@#$%^&*()_-=[]{}?/" src="../common/mobile.gif" />', '<img cond="$foo->bar > 100" alt="a!@#$%^&*()_-=[]{}?/" src="../common/mobile.gif" />',
'if($__Context->foo->bar > 100){ ?><img alt="a!@#$%^&*()_-=[]{}?/" src="' . $this->baseurl . 'tests/unit/classes/common/mobile.gif" /><?php } ?>' 'if($__Context->foo->bar > 100){ ?><img alt="a!@#$%^&*()_-=[]{}?/" src="' . $this->baseurl . 'tests/_data/common/mobile.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img src="../common/mobile.gif" cond="$foo->bar" />', '<img src="../common/mobile.gif" cond="$foo->bar" />',
'if($__Context->foo->bar ?? false){ ?><img src="' . $this->baseurl . 'tests/unit/classes/common/mobile.gif" /><?php } ?>' 'if($__Context->foo->bar ?? false){ ?><img src="' . $this->baseurl . 'tests/_data/common/mobile.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img class="tmp_class" cond="!$module_info->title" src="../img/common/blank.gif" />', '<img class="tmp_class" cond="!$module_info->title" src="../img/common/blank.gif" />',
'if(!$__Context->module_info->title){ ?><img class="tmp_class" src="' . $this->baseurl . 'tests/unit/classes/img/common/blank.gif" /><?php } ?>' 'if(!$__Context->module_info->title){ ?><img class="tmp_class" src="' . $this->baseurl . 'tests/_data/img/common/blank.gif" /><?php } ?>'
), ),
// https://github.com/xpressengine/xe-core/issues/1510 // https://github.com/xpressengine/xe-core/issues/1510
array( array(
'<img cond="$mi->title" class="tmp_class"|cond="$mi->use" src="../img/common/blank.gif" />', '<img cond="$mi->title" class="tmp_class"|cond="$mi->use" src="../img/common/blank.gif" />',
'if($__Context->mi->title ?? false){ ?><img<?php if($__Context->mi->use){ ?> class="tmp_class"<?php } ?> src="' . $this->baseurl . 'tests/unit/classes/img/common/blank.gif" /><?php } ?>' 'if($__Context->mi->title ?? false){ ?><img<?php if($__Context->mi->use){ ?> class="tmp_class"<?php } ?> src="' . $this->baseurl . 'tests/_data/img/common/blank.gif" /><?php } ?>'
), ),
array( array(
'<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />', '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" /> <?php if($__Context->foo->bar ?? false){ ?><img alt="alt" src="' . $this->baseurl . 'tests/unit/classes/common/mobile.gif" /><?php } ?>' '?><input foo="bar" /> <?php if($__Context->foo->bar ?? false){ ?><img alt="alt" src="' . $this->baseurl . 'tests/_data/common/mobile.gif" /><?php } ?>'
), ),
array( array(
'<input foo="bar" />' . "\r\n" . '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />', '<input foo="bar" />' . "\r\n" . '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" />' . "\n" . '<input foo="bar" /> <?php if($__Context->foo->bar ?? false){ ?><img alt="alt" src="' . $this->baseurl . 'tests/unit/classes/common/mobile.gif" /><?php } ?>' '?><input foo="bar" />' . "\n" . '<input foo="bar" /> <?php if($__Context->foo->bar ?? false){ ?><img alt="alt" src="' . $this->baseurl . 'tests/_data/common/mobile.gif" /><?php } ?>'
), ),
array( array(
'asf <img src="{$foo->bar}" />', 'asf <img src="{$foo->bar}" />',
@ -272,11 +272,11 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
), ),
array( array(
'<img alt="" '.PHP_EOL.' src="../whatever/img.png" />', '<img alt="" '.PHP_EOL.' src="../whatever/img.png" />',
'?><img alt="" '.PHP_EOL.' src="' . $this->baseurl . 'tests/unit/classes/whatever/img.png" />' '?><img alt="" '.PHP_EOL.' src="' . $this->baseurl . 'tests/_data/whatever/img.png" />'
), ),
array( array(
'<input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="../whatever/img.png" /> <input>asdf src="../img/img.gif" asdf</input>', '<input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="../whatever/img.png" /> <input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="' . $this->baseurl . 'tests/unit/classes/whatever/img.png" /> <input>asdf src="../img/img.gif" asdf</input>' '?><input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="' . $this->baseurl . 'tests/_data/whatever/img.png" /> <input>asdf src="../img/img.gif" asdf</input>'
), ),
array( array(
'<input>asdf src="../img/img.gif" asdf</input>', '<input>asdf src="../img/img.gif" asdf</input>',
@ -289,7 +289,7 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
// srcset (PR #1544) // srcset (PR #1544)
array( array(
'<img src="./img/sticker_banner_960w.png" alt="this is a test image." srcset="https://abc.com/static/img/test@2x.png 2x, http://abc.com/static/test@2.5x.png 2.5x,../img/test@3x.png 3x, ../img/test_960w.png 960w, {$mid}/image.png 480w">', '<img src="./img/sticker_banner_960w.png" alt="this is a test image." srcset="https://abc.com/static/img/test@2x.png 2x, http://abc.com/static/test@2.5x.png 2.5x,../img/test@3x.png 3x, ../img/test_960w.png 960w, {$mid}/image.png 480w">',
'?><img src="' . $this->baseurl . 'tests/unit/classes/template/img/sticker_banner_960w.png" alt="this is a test image." srcset="https://abc.com/static/img/test@2x.png 2x, http://abc.com/static/test@2.5x.png 2.5x, ' . $this->baseurl . 'tests/unit/classes/img/test@3x.png 3x, ' . $this->baseurl . 'tests/unit/classes/img/test_960w.png 960w, <?php echo $__Context->mid ?? \'\' ?>/image.png 480w">' '?><img src="' . $this->baseurl . 'tests/_data/template/img/sticker_banner_960w.png" alt="this is a test image." srcset="https://abc.com/static/img/test@2x.png 2x, http://abc.com/static/test@2.5x.png 2.5x, ' . $this->baseurl . 'tests/_data/img/test@3x.png 3x, ' . $this->baseurl . 'tests/_data/img/test_960w.png 960w, <?php echo $__Context->mid ?? \'\' ?>/image.png 480w">'
), ),
// Rhymix improvements (PR #604) // Rhymix improvements (PR #604)
array( array(
@ -505,7 +505,7 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
foreach ($tests as $test) foreach ($tests as $test)
{ {
$tmpl = new TemplateHandlerWrapper; $tmpl = new TemplateHandlerWrapper;
$tmpl->init(__DIR__ . '/template', 'no_file.html'); $tmpl->init('./tests/_data/template', 'no_file.html');
$result = $tmpl->parse($test[0]); $result = $tmpl->parse($test[0]);
$between = str_starts_with($test[1], '?>') ? '' : ' '; $between = str_starts_with($test[1], '?>') ? '' : ' ';
$this->assertEquals($this->prefix . $between . $test[1], $result); $this->assertEquals($this->prefix . $between . $test[1], $result);
@ -515,7 +515,7 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
public function testParseNoContent() public function testParseNoContent()
{ {
$tmpl = new TemplateHandlerWrapper; $tmpl = new TemplateHandlerWrapper;
$tmpl->init(__DIR__ . '/template', 'no_file.html'); $tmpl->init('./tests/_data/template', 'no_file.html');
$result = $tmpl->parse(null); $result = $tmpl->parse(null);
$this->assertEquals('', $result); $this->assertEquals('', $result);
@ -524,7 +524,7 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
public function testCompileDirect() public function testCompileDirect()
{ {
$tmpl = TemplateHandler::getInstance(); $tmpl = TemplateHandler::getInstance();
$result = $tmpl->compileDirect(__DIR__ . '/template', 'sample.html'); $result = $tmpl->compileDirect('./tests/_data/template', 'sample.html');
$result = trim($result); $result = trim($result);
$this->assertEquals($this->prefix . ' if($__Context->has_blog ?? false){ ?><a href="http://mygony.com">Taggon\'s blog</a><?php } ?>'.PHP_EOL.'<!--#Meta://external.host/js.js--><?php Context::loadFile([\'//external.host/js.js\', \'\', \'tests\', \'\']); ?>', $result); $this->assertEquals($this->prefix . ' if($__Context->has_blog ?? false){ ?><a href="http://mygony.com">Taggon\'s blog</a><?php } ?>'.PHP_EOL.'<!--#Meta://external.host/js.js--><?php Context::loadFile([\'//external.host/js.js\', \'\', \'tests\', \'\']); ?>', $result);