MediaWiki  master
ResourceLoaderContextTest.php
Go to the documentation of this file.
1 <?php
2 
11 
13 
14  protected static function getResourceLoader() {
15  return new EmptyResourceLoader( new HashConfig( [
16  'ResourceLoaderDebug' => false,
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( 'qqx', $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  $this->assertNull( $ctx->getContentOverrideCallback() );
32 
33  // Misc
34  $this->assertEquals( 'ltr', $ctx->getDirection() );
35  $this->assertEquals( 'qqx|fallback||||||||', $ctx->getHash() );
36  $this->assertInstanceOf( User::class, $ctx->getUserObj() );
37  }
38 
39  public function testDummy() {
40  $this->assertInstanceOf(
43  );
44  }
45 
46  public function testAccessors() {
47  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
48  $this->assertInstanceOf( WebRequest::class, $ctx->getRequest() );
49  $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $ctx->getLogger() );
50  }
51 
52  public function testTypicalRequest() {
53  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
54  'debug' => 'false',
55  'lang' => 'zh',
56  'modules' => 'foo|foo.quux,baz,bar|baz.quux',
57  'only' => 'styles',
58  'skin' => 'fallback',
59  ] ) );
60 
61  // Request parameters
62  $this->assertEquals(
63  $ctx->getModules(),
64  [ 'foo', 'foo.quux', 'foo.baz', 'foo.bar', 'baz.quux' ]
65  );
66  $this->assertEquals( false, $ctx->getDebug() );
67  $this->assertEquals( 'zh', $ctx->getLanguage() );
68  $this->assertEquals( 'styles', $ctx->getOnly() );
69  $this->assertEquals( 'fallback', $ctx->getSkin() );
70  $this->assertEquals( null, $ctx->getUser() );
71 
72  // Misc
73  $this->assertEquals( 'ltr', $ctx->getDirection() );
74  $this->assertEquals( 'zh|fallback|||styles|||||', $ctx->getHash() );
75  }
76 
77  public function testShouldInclude() {
78  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
79  $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in combined' );
80  $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in combined' );
81  $this->assertTrue( $ctx->shouldIncludeMessages(), 'Messages in combined' );
82 
83  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
84  'only' => 'styles'
85  ] ) );
86  $this->assertFalse( $ctx->shouldIncludeScripts(), 'Scripts not in styles-only' );
87  $this->assertTrue( $ctx->shouldIncludeStyles(), 'Styles in styles-only' );
88  $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in styles-only' );
89 
90  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
91  'only' => 'scripts'
92  ] ) );
93  $this->assertTrue( $ctx->shouldIncludeScripts(), 'Scripts in scripts-only' );
94  $this->assertFalse( $ctx->shouldIncludeStyles(), 'Styles not in scripts-only' );
95  $this->assertFalse( $ctx->shouldIncludeMessages(), 'Messages not in scripts-only' );
96  }
97 
98  public function testGetUser() {
99  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [] ) );
100  $this->assertSame( null, $ctx->getUser() );
101  $this->assertTrue( $ctx->getUserObj()->isAnon() );
102 
103  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
104  'user' => 'Example'
105  ] ) );
106  $this->assertSame( 'Example', $ctx->getUser() );
107  $this->assertEquals( 'Example', $ctx->getUserObj()->getName() );
108  }
109 
110  public function testMsg() {
111  $ctx = new ResourceLoaderContext( $this->getResourceLoader(), new FauxRequest( [
112  'lang' => 'en'
113  ] ) );
114  $msg = $ctx->msg( 'mainpage' );
115  $this->assertInstanceOf( Message::class, $msg );
116  $this->assertSame( 'Main Page', $msg->useDatabase( false )->plain() );
117  }
118 }
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don&#39;t "really" need...
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
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
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
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
Object passed around to modules which contains information about the state of a specific loader reque...