MediaWiki REL1_28
OutputPageTest.php
Go to the documentation of this file.
1<?php
2
13 const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
14 const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
15
30 protected function assertTransformCssMediaCase( $args ) {
31 $queryData = [];
32 if ( isset( $args['printableQuery'] ) ) {
33 $queryData['printable'] = $args['printableQuery'];
34 }
35
36 if ( isset( $args['handheldQuery'] ) ) {
37 $queryData['handheld'] = $args['handheldQuery'];
38 }
39
40 $fauxRequest = new FauxRequest( $queryData, false );
41 $this->setMwGlobals( [
42 'wgRequest' => $fauxRequest,
43 ] );
44
45 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
47 }
48
53 public function testPrintRequests() {
55 'printableQuery' => '1',
56 'media' => 'screen',
57 'expectedReturn' => null,
58 'message' => 'On printable request, screen returns null'
59 ] );
60
62 'printableQuery' => '1',
63 'media' => self::SCREEN_MEDIA_QUERY,
64 'expectedReturn' => null,
65 'message' => 'On printable request, screen media query returns null'
66 ] );
67
69 'printableQuery' => '1',
70 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
71 'expectedReturn' => null,
72 'message' => 'On printable request, screen media query with only returns null'
73 ] );
74
76 'printableQuery' => '1',
77 'media' => 'print',
78 'expectedReturn' => '',
79 'message' => 'On printable request, media print returns empty string'
80 ] );
81 }
82
87 public function testScreenRequests() {
89 'media' => 'screen',
90 'expectedReturn' => 'screen',
91 'message' => 'On screen request, screen media type is preserved'
92 ] );
93
95 'media' => 'handheld',
96 'expectedReturn' => 'handheld',
97 'message' => 'On screen request, handheld media type is preserved'
98 ] );
99
101 'media' => self::SCREEN_MEDIA_QUERY,
102 'expectedReturn' => self::SCREEN_MEDIA_QUERY,
103 'message' => 'On screen request, screen media query is preserved.'
104 ] );
105
107 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
108 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
109 'message' => 'On screen request, screen media query with only is preserved.'
110 ] );
111
113 'media' => 'print',
114 'expectedReturn' => 'print',
115 'message' => 'On screen request, print media type is preserved'
116 ] );
117 }
118
123 public function testHandheld() {
125 'handheldQuery' => '1',
126 'media' => 'handheld',
127 'expectedReturn' => '',
128 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
129 ] );
130
132 'handheldQuery' => '1',
133 'media' => 'screen',
134 'expectedReturn' => null,
135 'message' => 'On request with handheld querystring and media is screen, returns null'
136 ] );
137 }
138
139 public static function provideMakeResourceLoaderLink() {
140 // @codingStandardsIgnoreStart Generic.Files.LineLength
141 return [
142 // Single only=scripts load
143 [
145 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
146 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
147 . "});</script>"
148 ],
149 // Multiple only=styles load
150 [
151 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
152
153 '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.bar%2Cbaz%2Cfoo&amp;only=styles&amp;skin=fallback"/>'
154 ],
155 // Private embed (only=scripts)
156 [
157 [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
158 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
159 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
160 . "});</script>"
161 ],
162 ];
163 // @codingStandardsIgnoreEnd
164 }
165
172 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
173 $this->setMwGlobals( [
174 'wgResourceLoaderDebug' => false,
175 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
176 ] );
177 $class = new ReflectionClass( 'OutputPage' );
178 $method = $class->getMethod( 'makeResourceLoaderLink' );
179 $method->setAccessible( true );
180 $ctx = new RequestContext();
181 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
182 $ctx->setLanguage( 'en' );
183 $out = new OutputPage( $ctx );
184 $rl = $out->getResourceLoader();
185 $rl->setMessageBlobStore( new NullMessageBlobStore() );
186 $rl->register( [
187 'test.foo' => new ResourceLoaderTestModule( [
188 'script' => 'mw.test.foo( { a: true } );',
189 'styles' => '.mw-test-foo { content: "style"; }',
190 ] ),
191 'test.bar' => new ResourceLoaderTestModule( [
192 'script' => 'mw.test.bar( { a: true } );',
193 'styles' => '.mw-test-bar { content: "style"; }',
194 ] ),
195 'test.baz' => new ResourceLoaderTestModule( [
196 'script' => 'mw.test.baz( { a: true } );',
197 'styles' => '.mw-test-baz { content: "style"; }',
198 ] ),
199 'test.quux' => new ResourceLoaderTestModule( [
200 'script' => 'mw.test.baz( { token: 123 } );',
201 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
202 'group' => 'private',
203 ] ),
204 ] );
205 $links = $method->invokeArgs( $out, $args );
206 $actualHtml = strval( $links );
207 $this->assertEquals( $expectedHtml, $actualHtml );
208 }
209
216 public function testVaryHeaders( $calls, $vary, $key ) {
217 // get rid of default Vary fields
218 $outputPage = $this->getMockBuilder( 'OutputPage' )
219 ->setConstructorArgs( [ new RequestContext() ] )
220 ->setMethods( [ 'getCacheVaryCookies' ] )
221 ->getMock();
222 $outputPage->expects( $this->any() )
223 ->method( 'getCacheVaryCookies' )
224 ->will( $this->returnValue( [] ) );
225 TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = [];
226
227 foreach ( $calls as $call ) {
228 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
229 }
230 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
231 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
232 }
233
234 public function provideVaryHeaders() {
235 // note: getKeyHeader() automatically adds Vary: Cookie
236 return [
237 [ // single header
238 [
239 [ 'Cookie' ],
240 ],
241 'Vary: Cookie',
242 'Key: Cookie',
243 ],
244 [ // non-unique headers
245 [
246 [ 'Cookie' ],
247 [ 'Accept-Language' ],
248 [ 'Cookie' ],
249 ],
250 'Vary: Cookie, Accept-Language',
251 'Key: Cookie,Accept-Language',
252 ],
253 [ // two headers with single options
254 [
255 [ 'Cookie', [ 'param=phpsessid' ] ],
256 [ 'Accept-Language', [ 'substr=en' ] ],
257 ],
258 'Vary: Cookie, Accept-Language',
259 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
260 ],
261 [ // one header with multiple options
262 [
263 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
264 ],
265 'Vary: Cookie',
266 'Key: Cookie;param=phpsessid;param=userId',
267 ],
268 [ // Duplicate option
269 [
270 [ 'Cookie', [ 'param=phpsessid' ] ],
271 [ 'Cookie', [ 'param=phpsessid' ] ],
272 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
273 ],
274 'Vary: Cookie, Accept-Language',
275 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
276 ],
277 [ // Same header, different options
278 [
279 [ 'Cookie', [ 'param=phpsessid' ] ],
280 [ 'Cookie', [ 'param=userId' ] ],
281 ],
282 'Vary: Cookie',
283 'Key: Cookie;param=phpsessid;param=userId',
284 ],
285 ];
286 }
287
292 $request = new FauxRequest();
293 $context = new RequestContext();
294 $context->setRequest( $request );
295 $outputPage = new OutputPage( $context );
296
297 // No cookies are set.
298 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
299
300 // 'Token' is present but empty, so it shouldn't count.
301 $request->setCookie( 'Token', '' );
302 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
303
304 // 'Token' present and nonempty.
305 $request->setCookie( 'Token', '123' );
306 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
307 }
308}
309
314 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
315 return [];
316 }
317
318 public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
319 return false;
320 }
321
322 public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
323 }
324
325 public function updateMessage( $key ) {
326 }
327
328 public function clear() {
329 }
330}
if( $line===false) $args
Definition cdb.php:64
WebRequest clone which takes values from a provided array.
setMwGlobals( $pairs, $value=null)
This class generates message blobs for use by ResourceLoader modules.
MessageBlobStore that doesn't do anything.
clear()
Invalidate cache keys for all known modules.
insertMessageBlob( $name, ResourceLoaderModule $module, $lang)
updateMessage( $key)
Invalidate cache keys for modules using this message key.
updateModule( $name, ResourceLoaderModule $module, $lang)
const SCREEN_ONLY_MEDIA_QUERY
testHaveCacheVaryCookies()
OutputPage::haveCacheVaryCookies.
testHandheld()
Tests handheld behavior OutputPage::transformCssMedia.
testMakeResourceLoaderLink( $args, $expectedHtml)
See ResourceLoaderClientHtmlTest for full coverage.
static provideMakeResourceLoaderLink()
assertTransformCssMediaCase( $args)
Tests a particular case of transformCssMedia, using the given input, globals, expected return,...
testScreenRequests()
Tests screen requests, without either query parameter set OutputPage::transformCssMedia.
testPrintRequests()
Tests print requests OutputPage::transformCssMedia.
testVaryHeaders( $calls, $vary, $key)
provideVaryHeaders OutputPage::addVaryHeader OutputPage::getVaryHeader OutputPage::getKeyHeader
This class should be covered by a general architecture document which does not exist as of January 20...
static transformCssMedia( $media)
Transform "media" attribute based on request parameters.
Group all the pieces relevant to the context of a request into one instance.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Dynamic JavaScript and CSS resource loading system.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:886
error also a ContextSource you ll probably need to make sure the header is varied on such as when responding to a resource loader request or generating HTML output & $resourceLoader
Definition hooks.txt:2692
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
$context
Definition load.php:50
if(!isset( $args[0])) $lang