wiki.techinc.nl/tests/phpunit/includes/TemplateParserTest.php
Kunal Mehta 075ab54a44 Improve @covers for TemplateParserTest
The current @covers tags for TemplateParserTest are an incomplete call
stack for TemplateParser::processTemplate, and miss out on some
functions that are called. Those functions could be repeated in @covers
tags, but it would be burdensome to keep it up to date.

Instead, just use class level @covers for the whole test case. This also
takes care of the previously uncovered testEnableRecursivePartials().

Change-Id: I475ceba26b70a115b40d7735834a07f298f7bf99
2017-12-28 08:53:31 +00:00

123 lines
2.1 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',
],
];
}
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 );
}
}