wiki.techinc.nl/tests/phpunit/includes/TemplateParserTest.php
Sam Smith 52f50cd657 TemplateParser: Invalidate cache if partial changes
Changes:

- Update TemplateParser::compile to return all files read during the
  compilation of the template and the hash of those files

- Tweak the cache invalidation logic in TemplateParser::getTemplate
  accordingly. This is made trivial due to the friendly design of the
  FileContentsHasher::getFileContentsHash.

Bug: T113095
Change-Id: I948fdaecf720d7d16c5ccabb2d7f01b5cbf27c90
2020-03-03 11:14:38 +00:00

174 lines
3.2 KiB
PHP

<?php
use Wikimedia\TestingAccessWrapper;
/**
* @group Templates
* @covers TemplateParser
*/
class TemplateParserTest extends MediaWikiTestCase {
protected $templateDir;
protected function setUp() : void {
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->expectException( $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',
],
[
'invalid_syntax',
[],
false,
'Exception'
],
[
'parentvars',
[
'foo' => 'f',
'bar' => [
[ 'baz' => 'x' ],
[ 'baz' => 'y' ]
]
],
"f\n\tf x\n\tf y\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->expectException( Exception::class );
$tp->processTemplate( 'recurse', $data );
}
/**
* @covers TemplateParser::compile
*/
public function testCompileReturnsPHPCodeAndMetadata() {
// Code Under Test
$templateParser = TestingAccessWrapper::newFromObject(
new TemplateParser( $this->templateDir )
);
$compiledTemplate = $templateParser->compile( 'has_partial' );
$this->assertArrayHasKey( 'phpCode', $compiledTemplate );
// ---
$expectedFiles = [
"{$this->templateDir}/has_partial.mustache",
"{$this->templateDir}/foobar_args.mustache",
];
$this->assertEquals(
$expectedFiles,
$compiledTemplate['files'],
'::compile returns all files read during the compilation of the template.'
);
// ---
$this->assertEquals(
FileContentsHasher::getFileContentsHash( $expectedFiles ),
$compiledTemplate[ 'filesHash' ],
'::compile returns the hash of all files read during the compilation of the template.'
);
}
}