MediaWiki  REL1_31
ResourceLoaderContextTest.php
Go to the documentation of this file.
1 <?php
2 
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 
35  // Misc
36  $this->assertEquals( 'ltr', $ctx->getDirection() );
37  $this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
38  $this->assertInstanceOf( User::class, $ctx->getUserObj() );
39  }
40 
41  public function testDummy() {
42  $this->assertInstanceOf(
45  );
46  }
47 
48  public function testAccessors() {
49  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
50  $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
51  $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $ctx->getLogger() );
52  }
53 
54  public function testTypicalRequest() {
55  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
56  'debug' => 'false',
57  'lang' => 'zh',
58  'modules' => 'foo|foo.quux,baz,bar|baz.quux',
59  'only' => 'styles',
60  'skin' => 'fallback',
61  ] ) );
62 
63  // Request parameters
64  $this->assertEquals(
65  $ctx->getModules(),
66  [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
67  );
68  $this->assertEquals( false, $ctx->getDebug() );
69  $this->assertEquals( 'zh', $ctx->getLanguage() );
70  $this->assertEquals( 'styles', $ctx->getOnly() );
71  $this->assertEquals( 'fallback', $ctx->getSkin() );
72  $this->assertEquals( null, $ctx->getUser() );
73 
74  // Misc
75  $this->assertEquals( 'ltr', $ctx->getDirection() );
76  $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
77  }
78 
79  public function testShouldInclude() {
80  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
81  $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
82  $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
83  $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
84 
85  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
86  'only' => 'styles'
87  ] ) );
88  $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
89  $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
90  $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
91 
92  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
93  'only' => 'scripts'
94  ] ) );
95  $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
96  $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
97  $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
98  }
99 
100  public function testGetUser() {
101  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
102  $this->assertSame( null, $ctx->getUser() );
103  $this->assertTrue( $ctx->getUserObj()->isAnon() );
104 
105  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
106  'user' => 'Example'
107  ] ) );
108  $this->assertSame( 'Example', $ctx->getUser() );
109  $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
110  }
111 
112  public function testMsg() {
113  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
114  'lang' => 'en'
115  ] ) );
116  $msg = $ctx->msg( 'mainpage' );
117  $this->assertInstanceOf( Message::class, $msg );
118  $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
119  }
120 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
ResourceLoaderContext\newDummyContext
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
Definition: ResourceLoaderContext.php:138
ResourceLoaderContextTest\testMsg
testMsg()
Definition: ResourceLoaderContextTest.php:112
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
ResourceLoaderContextTest\testDummy
testDummy()
Definition: ResourceLoaderContextTest.php:41
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
ResourceLoaderContextTest\testEmpty
testEmpty()
Definition: ResourceLoaderContextTest.php:24
ResourceLoaderContextTest\testShouldInclude
testShouldInclude()
Definition: ResourceLoaderContextTest.php:79
ResourceLoaderContextTest\getResourceLoader
static getResourceLoader()
Definition: ResourceLoaderContextTest.php:15
ResourceLoaderContextTest\testAccessors
testAccessors()
Definition: ResourceLoaderContextTest.php:48
ResourceLoaderContextTest\testGetUser
testGetUser()
Definition: ResourceLoaderContextTest.php:100
ResourceLoaderContextTest
See also:
Definition: ResourceLoaderContextTest.php:11
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:171
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
ResourceLoaderContextTest\testTypicalRequest
testTypicalRequest()
Definition: ResourceLoaderContextTest.php:54