Merge "Improve changePassword.php error handling"
[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 ] ) );
19 }
20
21 public function testEmpty() {
22 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
23
24 // Request parameters
25 $this->assertEquals( [], $ctx->getModules() );
26 $this->assertEquals( 'qqx', $ctx->getLanguage() );
27 $this->assertEquals( false, $ctx->getDebug() );
28 $this->assertEquals( null, $ctx->getOnly() );
29 $this->assertEquals( 'fallback', $ctx->getSkin() );
30 $this->assertEquals( null, $ctx->getUser() );
31 $this->assertNull( $ctx->getContentOverrideCallback() );
32
33 // Misc
34 $this->assertEquals( 'ltr', $ctx->getDirection() );
35 $this->assertEquals( 'qqx|fallback||||||||', $ctx->getHash() );
36 $this->assertInstanceOf( User::class, $ctx->getUserObj() );
37 }
38
39 public function testDummy() {
40 $this->assertInstanceOf(
41 ResourceLoaderContext::class,
42 ResourceLoaderContext::newDummyContext()
43 );
44 }
45
46 public function testAccessors() {
47 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
48 $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
49 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $ctx->getLogger() );
50 }
51
52 public function testTypicalRequest() {
53 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
54 'debug' => 'false',
55 'lang' => 'zh',
56 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
57 'only' => 'styles',
58 'skin' => 'fallback',
59 ] ) );
60
61 // Request parameters
62 $this->assertEquals(
63 $ctx->getModules(),
64 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
65 );
66 $this->assertEquals( false, $ctx->getDebug() );
67 $this->assertEquals( 'zh', $ctx->getLanguage() );
68 $this->assertEquals( 'styles', $ctx->getOnly() );
69 $this->assertEquals( 'fallback', $ctx->getSkin() );
70 $this->assertEquals( null, $ctx->getUser() );
71
72 // Misc
73 $this->assertEquals( 'ltr', $ctx->getDirection() );
74 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
75 }
76
77 public function testShouldInclude() {
78 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
79 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
80 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
81 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
82
83 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
84 'only' => 'styles'
85 ] ) );
86 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
87 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
88 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
89
90 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
91 'only' => 'scripts'
92 ] ) );
93 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
94 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
95 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
96 }
97
98 public function testGetUser() {
99 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
100 $this->assertSame( null, $ctx->getUser() );
101 $this->assertTrue( $ctx->getUserObj()->isAnon() );
102
103 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
104 'user' => 'Example'
105 ] ) );
106 $this->assertSame( 'Example', $ctx->getUser() );
107 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
108 }
109
110 public function testMsg() {
111 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
112 'lang' => 'en'
113 ] ) );
114 $msg = $ctx->msg( 'mainpage' );
115 $this->assertInstanceOf( Message::class, $msg );
116 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
117 }
118 }