diff --git a/system/View/Parser.php b/system/View/Parser.php index 89f7dd7bd0..069ca1733d 100644 --- a/system/View/Parser.php +++ b/system/View/Parser.php @@ -297,15 +297,12 @@ class Parser extends View } //-------------------------------------------------------------------- - //FIXME the following method does not appear to be used anywhere, so commented out // protected function is_assoc($arr) // { // return array_keys($arr) !== range(0, count($arr) - 1); // } - //-------------------------------------------------------------------- - //FIXME the following method does not appear to be used anywhere, so commented out // function strpos_all($haystack, $needle) // { @@ -318,7 +315,6 @@ class Parser extends View // } // return $allpos; // } - //-------------------------------------------------------------------- /** @@ -526,14 +522,14 @@ class Parser extends View // Parse the PHP itself, or insert an error so they can debug ob_start(); extract($this->data); - $result = eval('?>' . $template . '' . $template . '', 'loader = new \CodeIgniter\Autoloader\FileLocator(new \Config\Autoload()); - $this->viewsDir = __DIR__.'/Views'; - $this->config = new Config\View(); + $this->loader = new \CodeIgniter\Autoloader\FileLocator(new \Config\Autoload()); + $this->viewsDir = __DIR__ . '/Views'; + $this->config = new Config\View(); } // -------------------------------------------------------------------- @@ -173,11 +173,11 @@ class ParserTest extends \CIUnitTestCase $eagle->home = 'Rockies'; $data = [ 'birds' => [[ - 'pop' => $eagle, - 'mom' => 'Owl', - 'kids' => ['Tom', 'Dick', 'Harry'], - 'home' => opendir('.'), - ]], + 'pop' => $eagle, + 'mom' => 'Owl', + 'kids' => ['Tom', 'Dick', 'Harry'], + 'home' => opendir('.'), + ]], ]; $template = "{ birds }{mom} and {pop} work at {home}{/birds}"; @@ -285,6 +285,32 @@ class ParserTest extends \CIUnitTestCase $this->assertEquals('http%3A%2F%2Ffoo.com', $parser->renderString($template)); } + public function testNoEscapingSetData() + { + $parser = new Parser($this->config, $this->viewsDir, $this->loader); + + $template = '{ foo | noescape}'; + + $parser->setData(['foo' => 'http://foo.com'], 'unknown'); + $this->assertEquals('http://foo.com', $parser->renderString($template)); + } + + public function testAutoEscaping() + { + $parser = new Parser($this->config, $this->viewsDir, $this->loader); + $parser->setData(['foo' => 'http://foo.com'], 'unknown'); + + $this->assertEquals('html', $parser->shouldAddEscaping('{ foo | this | that }')); + } + + public function testAutoEscapingNot() + { + $parser = new Parser($this->config, $this->viewsDir, $this->loader); + $parser->setData(['foo' => 'http://foo.com'], 'unknown'); + + $this->assertEquals(false, $parser->shouldAddEscaping('{ foo | noescape }')); + } + //-------------------------------------------------------------------- public function testFilterWithNoArgument() @@ -444,6 +470,24 @@ class ParserTest extends \CIUnitTestCase //-------------------------------------------------------------------- + public function testConditionalBadSyntax() + { + $this->expectException(ViewException::class); + $parser = new Parser($this->config, $this->viewsDir, $this->loader); + $data = [ + 'doit' => true, + 'dontdoit' => false + ]; + + // the template is purposefully malformed + $template = "{if doit}Howdy{elseif doit}Welcome{ endif )}"; + + $parser->setData($data); + $this->assertEquals('HowdyWelcome', $parser->renderString($template)); + } + + //-------------------------------------------------------------------- + public function testWontParsePHP() { $parser = new Parser($this->config, $this->viewsDir, $this->loader); @@ -666,9 +710,9 @@ class ParserTest extends \CIUnitTestCase $parser->setVar('teststring', 'Hello World'); $expected = '

Hello World

'; - $this->assertEquals($expected, $parser->render('template1', ['cache' => 10])); + $this->assertEquals($expected, $parser->render('template1', ['cache' => 10, 'cache_name' => 'HelloWorld'])); // this second renderings should go thru the cache - $this->assertEquals($expected, $parser->render('template1', ['cache' => 10])); + $this->assertEquals($expected, $parser->render('template1', ['cache' => 10, 'cache_name' => 'HelloWorld'])); } //-------------------------------------------------------------------- @@ -682,15 +726,6 @@ class ParserTest extends \CIUnitTestCase $this->assertEquals($expected, $parser->render('Simpler')); } - public function testRenderSearchesForView() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_GET = []; - $this->config = new \Config\Pager(); - $this->pager = new \CodeIgniter\Pager\Pager($this->config, \Config\Services::parser()); - $this->assertTrue(strpos($this->pager->links(), '