MediaWiki  1.30.2
ResourceLoaderContextTest.php
Go to the documentation of this file.
1 <?php
2 
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  ] ) );
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( '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() );
31 
32  // Misc
33  $this->assertEquals( 'ltr', $ctx->getDirection() );
34  $this->assertEquals( 'nl|fallback||||||||', $ctx->getHash() );
35  $this->assertInstanceOf( User::class, $ctx->getUserObj() );
36  }
37 
38  public function testDummy() {
39  $this->assertInstanceOf(
42  );
43  }
44 
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() );
49  }
50 
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  ] ) );
59 
60  // Request parameters
61  $this->assertEquals(
62  $ctx->getModules(),
63  [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
64  );
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() );
70 
71  // Misc
72  $this->assertEquals( 'ltr', $ctx->getDirection() );
73  $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
74  }
75 
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' );
81 
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' );
88 
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' );
95  }
96 
97  public function testGetUser() {
98  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
99  $this->assertSame( null, $ctx->getUser() );
100  $this->assertTrue( $ctx->getUserObj()->isAnon() );
101 
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() );
107  }
108 
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() );
116  }
117 }
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
ResourceLoaderContext\newDummyContext
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
Definition: ResourceLoaderContext.php:139
ResourceLoaderContextTest\testMsg
testMsg()
Definition: ResourceLoaderContextTest.php:109
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
ResourceLoaderContextTest\testDummy
testDummy()
Definition: ResourceLoaderContextTest.php:38
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:35
ResourceLoaderContextTest\testEmpty
testEmpty()
Definition: ResourceLoaderContextTest.php:21
ResourceLoaderContextTest\testShouldInclude
testShouldInclude()
Definition: ResourceLoaderContextTest.php:76
ResourceLoaderContextTest\getResourceLoader
static getResourceLoader()
Definition: ResourceLoaderContextTest.php:12
ResourceLoaderContextTest\testAccessors
testAccessors()
Definition: ResourceLoaderContextTest.php:45
ResourceLoaderContextTest\testGetUser
testGetUser()
Definition: ResourceLoaderContextTest.php:97
ResourceLoaderContextTest
See also:
Definition: ResourceLoaderContextTest.php:11
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:176
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:52
ResourceLoaderContextTest\testTypicalRequest
testTypicalRequest()
Definition: ResourceLoaderContextTest.php:51