diff --git a/includes/TemplateParser.php b/includes/TemplateParser.php index 65904a01e08..0131fe69455 100644 --- a/includes/TemplateParser.php +++ b/includes/TemplateParser.php @@ -74,7 +74,7 @@ class TemplateParser { /** * Returns a given template function if found, otherwise throws an exception. * @param string $templateName The name of the template (without file suffix) - * @return Function + * @return callable * @throws RuntimeException */ public function getTemplate( $templateName ) { @@ -114,11 +114,8 @@ class TemplateParser { if ( !$code ) { $code = $this->compileForEval( $fileContents, $filename ); - // Prefix the code with a keyed hash (64 hex chars) as an integrity check - $code = hash_hmac( 'sha256', $code, $secretKey ) . $code; - - // Cache the compiled PHP code - $cache->set( $key, $code ); + // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check + $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code ); } else { // Verify the integrity of the cached PHP code $keyedHash = substr( $code, 0, 64 ); diff --git a/tests/phpunit/data/templates/foobar.mustache b/tests/phpunit/data/templates/foobar.mustache new file mode 100644 index 00000000000..a0423896973 --- /dev/null +++ b/tests/phpunit/data/templates/foobar.mustache @@ -0,0 +1 @@ +hello world! diff --git a/tests/phpunit/includes/TemplateParserTest.php b/tests/phpunit/includes/TemplateParserTest.php index ccfccd1d75d..f884a8ef17b 100644 --- a/tests/phpunit/includes/TemplateParserTest.php +++ b/tests/phpunit/includes/TemplateParserTest.php @@ -4,6 +4,58 @@ * @group Templates */ class TemplateParserTest extends MediaWikiTestCase { + + protected $templateDir; + + protected function setUp() { + parent::setUp(); + + $this->setMwGlobals( array( + 'wgSecretKey' => 'foo', + 'wgMemc' => new EmptyBagOStuff(), + ) ); + + $this->templateDir = dirname( __DIR__ ) . '/data/templates/'; + } + + /** + * @covers TemplateParser::getTemplateFilename + * @dataProvider provideGetTemplateFilename + */ + public function testGetTemplateFilename( $dir, $name, $result, $exception = false ) { + if ( $exception ) { + $this->setExpectedException( $exception ); + } + + $tp = new TemplateParser( $dir ); + $path = $tp->getTemplateFilename( $name ); + $this->assertEquals( $result, $path ); + } + + public static function provideGetTemplateFilename() { + return array( + array( + 'dir/templates', + 'foobar', + 'dir/templates/foobar.mustache', + ), + array( + 'dir/templates', + '../foobar', + '', + 'UnexpectedValueException' + ), + ); + } + + /** + * @covers TemplateParser::getTemplate + */ + public function testGetTemplate() { + $tp = new TemplateParser( $this->templateDir ); + $this->assertTrue( is_callable( $tp->getTemplate( 'foobar' ) ) ); + } + /** * @covers TemplateParser::compile */