c748e2cf6e894dc0f1e3e7adf2c6a6e73e31cb1d
[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->assertSame( [], $ctx->getReqBase() );
39 $this->assertInstanceOf( User::class, $ctx->getUserObj() );
40 }
41
42 public function testDummy() {
43 $this->assertInstanceOf(
44 ResourceLoaderContext::class,
45 ResourceLoaderContext::newDummyContext()
46 );
47 }
48
49 public function testAccessors() {
50 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
51 $this->assertInstanceOf( ResourceLoader::class, $ctx->getResourceLoader() );
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 $this->assertSame( [ 'lang' => 'zh' ], $ctx->getReqBase() );
80 }
81
82 public static function provideDirection() {
83 yield 'LTR language' => [
84 [ 'lang' => 'en' ],
85 'ltr',
86 ];
87 yield 'RTL language' => [
88 [ 'lang' => 'he' ],
89 'rtl',
90 ];
91 yield 'explicit LTR' => [
92 [ 'lang' => 'he', 'dir' => 'ltr' ],
93 'ltr',
94 ];
95 yield 'explicit RTL' => [
96 [ 'lang' => 'en', 'dir' => 'rtl' ],
97 'rtl',
98 ];
99 // Not supported, but tested to cover the case and detect change
100 yield 'invalid dir' => [
101 [ 'lang' => 'he', 'dir' => 'xyz' ],
102 'rtl',
103 ];
104 }
105
106 /**
107 * @dataProvider provideDirection
108 */
109 public function testDirection( array $params, $expected ) {
110 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) );
111 $this->assertEquals( $expected, $ctx->getDirection() );
112 }
113
114 public function testShouldInclude() {
115 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
116 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
117 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
118 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
119
120 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
121 'only' => 'styles'
122 ] ) );
123 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
124 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
125 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
126
127 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
128 'only' => 'scripts'
129 ] ) );
130 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
131 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
132 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
133 }
134
135 public function testGetUser() {
136 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
137 $this->assertSame( null, $ctx->getUser() );
138 $this->assertTrue( $ctx->getUserObj()->isAnon() );
139
140 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
141 'user' => 'Example'
142 ] ) );
143 $this->assertSame( 'Example', $ctx->getUser() );
144 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
145 }
146
147 public function testMsg() {
148 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
149 'lang' => 'en'
150 ] ) );
151 $msg = $ctx->msg( 'mainpage' );
152 $this->assertInstanceOf( Message::class, $msg );
153 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
154 }
155 }