MediaWiki  1.33.1
ResourceLoaderContextTest.php
Go to the documentation of this file.
1 <?php
2 
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  'DefaultSkin' => 'fallback',
18  'LanguageCode' => 'nl',
19  'LoadScript' => '/w/load.php',
20  ] ) );
21  }
22 
23  public function testEmpty() {
24  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
25 
26  // Request parameters
27  $this->assertEquals( [], $ctx->getModules() );
28  $this->assertEquals( 'nl', $ctx->getLanguage() );
29  $this->assertEquals( false, $ctx->getDebug() );
30  $this->assertEquals( null, $ctx->getOnly() );
31  $this->assertEquals( 'fallback', $ctx->getSkin() );
32  $this->assertEquals( null, $ctx->getUser() );
33  $this->assertNull( $ctx->getContentOverrideCallback() );
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
ResourceLoaderContext\newDummyContext
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
Definition: ResourceLoaderContext.php:115
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:35
ResourceLoaderContextTest\testEmpty
testEmpty()
Definition: ResourceLoaderContextTest.php:23
ResourceLoaderContextTest\testShouldInclude
testShouldInclude()
Definition: ResourceLoaderContextTest.php:79
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ResourceLoaderContextTest\getResourceLoader
static getResourceLoader()
Definition: ResourceLoaderContextTest.php:14
ResourceLoaderContextTest\testAccessors
testAccessors()
Definition: ResourceLoaderContextTest.php:48
ResourceLoaderContextTest\testGetUser
testGetUser()
Definition: ResourceLoaderContextTest.php:100
ResourceLoaderContextTest
See also:
Definition: ResourceLoaderContextTest.php:10
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:172
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:54