Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderContextTest.php
blobb658efba9627714eb1a20e148a4cffcc62a52356
1 <?php
3 /**
4 * See also:
5 * - ResourceLoaderTest::testExpandModuleNames
6 * - ResourceLoaderImageModuleTest::testContext
8 * @group Cache
9 * @covers ResourceLoaderContext
11 class ResourceLoaderContextTest extends PHPUnit_Framework_TestCase {
12 protected static function getResourceLoader() {
13 return new EmptyResourceLoader( new HashConfig( [
14 'ResourceLoaderDebug' => false,
15 'DefaultSkin' => 'fallback',
16 'LanguageCode' => 'nl',
17 'LoadScript' => '/w/load.php',
18 ] ) );
21 public function testEmpty() {
22 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
24 // Request parameters
25 $this->assertEquals( [], $ctx->getModules() );
26 $this->assertEquals( 'nl', $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() );
32 // Misc
33 $this->assertEquals( 'ltr', $ctx->getDirection() );
34 $this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
35 $this->assertInstanceOf( User::class, $ctx->getUserObj() );
38 public function testDummy() {
39 $this->assertInstanceOf(
40 ResourceLoaderContext::class,
41 ResourceLoaderContext::newDummyContext()
45 public function testAccessors() {
46 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
47 $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
48 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $ctx->getLogger() );
51 public function testTypicalRequest() {
52 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
53 'debug' => 'false',
54 'lang' => 'zh',
55 'modules' => 'foo|foo.quux,baz,bar|baz.quux',
56 'only' => 'styles',
57 'skin' => 'fallback',
58 ] ) );
60 // Request parameters
61 $this->assertEquals(
62 $ctx->getModules(),
63 [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
65 $this->assertEquals( false, $ctx->getDebug() );
66 $this->assertEquals( 'zh', $ctx->getLanguage() );
67 $this->assertEquals( 'styles', $ctx->getOnly() );
68 $this->assertEquals( 'fallback', $ctx->getSkin() );
69 $this->assertEquals( null, $ctx->getUser() );
71 // Misc
72 $this->assertEquals( 'ltr', $ctx->getDirection() );
73 $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
76 public function testShouldInclude() {
77 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
78 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
79 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
80 $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
82 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
83 'only' => 'styles'
84 ] ) );
85 $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
86 $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
87 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
89 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
90 'only' => 'scripts'
91 ] ) );
92 $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
93 $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
94 $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
97 public function testGetUser() {
98 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
99 $this->assertSame( null, $ctx->getUser() );
100 $this->assertTrue( $ctx->getUserObj()->isAnon() );
102 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
103 'user' => 'Example'
104 ] ) );
105 $this->assertSame( 'Example', $ctx->getUser() );
106 $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
109 public function testMsg() {
110 $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
111 'lang' => 'en'
112 ] ) );
113 $msg = $ctx->msg( 'mainpage' );
114 $this->assertInstanceOf( Message::class, $msg );
115 $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );