f6fd82408dd93f39bc3cf1e72fa96041a85148c4
[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( WebRequest::class, $ctx->getRequest() );
52 $this->assertInstanceOf( Psr\Log\LoggerInterface::class, $ctx->getLogger() );
53 }
54
55 public function testTypicalRequest() {
56 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
57 'debug' => 'false',
58 'lang' => 'zh',
59 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
60 'only' => 'styles',
61 'skin' => 'fallback',
62 ] ) );
63
64 // Request parameters
65 $this->assertEquals(
66 $ctx->getModules(),
67 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
68 );
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() );
74
75 // Misc
76 $this->assertEquals( 'ltr', $ctx->getDirection() );
77 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
78 }
79
80 public static function provideDirection() {
81 yield 'LTR language' => [
82 [ 'lang' => 'en' ],
83 'ltr',
84 ];
85 yield 'RTL language' => [
86 [ 'lang' => 'he' ],
87 'rtl',
88 ];
89 yield 'explicit LTR' => [
90 [ 'lang' => 'he', 'dir' => 'ltr' ],
91 'ltr',
92 ];
93 yield 'explicit RTL' => [
94 [ 'lang' => 'en', 'dir' => 'rtl' ],
95 'rtl',
96 ];
97 // Not supported, but tested to cover the case and detect change
98 yield 'invalid dir' => [
99 [ 'lang' => 'he', 'dir' => 'xyz' ],
100 'rtl',
101 ];
102 }
103
104 /**
105 * @dataProvider provideDirection
106 */
107 public function testDirection( array $params, $expected ) {
108 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( $params ) );
109 $this->assertEquals( $expected, $ctx->getDirection() );
110 }
111
112 public function testShouldInclude() {
113 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
114 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
115 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
116 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
117
118 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
119 'only' => 'styles'
120 ] ) );
121 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
122 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
123 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
124
125 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
126 'only' => 'scripts'
127 ] ) );
128 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
129 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
130 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
131 }
132
133 public function testGetUser() {
134 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
135 $this->assertSame( null, $ctx->getUser() );
136 $this->assertTrue( $ctx->getUserObj()->isAnon() );
137
138 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
139 'user' => 'Example'
140 ] ) );
141 $this->assertSame( 'Example', $ctx->getUser() );
142 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
143 }
144
145 public function testMsg() {
146 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
147 'lang' => 'en'
148 ] ) );
149 $msg = $ctx->msg( 'mainpage' );
150 $this->assertInstanceOf( Message::class, $msg );
151 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
152 }
153 }