| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use MediaWiki\Page\PageStoreRecord; |
| 6 |
use MediaWiki\Parser\ParserCacheFilter; |
| 7 |
use MediaWiki\Parser\ParserOptions; |
| 8 |
use MediaWiki\Parser\ParserOutput; |
| 9 |
use MediaWikiUnitTestCase; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ParserCacheFilterTest extends MediaWikiUnitTestCase { |
| 15 |
|
| 16 |
private function makeParserOutput( $profile ) { |
| 17 |
$map = []; |
| 18 |
|
| 19 |
foreach ( $profile as $clock => $duration ) { |
| 20 |
$map[] = [ $clock, $duration ]; |
| 21 |
} |
| 22 |
|
| 23 |
$mock = $this->createNoOpMock( ParserOutput::class, [ 'getTimeProfile' ] ); |
| 24 |
$mock->method( 'getTimeProfile' )->willReturnMap( $map ); |
| 25 |
return $mock; |
| 26 |
} |
| 27 |
|
| 28 |
private function makePageRecord() { |
| 29 |
return new PageStoreRecord( (object)[ |
| 30 |
'page_id' => 7, |
| 31 |
'page_namespace' => NS_MAIN, |
| 32 |
'page_title' => 'Testing', |
| 33 |
'page_is_redirect' => 0, |
| 34 |
'page_is_new' => 0, |
| 35 |
'page_latest' => 12345, |
| 36 |
'page_touched' => '20220101334455', |
| 37 |
], false ); |
| 38 |
} |
| 39 |
|
| 40 |
private function makeParserOptions() { |
| 41 |
return $this->createNoOpMock( ParserOptions::class ); |
| 42 |
} |
| 43 |
|
| 44 |
public static function provideShouldCache() { |
| 45 |
$profile = [ |
| 46 |
'wall' => 1.0, |
| 47 |
'cpu' => 0.5 |
| 48 |
]; |
| 49 |
|
| 50 |
yield 'no config' => [ [], $profile ]; |
| 51 |
yield 'use default' => [ |
| 52 |
[ 'default' => [ 'minCpuTime' => 0.25 ] ], |
| 53 |
$profile |
| 54 |
]; |
| 55 |
yield 'ignore default' => [ |
| 56 |
[ |
| 57 |
'default' => [ 'minCpuTime' => 2 ], |
| 58 |
NS_MAIN => [ 'minCpuTime' => 0.25 ], |
| 59 |
], |
| 60 |
$profile |
| 61 |
]; |
| 62 |
yield 'no profile' => [ |
| 63 |
[ 'default' => [ 'minCpuTime' => 2 ] ], |
| 64 |
[] |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function testShouldCache( array $config, array $profile ) { |
| 72 |
$po = $this->makeParserOutput( $profile ); |
| 73 |
$filter = new ParserCacheFilter( $config ); |
| 74 |
|
| 75 |
$page = $this->makePageRecord(); |
| 76 |
$options = $this->makeParserOptions(); |
| 77 |
|
| 78 |
$this->assertTrue( $filter->shouldCache( $po, $page, $options ) ); |
| 79 |
} |
| 80 |
|
| 81 |
public static function provideShouldNotCache() { |
| 82 |
$profile = [ |
| 83 |
'wall' => 1.0, |
| 84 |
'cpu' => 0.5 |
| 85 |
]; |
| 86 |
|
| 87 |
yield 'use default' => [ |
| 88 |
[ 'default' => [ 'minCpuTime' => 0.7 ] ], |
| 89 |
$profile |
| 90 |
]; |
| 91 |
yield 'ignore default' => [ |
| 92 |
[ |
| 93 |
'default' => [ 'minCpuTime' => 0.25 ], |
| 94 |
NS_MAIN => [ 'minCpuTime' => 2 ], |
| 95 |
], |
| 96 |
$profile |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function testShouldNotCache( array $config, array $profile ) { |
| 104 |
$po = $this->makeParserOutput( $profile ); |
| 105 |
$filter = new ParserCacheFilter( $config ); |
| 106 |
|
| 107 |
$page = $this->makePageRecord(); |
| 108 |
$options = $this->makeParserOptions(); |
| 109 |
|
| 110 |
$this->assertFalse( $filter->shouldCache( $po, $page, $options ) ); |
| 111 |
} |
| 112 |
|
| 113 |
} |