wiki.techinc.nl/tests/phpunit/includes/resourceloader/ResourceLoaderContextTest.php
Timo Tijhof c9318edc2d resourceloader: Warn on ResourceLoader::construct without Config
The only remaining use of 'new ResourceLoader' is in tests, which have
been migrated in this commit to either passing the real config explicitly
(for integration tests), or by passing a HashConfig from a new
'getMinimalConfig' method which has only the keys required for the tests
to pass (e.g. avoid any ConfigExeption for unknown keys).

Also clean up some related code quality issues:

* Migrate wfScript() to $conf->get() so that the local Config is used,
  instead of implicitly using global variables. This isn't deprecated for
  MediaWiki generally, but done here to prepare ResourceLoader for becoming
  a standalone library.

* Remove mocking of 'CacheEpoch' config, this is no longer used anywhere
  in ResourceLoader.

* Change EmptyResourceLoader to use the minimal config by default and
  remove code duplication by calling the parent.

  Update the small number of uses that are integration tests, to explicitly
  pass in the live config as needed. And for the one case that tests the
  'startup' module, it no longer needs to register it manually given this
  is part of ResourceLoader::__construct() by default.

Bug: T32956
Change-Id: I127346fd530fa66f205156e545758b1c29d0fac0
2019-06-10 15:24:45 +00:00

120 lines
4.2 KiB
PHP

<?php
/**
* See also:
* - ResourceLoaderImageModuleTest::testContext
*
* @group ResourceLoader
* @covers ResourceLoaderContext
*/
class ResourceLoaderContextTest extends PHPUnit\Framework\TestCase {
use MediaWikiCoversValidator;
protected static function getResourceLoader() {
return new EmptyResourceLoader( new HashConfig( [
'ResourceLoaderDebug' => false,
'LoadScript' => '/w/load.php',
// For ResourceLoader::register()
'ResourceModuleSkinStyles' => [],
] ) );
}
public function testEmpty() {
$ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
// Request parameters
$this->assertEquals( [], $ctx->getModules() );
$this->assertEquals( 'qqx', $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( 'qqx|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() );
}
}