Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderContextTest.php
1 <?php
2
3 /**
4 * See also:
5 * - ResourceLoaderImageModuleTest::testContext
6 *
7 * @group ResourceLoader
8 * @covers ResourceLoaderContext
9 */
10 class ResourceLoaderContextTest extends PHPUnit\Framework\TestCase {
11
12 use MediaWikiCoversValidator;
13
14 protected static function getResourceLoader() {
15 return new EmptyResourceLoader( new HashConfig( [
16 'ResourceLoaderDebug' => false,
17 'LoadScript' => '/w/load.php',
18 // For ResourceLoader::register()
19 'ResourceModuleSkinStyles' => [],
20 ] ) );
21 }
22
23 public function testEmpty() {
24 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
25
26 // Request parameters
27 $this->assertEquals( [], $ctx->getModules() );
28 $this->assertEquals( 'qqx', $ctx->getLanguage() );
29 $this->assertEquals( false, $ctx->getDebug() );
30 $this->assertEquals( null, $ctx->getOnly() );
31 $this->assertEquals( 'fallback', $ctx->getSkin() );
32 $this->assertEquals( null, $ctx->getUser() );
33 $this->assertNull( $ctx->getContentOverrideCallback() );
34
35 // Misc
36 $this->assertEquals( 'ltr', $ctx->getDirection() );
37 $this->assertEquals( 'qqx|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( ResourceLoader::class, $ctx->getResourceLoader() );
51 $this->assertInstanceOf( Config::class, $ctx->getConfig() );
52 $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
53 $this->assertInstanceOf( Psr\Log\LoggerInterface::class, $ctx->getLogger() );
54 }
55
56 public function testTypicalRequest() {
57 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
58 'debug' => 'false',
59 'lang' => 'zh',
60 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
61 'only' => 'styles',
62 'skin' => 'fallback',
63 ] ) );
64
65 // Request parameters
66 $this->assertEquals(
67 $ctx->getModules(),
68 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
69 );
70 $this->assertEquals( false, $ctx->getDebug() );
71 $this->assertEquals( 'zh', $ctx->getLanguage() );
72 $this->assertEquals( 'styles', $ctx->getOnly() );
73 $this->assertEquals( 'fallback', $ctx->getSkin() );
74 $this->assertEquals( null, $ctx->getUser() );
75
76 // Misc
77 $this->assertEquals( 'ltr', $ctx->getDirection() );
78 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
79 }
80
81 public static function provideDirection() {
82 yield 'LTR language' => [
83 [ 'lang' => 'en' ],
84 'ltr',
85 ];
86 yield 'RTL language' => [
87 [ 'lang' => 'he' ],
88 'rtl',
89 ];
90 yield 'explicit LTR' => [
91 [ 'lang' => 'he', 'dir' => 'ltr' ],
92 'ltr',
93 ];
94 yield 'explicit RTL' => [
95 [ 'lang' => 'en', 'dir' => 'rtl' ],
96 'rtl',
97 ];
98 // Not supported, but tested to cover the case and detect change
99 yield 'invalid dir' => [
100 [ 'lang' => 'he', 'dir' => 'xyz' ],
101 'rtl',
102 ];
103 }
104
105 /**
106 * @dataProvider provideDirection
107 */
108 public function testDirection( array $params, $expected ) {
109 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) );
110 $this->assertEquals( $expected, $ctx->getDirection() );
111 }
112
113 public function testShouldInclude() {
114 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
115 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
116 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
117 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
118
119 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
120 'only' => 'styles'
121 ] ) );
122 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
123 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
124 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
125
126 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
127 'only' => 'scripts'
128 ] ) );
129 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
130 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
131 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
132 }
133
134 public function testGetUser() {
135 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
136 $this->assertSame( null, $ctx->getUser() );
137 $this->assertTrue( $ctx->getUserObj()->isAnon() );
138
139 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
140 'user' => 'Example'
141 ] ) );
142 $this->assertSame( 'Example', $ctx->getUser() );
143 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
144 }
145
146 public function testMsg() {
147 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
148 'lang' => 'en'
149 ] ) );
150 $msg = $ctx->msg( 'mainpage' );
151 $this->assertInstanceOf( Message::class, $msg );
152 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
153 }
154 }