wiki.techinc.nl/tests/phpunit/includes/TemplateParserTest.php
Gergő Tisza 49350108bf Enable recursive partials in TemplateParser
Recursive partials are the only way to handle tree-like structures
such as nested lists. Allow setting FLAG_RUNTIMEPARTIAL in LightnCandy
so they can be used.

Since this has a slight performance impact (makes partial invocations
evaluation-time functions calls instead of compilation-time transclusions)
make it optional.

Change-Id: Ie37105a9f1ff92e1a79bfcd9f8578965e3d347f0
2017-09-10 05:51:07 +00:00

125 lines
2.2 KiB
PHP

<?php
/**
* @group Templates
*/
class TemplateParserTest extends MediaWikiTestCase {
protected $templateDir;
protected function setUp() {
parent::setUp();
$this->setMwGlobals( [
'wgSecretKey' => 'foo',
] );
$this->templateDir = dirname( __DIR__ ) . '/data/templates/';
}
/**
* @dataProvider provideProcessTemplate
* @covers TemplateParser::processTemplate
* @covers TemplateParser::getTemplate
* @covers TemplateParser::getTemplateFilename
*/
public function testProcessTemplate( $name, $args, $result, $exception = false ) {
if ( $exception ) {
$this->setExpectedException( $exception );
}
$tp = new TemplateParser( $this->templateDir );
$this->assertEquals( $result, $tp->processTemplate( $name, $args ) );
}
public static function provideProcessTemplate() {
return [
[
'foobar',
[],
"hello world!\n"
],
[
'foobar_args',
[
'planet' => 'world',
],
"hello world!\n",
],
[
'../foobar',
[],
false,
'UnexpectedValueException'
],
[
"\000../foobar",
[],
false,
'UnexpectedValueException'
],
[
'/',
[],
false,
'UnexpectedValueException'
],
[
// Allegedly this can strip ext in windows.
'baz<',
[],
false,
'UnexpectedValueException'
],
[
'\\foo',
[],
false,
'UnexpectedValueException'
],
[
'C:\bar',
[],
false,
'UnexpectedValueException'
],
[
"foo\000bar",
[],
false,
'UnexpectedValueException'
],
[
'nonexistenttemplate',
[],
false,
'RuntimeException',
],
[
'has_partial',
[
'planet' => 'world',
],
"Partial hello world!\n in here\n",
],
[
'bad_partial',
[],
false,
'Exception',
],
];
}
public function testEnableRecursivePartials() {
$tp = new TemplateParser( $this->templateDir );
$data = [ 'r' => [ 'r' => [ 'r' => [] ] ] ];
$tp->enableRecursivePartials( true );
$this->assertEquals( 'rrr', $tp->processTemplate( 'recurse', $data ) );
$tp->enableRecursivePartials( false );
$this->setExpectedException( 'Exception' );
$tp->processTemplate( 'recurse', $data );
}
}