2015-01-30 19:31:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Templates
|
2017-12-28 08:31:08 +00:00
|
|
|
* @covers TemplateParser
|
2015-01-30 19:31:44 +00:00
|
|
|
*/
|
|
|
|
|
class TemplateParserTest extends MediaWikiTestCase {
|
2015-03-22 22:01:52 +00:00
|
|
|
|
|
|
|
|
protected $templateDir;
|
|
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
protected function setUp() : void {
|
2015-03-22 22:01:52 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2015-03-22 22:01:52 +00:00
|
|
|
'wgSecretKey' => 'foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-03-22 22:01:52 +00:00
|
|
|
|
|
|
|
|
$this->templateDir = dirname( __DIR__ ) . '/data/templates/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-23 03:06:28 +00:00
|
|
|
* @dataProvider provideProcessTemplate
|
2015-03-22 22:01:52 +00:00
|
|
|
*/
|
2015-03-23 03:06:28 +00:00
|
|
|
public function testProcessTemplate( $name, $args, $result, $exception = false ) {
|
2015-03-22 22:01:52 +00:00
|
|
|
if ( $exception ) {
|
2019-10-06 09:52:39 +00:00
|
|
|
$this->expectException( $exception );
|
2015-03-22 22:01:52 +00:00
|
|
|
}
|
2015-03-23 03:06:28 +00:00
|
|
|
$tp = new TemplateParser( $this->templateDir );
|
|
|
|
|
$this->assertEquals( $result, $tp->processTemplate( $name, $args ) );
|
2015-03-22 22:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-23 03:06:28 +00:00
|
|
|
public static function provideProcessTemplate() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2015-03-22 22:01:52 +00:00
|
|
|
'foobar',
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2015-03-23 03:06:28 +00:00
|
|
|
"hello world!\n"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-03-23 03:06:28 +00:00
|
|
|
'foobar_args',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-03-23 03:06:28 +00:00
|
|
|
'planet' => 'world',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-03-23 03:06:28 +00:00
|
|
|
"hello world!\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-03-22 22:01:52 +00:00
|
|
|
'../foobar',
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2015-03-23 03:06:28 +00:00
|
|
|
false,
|
2015-03-22 22:01:52 +00:00
|
|
|
'UnexpectedValueException'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2017-03-14 04:01:09 +00:00
|
|
|
[
|
|
|
|
|
"\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'
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-03-23 03:06:28 +00:00
|
|
|
'nonexistenttemplate',
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2015-03-23 03:06:28 +00:00
|
|
|
false,
|
|
|
|
|
'RuntimeException',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-07-07 01:32:21 +00:00
|
|
|
'has_partial',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-07-07 01:32:21 +00:00
|
|
|
'planet' => 'world',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-07-07 01:32:21 +00:00
|
|
|
"Partial hello world!\n in here\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-07-07 01:32:21 +00:00
|
|
|
'bad_partial',
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2015-07-07 01:32:21 +00:00
|
|
|
false,
|
|
|
|
|
'Exception',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2018-08-31 03:18:44 +00:00
|
|
|
[
|
|
|
|
|
'parentvars',
|
|
|
|
|
[
|
|
|
|
|
'foo' => 'f',
|
|
|
|
|
'bar' => [
|
|
|
|
|
[ 'baz' => 'x' ],
|
|
|
|
|
[ 'baz' => 'y' ]
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
"f\n\n\tf x\n\n\tf y\n\n"
|
|
|
|
|
]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|
2017-09-10 03:08:43 +00:00
|
|
|
|
|
|
|
|
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 );
|
2019-10-05 15:39:46 +00:00
|
|
|
$this->expectException( Exception::class );
|
2017-09-10 03:08:43 +00:00
|
|
|
$tp->processTemplate( 'recurse', $data );
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 19:31:44 +00:00
|
|
|
}
|