Merge "rdbms: mark DatabaseSqlite::checkForEnabledSearch() as public"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderContextTest.php
1 <?php
2
3 /**
4 * See also:
5 * - ResourceLoaderTest::testExpandModuleNames
6 * - ResourceLoaderImageModuleTest::testContext
7 *
8 * @group Cache
9 * @covers ResourceLoaderContext
10 */
11 class 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 $this->assertNull( $ctx->getContentOverrideCallback() );
35
36 // Misc
37 $this->assertEquals( 'ltr', $ctx->getDirection() );
38 $this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
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( 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 function testShouldInclude() {
81 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
82 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
83 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
84 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
85
86 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
87 'only' => 'styles'
88 ] ) );
89 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
90 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
91 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
92
93 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
94 'only' => 'scripts'
95 ] ) );
96 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
97 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
98 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
99 }
100
101 public function testGetUser() {
102 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
103 $this->assertSame( null, $ctx->getUser() );
104 $this->assertTrue( $ctx->getUserObj()->isAnon() );
105
106 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
107 'user' => 'Example'
108 ] ) );
109 $this->assertSame( 'Example', $ctx->getUser() );
110 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
111 }
112
113 public function testMsg() {
114 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
115 'lang' => 'en'
116 ] ) );
117 $msg = $ctx->msg( 'mainpage' );
118 $this->assertInstanceOf( Message::class, $msg );
119 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
120 }
121 }