5 * - ResourceLoaderTest::testExpandModuleNames
6 * - ResourceLoaderImageModuleTest::testContext
9 * @covers ResourceLoaderContext
11 class ResourceLoaderContextTest
extends PHPUnit\Framework\TestCase
{
13 use MediaWikiCoversValidator
;
15 protected static function getResourceLoader() {
16 return new EmptyResourceLoader( new HashConfig( [
17 'ResourceLoaderDebug' => false,
18 'DefaultSkin' => 'fallback',
19 'LanguageCode' => 'nl',
20 'LoadScript' => '/w/load.php',
24 public function testEmpty() {
25 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
28 $this->assertEquals( [], $ctx->getModules() );
29 $this->assertEquals( 'nl', $ctx->getLanguage() );
30 $this->assertEquals( false, $ctx->getDebug() );
31 $this->assertEquals( null, $ctx->getOnly() );
32 $this->assertEquals( 'fallback', $ctx->getSkin() );
33 $this->assertEquals( null, $ctx->getUser() );
34 $this->assertNull( $ctx->getContentOverrideCallback() );
37 $this->assertEquals( 'ltr', $ctx->getDirection() );
38 $this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
39 $this->assertInstanceOf( User
::class, $ctx->getUserObj() );
42 public function testDummy() {
43 $this->assertInstanceOf(
44 ResourceLoaderContext
::class,
45 ResourceLoaderContext
::newDummyContext()
49 public function testAccessors() {
50 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
51 $this->assertInstanceOf( WebRequest
::class, $ctx->getRequest() );
52 $this->assertInstanceOf( \Psr\Log\LoggerInterface
::class, $ctx->getLogger() );
55 public function testTypicalRequest() {
56 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
59 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
67 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
69 $this->assertEquals( false, $ctx->getDebug() );
70 $this->assertEquals( 'zh', $ctx->getLanguage() );
71 $this->assertEquals( 'styles', $ctx->getOnly() );
72 $this->assertEquals( 'fallback', $ctx->getSkin() );
73 $this->assertEquals( null, $ctx->getUser() );
76 $this->assertEquals( 'ltr', $ctx->getDirection() );
77 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
80 public function testShouldInclude() {
81 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
82 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
83 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
84 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
86 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
89 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
90 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
91 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
93 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
96 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
97 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
98 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
101 public function testGetUser() {
102 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
103 $this->assertSame( null, $ctx->getUser() );
104 $this->assertTrue( $ctx->getUserObj()->isAnon() );
106 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
109 $this->assertSame( 'Example', $ctx->getUser() );
110 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
113 public function testMsg() {
114 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
117 $msg = $ctx->msg( 'mainpage' );
118 $this->assertInstanceOf( Message
::class, $msg );
119 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );