| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Parser\CacheTime; |
| 7 |
use MediaWiki\Parser\ParserOptions; |
| 8 |
use MediaWiki\Utils\MWTimestamp; |
| 9 |
use MediaWikiIntegrationTestCase; |
| 10 |
use Wikimedia\Tests\SerializationTestTrait; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class CacheTimeTest extends MediaWikiIntegrationTestCase { |
| 16 |
use SerializationTestTrait; |
| 17 |
|
| 18 |
protected function setUp(): void { |
| 19 |
parent::setUp(); |
| 20 |
|
| 21 |
MWTimestamp::setFakeTime( ParserCacheSerializationTestCases::FAKE_TIME ); |
| 22 |
$this->overrideConfigValue( |
| 23 |
MainConfigNames::ParserCacheExpireTime, |
| 24 |
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static function getClassToTest(): string { |
| 33 |
return CacheTime::class; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static function getSerializedDataPath(): string { |
| 41 |
return __DIR__ . '/../../data/ParserCache'; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static function getTestInstancesAndAssertions(): array { |
| 49 |
return ParserCacheSerializationTestCases::getCacheTimeTestCases(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function getSupportedSerializationFormats(): array { |
| 57 |
return ParserCacheSerializationTestCases::getSupportedSerializationFormats( |
| 58 |
self::getClassToTest() |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function testCacheExpiryDoesNotIncrease() { |
| 63 |
$cacheTime = new CacheTime(); |
| 64 |
$this->assertSame( |
| 65 |
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY, |
| 66 |
$cacheTime->getCacheExpiry() |
| 67 |
); |
| 68 |
|
| 69 |
$cacheTime->updateCacheExpiry( 10 ); |
| 70 |
$this->assertSame( 10, $cacheTime->getCacheExpiry() ); |
| 71 |
|
| 72 |
$cacheTime->updateCacheExpiry( 5 ); |
| 73 |
$this->assertSame( 5, $cacheTime->getCacheExpiry() ); |
| 74 |
|
| 75 |
$cacheTime->updateCacheExpiry( 100500 ); |
| 76 |
$this->assertSame( 5, $cacheTime->getCacheExpiry() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function testCacheExpiryDoesNotIncreaseNotNegative() { |
| 80 |
$cacheTime = new CacheTime(); |
| 81 |
$cacheTime->updateCacheExpiry( -10 ); |
| 82 |
$this->assertSame( 0, $cacheTime->getCacheExpiry() ); |
| 83 |
} |
| 84 |
|
| 85 |
public function testCacheExpiryNotMoreThenGlobal() { |
| 86 |
$cacheTime = new CacheTime(); |
| 87 |
$cacheTime->updateCacheExpiry( |
| 88 |
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY + 1 |
| 89 |
); |
| 90 |
$this->assertSame( |
| 91 |
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY, |
| 92 |
$cacheTime->getCacheExpiry() |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
public function testExpired() { |
| 97 |
$cacheTime = new CacheTime(); |
| 98 |
$cacheTime->updateCacheExpiry( 0 ); |
| 99 |
$this->assertTrue( $cacheTime->expired( MWTimestamp::now() ) ); |
| 100 |
|
| 101 |
$cacheTime = new CacheTime(); |
| 102 |
$cacheTime->setCacheTime( MWTimestamp::now() ); |
| 103 |
$this->assertTrue( |
| 104 |
$cacheTime->expired( |
| 105 |
MWTimestamp::convert( TS_MW, MWTimestamp::now( TS_UNIX ) + 10 ) |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
$cacheTime = new CacheTime(); |
| 110 |
$cacheTime->updateCacheExpiry( 10 ); |
| 111 |
$cacheTime->setCacheTime( MWTimestamp::now() ); |
| 112 |
$this->assertTrue( |
| 113 |
$cacheTime->expired( |
| 114 |
MWTimestamp::convert( TS_MW, MWTimestamp::now( TS_UNIX ) + 15 ) |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
public function testGetSetOptions() { |
| 120 |
$options = ParserOptions::allCacheVaryingOptions(); |
| 121 |
$cacheTime = new CacheTime(); |
| 122 |
$cacheTime->recordOptions( $options ); |
| 123 |
$this->assertArrayEquals( $options, $cacheTime->getUsedOptions() ); |
| 124 |
} |
| 125 |
} |