| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Html\TemplateParser; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use Wikimedia\ObjectCache\BagOStuff; |
| 6 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class TemplateParserIntegrationTest extends MediaWikiIntegrationTestCase { |
| 13 |
|
| 14 |
private const NAME = 'foobar'; |
| 15 |
private const RESULT = "hello world!\n"; |
| 16 |
private const DIR = __DIR__ . '/../../../data/templates'; |
| 17 |
private const SECRET_KEY = 'foo'; |
| 18 |
|
| 19 |
protected function setUp(): void { |
| 20 |
parent::setUp(); |
| 21 |
|
| 22 |
$this->overrideConfigValue( MainConfigNames::SecretKey, self::SECRET_KEY ); |
| 23 |
} |
| 24 |
|
| 25 |
public function testGetTemplateNeverCacheWithoutSecretKey() { |
| 26 |
$this->overrideConfigValue( MainConfigNames::SecretKey, false ); |
| 27 |
|
| 28 |
|
| 29 |
$cache = $this->createMock( BagOStuff::class ); |
| 30 |
$cache->expects( $this->never() )->method( 'get' ); |
| 31 |
$cache->expects( $this->never() )->method( 'set' ); |
| 32 |
|
| 33 |
$tp = new TemplateParser( self::DIR, $cache ); |
| 34 |
$this->assertEquals( self::RESULT, $tp->processTemplate( self::NAME, [] ) ); |
| 35 |
} |
| 36 |
|
| 37 |
public function testGetTemplateCachesCompilationResult() { |
| 38 |
$store = null; |
| 39 |
|
| 40 |
|
| 41 |
$cache1 = $this->createMock( BagOStuff::class ); |
| 42 |
$cache1->expects( $this->once() )->method( 'get' )->willReturn( false ); |
| 43 |
$cache1->expects( $this->once() )->method( 'set' ) |
| 44 |
->willReturnCallback( static function ( $key, $val ) use ( &$store ) { |
| 45 |
$store = [ 'key' => $key, 'val' => $val ]; |
| 46 |
} ); |
| 47 |
|
| 48 |
$tp1 = new TemplateParser( self::DIR, $cache1 ); |
| 49 |
$this->assertEquals( self::RESULT, $tp1->processTemplate( self::NAME, [] ) ); |
| 50 |
|
| 51 |
|
| 52 |
$this->assertEquals( |
| 53 |
[ |
| 54 |
'phpCode', |
| 55 |
'files', |
| 56 |
'filesHash', |
| 57 |
'integrityHash', |
| 58 |
], |
| 59 |
array_keys( $store['val'] ), |
| 60 |
'keys of the cached array' |
| 61 |
); |
| 62 |
$this->assertEquals( |
| 63 |
FileContentsHasher::getFileContentsHash( [ |
| 64 |
self::DIR . '/' . self::NAME . '.mustache' |
| 65 |
] ), |
| 66 |
$store['val']['filesHash'], |
| 67 |
'content hash for the compiled template' |
| 68 |
); |
| 69 |
$this->assertEquals( |
| 70 |
hash_hmac( 'sha256', $store['val']['phpCode'], self::SECRET_KEY ), |
| 71 |
$store['val']['integrityHash'], |
| 72 |
'integrity hash for the compiled template' |
| 73 |
); |
| 74 |
|
| 75 |
|
| 76 |
$cache2 = $this->createMock( BagOStuff::class ); |
| 77 |
$cache2->expects( $this->once() )->method( 'get' ) |
| 78 |
->willReturnCallback( static function ( $key ) use ( &$store ) { |
| 79 |
return $key === $store['key'] ? $store['val'] : false; |
| 80 |
} ); |
| 81 |
$cache2->expects( $this->never() )->method( 'set' ); |
| 82 |
|
| 83 |
$tp2 = $this->getMockBuilder( TemplateParser::class ) |
| 84 |
->setConstructorArgs( [ self::DIR, $cache2 ] ) |
| 85 |
->onlyMethods( [ 'compile' ] ) |
| 86 |
->getMock(); |
| 87 |
$tp2->expects( $this->never() )->method( 'compile' ); |
| 88 |
|
| 89 |
$this->assertEquals( self::RESULT, $tp2->processTemplate( self::NAME, [] ) ); |
| 90 |
} |
| 91 |
|
| 92 |
public function testGetTemplateInvalidatesCacheWhenFilesHashIsInvalid() { |
| 93 |
$store = null; |
| 94 |
|
| 95 |
|
| 96 |
$cache1 = $this->createMock( BagOStuff::class ); |
| 97 |
$cache1->expects( $this->once() )->method( 'get' )->willReturn( false ); |
| 98 |
$cache1->expects( $this->once() )->method( 'set' ) |
| 99 |
->willReturnCallback( static function ( $key, $val ) use ( &$store ) { |
| 100 |
$store = [ 'key' => $key, 'val' => $val ]; |
| 101 |
} ); |
| 102 |
|
| 103 |
$tp1 = new TemplateParser( self::DIR, $cache1 ); |
| 104 |
$this->assertEquals( self::RESULT, $tp1->processTemplate( self::NAME, [] ) ); |
| 105 |
|
| 106 |
|
| 107 |
$store['val']['filesHash'] = 'baz'; |
| 108 |
|
| 109 |
|
| 110 |
$cache2 = $this->createMock( BagOStuff::class ); |
| 111 |
$cache2->expects( $this->once() )->method( 'get' ) |
| 112 |
->willReturnCallback( static function ( $key ) use ( &$store ) { |
| 113 |
return $key === $store['key'] ? $store['val'] : false; |
| 114 |
} ); |
| 115 |
$cache2->expects( $this->once() )->method( 'set' ); |
| 116 |
|
| 117 |
$tp2 = $this->getMockBuilder( TemplateParser::class ) |
| 118 |
->setConstructorArgs( [ self::DIR, $cache2 ] ) |
| 119 |
->onlyMethods( [ 'compile' ] ) |
| 120 |
->getMock(); |
| 121 |
$tp2->expects( $this->once() )->method( 'compile' ) |
| 122 |
->willReturn( $store['val'] ); |
| 123 |
|
| 124 |
$this->assertEquals( self::RESULT, $tp2->processTemplate( self::NAME, [] ) ); |
| 125 |
} |
| 126 |
|
| 127 |
public function testGetTemplateInvalidatesCacheWhenIntegrityHashIsInvalid() { |
| 128 |
$store = null; |
| 129 |
|
| 130 |
|
| 131 |
$cache1 = $this->createMock( BagOStuff::class ); |
| 132 |
$cache1->expects( $this->once() )->method( 'get' )->willReturn( false ); |
| 133 |
$cache1->expects( $this->once() )->method( 'set' ) |
| 134 |
->willReturnCallback( static function ( $key, $val ) use ( &$store ) { |
| 135 |
$store = [ 'key' => $key, 'val' => $val ]; |
| 136 |
} ); |
| 137 |
|
| 138 |
$tp1 = new TemplateParser( self::DIR, $cache1 ); |
| 139 |
$this->assertEquals( self::RESULT, $tp1->processTemplate( self::NAME, [] ) ); |
| 140 |
|
| 141 |
|
| 142 |
$store['val']['integrityHash'] = 'foo'; |
| 143 |
|
| 144 |
|
| 145 |
$cache2 = $this->createMock( BagOStuff::class ); |
| 146 |
$cache2->expects( $this->once() )->method( 'get' ) |
| 147 |
->willReturnCallback( static function ( $key ) use ( &$store ) { |
| 148 |
return $key === $store['key'] ? $store['val'] : false; |
| 149 |
} ); |
| 150 |
$cache2->expects( $this->once() )->method( 'set' ); |
| 151 |
|
| 152 |
$tp2 = $this->getMockBuilder( TemplateParser::class ) |
| 153 |
->setConstructorArgs( [ self::DIR, $cache2 ] ) |
| 154 |
->onlyMethods( [ 'compile' ] ) |
| 155 |
->getMock(); |
| 156 |
$tp2->expects( $this->once() )->method( 'compile' ) |
| 157 |
->willReturn( $store['val'] ); |
| 158 |
|
| 159 |
$this->assertEquals( self::RESULT, $tp2->processTemplate( self::NAME, [] ) ); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function testProcessTemplate( $name, $args, $result, $exception = false ) { |
| 166 |
$tp = new TemplateParser( self::DIR, new EmptyBagOStuff ); |
| 167 |
if ( $exception ) { |
| 168 |
$this->expectException( $exception ); |
| 169 |
} |
| 170 |
$this->assertEquals( $result, $tp->processTemplate( $name, $args ) ); |
| 171 |
} |
| 172 |
|
| 173 |
public static function provideProcessTemplate() { |
| 174 |
return [ |
| 175 |
[ |
| 176 |
'foobar', |
| 177 |
[], |
| 178 |
"hello world!\n" |
| 179 |
], |
| 180 |
[ |
| 181 |
'foobar_args', |
| 182 |
[ |
| 183 |
'planet' => 'world', |
| 184 |
], |
| 185 |
self::RESULT, |
| 186 |
], |
| 187 |
[ |
| 188 |
'../foobar', |
| 189 |
[], |
| 190 |
false, |
| 191 |
UnexpectedValueException::class |
| 192 |
], |
| 193 |
[ |
| 194 |
"\000../foobar", |
| 195 |
[], |
| 196 |
false, |
| 197 |
UnexpectedValueException::class |
| 198 |
], |
| 199 |
[ |
| 200 |
'/', |
| 201 |
[], |
| 202 |
false, |
| 203 |
UnexpectedValueException::class |
| 204 |
], |
| 205 |
[ |
| 206 |
|
| 207 |
'baz<', |
| 208 |
[], |
| 209 |
false, |
| 210 |
UnexpectedValueException::class |
| 211 |
], |
| 212 |
[ |
| 213 |
'\\foo', |
| 214 |
[], |
| 215 |
false, |
| 216 |
UnexpectedValueException::class |
| 217 |
], |
| 218 |
[ |
| 219 |
'C:\bar', |
| 220 |
[], |
| 221 |
false, |
| 222 |
UnexpectedValueException::class |
| 223 |
], |
| 224 |
[ |
| 225 |
"foo\000bar", |
| 226 |
[], |
| 227 |
false, |
| 228 |
UnexpectedValueException::class |
| 229 |
], |
| 230 |
[ |
| 231 |
'nonexistenttemplate', |
| 232 |
[], |
| 233 |
false, |
| 234 |
RuntimeException::class, |
| 235 |
], |
| 236 |
[ |
| 237 |
'has_partial', |
| 238 |
[ |
| 239 |
'planet' => 'world', |
| 240 |
], |
| 241 |
"Partial hello world!\n in here\n", |
| 242 |
], |
| 243 |
[ |
| 244 |
'bad_partial', |
| 245 |
[], |
| 246 |
false, |
| 247 |
Exception::class, |
| 248 |
], |
| 249 |
[ |
| 250 |
'invalid_syntax', |
| 251 |
[], |
| 252 |
false, |
| 253 |
Exception::class |
| 254 |
], |
| 255 |
[ |
| 256 |
'parentvars', |
| 257 |
[ |
| 258 |
'foo' => 'f', |
| 259 |
'bar' => [ |
| 260 |
[ 'baz' => 'x' ], |
| 261 |
[ 'baz' => 'y' ] |
| 262 |
] |
| 263 |
], |
| 264 |
"f\n\tf x\n\tf y\n" |
| 265 |
] |
| 266 |
]; |
| 267 |
} |
| 268 |
|
| 269 |
public function testEnableRecursivePartials() { |
| 270 |
$tp = new TemplateParser( self::DIR, new EmptyBagOStuff ); |
| 271 |
$data = [ 'r' => [ 'r' => [ 'r' => [] ] ] ]; |
| 272 |
|
| 273 |
$tp->enableRecursivePartials( true ); |
| 274 |
$this->assertEquals( 'rrr', $tp->processTemplate( 'recurse', $data ) ); |
| 275 |
|
| 276 |
$tp->enableRecursivePartials( false ); |
| 277 |
$this->expectException( Exception::class ); |
| 278 |
$tp->processTemplate( 'recurse', $data ); |
| 279 |
} |
| 280 |
|
| 281 |
public function testCompileReturnsPHPCodeAndMetadata() { |
| 282 |
$store = null; |
| 283 |
|
| 284 |
|
| 285 |
$cache = $this->createMock( BagOStuff::class ); |
| 286 |
$cache->expects( $this->once() )->method( 'get' )->willReturn( false ); |
| 287 |
$cache->expects( $this->once() )->method( 'set' ) |
| 288 |
->willReturnCallback( static function ( $key, $val ) use ( &$store ) { |
| 289 |
$store = [ 'key' => $key, 'val' => $val ]; |
| 290 |
} ); |
| 291 |
$tp = new TemplateParser( self::DIR, $cache ); |
| 292 |
$tp->processTemplate( 'has_partial', [] ); |
| 293 |
|
| 294 |
|
| 295 |
$expectedFiles = [ |
| 296 |
self::DIR . '/has_partial.mustache', |
| 297 |
self::DIR . '/foobar_args.mustache', |
| 298 |
]; |
| 299 |
$this->assertEquals( |
| 300 |
$expectedFiles, |
| 301 |
$store['val']['files'], |
| 302 |
'track all files read during the compilation' |
| 303 |
); |
| 304 |
$this->assertEquals( |
| 305 |
FileContentsHasher::getFileContentsHash( $expectedFiles ), |
| 306 |
$store['val'][ 'filesHash' ], |
| 307 |
'hash of all files read during the compilation' |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
public function testGetTemplateCachingHandlesRecursivePartials() { |
| 312 |
$store = null; |
| 313 |
|
| 314 |
$cache = $this->createMock( BagOStuff::class ); |
| 315 |
$cache->expects( $this->once() )->method( 'get' )->willReturn( false ); |
| 316 |
$cache->expects( $this->once() )->method( 'set' ) |
| 317 |
->willReturnCallback( static function ( $key, $val ) use ( &$store ) { |
| 318 |
$store = [ 'key' => $key, 'val' => $val ]; |
| 319 |
} ); |
| 320 |
|
| 321 |
$tp = new TemplateParser( self::DIR, $cache ); |
| 322 |
$tp->enableRecursivePartials( true ); |
| 323 |
|
| 324 |
$data = [ 'r' => [ 'r' => [ 'r' => [] ] ] ]; |
| 325 |
$tp->processTemplate( 'recurse', $data ); |
| 326 |
|
| 327 |
$this->assertArrayEquals( |
| 328 |
[ self::DIR . '/recurse.mustache' ], |
| 329 |
$store['val']['files'], |
| 330 |
'The hash is computed from unique template files.' |
| 331 |
); |
| 332 |
} |
| 333 |
} |