mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Flesh out Parser tests; handle ParseError
This commit is contained in:
parent
8389b93178
commit
ac6ec00354
@ -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 . '<?php ');
|
||||
|
||||
//TODO under what circumstances would we ever get a FALSE? Could not induce one
|
||||
if ($result === false)
|
||||
try
|
||||
{
|
||||
$result = eval('?>' . $template . '<?php ');
|
||||
} catch (\ParseError $e)
|
||||
{
|
||||
ob_end_clean();
|
||||
throw ViewException::forTagSyntaxError(str_replace(['?>', '<?php '], '', $template));
|
||||
}
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
@ -653,7 +649,7 @@ class Parser extends View
|
||||
$escape = false;
|
||||
}
|
||||
// If no `esc` filter is found, then we'll need to add one.
|
||||
elseif ( ! preg_match('/^|\s+esc/', $key))
|
||||
elseif ( ! preg_match('/\s+esc/', $key))
|
||||
{
|
||||
$escape = 'html';
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ class ParserTest extends \CIUnitTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->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 = '<h1>Hello World</h1>';
|
||||
$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(), '<ul class="pagination">') > 0);
|
||||
}
|
||||
|
||||
public function testRenderCantFindView()
|
||||
{
|
||||
$this->expectException(ViewException::class);
|
||||
|
Loading…
x
Reference in New Issue
Block a user