2014-08-29 06:31:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
namespace MediaWiki\Tests\ResourceLoader;
|
|
|
|
|
|
|
|
|
|
use LinkCacheTestTrait;
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\Config;
|
|
|
|
|
use MediaWiki\Config\HashConfig;
|
2024-08-08 09:14:35 +00:00
|
|
|
use MediaWiki\Content\Content;
|
2024-05-17 21:55:30 +00:00
|
|
|
use MediaWiki\Content\CssContent;
|
2024-05-17 10:11:24 +00:00
|
|
|
use MediaWiki\Content\JavaScriptContent;
|
2024-05-17 11:31:13 +00:00
|
|
|
use MediaWiki\Content\JavaScriptContentHandler;
|
2024-08-06 13:40:20 +00:00
|
|
|
use MediaWiki\Content\WikitextContent;
|
2023-06-19 19:21:47 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2021-05-05 20:58:49 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
|
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
|
|
|
|
use MediaWiki\Page\PageRecord;
|
|
|
|
|
use MediaWiki\Page\PageStore;
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2022-05-06 09:09:56 +00:00
|
|
|
use MediaWiki\ResourceLoader\Context;
|
|
|
|
|
use MediaWiki\ResourceLoader\DerivativeContext;
|
|
|
|
|
use MediaWiki\ResourceLoader\WikiModule;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-18 13:56:39 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2022-05-06 09:09:56 +00:00
|
|
|
use ReflectionMethod;
|
2024-03-14 01:16:40 +00:00
|
|
|
use RuntimeException;
|
2023-02-28 14:08:53 +00:00
|
|
|
use Wikimedia\Rdbms\IReadableDatabase;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2024-03-14 01:16:40 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2016-02-11 04:57:03 +00:00
|
|
|
|
2019-07-13 18:27:33 +00:00
|
|
|
/**
|
2024-03-09 19:59:18 +00:00
|
|
|
* @group ResourceLoader
|
2023-07-20 21:37:24 +00:00
|
|
|
* @group Database
|
2024-03-09 19:59:18 +00:00
|
|
|
* @covers \MediaWiki\ResourceLoader\WikiModule
|
2019-07-13 18:27:33 +00:00
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
class WikiModuleTest extends ResourceLoaderTestCase {
|
2021-06-09 21:17:21 +00:00
|
|
|
use LinkCacheTestTrait;
|
2014-08-29 06:31:44 +00:00
|
|
|
|
2014-10-21 00:43:26 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideConstructor
|
|
|
|
|
*/
|
|
|
|
|
public function testConstructor( $params ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = new WikiModule( $params );
|
|
|
|
|
$this->assertInstanceOf( WikiModule::class, $module );
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-13 18:27:33 +00:00
|
|
|
public static function provideConstructor() {
|
|
|
|
|
yield 'null' => [ null ];
|
|
|
|
|
yield 'empty' => [ [] ];
|
|
|
|
|
yield 'unknown settings' => [ [ 'foo' => 'baz' ] ];
|
|
|
|
|
yield 'real settings' => [ [ 'MediaWiki:Common.js' ] ];
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 00:54:35 +00:00
|
|
|
private function makeTitleInfo( array $mockInfo ) {
|
|
|
|
|
$wikiModuleClass = TestingAccessWrapper::newFromClass( WikiModule::class );
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
$info = [];
|
2024-03-14 00:54:35 +00:00
|
|
|
foreach ( $mockInfo as $val ) {
|
|
|
|
|
$title = $val['title'];
|
|
|
|
|
unset( $val['title'] );
|
|
|
|
|
$info[ $wikiModuleClass->makeTitleKey( $title ) ] = $val;
|
resourceloader: Improve titleInfo docs and simplify title key
* Document the structure of the in-process $titleInfo cache.
Specifically, specify that it is not the value from getTitleInfo(),
but rather a container for zero or more versions of such values.
The reason this is fragmented is because ResourceLoaderContext
is a parameter to most methods and as such, makes everything
variable. Tracked as T99107.
* Make various bits easier to understand by consistently refering
to the container keys as "batchKey", and referring to the internal
keys as "titleKey".
* Centralise title key logic by moving to private method.
* Replace the internal creation of titleKey to be based on LinkTarget
with plain namespace IDs and db keys, instead of invoking the
expensive getPrefixedTitle function which involves quite a lot
of overhead (TitleCodec, GenderCache, Database, Language,
LocalisationCache, ..).
Change-Id: I701e5156ef7815a0e36caefae5871524eff3f688
2018-04-10 17:29:50 +00:00
|
|
|
}
|
|
|
|
|
return $info;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-21 00:43:26 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetPages
|
|
|
|
|
*/
|
|
|
|
|
public function testGetPages( $params, Config $config, $expected ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = new WikiModule( $params );
|
2014-10-21 00:43:26 +00:00
|
|
|
$module->setConfig( $config );
|
|
|
|
|
|
resourceloader: Replace timestamp system with version hashing
Modules now track their version via getVersionHash() instead of getModifiedTime().
== Background ==
While some resources have observeable timestamps (e.g. files stored on disk),
many other resources do not. E.g. config variables, and module definitions.
For static file modules, one can e.g. revert one of more files in a module to a
previous version and not affect the max timestamp.
Wiki modules include pages only if they exist. The user module supports common.js
and skin.js. By default neither exists. If a user has both, and then the
less-recently modified one is deleted, the max-timestamp remains unchanged.
For client-side caching, batch requests use "Math.max" on the relevant timestamps.
Again, if a module changes but another module is more recent (e.g. out-of-order
deployment, or out-of-order discovery), the change would not result in a cache miss.
More scenarios can be found in the associated Phabricator tasks.
== Version hash ==
Previously we virtually mapped these variables to a timestamp by storing the current
time alongside a hash of the value in ObjectCache. Considering the number of
possible request contexts (wikis * modules * users * skins * languages) this doesn't
work well. It results in needless cache invalidation when the first time observation
is purged due to LRU algorithms. It also has other minor bugs leading to fewer
cache hits.
All modules automatically get the benefits of version hashing with this change.
The old getDefinitionMtime() and getHashMtime() have been replaced with dummies
that return 1. These functions are often called from getModifiedTime() in subclasses.
For backward-compatibility, their respective values (definition summary and hash)
are now included in getVersionHash directly.
As examples, the following modules have been updated to use getVersionHash directly.
Other modules still work fine and can be updated later.
* ResourceLoaderFileModule
* ResourceLoaderEditToolbarModule
* ResourceLoaderStartUpModule
* ResourceLoaderWikiModule
The presence of hashes in place of timestamps increases the startup module size on
a default MediaWiki install from 4.4k to 5.8k (after gzip and minification).
== ETag ==
Since timestamps are no longer tracked, we need a different way to implement caching
for cache proxies (e.g. Varnish) and web browsers. Previously we used the
Last-Modified header (in combination with Cache-Control and Expires).
Instead of Last-Modified (and If-Modified-Since), we use ETag (and If-None-Match).
Entity tags (new in HTTP/1.1) are much stricter than Last-Modified by default.
They instruct browsers to allow usage of partial Range requests. Since our responses
are dynamically generated, we need to use the Weak version of ETag.
While this sounds bad, it's no different than Last-Modified. As reassured by
RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.3> the
specified behaviour behind Last-Modified follows the same "Weak" caching logic as
Entity tags. It's just that entity tags are capable of a stricter mode (whereas
Last-Modified is inherently weak).
== File cache ==
If $wgUseFileCache is enabled, ResourceLoader uses ResourceFileCache to cache
load.php responses. While the blind TTL handling (during the allowed expiry period)
is still maxage/timestamp based, tryRespondNotModified() now requires the caller to
know the expected ETag.
For this to work, the FileCache handling had to be moved from the top of
ResoureLoader::respond() to after the expected ETag is computed.
This also allows us to remove the duplicate tryRespondNotModified() handling since
that's is already handled by ResourceLoader::respond() meanwhile.
== Misc ==
* Remove redundant modifiedTime cache in ResourceLoaderFileModule.
* Change bugzilla references to Phabricator.
* Centralised inclusion of wgCacheEpoch using getDefinitionSummary. Previously this
logic was duplicated in each place the modified timestamp was used.
* It's easy to forget calling the parent class in getDefinitionSummary().
Previously this method only tracked 'class' by default. As such, various
extensions hardcoded that one value instead of calling the parent and extending
the array. To better prevent this in the future, getVersionHash() now asserts
that the '_cacheEpoch' property made it through.
* tests: Don't use getDefinitionSummary() as an API.
Fix ResourceLoaderWikiModuleTest to call getPages properly.
* In tests, the default timestamp used to be 1388534400000 (which is the unix time
of 20140101000000; the unit tests' CacheEpoch). The new version hash of these
modules is "XyCC+PSK", which is the base64 encoded prefix of the SHA1 digest of:
'{"_class":"ResourceLoaderTestModule","_cacheEpoch":"20140101000000"}'
* Add sha1.js library for client-side hash generation.
Compared various different implementations for code size (after minfication/gzip),
and speed (when used for short hexidecimal strings).
https://jsperf.com/sha1-implementations
- CryptoJS <https://code.google.com/p/crypto-js/#SHA-1> (min+gzip: 2.5k)
http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js
Chrome: 45k, Firefox: 89k, Safari: 92k
- jsSHA <https://github.com/Caligatio/jsSHA>
https://github.com/Caligatio/jsSHA/blob/3c1d4f2e/src/sha1.js (min+gzip: 1.8k)
Chrome: 65k, Firefox: 53k, Safari: 69k
- phpjs-sha1 <https://github.com/kvz/phpjs> (RL min+gzip: 0.8k)
https://github.com/kvz/phpjs/blob/1eaab15d/functions/strings/sha1.js
Chrome: 200k, Firefox: 280k, Safari: 78k
Modern browsers implement the HTML5 Crypto API. However, this API is asynchronous,
only enabled when on HTTPS in Chromium, and is quite low-level. It requires boilerplate
code to actually use with TextEncoder, ArrayBuffer and Uint32Array. Due this being
needed in the module loader, we'd have to load the fallback regardless. Considering
this is not used in a critical path for performance, it's not worth shipping two
implementations for this optimisation.
May also resolve:
* T44094
* T90411
* T94810
Bug: T94074
Change-Id: Ibb292d2416839327d1807a66c78fd96dac0637d0
2015-04-29 22:53:24 +00:00
|
|
|
// Because getPages is protected..
|
|
|
|
|
$getPages = new ReflectionMethod( $module, 'getPages' );
|
|
|
|
|
$getPages->setAccessible( true );
|
2022-05-06 09:09:56 +00:00
|
|
|
$out = $getPages->invoke( $module, Context::newDummyContext() );
|
2019-07-13 18:27:33 +00:00
|
|
|
$this->assertSame( $expected, $out );
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideGetPages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$settings = self::getSettings() + [
|
2023-06-19 19:21:47 +00:00
|
|
|
MainConfigNames::UseSiteJs => true,
|
|
|
|
|
MainConfigNames::UseSiteCss => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-10-21 00:43:26 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
|
|
|
|
'styles' => [ 'MediaWiki:Common.css' ],
|
|
|
|
|
'scripts' => [ 'MediaWiki:Common.js' ],
|
|
|
|
|
];
|
2014-10-21 00:43:26 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [], new HashConfig( $settings ), [] ],
|
|
|
|
|
[ $params, new HashConfig( $settings ), [
|
|
|
|
|
'MediaWiki:Common.js' => [ 'type' => 'script' ],
|
|
|
|
|
'MediaWiki:Common.css' => [ 'type' => 'style' ]
|
|
|
|
|
] ],
|
2023-06-19 19:21:47 +00:00
|
|
|
[ $params, new HashConfig( [ MainConfigNames::UseSiteCss => false ] + $settings ), [
|
2016-02-17 09:09:32 +00:00
|
|
|
'MediaWiki:Common.js' => [ 'type' => 'script' ],
|
|
|
|
|
] ],
|
2023-06-19 19:21:47 +00:00
|
|
|
[ $params, new HashConfig( [ MainConfigNames::UseSiteJs => false ] + $settings ), [
|
2016-02-17 09:09:32 +00:00
|
|
|
'MediaWiki:Common.css' => [ 'type' => 'style' ],
|
|
|
|
|
] ],
|
|
|
|
|
[ $params,
|
2015-09-26 19:48:17 +00:00
|
|
|
new HashConfig(
|
2023-06-19 19:21:47 +00:00
|
|
|
[ MainConfigNames::UseSiteJs => false, MainConfigNames::UseSiteCss => false ]
|
2015-09-26 19:48:17 +00:00
|
|
|
),
|
2016-02-17 09:09:32 +00:00
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
];
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetGroup
|
|
|
|
|
*/
|
|
|
|
|
public function testGetGroup( $params, $expected ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = new WikiModule( $params );
|
2019-07-13 18:27:33 +00:00
|
|
|
$this->assertSame( $expected, $module->getGroup() );
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideGetGroup() {
|
2019-07-13 18:27:33 +00:00
|
|
|
yield 'no group' => [ [], null ];
|
|
|
|
|
yield 'some group' => [ [ 'group' => 'foobar' ], 'foobar' ];
|
2014-10-21 00:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-13 18:32:17 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetType
|
|
|
|
|
*/
|
|
|
|
|
public function testGetType( $params, $expected ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = new WikiModule( $params );
|
2019-07-13 18:32:17 +00:00
|
|
|
$this->assertSame( $expected, $module->getType() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideGetType() {
|
|
|
|
|
yield 'empty' => [
|
|
|
|
|
[],
|
2022-05-06 09:09:56 +00:00
|
|
|
WikiModule::LOAD_GENERAL,
|
2019-07-13 18:32:17 +00:00
|
|
|
];
|
|
|
|
|
yield 'scripts' => [
|
|
|
|
|
[ 'scripts' => [ 'Example.js' ] ],
|
2022-05-06 09:09:56 +00:00
|
|
|
WikiModule::LOAD_GENERAL,
|
2019-07-13 18:32:17 +00:00
|
|
|
];
|
|
|
|
|
yield 'styles' => [
|
|
|
|
|
[ 'styles' => [ 'Example.css' ] ],
|
2022-05-06 09:09:56 +00:00
|
|
|
WikiModule::LOAD_STYLES,
|
2019-07-13 18:32:17 +00:00
|
|
|
];
|
|
|
|
|
yield 'styles and scripts' => [
|
|
|
|
|
[ 'styles' => [ 'Example.css' ], 'scripts' => [ 'Example.js' ] ],
|
2022-05-06 09:09:56 +00:00
|
|
|
WikiModule::LOAD_GENERAL,
|
2019-07-13 18:32:17 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-29 06:31:44 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsKnownEmpty
|
|
|
|
|
*/
|
2018-04-24 00:11:27 +00:00
|
|
|
public function testIsKnownEmpty( $titleInfo, $group, $dependencies, $expected ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = $this->getMockBuilder( WikiModule::class )
|
2014-08-29 06:31:44 +00:00
|
|
|
->disableOriginalConstructor()
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
|
2014-08-29 06:31:44 +00:00
|
|
|
->getMock();
|
2019-07-13 18:27:33 +00:00
|
|
|
$module->method( 'getTitleInfo' )
|
2024-03-14 00:54:35 +00:00
|
|
|
->willReturn( $this->makeTitleInfo( $titleInfo ) );
|
2019-07-13 18:27:33 +00:00
|
|
|
$module->method( 'getGroup' )
|
|
|
|
|
->willReturn( $group );
|
|
|
|
|
$module->method( 'getDependencies' )
|
|
|
|
|
->willReturn( $dependencies );
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = $this->createMock( Context::class );
|
2019-07-13 18:27:33 +00:00
|
|
|
$this->assertSame( $expected, $module->isKnownEmpty( $context ) );
|
2014-08-29 06:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideIsKnownEmpty() {
|
2019-07-13 18:27:33 +00:00
|
|
|
yield 'nothing' => [
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
[],
|
|
|
|
|
// No pages exist, considered empty.
|
|
|
|
|
true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'an empty page exists (no group)' => [
|
2024-03-14 00:54:35 +00:00
|
|
|
[ [ 'title' => new TitleValue( NS_USER, 'Example/foo.js' ), 'page_len' => 0 ] ],
|
2019-07-13 18:27:33 +00:00
|
|
|
null,
|
|
|
|
|
[],
|
|
|
|
|
// There is an existing page, so we should let the module be queued.
|
|
|
|
|
// Its emptiness might be temporary, hence considered non-empty (T70488).
|
|
|
|
|
false,
|
|
|
|
|
];
|
|
|
|
|
yield 'an empty page exists (site group)' => [
|
2024-03-14 00:54:35 +00:00
|
|
|
[ [ 'title' => new TitleValue( NS_MEDIAWIKI, 'Foo.js' ), 'page_len' => 0 ] ],
|
2019-07-13 18:27:33 +00:00
|
|
|
'site',
|
|
|
|
|
[],
|
|
|
|
|
// There is an existing page, hence considered non-empty.
|
|
|
|
|
false,
|
|
|
|
|
];
|
|
|
|
|
yield 'an empty page exists (user group)' => [
|
2024-03-14 00:54:35 +00:00
|
|
|
[ [ 'title' => new TitleValue( NS_USER, 'Example/foo.js' ), 'page_len' => 0 ] ],
|
2019-07-13 18:27:33 +00:00
|
|
|
'user',
|
|
|
|
|
[],
|
|
|
|
|
// There is an existing page, but it is empty.
|
|
|
|
|
// For user-specific modules, don't bother loading a known-empty module.
|
|
|
|
|
// Given user-specific HTML output, this will vary and re-appear if/when
|
|
|
|
|
// the page becomes non-empty again.
|
|
|
|
|
true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'no pages but having dependencies (no group)' => [
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
[ 'another-module' ],
|
|
|
|
|
false,
|
|
|
|
|
];
|
|
|
|
|
yield 'no pages but having dependencies (site group)' => [
|
|
|
|
|
[],
|
|
|
|
|
'site',
|
|
|
|
|
[ 'another-module' ],
|
|
|
|
|
false,
|
|
|
|
|
];
|
|
|
|
|
yield 'no pages but having dependencies (user group)' => [
|
|
|
|
|
[],
|
|
|
|
|
'user',
|
|
|
|
|
[ 'another-module' ],
|
|
|
|
|
false,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield 'a non-empty page exists (user group)' => [
|
2024-03-14 00:54:35 +00:00
|
|
|
[ [ 'title' => new TitleValue( NS_USER, 'Example/foo.js' ), 'page_len' => 25 ] ],
|
2019-07-13 18:27:33 +00:00
|
|
|
'user',
|
|
|
|
|
[],
|
|
|
|
|
false,
|
|
|
|
|
];
|
|
|
|
|
yield 'a non-empty page exists (site group)' => [
|
2024-03-14 00:54:35 +00:00
|
|
|
[ [ 'title' => new TitleValue( NS_MEDIAWIKI, 'Foo.js' ), 'page_len' => 25 ] ],
|
2019-07-13 18:27:33 +00:00
|
|
|
'site',
|
|
|
|
|
[],
|
|
|
|
|
false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-29 06:31:44 +00:00
|
|
|
}
|
2016-09-14 21:14:31 +00:00
|
|
|
|
|
|
|
|
public function testGetPreloadedTitleInfo() {
|
2024-03-14 01:16:40 +00:00
|
|
|
// Set up
|
|
|
|
|
ConvertibleTimestamp::setFakeTime( '20110401090000' );
|
|
|
|
|
$this->editPage( 'MediaWiki:TestA.css', '.mw-first {}', 'First' );
|
|
|
|
|
$this->editPage( 'MediaWiki:TestEmpty.css', '', 'Empty' );
|
2024-03-13 11:09:25 +00:00
|
|
|
$this->editPage( 'MediaWiki:TestB.css', '.mw-second {}', 'Second' );
|
2016-09-14 21:14:31 +00:00
|
|
|
$rl = new EmptyResourceLoader();
|
2024-08-06 13:04:05 +00:00
|
|
|
$rl->getConfig()->set( MainConfigNames::UseSiteJs, true );
|
|
|
|
|
$rl->getConfig()->set( MainConfigNames::UseSiteCss, true );
|
2024-03-13 11:09:25 +00:00
|
|
|
$rl->register( 'testmodule1', [
|
2024-03-14 01:16:40 +00:00
|
|
|
'class' => TestResourceLoaderWikiModule::class,
|
|
|
|
|
'styles' => [
|
|
|
|
|
'MediaWiki:TestA.css',
|
|
|
|
|
// Regression against T145673. It's impossible to statically declare page names in
|
|
|
|
|
// a canonical way since the canonical prefix is localised. As such, the preload
|
|
|
|
|
// cache computed the right cache key, but failed to find the results when
|
|
|
|
|
// doing an intersect on the canonical result, producing an empty array.
|
|
|
|
|
'mediawiki: testEmpty.css',
|
|
|
|
|
],
|
|
|
|
|
] );
|
2024-03-13 11:09:25 +00:00
|
|
|
$rl->register( 'testmodule2', [
|
|
|
|
|
'class' => TestResourceLoaderWikiModule::class,
|
|
|
|
|
'styles' => [
|
|
|
|
|
'MediaWiki:TestB.css',
|
|
|
|
|
],
|
|
|
|
|
] );
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( $rl, new FauxRequest() );
|
2016-09-14 21:14:31 +00:00
|
|
|
|
2024-03-14 01:16:40 +00:00
|
|
|
// Warm up the cache
|
|
|
|
|
WikiModule::preloadTitleInfo(
|
2016-09-14 21:14:31 +00:00
|
|
|
$context,
|
2024-03-13 11:09:25 +00:00
|
|
|
[ 'testmodule1', 'testmodule2' ]
|
2016-09-14 21:14:31 +00:00
|
|
|
);
|
2024-03-14 01:16:40 +00:00
|
|
|
// The module uses TestResourceLoaderWikiModule, which disables fetchTitleInfo() by default.
|
|
|
|
|
// If getTitleInfo() returns the data here, it means preloadTitleInfo succeeded.
|
2024-03-13 11:09:25 +00:00
|
|
|
$module1 = TestingAccessWrapper::newFromObject( $rl->getModule( 'testmodule1' ) );
|
2024-03-14 01:16:40 +00:00
|
|
|
$this->assertArrayContains( [
|
|
|
|
|
'8:TestA.css' => [ 'page_len' => '12', 'page_touched' => '20110401090000' ],
|
|
|
|
|
'8:TestEmpty.css' => [ 'page_len' => '0', 'page_touched' => '20110401090000' ],
|
2024-03-13 11:09:25 +00:00
|
|
|
], $module1->getTitleInfo( $context ), 'Title info' );
|
|
|
|
|
$module2 = TestingAccessWrapper::newFromObject( $rl->getModule( 'testmodule2' ) );
|
|
|
|
|
$this->assertArrayContains( [
|
|
|
|
|
'8:TestB.css' => [ 'page_len' => '13', 'page_touched' => '20110401090000' ],
|
|
|
|
|
], $module2->getTitleInfo( $context ), 'Title info' );
|
2016-09-14 21:14:31 +00:00
|
|
|
}
|
2016-02-11 04:57:03 +00:00
|
|
|
|
2017-04-01 01:35:09 +00:00
|
|
|
public function testGetPreloadedBadTitle() {
|
2019-07-11 19:48:57 +00:00
|
|
|
// Set up
|
|
|
|
|
TestResourceLoaderWikiModule::$returnFetchTitleInfo = [];
|
2017-04-01 01:35:09 +00:00
|
|
|
$rl = new EmptyResourceLoader();
|
2024-08-06 13:04:05 +00:00
|
|
|
$rl->getConfig()->set( MainConfigNames::UseSiteJs, true );
|
|
|
|
|
$rl->getConfig()->set( MainConfigNames::UseSiteCss, true );
|
2019-07-11 19:48:57 +00:00
|
|
|
$rl->register( 'testmodule', [
|
|
|
|
|
'class' => TestResourceLoaderWikiModule::class,
|
|
|
|
|
// Covers preloadTitleInfo branch for invalid page name
|
|
|
|
|
'styles' => [ '[x]' ],
|
|
|
|
|
] );
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( $rl, new FauxRequest() );
|
2017-04-01 01:35:09 +00:00
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
TestResourceLoaderWikiModule::preloadTitleInfo(
|
|
|
|
|
$context,
|
|
|
|
|
[ 'testmodule' ]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Assert
|
2019-07-11 19:48:57 +00:00
|
|
|
$module = TestingAccessWrapper::newFromObject( $rl->getModule( 'testmodule' ) );
|
|
|
|
|
$this->assertSame( [], $module->getTitleInfo( $context ), 'Title info' );
|
2017-04-01 01:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetPreloadedTitleInfoEmpty() {
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( new EmptyResourceLoader(), new FauxRequest() );
|
2019-07-13 18:27:33 +00:00
|
|
|
// This covers the early return case
|
2017-04-01 01:35:09 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
null,
|
2022-05-06 09:09:56 +00:00
|
|
|
WikiModule::preloadTitleInfo(
|
2017-04-01 01:35:09 +00:00
|
|
|
$context,
|
|
|
|
|
[]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideGetContent() {
|
2019-07-13 18:27:33 +00:00
|
|
|
yield 'Bad title' => [ null, '[x]' ];
|
2019-07-25 22:23:24 +00:00
|
|
|
|
2019-07-13 18:27:33 +00:00
|
|
|
yield 'No JS content found' => [ null, [
|
2019-07-25 22:23:24 +00:00
|
|
|
'text' => 'MediaWiki:Foo.js',
|
2019-07-13 18:27:33 +00:00
|
|
|
'ns' => NS_MEDIAWIKI,
|
2019-07-25 22:23:24 +00:00
|
|
|
'title' => 'Foo.js',
|
2019-07-13 18:27:33 +00:00
|
|
|
] ];
|
2019-07-25 22:23:24 +00:00
|
|
|
|
|
|
|
|
yield 'JS content' => [ 'code;', [
|
|
|
|
|
'text' => 'MediaWiki:Foo.js',
|
2019-07-13 18:27:33 +00:00
|
|
|
'ns' => NS_MEDIAWIKI,
|
2019-07-25 22:23:24 +00:00
|
|
|
'title' => 'Foo.js',
|
|
|
|
|
], new JavaScriptContent( 'code;' ) ];
|
|
|
|
|
|
|
|
|
|
yield 'CSS content' => [ 'code {}', [
|
|
|
|
|
'text' => 'MediaWiki:Foo.css',
|
|
|
|
|
'ns' => NS_MEDIAWIKI,
|
|
|
|
|
'title' => 'Foo.css',
|
|
|
|
|
], new CssContent( 'code {}' ) ];
|
|
|
|
|
|
|
|
|
|
yield 'Wikitext content' => [ null, [
|
|
|
|
|
'text' => 'MediaWiki:Foo',
|
|
|
|
|
'ns' => NS_MEDIAWIKI,
|
|
|
|
|
'title' => 'Foo',
|
|
|
|
|
], new WikitextContent( 'code;' ) ];
|
2017-04-01 01:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetContent
|
|
|
|
|
*/
|
2024-10-16 18:58:33 +00:00
|
|
|
public function testGetContent( $expected, $title, ?Content $contentObj = null ) {
|
2017-04-01 01:35:09 +00:00
|
|
|
$context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = $this->getMockBuilder( WikiModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getContentObj' ] )->getMock();
|
2019-07-13 18:27:33 +00:00
|
|
|
$module->method( 'getContentObj' )
|
2019-07-25 22:23:24 +00:00
|
|
|
->willReturn( $contentObj );
|
2017-04-01 01:35:09 +00:00
|
|
|
|
|
|
|
|
if ( is_array( $title ) ) {
|
|
|
|
|
$title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
|
|
|
|
|
$titleText = $title['text'];
|
2021-05-05 20:58:49 +00:00
|
|
|
// Mock page table access via PageStore
|
|
|
|
|
$pageStore = $this->createNoOpMock( PageStore::class, [ 'getPageByText' ] );
|
|
|
|
|
$pageStore->method( 'getPageByText' )->willReturn(
|
|
|
|
|
new PageIdentityValue(
|
|
|
|
|
$title['id'], $title['ns'], $title['text'], PageRecord::LOCAL
|
|
|
|
|
)
|
2017-04-01 01:35:09 +00:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$titleText = $title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$module = TestingAccessWrapper::newFromObject( $module );
|
2019-07-13 18:27:33 +00:00
|
|
|
$this->assertSame(
|
2017-04-01 01:35:09 +00:00
|
|
|
$expected,
|
2017-02-28 20:52:17 +00:00
|
|
|
$module->getContent( $titleText, $context )
|
2017-04-01 01:35:09 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-28 20:52:17 +00:00
|
|
|
public function testContentOverrides() {
|
|
|
|
|
$pages = [
|
|
|
|
|
'MediaWiki:Common.css' => [ 'type' => 'style' ],
|
|
|
|
|
];
|
|
|
|
|
|
2021-06-11 15:11:37 +00:00
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = $this->getMockBuilder( WikiModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getPages' ] )
|
2017-02-28 20:52:17 +00:00
|
|
|
->getMock();
|
|
|
|
|
$module->method( 'getPages' )->willReturn( $pages );
|
2021-06-11 15:11:37 +00:00
|
|
|
$module->setConfig( $rl->getConfig() );
|
2017-02-28 20:52:17 +00:00
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new DerivativeContext(
|
|
|
|
|
new Context( $rl, new FauxRequest() )
|
2017-02-28 20:52:17 +00:00
|
|
|
);
|
2021-05-05 20:58:49 +00:00
|
|
|
$context->setContentOverrideCallback( static function ( PageIdentity $t ) {
|
|
|
|
|
if ( $t->getDBkey() === 'Common.css' ) {
|
2017-02-28 20:52:17 +00:00
|
|
|
return new CssContent( '.override{}' );
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $module->shouldEmbedModule( $context ) );
|
2019-07-13 18:27:33 +00:00
|
|
|
$this->assertSame( [
|
2017-02-28 20:52:17 +00:00
|
|
|
'all' => [
|
|
|
|
|
"/*\nMediaWiki:Common.css\n*/\n.override{}"
|
|
|
|
|
]
|
|
|
|
|
], $module->getStyles( $context ) );
|
|
|
|
|
|
2021-05-05 20:58:49 +00:00
|
|
|
$context->setContentOverrideCallback( static function ( PageIdentity $t ) {
|
|
|
|
|
if ( $t->getDBkey() === 'Skin.css' ) {
|
2017-02-28 20:52:17 +00:00
|
|
|
return new CssContent( '.override{}' );
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
} );
|
|
|
|
|
$this->assertFalse( $module->shouldEmbedModule( $context ) );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 04:57:03 +00:00
|
|
|
public function testGetContentForRedirects() {
|
|
|
|
|
// Set up context and module object
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new DerivativeContext(
|
2017-02-28 20:52:17 +00:00
|
|
|
$this->getResourceLoaderContext( [], new EmptyResourceLoader )
|
|
|
|
|
);
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = $this->getMockBuilder( WikiModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getPages' ] )
|
2016-02-11 04:57:03 +00:00
|
|
|
->getMock();
|
2019-07-13 18:27:33 +00:00
|
|
|
$module->method( 'getPages' )
|
|
|
|
|
->willReturn( [
|
2016-02-11 04:57:03 +00:00
|
|
|
'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
|
2019-07-13 18:27:33 +00:00
|
|
|
] );
|
2021-06-11 15:11:37 +00:00
|
|
|
$module->setConfig( $context->getResourceLoader()->getConfig() );
|
2021-05-05 20:58:49 +00:00
|
|
|
$context->setContentOverrideCallback( static function ( PageIdentity $title ) {
|
|
|
|
|
if ( $title->getDBkey() === 'Redirect.js' ) {
|
2017-02-28 20:52:17 +00:00
|
|
|
$handler = new JavaScriptContentHandler();
|
|
|
|
|
return $handler->makeRedirectContent(
|
|
|
|
|
Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
|
|
|
|
|
);
|
2021-05-05 20:58:49 +00:00
|
|
|
} elseif ( $title->getDBkey() === 'Target.js' ) {
|
2017-02-28 20:52:17 +00:00
|
|
|
return new JavaScriptContent( 'target;' );
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} );
|
2016-02-11 04:57:03 +00:00
|
|
|
|
|
|
|
|
// Mock away Title's db queries with LinkCache
|
2021-06-09 21:17:21 +00:00
|
|
|
$this->addGoodLinkObject( 1, new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ), 1, 1 );
|
2016-02-11 04:57:03 +00:00
|
|
|
|
2019-07-13 18:27:33 +00:00
|
|
|
$this->assertSame(
|
2016-02-11 04:57:03 +00:00
|
|
|
"/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
|
|
|
|
|
$module->getScript( $context ),
|
|
|
|
|
'Redirect resolved by getContent'
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-09-14 21:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
class TestResourceLoaderWikiModule extends WikiModule {
|
2024-09-07 19:25:51 +00:00
|
|
|
/** @var array|null */
|
2016-09-14 21:14:31 +00:00
|
|
|
public static $returnFetchTitleInfo = null;
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2023-02-28 14:08:53 +00:00
|
|
|
protected static function fetchTitleInfo( IReadableDatabase $db, array $pages, $fname = null ) {
|
2016-09-14 21:14:31 +00:00
|
|
|
$ret = self::$returnFetchTitleInfo;
|
|
|
|
|
self::$returnFetchTitleInfo = null;
|
2024-03-14 01:16:40 +00:00
|
|
|
if ( $ret === null ) {
|
|
|
|
|
// If a call is expected, a mock return value must be planted first
|
|
|
|
|
throw new RuntimeException( 'Unexpected fetchTitleInfo call' );
|
|
|
|
|
}
|
2016-09-14 21:14:31 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2014-08-29 06:31:44 +00:00
|
|
|
}
|