wiki.techinc.nl/tests/phpunit/includes/TemplateParserTest.php
Roan Kattouw 3c06d76521 TemplateParser: Pass FLAG_MUSTACHELOOKUP to enable parent context access
This allows variables defined in an outer context to be used in inner
contexts. For example:

<h2>{{foo}}</h2>
<ul>
{{#things}}
    <!-- bar is a property of each thing, foo is an outer variable -->
    <li>{{foo}} is a {{bar}}</li>
{{/things}}
</ul>

Bug: T203209
Change-Id: Ib0ae0fb0b4be6b161f548c79db6fb6f4b831f7c1
2018-09-04 12:39:34 -07:00

134 lines
2.2 KiB
PHP

<?php
/**
* @group Templates
* @covers TemplateParser
*/
class TemplateParserTest extends MediaWikiTestCase {
protected $templateDir;
protected function setUp() {
parent::setUp();
$this->setMwGlobals( [
'wgSecretKey' => 'foo',
] );
$this->templateDir = dirname( __DIR__ ) . '/data/templates/';
}
/**
* @dataProvider provideProcessTemplate
*/
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',
],
[
'parentvars',
[
'foo' => 'f',
'bar' => [
[ 'baz' => 'x' ],
[ 'baz' => 'y' ]
]
],
"f\n\n\tf x\n\n\tf y\n\n"
]
];
}
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::class );
$tp->processTemplate( 'recurse', $data );
}
}