Consolidate cache TTL handling within CoreMagicVariables. Make the TTL account for how many seconds away the value is from changing. For example, CURRENTHOUR should change soon after the next hour is reached. There is a minimum adjustment TTL to avoid parser-after-save delays. This allows for longer caching in most cases, as well as more up-to-date rendering when the hour/day/week/year is about to change. Previously, there were blind TTLs, which are either way too pessimistic or way too generous. This commit does not change the CURRENTTIME, CURRENTTIMESTAMP, LOCALTIME, and LOCALTIMESTAMP words, since there is no reasonable way to cache output while keeping them up-to-date. Bug: T320668 Change-Id: I9acb42b0d9ff67798a1624cbf9c7cac99c8fbe2f
71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Parser\MagicWord;
|
|
use MediaWiki\Parser\MagicWordArray;
|
|
use MediaWiki\Parser\MagicWordFactory;
|
|
|
|
/**
|
|
* @covers \MediaWiki\Parser\MagicWordFactory
|
|
*
|
|
* @author Derick N. Alangi
|
|
*/
|
|
class MagicWordFactoryTest extends MediaWikiIntegrationTestCase {
|
|
private function makeMagicWordFactory( Language $contLang = null ) {
|
|
$services = $this->getServiceContainer();
|
|
return new MagicWordFactory( $contLang ?:
|
|
$services->getLanguageFactory()->getLanguage( 'en' ),
|
|
$services->getHookContainer()
|
|
);
|
|
}
|
|
|
|
public function testGetContentLanguage() {
|
|
$contLang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
|
|
|
|
$magicWordFactory = $this->makeMagicWordFactory( $contLang );
|
|
$magicWordContLang = $magicWordFactory->getContentLanguage();
|
|
|
|
$this->assertSame( $contLang, $magicWordContLang );
|
|
}
|
|
|
|
public function testGetMagicWord() {
|
|
$magicWordIdValid = 'pageid';
|
|
$magicWordFactory = $this->makeMagicWordFactory();
|
|
$mwActual = $magicWordFactory->get( $magicWordIdValid );
|
|
$contLang = $magicWordFactory->getContentLanguage();
|
|
$expected = new MagicWord( $magicWordIdValid, [ 'PAGEID' ], false, $contLang );
|
|
|
|
$this->assertEquals( $expected, $mwActual );
|
|
}
|
|
|
|
public function testGetInvalidMagicWord() {
|
|
$magicWordFactory = $this->makeMagicWordFactory();
|
|
|
|
$this->expectException( MWException::class );
|
|
@$magicWordFactory->get( 'invalid magic word' );
|
|
}
|
|
|
|
public function testGetVariableIDs() {
|
|
$magicWordFactory = $this->makeMagicWordFactory();
|
|
$varIds = $magicWordFactory->getVariableIDs();
|
|
|
|
$this->assertIsArray( $varIds );
|
|
$this->assertNotEmpty( $varIds );
|
|
$this->assertContainsOnly( 'string', $varIds );
|
|
}
|
|
|
|
public function testGetSubstIDs() {
|
|
$magicWordFactory = $this->makeMagicWordFactory();
|
|
$substIds = $magicWordFactory->getSubstIDs();
|
|
|
|
$this->assertIsArray( $substIds );
|
|
$this->assertNotEmpty( $substIds );
|
|
$this->assertContainsOnly( 'string', $substIds );
|
|
}
|
|
|
|
public function testGetDoubleUnderscoreArray() {
|
|
$magicWordFactory = $this->makeMagicWordFactory();
|
|
$actual = $magicWordFactory->getDoubleUnderscoreArray();
|
|
|
|
$this->assertInstanceOf( MagicWordArray::class, $actual );
|
|
}
|
|
}
|