MediaWiki REL1_31
ResourceLoaderContextTest.php
Go to the documentation of this file.
1<?php
2
11class ResourceLoaderContextTest extends PHPUnit\Framework\TestCase {
12
13 use MediaWikiCoversValidator;
14
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',
21 ] ) );
22 }
23
24 public function testEmpty() {
25 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
26
27 // Request parameters
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
35 // Misc
36 $this->assertEquals( 'ltr', $ctx->getDirection() );
37 $this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
38 $this->assertInstanceOf( User::class, $ctx->getUserObj() );
39 }
40
41 public function testDummy() {
42 $this->assertInstanceOf(
43 ResourceLoaderContext::class,
44 ResourceLoaderContext::newDummyContext()
45 );
46 }
47
48 public function testAccessors() {
49 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
50 $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
51 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $ctx->getLogger() );
52 }
53
54 public function testTypicalRequest() {
55 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
56 'debug' => 'false',
57 'lang' => 'zh',
58 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
59 'only' => 'styles',
60 'skin' => 'fallback',
61 ] ) );
62
63 // Request parameters
64 $this->assertEquals(
65 $ctx->getModules(),
66 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
67 );
68 $this->assertEquals( false, $ctx->getDebug() );
69 $this->assertEquals( 'zh', $ctx->getLanguage() );
70 $this->assertEquals( 'styles', $ctx->getOnly() );
71 $this->assertEquals( 'fallback', $ctx->getSkin() );
72 $this->assertEquals( null, $ctx->getUser() );
73
74 // Misc
75 $this->assertEquals( 'ltr', $ctx->getDirection() );
76 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
77 }
78
79 public function testShouldInclude() {
80 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
81 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
82 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
83 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
84
85 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
86 'only' => 'styles'
87 ] ) );
88 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
89 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
90 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
91
92 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
93 'only' => 'scripts'
94 ] ) );
95 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
96 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
97 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
98 }
99
100 public function testGetUser() {
101 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
102 $this->assertSame( null, $ctx->getUser() );
103 $this->assertTrue( $ctx->getUserObj()->isAnon() );
104
105 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
106 'user' => 'Example'
107 ] ) );
108 $this->assertSame( 'Example', $ctx->getUser() );
109 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
110 }
111
112 public function testMsg() {
113 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
114 'lang' => 'en'
115 ] ) );
116 $msg = $ctx->msg( 'mainpage' );
117 $this->assertInstanceOf( Message::class, $msg );
118 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
119 }
120}
WebRequest clone which takes values from a provided array.
A Config instance which stores all settings as a member variable.
Object passed around to modules which contains information about the state of a specific loader reque...