There has long been a hack for previewing edits to user JS/CSS, where OutputPage would pass an 'excludepage' parameter to ResourceLoaderUserModule to tell it not to load one particular page and would instead embed that page statically. That's nice, but there are other places where we could use the same thing. This patch generalizes it: * DerivativeResourceLoaderContext may now contain a callback for mapping titles to replacement Content objects. * ResourceLoaderWikiModule::getContent() uses the overrides, and requests embedding when they're used. All subclasses in Gerrit should pick it up automatically. * OutputPage gains methods for callers to add to the override mapping, which it passes on to RL. It loses a bunch of the special casing it had for the 'user' and 'user.styles' modules. * EditPage sets the overrides on OutputPage when doing the preview, as does ApiParse for prop=headhtml. TemplateSandbox does too in I83fa0856. * OutputPage::userCanPreview() gets less specific to editing user CSS and JS, since RL now handles the embedding based on the actual modules' dependencies and EditPage only requests it on preview. ApiParse also gets a new hook to support TemplateSandbox's API integration (used in I83fa0856). Bug: T112474 Change-Id: Ib9d2ce42931c1de8372e231314a1f672d7e2ac0e
121 lines
4.2 KiB
PHP
121 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* See also:
|
|
* - ResourceLoaderTest::testExpandModuleNames
|
|
* - ResourceLoaderImageModuleTest::testContext
|
|
*
|
|
* @group Cache
|
|
* @covers ResourceLoaderContext
|
|
*/
|
|
class ResourceLoaderContextTest extends PHPUnit\Framework\TestCase {
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
protected static function getResourceLoader() {
|
|
return new EmptyResourceLoader( new HashConfig( [
|
|
'ResourceLoaderDebug' => false,
|
|
'DefaultSkin' => 'fallback',
|
|
'LanguageCode' => 'nl',
|
|
'LoadScript' => '/w/load.php',
|
|
] ) );
|
|
}
|
|
|
|
public function testEmpty() {
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
|
|
|
|
// Request parameters
|
|
$this->assertEquals( [], $ctx->getModules() );
|
|
$this->assertEquals( 'nl', $ctx->getLanguage() );
|
|
$this->assertEquals( false, $ctx->getDebug() );
|
|
$this->assertEquals( null, $ctx->getOnly() );
|
|
$this->assertEquals( 'fallback', $ctx->getSkin() );
|
|
$this->assertEquals( null, $ctx->getUser() );
|
|
$this->assertNull( $ctx->getContentOverrideCallback() );
|
|
|
|
// Misc
|
|
$this->assertEquals( 'ltr', $ctx->getDirection() );
|
|
$this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
|
|
$this->assertInstanceOf( User::class, $ctx->getUserObj() );
|
|
}
|
|
|
|
public function testDummy() {
|
|
$this->assertInstanceOf(
|
|
ResourceLoaderContext::class,
|
|
ResourceLoaderContext::newDummyContext()
|
|
);
|
|
}
|
|
|
|
public function testAccessors() {
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
|
|
$this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
|
|
$this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $ctx->getLogger() );
|
|
}
|
|
|
|
public function testTypicalRequest() {
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
|
|
'debug' => 'false',
|
|
'lang' => 'zh',
|
|
'modules' => 'foo|foo.quux,baz,bar|baz.quux',
|
|
'only' => 'styles',
|
|
'skin' => 'fallback',
|
|
] ) );
|
|
|
|
// Request parameters
|
|
$this->assertEquals(
|
|
$ctx->getModules(),
|
|
[ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
|
|
);
|
|
$this->assertEquals( false, $ctx->getDebug() );
|
|
$this->assertEquals( 'zh', $ctx->getLanguage() );
|
|
$this->assertEquals( 'styles', $ctx->getOnly() );
|
|
$this->assertEquals( 'fallback', $ctx->getSkin() );
|
|
$this->assertEquals( null, $ctx->getUser() );
|
|
|
|
// Misc
|
|
$this->assertEquals( 'ltr', $ctx->getDirection() );
|
|
$this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
|
|
}
|
|
|
|
public function testShouldInclude() {
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
|
|
$this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
|
|
$this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
|
|
$this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
|
|
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
|
|
'only' => 'styles'
|
|
] ) );
|
|
$this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
|
|
$this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
|
|
$this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
|
|
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
|
|
'only' => 'scripts'
|
|
] ) );
|
|
$this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
|
|
$this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
|
|
$this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
|
|
}
|
|
|
|
public function testGetUser() {
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
|
|
$this->assertSame( null, $ctx->getUser() );
|
|
$this->assertTrue( $ctx->getUserObj()->isAnon() );
|
|
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
|
|
'user' => 'Example'
|
|
] ) );
|
|
$this->assertSame( 'Example', $ctx->getUser() );
|
|
$this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
|
|
}
|
|
|
|
public function testMsg() {
|
|
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
|
|
'lang' => 'en'
|
|
] ) );
|
|
$msg = $ctx->msg( 'mainpage' );
|
|
$this->assertInstanceOf( Message::class, $msg );
|
|
$this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
|
|
}
|
|
}
|