MediaWiki  master
DerivativeResourceLoaderContextTest.php
Go to the documentation of this file.
1 <?php
2 
8 
10 
11  protected static function makeContext() {
12  $request = new FauxRequest( [
13  'lang' => 'qqx',
14  'modules' => 'test.default',
15  'only' => 'scripts',
16  'skin' => 'fallback',
17  'target' => 'test',
18  ] );
19  return new ResourceLoaderContext( new ResourceLoader(), $request );
20  }
21 
22  public function testChangeModules() {
23  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
24  $this->assertSame( $derived->getModules(), [ 'test.default' ], 'inherit from parent' );
25 
26  $derived->setModules( [ 'test.override' ] );
27  $this->assertSame( $derived->getModules(), [ 'test.override' ] );
28  }
29 
30  public function testChangeLanguageAndDirection() {
31  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
32  $this->assertSame( $derived->getLanguage(), 'qqx', 'inherit from parent' );
33 
34  $derived->setLanguage( 'nl' );
35  $this->assertSame( $derived->getLanguage(), 'nl' );
36  $this->assertSame( $derived->getDirection(), 'ltr' );
37 
38  // Changing the language must clear cache of computed direction
39  $derived->setLanguage( 'he' );
40  $this->assertSame( $derived->getDirection(), 'rtl' );
41  $this->assertSame( $derived->getLanguage(), 'he' );
42 
43  // Overriding the direction explicitly is allowed
44  $derived->setDirection( 'ltr' );
45  $this->assertSame( $derived->getDirection(), 'ltr' );
46  $this->assertSame( $derived->getLanguage(), 'he' );
47  }
48 
49  public function testChangeSkin() {
50  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
51  $this->assertSame( $derived->getSkin(), 'fallback', 'inherit from parent' );
52 
53  $derived->setSkin( 'myskin' );
54  $this->assertSame( $derived->getSkin(), 'myskin' );
55  }
56 
57  public function testChangeUser() {
58  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
59  $this->assertSame( $derived->getUser(), null, 'inherit from parent' );
60 
61  $derived->setUser( 'MyUser' );
62  $this->assertSame( $derived->getUser(), 'MyUser' );
63  }
64 
65  public function testChangeDebug() {
66  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
67  $this->assertSame( $derived->getDebug(), false, 'inherit from parent' );
68 
69  $derived->setDebug( true );
70  $this->assertSame( $derived->getDebug(), true );
71  }
72 
73  public function testChangeOnly() {
74  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
75  $this->assertSame( $derived->getOnly(), 'scripts', 'inherit from parent' );
76 
77  $derived->setOnly( 'styles' );
78  $this->assertSame( $derived->getOnly(), 'styles' );
79 
80  $derived->setOnly( null );
81  $this->assertSame( $derived->getOnly(), null );
82  }
83 
84  public function testChangeVersion() {
85  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
86  $this->assertSame( $derived->getVersion(), null );
87 
88  $derived->setVersion( 'hw1' );
89  $this->assertSame( $derived->getVersion(), 'hw1' );
90  }
91 
92  public function testChangeRaw() {
93  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
94  $this->assertSame( $derived->getRaw(), false, 'inherit from parent' );
95 
96  $derived->setRaw( true );
97  $this->assertSame( $derived->getRaw(), true );
98  }
99 
100  public function testChangeHash() {
101  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
102  $this->assertSame( $derived->getHash(), 'qqx|fallback|||scripts|||||', 'inherit' );
103 
104  $derived->setLanguage( 'nl' );
105  $derived->setUser( 'Example' );
106  // Assert that subclass is able to clear parent class "hash" member
107  $this->assertSame( $derived->getHash(), 'nl|fallback||Example|scripts|||||' );
108  }
109 
110  public function testChangeContentOverrides() {
111  $derived = new DerivativeResourceLoaderContext( self::makeContext() );
112  $this->assertNull( $derived->getContentOverrideCallback(), 'default' );
113 
114  $override = function ( Title $t ) {
115  return null;
116  };
117  $derived->setContentOverrideCallback( $override );
118  $this->assertSame( $override, $derived->getContentOverrideCallback(), 'changed' );
119 
120  $derived2 = new DerivativeResourceLoaderContext( $derived );
121  $this->assertSame(
122  $override,
123  $derived2->getContentOverrideCallback(),
124  'change via a second derivative layer'
125  );
126  }
127 
128  public function testImmutableAccessors() {
129  $context = self::makeContext();
130  $derived = new DerivativeResourceLoaderContext( $context );
131  $this->assertSame( $derived->getRequest(), $context->getRequest() );
132  $this->assertSame( $derived->getResourceLoader(), $context->getResourceLoader() );
133  }
134 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Allows changing specific properties of a context object, without changing the main one...
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1982
ResourceLoader DerivativeResourceLoaderContext.
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
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2633
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2633
Object passed around to modules which contains information about the state of a specific loader reque...