wiki.techinc.nl/tests/phpunit/includes/TemplateParserTest.php
Timo Tijhof cfcaa33fcc TemplateParser: Don't fatal on cache misses
Also add regression test, and coverage for more methods.

Was trying to eval the code which had the hmac integrity check in front of it,
which causes a syntax error in valid PHP code.

Follows-up db1866da4, 50c50bea2e.

Bug: T93436
Bug: T93511
Change-Id: Ie90074e4885de7340e53f59fdd479f5384b5eac6
2015-03-23 01:38:56 +00:00

82 lines
1.8 KiB
PHP

<?php
/**
* @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
*/
public function testTemplateCompilation() {
$this->assertRegExp(
'/^<\?php return function/',
TemplateParser::compile( "test" ),
'compile a simple mustache template'
);
}
/**
* @covers TemplateParser::compile
*/
public function testTemplateCompilationWithVariable() {
$this->assertRegExp(
'/return \'\'\.htmlentities\(\(string\)\(\(isset\(\$in\[\'value\'\]\) && '
. 'is_array\(\$in\)\) \? \$in\[\'value\'\] : null\), ENT_QUOTES, '
. '\'UTF-8\'\)\.\'\';/',
TemplateParser::compile( "{{value}}" ),
'compile a mustache template with an escaped variable'
);
}
}