MediaWiki  1.27.1
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  // Load module script only
143  [
144  [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
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  [
150  // Don't condition wrap raw modules (like the startup module)
151  [ 'test.raw', ResourceLoaderModule::TYPE_SCRIPTS ],
152  '<script async="" src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.raw&amp;only=scripts&amp;skin=fallback"></script>'
153  ],
154  // Load module styles only
155  // This also tests the order the modules are put into the url
156  [
157  [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
158 
159  '<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"/>'
160  ],
161  // Load private module (only=scripts)
162  [
163  [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
164  "<script>(window.RLQ=window.RLQ||[]).push(function(){"
165  . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
166  . "});</script>"
167  ],
168  // Load private module (combined)
169  [
170  [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ],
171  "<script>(window.RLQ=window.RLQ||[]).push(function(){"
172  . "mw.loader.implement(\"test.quux\",function($,jQuery,require,module){"
173  . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}"
174  . "\"]});});</script>"
175  ],
176  // Load no modules
177  [
179  '',
180  ],
181  // noscript group
182  [
183  [ 'test.noscript', ResourceLoaderModule::TYPE_STYLES ],
184  '<noscript><link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.noscript&amp;only=styles&amp;skin=fallback"/></noscript>'
185  ],
186  // Load two modules in separate groups
187  [
188  [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ],
189  "<script>(window.RLQ=window.RLQ||[]).push(function(){"
190  . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.bar\u0026skin=fallback");'
191  . "});</script>\n"
192  . "<script>(window.RLQ=window.RLQ||[]).push(function(){"
193  . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.foo\u0026skin=fallback");'
194  . "});</script>"
195  ],
196  ];
197  // @codingStandardsIgnoreEnd
198  }
199 
209  public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
210  $this->setMwGlobals( [
211  'wgResourceLoaderDebug' => false,
212  'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
213  ] );
214  $class = new ReflectionClass( 'OutputPage' );
215  $method = $class->getMethod( 'makeResourceLoaderLink' );
216  $method->setAccessible( true );
217  $ctx = new RequestContext();
218  $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
219  $ctx->setLanguage( 'en' );
220  $out = new OutputPage( $ctx );
221  $rl = $out->getResourceLoader();
222  $rl->setMessageBlobStore( new NullMessageBlobStore() );
223  $rl->register( [
224  'test.foo' => new ResourceLoaderTestModule( [
225  'script' => 'mw.test.foo( { a: true } );',
226  'styles' => '.mw-test-foo { content: "style"; }',
227  ] ),
228  'test.bar' => new ResourceLoaderTestModule( [
229  'script' => 'mw.test.bar( { a: true } );',
230  'styles' => '.mw-test-bar { content: "style"; }',
231  ] ),
232  'test.baz' => new ResourceLoaderTestModule( [
233  'script' => 'mw.test.baz( { a: true } );',
234  'styles' => '.mw-test-baz { content: "style"; }',
235  ] ),
236  'test.quux' => new ResourceLoaderTestModule( [
237  'script' => 'mw.test.baz( { token: 123 } );',
238  'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
239  'group' => 'private',
240  ] ),
241  'test.raw' => new ResourceLoaderTestModule( [
242  'script' => 'mw.test.baz( { token: 123 } );',
243  'isRaw' => true,
244  ] ),
245  'test.noscript' => new ResourceLoaderTestModule( [
246  'styles' => '.mw-test-noscript { content: "style"; }',
247  'group' => 'noscript',
248  ] ),
249  'test.group.bar' => new ResourceLoaderTestModule( [
250  'styles' => '.mw-group-bar { content: "style"; }',
251  'group' => 'bar',
252  ] ),
253  'test.group.foo' => new ResourceLoaderTestModule( [
254  'styles' => '.mw-group-foo { content: "style"; }',
255  'group' => 'foo',
256  ] ),
257  ] );
258  $links = $method->invokeArgs( $out, $args );
259  $actualHtml = implode( "\n", $links['html'] );
260  $this->assertEquals( $expectedHtml, $actualHtml );
261  }
262 
269  public function testVaryHeaders( $calls, $vary, $key ) {
270  // get rid of default Vary fields
271  $outputPage = $this->getMockBuilder( 'OutputPage' )
272  ->setConstructorArgs( [ new RequestContext() ] )
273  ->setMethods( [ 'getCacheVaryCookies' ] )
274  ->getMock();
275  $outputPage->expects( $this->any() )
276  ->method( 'getCacheVaryCookies' )
277  ->will( $this->returnValue( [] ) );
278  TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = [];
279 
280  foreach ( $calls as $call ) {
281  call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
282  }
283  $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
284  $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
285  }
286 
287  public function provideVaryHeaders() {
288  // note: getKeyHeader() automatically adds Vary: Cookie
289  return [
290  [ // single header
291  [
292  [ 'Cookie' ],
293  ],
294  'Vary: Cookie',
295  'Key: Cookie',
296  ],
297  [ // non-unique headers
298  [
299  [ 'Cookie' ],
300  [ 'Accept-Language' ],
301  [ 'Cookie' ],
302  ],
303  'Vary: Cookie, Accept-Language',
304  'Key: Cookie,Accept-Language',
305  ],
306  [ // two headers with single options
307  [
308  [ 'Cookie', [ 'param=phpsessid' ] ],
309  [ 'Accept-Language', [ 'substr=en' ] ],
310  ],
311  'Vary: Cookie, Accept-Language',
312  'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
313  ],
314  [ // one header with multiple options
315  [
316  [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
317  ],
318  'Vary: Cookie',
319  'Key: Cookie;param=phpsessid;param=userId',
320  ],
321  [ // Duplicate option
322  [
323  [ 'Cookie', [ 'param=phpsessid' ] ],
324  [ 'Cookie', [ 'param=phpsessid' ] ],
325  [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
326  ],
327  'Vary: Cookie, Accept-Language',
328  'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
329  ],
330  [ // Same header, different options
331  [
332  [ 'Cookie', [ 'param=phpsessid' ] ],
333  [ 'Cookie', [ 'param=userId' ] ],
334  ],
335  'Vary: Cookie',
336  'Key: Cookie;param=phpsessid;param=userId',
337  ],
338  ];
339  }
340 
345  $request = new FauxRequest();
346  $context = new RequestContext();
347  $context->setRequest( $request );
348  $outputPage = new OutputPage( $context );
349 
350  // No cookies are set.
351  $this->assertFalse( $outputPage->haveCacheVaryCookies() );
352 
353  // 'Token' is present but empty, so it shouldn't count.
354  $request->setCookie( 'Token', '' );
355  $this->assertFalse( $outputPage->haveCacheVaryCookies() );
356 
357  // 'Token' present and nonempty.
358  $request->setCookie( 'Token', '123' );
359  $this->assertTrue( $outputPage->haveCacheVaryCookies() );
360  }
361 }
362 
367  public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
368  return [];
369  }
370 
371  public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
372  return false;
373  }
374 
375  public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
376  }
377 
378  public function updateMessage( $key ) {
379  }
380 
381  public function clear() {
382  }
383 }
This class generates message blobs for use by ResourceLoader modules.
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:762
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
Group all the pieces relevant to the context of a request into one instance.
$context
Definition: load.php:44
assertTransformCssMediaCase($args)
Tests a particular case of transformCssMedia, using the given input, globals, expected return...
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
const SCREEN_ONLY_MEDIA_QUERY
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:2418
if(!isset($args[0])) $lang
testVaryHeaders($calls, $vary, $key)
provideVaryHeaders OutputPage::addVaryHeader OutputPage::getVaryHeader OutputPage::getKeyHeader ...
testPrintRequests()
Tests print requests OutputPage::transformCssMedia.
const SCREEN_MEDIA_QUERY
if($line===false) $args
Definition: cdb.php:64
static provideMakeResourceLoaderLink()
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
Definition: distributors.txt:9
This class should be covered by a general architecture document which does not exist as of January 20...
Definition: OutputPage.php:42
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
testHaveCacheVaryCookies()
OutputPage::haveCacheVaryCookies.
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2418
static transformCssMedia($media)
Transform "media" attribute based on request parameters.
testMakeResourceLoaderLink($args, $expectedHtml)
provideMakeResourceLoaderLink OutputPage::makeResourceLoaderLink ResourceLoader::makeLoaderImplementS...
testHandheld()
Tests handheld behavior OutputPage::transformCssMedia.
testScreenRequests()
Tests screen requests, without either query parameter set OutputPage::transformCssMedia.
MessageBlobStore that doesn't do anything.
updateModule($name, ResourceLoaderModule $module, $lang)
static newFromObject($object)
Return the same object, without access restrictions.
setMwGlobals($pairs, $value=null)
Dynamic JavaScript and CSS resource loading system.
insertMessageBlob($name, ResourceLoaderModule $module, $lang)
static getDefaultInstance()
Definition: SkinFactory.php:50
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310