Merge "Make setSubmitProgressive() Deprecate"
[lhc/web/wiklou.git] / tests / phpunit / includes / OutputPageTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 *
7 * @author Matthew Flaschen
8 *
9 * @group Database
10 * @group Output
11 *
12 * @todo factor tests in this class into providers and test methods
13 */
14 class OutputPageTest extends MediaWikiTestCase {
15 const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
16 const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
17
18 /**
19 * @covers OutputPage::addMeta
20 * @covers OutputPage::getMetaTags
21 * @covers OutputPage::getHeadLinksArray
22 */
23 public function testMetaTags() {
24 $outputPage = $this->newInstance();
25 $outputPage->addMeta( 'http:expires', '0' );
26 $outputPage->addMeta( 'keywords', 'first' );
27 $outputPage->addMeta( 'keywords', 'second' );
28 $outputPage->addMeta( 'og:title', 'Ta-duh' );
29
30 $expected = [
31 [ 'http:expires', '0' ],
32 [ 'keywords', 'first' ],
33 [ 'keywords', 'second' ],
34 [ 'og:title', 'Ta-duh' ],
35 ];
36 $this->assertSame( $expected, $outputPage->getMetaTags() );
37
38 $links = $outputPage->getHeadLinksArray();
39 $this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
40 $this->assertContains( '<meta name="keywords" content="first"/>', $links );
41 $this->assertContains( '<meta name="keywords" content="second"/>', $links );
42 $this->assertContains( '<meta property="og:title" content="Ta-duh"/>', $links );
43 $this->assertArrayNotHasKey( 'meta-robots', $links );
44 }
45
46 /**
47 * @covers OutputPage::setIndexPolicy
48 * @covers OutputPage::setFollowPolicy
49 * @covers OutputPage::getHeadLinksArray
50 */
51 public function testRobotsPolicies() {
52 $outputPage = $this->newInstance();
53 $outputPage->setIndexPolicy( 'noindex' );
54 $outputPage->setFollowPolicy( 'nofollow' );
55
56 $links = $outputPage->getHeadLinksArray();
57 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
58 }
59
60 /**
61 * Tests a particular case of transformCssMedia, using the given input, globals,
62 * expected return, and message
63 *
64 * Asserts that $expectedReturn is returned.
65 *
66 * options['printableQuery'] - value of query string for printable, or omitted for none
67 * options['handheldQuery'] - value of query string for handheld, or omitted for none
68 * options['media'] - passed into the method under the same name
69 * options['expectedReturn'] - expected return value
70 * options['message'] - PHPUnit message for assertion
71 *
72 * @param array $args Key-value array of arguments as shown above
73 */
74 protected function assertTransformCssMediaCase( $args ) {
75 $queryData = [];
76 if ( isset( $args['printableQuery'] ) ) {
77 $queryData['printable'] = $args['printableQuery'];
78 }
79
80 if ( isset( $args['handheldQuery'] ) ) {
81 $queryData['handheld'] = $args['handheldQuery'];
82 }
83
84 $fauxRequest = new FauxRequest( $queryData, false );
85 $this->setMwGlobals( [
86 'wgRequest' => $fauxRequest,
87 ] );
88
89 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
90 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
91 }
92
93 /**
94 * Tests print requests
95 * @covers OutputPage::transformCssMedia
96 */
97 public function testPrintRequests() {
98 $this->assertTransformCssMediaCase( [
99 'printableQuery' => '1',
100 'media' => 'screen',
101 'expectedReturn' => null,
102 'message' => 'On printable request, screen returns null'
103 ] );
104
105 $this->assertTransformCssMediaCase( [
106 'printableQuery' => '1',
107 'media' => self::SCREEN_MEDIA_QUERY,
108 'expectedReturn' => null,
109 'message' => 'On printable request, screen media query returns null'
110 ] );
111
112 $this->assertTransformCssMediaCase( [
113 'printableQuery' => '1',
114 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
115 'expectedReturn' => null,
116 'message' => 'On printable request, screen media query with only returns null'
117 ] );
118
119 $this->assertTransformCssMediaCase( [
120 'printableQuery' => '1',
121 'media' => 'print',
122 'expectedReturn' => '',
123 'message' => 'On printable request, media print returns empty string'
124 ] );
125 }
126
127 public static function provideCdnCacheEpoch() {
128 $base = [
129 'pageTime' => '2011-04-01T12:00:00+00:00',
130 'maxAge' => 24 * 3600,
131 ];
132 return [
133 'after 1s' => [ $base + [
134 'reqTime' => '2011-04-01T12:00:01+00:00',
135 'expect' => '2011-04-01T12:00:00+00:00',
136 ] ],
137 'after 23h' => [ $base + [
138 'reqTime' => '2011-04-02T11:00:00+00:00',
139 'expect' => '2011-04-01T12:00:00+00:00',
140 ] ],
141 'after 24h and a bit' => [ $base + [
142 'reqTime' => '2011-04-02T12:34:56+00:00',
143 'expect' => '2011-04-01T12:34:56+00:00',
144 ] ],
145 'after a year' => [ $base + [
146 'reqTime' => '2012-05-06T00:12:07+00:00',
147 'expect' => '2012-05-05T00:12:07+00:00',
148 ] ],
149 ];
150 }
151
152 /**
153 * @dataProvider provideCdnCacheEpoch
154 */
155 public function testCdnCacheEpoch( $params ) {
156 $out = TestingAccessWrapper::newFromObject( $this->newInstance() );
157 $reqTime = strtotime( $params['reqTime'] );
158 $pageTime = strtotime( $params['pageTime'] );
159 $actual = max( $pageTime, $out->getCdnCacheEpoch( $reqTime, $params['maxAge'] ) );
160
161 $this->assertEquals(
162 $params['expect'],
163 gmdate( DateTime::ATOM, $actual ),
164 'cdn epoch'
165 );
166 }
167
168 /**
169 * Tests screen requests, without either query parameter set
170 * @covers OutputPage::transformCssMedia
171 */
172 public function testScreenRequests() {
173 $this->assertTransformCssMediaCase( [
174 'media' => 'screen',
175 'expectedReturn' => 'screen',
176 'message' => 'On screen request, screen media type is preserved'
177 ] );
178
179 $this->assertTransformCssMediaCase( [
180 'media' => 'handheld',
181 'expectedReturn' => 'handheld',
182 'message' => 'On screen request, handheld media type is preserved'
183 ] );
184
185 $this->assertTransformCssMediaCase( [
186 'media' => self::SCREEN_MEDIA_QUERY,
187 'expectedReturn' => self::SCREEN_MEDIA_QUERY,
188 'message' => 'On screen request, screen media query is preserved.'
189 ] );
190
191 $this->assertTransformCssMediaCase( [
192 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
193 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
194 'message' => 'On screen request, screen media query with only is preserved.'
195 ] );
196
197 $this->assertTransformCssMediaCase( [
198 'media' => 'print',
199 'expectedReturn' => 'print',
200 'message' => 'On screen request, print media type is preserved'
201 ] );
202 }
203
204 /**
205 * Tests handheld behavior
206 * @covers OutputPage::transformCssMedia
207 */
208 public function testHandheld() {
209 $this->assertTransformCssMediaCase( [
210 'handheldQuery' => '1',
211 'media' => 'handheld',
212 'expectedReturn' => '',
213 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
214 ] );
215
216 $this->assertTransformCssMediaCase( [
217 'handheldQuery' => '1',
218 'media' => 'screen',
219 'expectedReturn' => null,
220 'message' => 'On request with handheld querystring and media is screen, returns null'
221 ] );
222 }
223
224 public static function provideTransformFilePath() {
225 $baseDir = dirname( __DIR__ ) . '/data/media';
226 return [
227 // File that matches basePath, and exists. Hash found and appended.
228 [
229 'baseDir' => $baseDir, 'basePath' => '/w',
230 '/w/test.jpg',
231 '/w/test.jpg?edcf2'
232 ],
233 // File that matches basePath, but not found on disk. Empty query.
234 [
235 'baseDir' => $baseDir, 'basePath' => '/w',
236 '/w/unknown.png',
237 '/w/unknown.png?'
238 ],
239 // File not matching basePath. Ignored.
240 [
241 'baseDir' => $baseDir, 'basePath' => '/w',
242 '/files/test.jpg'
243 ],
244 // Empty string. Ignored.
245 [
246 'baseDir' => $baseDir, 'basePath' => '/w',
247 '',
248 ''
249 ],
250 // Similar path, but with domain component. Ignored.
251 [
252 'baseDir' => $baseDir, 'basePath' => '/w',
253 '//example.org/w/test.jpg'
254 ],
255 [
256 'baseDir' => $baseDir, 'basePath' => '/w',
257 'https://example.org/w/test.jpg'
258 ],
259 // Unrelated path with domain component. Ignored.
260 [
261 'baseDir' => $baseDir, 'basePath' => '/w',
262 'https://example.org/files/test.jpg'
263 ],
264 [
265 'baseDir' => $baseDir, 'basePath' => '/w',
266 '//example.org/files/test.jpg'
267 ],
268 // Unrelated path with domain, and empty base path (root mw install). Ignored.
269 [
270 'baseDir' => $baseDir, 'basePath' => '',
271 'https://example.org/files/test.jpg'
272 ],
273 [
274 'baseDir' => $baseDir, 'basePath' => '',
275 // T155310
276 '//example.org/files/test.jpg'
277 ],
278 // Check UploadPath before ResourceBasePath (T155146)
279 [
280 'baseDir' => dirname( $baseDir ), 'basePath' => '',
281 'uploadDir' => $baseDir, 'uploadPath' => '/images',
282 '/images/test.jpg',
283 '/images/test.jpg?edcf2'
284 ],
285 ];
286 }
287
288 /**
289 * @dataProvider provideTransformFilePath
290 * @covers OutputPage::transformFilePath
291 * @covers OutputPage::transformResourcePath
292 */
293 public function testTransformResourcePath( $baseDir, $basePath, $uploadDir = null,
294 $uploadPath = null, $path = null, $expected = null
295 ) {
296 if ( $path === null ) {
297 // Skip optional $uploadDir and $uploadPath
298 $path = $uploadDir;
299 $expected = $uploadPath;
300 $uploadDir = "$baseDir/images";
301 $uploadPath = "$basePath/images";
302 }
303 $this->setMwGlobals( 'IP', $baseDir );
304 $conf = new HashConfig( [
305 'ResourceBasePath' => $basePath,
306 'UploadDirectory' => $uploadDir,
307 'UploadPath' => $uploadPath,
308 ] );
309
310 Wikimedia\suppressWarnings();
311 $actual = OutputPage::transformResourcePath( $conf, $path );
312 Wikimedia\restoreWarnings();
313
314 $this->assertEquals( $expected ?: $path, $actual );
315 }
316
317 public static function provideMakeResourceLoaderLink() {
318 // phpcs:disable Generic.Files.LineLength
319 return [
320 // Single only=scripts load
321 [
322 [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
323 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
324 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
325 . "});</script>"
326 ],
327 // Multiple only=styles load
328 [
329 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
330
331 '<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"/>'
332 ],
333 // Private embed (only=scripts)
334 [
335 [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
336 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
337 . "mw.test.baz({token:123});\nmw.loader.state({\"test.quux\":\"ready\"});"
338 . "});</script>"
339 ],
340 ];
341 // phpcs:enable
342 }
343
344 /**
345 * See ResourceLoaderClientHtmlTest for full coverage.
346 *
347 * @dataProvider provideMakeResourceLoaderLink
348 * @covers OutputPage::makeResourceLoaderLink
349 */
350 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
351 $this->setMwGlobals( [
352 'wgResourceLoaderDebug' => false,
353 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
354 ] );
355 $class = new ReflectionClass( OutputPage::class );
356 $method = $class->getMethod( 'makeResourceLoaderLink' );
357 $method->setAccessible( true );
358 $ctx = new RequestContext();
359 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
360 $ctx->setLanguage( 'en' );
361 $out = new OutputPage( $ctx );
362 $rl = $out->getResourceLoader();
363 $rl->setMessageBlobStore( new NullMessageBlobStore() );
364 $rl->register( [
365 'test.foo' => new ResourceLoaderTestModule( [
366 'script' => 'mw.test.foo( { a: true } );',
367 'styles' => '.mw-test-foo { content: "style"; }',
368 ] ),
369 'test.bar' => new ResourceLoaderTestModule( [
370 'script' => 'mw.test.bar( { a: true } );',
371 'styles' => '.mw-test-bar { content: "style"; }',
372 ] ),
373 'test.baz' => new ResourceLoaderTestModule( [
374 'script' => 'mw.test.baz( { a: true } );',
375 'styles' => '.mw-test-baz { content: "style"; }',
376 ] ),
377 'test.quux' => new ResourceLoaderTestModule( [
378 'script' => 'mw.test.baz( { token: 123 } );',
379 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
380 'group' => 'private',
381 ] ),
382 ] );
383 $links = $method->invokeArgs( $out, $args );
384 $actualHtml = strval( $links );
385 $this->assertEquals( $expectedHtml, $actualHtml );
386 }
387
388 public static function provideBuildExemptModules() {
389 // phpcs:disable Generic.Files.LineLength
390 return [
391 'empty' => [
392 'exemptStyleModules' => [],
393 '<meta name="ResourceLoaderDynamicStyles" content=""/>',
394 ],
395 'empty sets' => [
396 'exemptStyleModules' => [ 'site' => [], 'noscript' => [], 'private' => [], 'user' => [] ],
397 '<meta name="ResourceLoaderDynamicStyles" content=""/>',
398 ],
399 'default logged-out' => [
400 'exemptStyleModules' => [ 'site' => [ 'site.styles' ] ],
401 '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" .
402 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=fallback"/>',
403 ],
404 'default logged-in' => [
405 'exemptStyleModules' => [ 'site' => [ 'site.styles' ], 'user' => [ 'user.styles' ] ],
406 '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" .
407 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=fallback"/>' . "\n" .
408 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=user.styles&amp;only=styles&amp;skin=fallback&amp;version=1e9z0ox"/>',
409 ],
410 'custom modules' => [
411 'exemptStyleModules' => [
412 'site' => [ 'site.styles', 'example.site.a', 'example.site.b' ],
413 'user' => [ 'user.styles', 'example.user' ],
414 ],
415 '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" .
416 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=example.site.a%2Cb&amp;only=styles&amp;skin=fallback"/>' . "\n" .
417 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=fallback"/>' . "\n" .
418 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=example.user&amp;only=styles&amp;skin=fallback&amp;version=0a56zyi"/>' . "\n" .
419 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=en&amp;modules=user.styles&amp;only=styles&amp;skin=fallback&amp;version=1e9z0ox"/>',
420 ],
421 ];
422 // phpcs:enable
423 }
424
425 /**
426 * @dataProvider provideBuildExemptModules
427 * @covers OutputPage::buildExemptModules
428 */
429 public function testBuildExemptModules( array $exemptStyleModules, $expect ) {
430 $this->setMwGlobals( [
431 'wgResourceLoaderDebug' => false,
432 'wgLoadScript' => '/w/load.php',
433 // Stub wgCacheEpoch as it influences getVersionHash used for the
434 // urls in the expected HTML
435 'wgCacheEpoch' => '20140101000000',
436 ] );
437
438 // Set up stubs
439 $ctx = new RequestContext();
440 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
441 $ctx->setLanguage( 'en' );
442 $outputPage = $this->getMockBuilder( OutputPage::class )
443 ->setConstructorArgs( [ $ctx ] )
444 ->setMethods( [ 'buildCssLinksArray' ] )
445 ->getMock();
446 $outputPage->expects( $this->any() )
447 ->method( 'buildCssLinksArray' )
448 ->willReturn( [] );
449 $rl = $outputPage->getResourceLoader();
450 $rl->setMessageBlobStore( new NullMessageBlobStore() );
451
452 // Register custom modules
453 $rl->register( [
454 'example.site.a' => new ResourceLoaderTestModule( [ 'group' => 'site' ] ),
455 'example.site.b' => new ResourceLoaderTestModule( [ 'group' => 'site' ] ),
456 'example.user' => new ResourceLoaderTestModule( [ 'group' => 'user' ] ),
457 ] );
458
459 $outputPage = TestingAccessWrapper::newFromObject( $outputPage );
460 $outputPage->rlExemptStyleModules = $exemptStyleModules;
461 $this->assertEquals(
462 $expect,
463 strval( $outputPage->buildExemptModules() )
464 );
465 }
466
467 /**
468 * @dataProvider provideVaryHeaders
469 * @covers OutputPage::addVaryHeader
470 * @covers OutputPage::getVaryHeader
471 * @covers OutputPage::getKeyHeader
472 */
473 public function testVaryHeaders( $calls, $vary, $key ) {
474 // get rid of default Vary fields
475 $outputPage = $this->getMockBuilder( OutputPage::class )
476 ->setConstructorArgs( [ new RequestContext() ] )
477 ->setMethods( [ 'getCacheVaryCookies' ] )
478 ->getMock();
479 $outputPage->expects( $this->any() )
480 ->method( 'getCacheVaryCookies' )
481 ->will( $this->returnValue( [] ) );
482 TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = [];
483
484 foreach ( $calls as $call ) {
485 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
486 }
487 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
488 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
489 }
490
491 public function provideVaryHeaders() {
492 // note: getKeyHeader() automatically adds Vary: Cookie
493 return [
494 [ // single header
495 [
496 [ 'Cookie' ],
497 ],
498 'Vary: Cookie',
499 'Key: Cookie',
500 ],
501 [ // non-unique headers
502 [
503 [ 'Cookie' ],
504 [ 'Accept-Language' ],
505 [ 'Cookie' ],
506 ],
507 'Vary: Cookie, Accept-Language',
508 'Key: Cookie,Accept-Language',
509 ],
510 [ // two headers with single options
511 [
512 [ 'Cookie', [ 'param=phpsessid' ] ],
513 [ 'Accept-Language', [ 'substr=en' ] ],
514 ],
515 'Vary: Cookie, Accept-Language',
516 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
517 ],
518 [ // one header with multiple options
519 [
520 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
521 ],
522 'Vary: Cookie',
523 'Key: Cookie;param=phpsessid;param=userId',
524 ],
525 [ // Duplicate option
526 [
527 [ 'Cookie', [ 'param=phpsessid' ] ],
528 [ 'Cookie', [ 'param=phpsessid' ] ],
529 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
530 ],
531 'Vary: Cookie, Accept-Language',
532 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
533 ],
534 [ // Same header, different options
535 [
536 [ 'Cookie', [ 'param=phpsessid' ] ],
537 [ 'Cookie', [ 'param=userId' ] ],
538 ],
539 'Vary: Cookie',
540 'Key: Cookie;param=phpsessid;param=userId',
541 ],
542 ];
543 }
544
545 /**
546 * @covers OutputPage::haveCacheVaryCookies
547 */
548 public function testHaveCacheVaryCookies() {
549 $request = new FauxRequest();
550 $context = new RequestContext();
551 $context->setRequest( $request );
552 $outputPage = new OutputPage( $context );
553
554 // No cookies are set.
555 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
556
557 // 'Token' is present but empty, so it shouldn't count.
558 $request->setCookie( 'Token', '' );
559 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
560
561 // 'Token' present and nonempty.
562 $request->setCookie( 'Token', '123' );
563 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
564 }
565
566 /**
567 * @covers OutputPage::addCategoryLinks
568 * @covers OutputPage::getCategories
569 */
570 public function testGetCategories() {
571 $fakeResultWrapper = new FakeResultWrapper( [
572 (object)[
573 'pp_value' => 1,
574 'page_title' => 'Test'
575 ],
576 (object)[
577 'page_title' => 'Test2'
578 ]
579 ] );
580 $outputPage = $this->getMockBuilder( OutputPage::class )
581 ->setConstructorArgs( [ new RequestContext() ] )
582 ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
583 ->getMock();
584 $outputPage->expects( $this->any() )
585 ->method( 'addCategoryLinksToLBAndGetResult' )
586 ->will( $this->returnValue( $fakeResultWrapper ) );
587
588 $outputPage->addCategoryLinks( [
589 'Test' => 'Test',
590 'Test2' => 'Test2',
591 ] );
592 $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
593 $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
594 $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
595 }
596
597 /**
598 * @dataProvider provideLinkHeaders
599 * @covers OutputPage::addLinkHeader
600 * @covers OutputPage::getLinkHeader
601 */
602 public function testLinkHeaders( $headers, $result ) {
603 $outputPage = $this->newInstance();
604
605 foreach ( $headers as $header ) {
606 $outputPage->addLinkHeader( $header );
607 }
608
609 $this->assertEquals( $result, $outputPage->getLinkHeader() );
610 }
611
612 public function provideLinkHeaders() {
613 return [
614 [
615 [],
616 false
617 ],
618 [
619 [ '<https://foo/bar.jpg>;rel=preload;as=image' ],
620 'Link: <https://foo/bar.jpg>;rel=preload;as=image',
621 ],
622 [
623 [ '<https://foo/bar.jpg>;rel=preload;as=image','<https://foo/baz.jpg>;rel=preload;as=image' ],
624 'Link: <https://foo/bar.jpg>;rel=preload;as=image,<https://foo/baz.jpg>;rel=preload;as=image',
625 ],
626 ];
627 }
628
629 /**
630 * @dataProvider providePreloadLinkHeaders
631 * @covers OutputPage::addLogoPreloadLinkHeaders
632 * @covers ResourceLoaderSkinModule::getLogo
633 */
634 public function testPreloadLinkHeaders( $config, $result, $baseDir = null ) {
635 if ( $baseDir ) {
636 $this->setMwGlobals( 'IP', $baseDir );
637 }
638 $out = TestingAccessWrapper::newFromObject( $this->newInstance( $config ) );
639 $out->addLogoPreloadLinkHeaders();
640
641 $this->assertEquals( $result, $out->getLinkHeader() );
642 }
643
644 public function providePreloadLinkHeaders() {
645 return [
646 [
647 [
648 'ResourceBasePath' => '/w',
649 'Logo' => '/img/default.png',
650 'LogoHD' => [
651 '1.5x' => '/img/one-point-five.png',
652 '2x' => '/img/two-x.png',
653 ],
654 ],
655 'Link: </img/default.png>;rel=preload;as=image;media=' .
656 'not all and (min-resolution: 1.5dppx),' .
657 '</img/one-point-five.png>;rel=preload;as=image;media=' .
658 '(min-resolution: 1.5dppx) and (max-resolution: 1.999999dppx),' .
659 '</img/two-x.png>;rel=preload;as=image;media=(min-resolution: 2dppx)'
660 ],
661 [
662 [
663 'ResourceBasePath' => '/w',
664 'Logo' => '/img/default.png',
665 'LogoHD' => false,
666 ],
667 'Link: </img/default.png>;rel=preload;as=image'
668 ],
669 [
670 [
671 'ResourceBasePath' => '/w',
672 'Logo' => '/img/default.png',
673 'LogoHD' => [
674 '2x' => '/img/two-x.png',
675 ],
676 ],
677 'Link: </img/default.png>;rel=preload;as=image;media=' .
678 'not all and (min-resolution: 2dppx),' .
679 '</img/two-x.png>;rel=preload;as=image;media=(min-resolution: 2dppx)'
680 ],
681 [
682 [
683 'ResourceBasePath' => '/w',
684 'Logo' => '/img/default.png',
685 'LogoHD' => [
686 'svg' => '/img/vector.svg',
687 ],
688 ],
689 'Link: </img/vector.svg>;rel=preload;as=image'
690
691 ],
692 [
693 [
694 'ResourceBasePath' => '/w',
695 'Logo' => '/w/test.jpg',
696 'LogoHD' => false,
697 'UploadPath' => '/w/images',
698 ],
699 'Link: </w/test.jpg?edcf2>;rel=preload;as=image',
700 'baseDir' => dirname( __DIR__ ) . '/data/media',
701 ],
702 ];
703 }
704
705 /**
706 * @return OutputPage
707 */
708 private function newInstance( $config = [] ) {
709 $context = new RequestContext();
710
711 $context->setConfig( new HashConfig( $config + [
712 'AppleTouchIcon' => false,
713 'DisableLangConversion' => true,
714 'EnableCanonicalServerLink' => false,
715 'Favicon' => false,
716 'Feed' => false,
717 'LanguageCode' => false,
718 'ReferrerPolicy' => false,
719 'RightsPage' => false,
720 'RightsUrl' => false,
721 'UniversalEditButton' => false,
722 ] ) );
723
724 return new OutputPage( $context );
725 }
726 }
727
728 /**
729 * MessageBlobStore that doesn't do anything
730 */
731 class NullMessageBlobStore extends MessageBlobStore {
732 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
733 return [];
734 }
735
736 public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
737 }
738
739 public function updateMessage( $key ) {
740 }
741
742 public function clear() {
743 }
744 }