/**
* 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 ) {
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 );
* @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
*/