OutputPage::addScriptFile: Drop silencing invalid paths, deprecated in 1.24
[lhc/web/wiklou.git] / tests / phpunit / includes / OutputPageTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @author Matthew Flaschen
8 *
9 * @group Database
10 * @group Output
11 */
12 class OutputPageTest extends MediaWikiTestCase {
13 const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
14 const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
15
16 // @codingStandardsIgnoreStart Generic.Files.LineLength
17 const RSS_RC_LINK = '<link rel="alternate" type="application/rss+xml" title=" RSS feed" href="/w/index.php?title=Special:RecentChanges&amp;feed=rss"/>';
18 const ATOM_RC_LINK = '<link rel="alternate" type="application/atom+xml" title=" Atom feed" href="/w/index.php?title=Special:RecentChanges&amp;feed=atom"/>';
19
20 const RSS_TEST_LINK = '<link rel="alternate" type="application/rss+xml" title="&quot;Test&quot; RSS feed" href="fake-link"/>';
21 const ATOM_TEST_LINK = '<link rel="alternate" type="application/atom+xml" title="&quot;Test&quot; Atom feed" href="fake-link"/>';
22 // @codingStandardsIgnoreEnd
23
24 // Ensure that we don't affect the global ResourceLoader state.
25 protected function setUp() {
26 parent::setUp();
27 ResourceLoader::clearCache();
28 }
29
30 protected function tearDown() {
31 parent::tearDown();
32 ResourceLoader::clearCache();
33 }
34
35 /**
36 * @dataProvider provideRedirect
37 *
38 * @covers OutputPage::__construct
39 * @covers OutputPage::redirect
40 * @covers OutputPage::getRedirect
41 */
42 public function testRedirect( $url, $code = null ) {
43 $op = $this->newInstance();
44 if ( isset( $code ) ) {
45 $op->redirect( $url, $code );
46 } else {
47 $op->redirect( $url );
48 }
49 $expectedUrl = str_replace( "\n", '', $url );
50 $this->assertSame( $expectedUrl, $op->getRedirect() );
51 $this->assertSame( $expectedUrl, $op->mRedirect );
52 $this->assertSame( $code ?? '302', $op->mRedirectCode );
53 }
54
55 public function provideRedirect() {
56 return [
57 [ 'http://example.com' ],
58 [ 'http://example.com', '400' ],
59 [ 'http://example.com', 'squirrels!!!' ],
60 [ "a\nb" ],
61 ];
62 }
63
64 private function setupFeedLinks( $feed, $types ) {
65 $outputPage = $this->newInstance( [
66 'AdvertisedFeedTypes' => $types,
67 'Feed' => $feed,
68 'OverrideSiteFeed' => false,
69 'Script' => '/w',
70 'Sitename' => false,
71 ] );
72 $outputPage->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) );
73 $this->setMwGlobals( [
74 'wgScript' => '/w/index.php',
75 ] );
76 return $outputPage;
77 }
78
79 private function assertFeedLinks( $outputPage, $message, $present, $non_present ) {
80 $links = $outputPage->getHeadLinksArray();
81 foreach ( $present as $link ) {
82 $this->assertContains( $link, $links, $message );
83 }
84 foreach ( $non_present as $link ) {
85 $this->assertNotContains( $link, $links, $message );
86 }
87 }
88
89 private function assertFeedUILinks( $outputPage, $ui_links ) {
90 if ( $ui_links ) {
91 $this->assertTrue( $outputPage->isSyndicated(), 'Syndication should be offered' );
92 $this->assertGreaterThan( 0, count( $outputPage->getSyndicationLinks() ),
93 'Some syndication links should be there' );
94 } else {
95 $this->assertFalse( $outputPage->isSyndicated(), 'No syndication should be offered' );
96 $this->assertEquals( 0, count( $outputPage->getSyndicationLinks() ),
97 'No syndication links should be there' );
98 }
99 }
100
101 public static function provideFeedLinkData() {
102 return [
103 [
104 true, [ 'rss' ], 'Only RSS RC link should be offerred',
105 [ self::RSS_RC_LINK ], [ self::ATOM_RC_LINK ]
106 ],
107 [
108 true, [ 'atom' ], 'Only Atom RC link should be offerred',
109 [ self::ATOM_RC_LINK ], [ self::RSS_RC_LINK ]
110 ],
111 [
112 true, [], 'No RC feed formats should be offerred',
113 [], [ self::ATOM_RC_LINK, self::RSS_RC_LINK ]
114 ],
115 [
116 false, [ 'atom' ], 'No RC feeds should be offerred',
117 [], [ self::ATOM_RC_LINK, self::RSS_RC_LINK ]
118 ],
119 ];
120 }
121
122 /**
123 * @covers OutputPage::setCopyrightUrl
124 * @covers OutputPage::getHeadLinksArray
125 */
126 public function testSetCopyrightUrl() {
127 $op = $this->newInstance();
128 $op->setCopyrightUrl( 'http://example.com' );
129
130 $this->assertSame(
131 Html::element( 'link', [ 'rel' => 'license', 'href' => 'http://example.com' ] ),
132 $op->getHeadLinksArray()['copyright']
133 );
134 }
135
136 /**
137 * @dataProvider provideFeedLinkData
138 * @covers OutputPage::getHeadLinksArray
139 */
140 public function testRecentChangesFeed( $feed, $advertised_feed_types,
141 $message, $present, $non_present ) {
142 $outputPage = $this->setupFeedLinks( $feed, $advertised_feed_types );
143 $this->assertFeedLinks( $outputPage, $message, $present, $non_present );
144 }
145
146 public static function provideAdditionalFeedData() {
147 return [
148 [
149 true, [ 'atom' ], 'Additional Atom feed should be offered',
150 'atom',
151 [ self::ATOM_TEST_LINK, self::ATOM_RC_LINK ],
152 [ self::RSS_TEST_LINK, self::RSS_RC_LINK ],
153 true,
154 ],
155 [
156 true, [ 'rss' ], 'Additional RSS feed should be offered',
157 'rss',
158 [ self::RSS_TEST_LINK, self::RSS_RC_LINK ],
159 [ self::ATOM_TEST_LINK, self::ATOM_RC_LINK ],
160 true,
161 ],
162 [
163 true, [ 'rss' ], 'Additional Atom feed should NOT be offered with RSS enabled',
164 'atom',
165 [ self::RSS_RC_LINK ],
166 [ self::RSS_TEST_LINK, self::ATOM_TEST_LINK, self::ATOM_RC_LINK ],
167 false,
168 ],
169 [
170 false, [ 'atom' ], 'Additional Atom feed should NOT be offered, all feeds disabled',
171 'atom',
172 [],
173 [
174 self::RSS_TEST_LINK, self::ATOM_TEST_LINK,
175 self::ATOM_RC_LINK, self::ATOM_RC_LINK,
176 ],
177 false,
178 ],
179 ];
180 }
181
182 /**
183 * @dataProvider provideAdditionalFeedData
184 * @covers OutputPage::getHeadLinksArray
185 * @covers OutputPage::addFeedLink
186 * @covers OutputPage::getSyndicationLinks
187 * @covers OutputPage::isSyndicated
188 */
189 public function testAdditionalFeeds( $feed, $advertised_feed_types, $message,
190 $additional_feed_type, $present, $non_present, $any_ui_links ) {
191 $outputPage = $this->setupFeedLinks( $feed, $advertised_feed_types );
192 $outputPage->addFeedLink( $additional_feed_type, 'fake-link' );
193 $this->assertFeedLinks( $outputPage, $message, $present, $non_present );
194 $this->assertFeedUILinks( $outputPage, $any_ui_links );
195 }
196
197 // @todo How to test setStatusCode?
198
199 /**
200 * @covers OutputPage::addMeta
201 * @covers OutputPage::getMetaTags
202 * @covers OutputPage::getHeadLinksArray
203 */
204 public function testMetaTags() {
205 $op = $this->newInstance();
206 $op->addMeta( 'http:expires', '0' );
207 $op->addMeta( 'keywords', 'first' );
208 $op->addMeta( 'keywords', 'second' );
209 $op->addMeta( 'og:title', 'Ta-duh' );
210
211 $expected = [
212 [ 'http:expires', '0' ],
213 [ 'keywords', 'first' ],
214 [ 'keywords', 'second' ],
215 [ 'og:title', 'Ta-duh' ],
216 ];
217 $this->assertSame( $expected, $op->getMetaTags() );
218
219 $links = $op->getHeadLinksArray();
220 $this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
221 $this->assertContains( '<meta name="keywords" content="first"/>', $links );
222 $this->assertContains( '<meta name="keywords" content="second"/>', $links );
223 $this->assertContains( '<meta property="og:title" content="Ta-duh"/>', $links );
224 $this->assertArrayNotHasKey( 'meta-robots', $links );
225 }
226
227 /**
228 * @covers OutputPage::addLink
229 * @covers OutputPage::getLinkTags
230 * @covers OutputPage::getHeadLinksArray
231 */
232 public function testAddLink() {
233 $op = $this->newInstance();
234
235 $links = [
236 [],
237 [ 'rel' => 'foo', 'href' => 'http://example.com' ],
238 ];
239
240 foreach ( $links as $link ) {
241 $op->addLink( $link );
242 }
243
244 $this->assertSame( $links, $op->getLinkTags() );
245
246 $result = $op->getHeadLinksArray();
247
248 foreach ( $links as $link ) {
249 $this->assertContains( Html::element( 'link', $link ), $result );
250 }
251 }
252
253 /**
254 * @covers OutputPage::setCanonicalUrl
255 * @covers OutputPage::getCanonicalUrl
256 * @covers OutputPage::getHeadLinksArray
257 */
258 public function testSetCanonicalUrl() {
259 $op = $this->newInstance();
260 $op->setCanonicalUrl( 'http://example.comm' );
261 $op->setCanonicalUrl( 'http://example.com' );
262
263 $this->assertSame( 'http://example.com', $op->getCanonicalUrl() );
264
265 $headLinks = $op->getHeadLinksArray();
266
267 $this->assertContains( Html::element( 'link', [
268 'rel' => 'canonical', 'href' => 'http://example.com'
269 ] ), $headLinks );
270
271 $this->assertNotContains( Html::element( 'link', [
272 'rel' => 'canonical', 'href' => 'http://example.comm'
273 ] ), $headLinks );
274 }
275
276 /**
277 * @covers OutputPage::addScript
278 */
279 public function testAddScript() {
280 $op = $this->newInstance();
281 $op->addScript( 'some random string' );
282
283 $this->assertContains( "\nsome random string\n", "\n" . $op->getBottomScripts() . "\n" );
284 }
285
286 /**
287 * @covers OutputPage::addScriptFile
288 */
289 public function testAddScriptFile() {
290 $op = $this->newInstance();
291 $op->addScriptFile( '/somescript.js' );
292 $op->addScriptFile( '//example.com/somescript.js' );
293
294 $this->assertContains(
295 "\n" . Html::linkedScript( '/somescript.js', $op->getCSPNonce() ) .
296 Html::linkedScript( '//example.com/somescript.js', $op->getCSPNonce() ) . "\n",
297 "\n" . $op->getBottomScripts() . "\n"
298 );
299 }
300
301 /**
302 * @covers OutputPage::addInlineScript
303 */
304 public function testAddInlineScript() {
305 $op = $this->newInstance();
306 $op->addInlineScript( 'let foo = "bar";' );
307 $op->addInlineScript( 'alert( foo );' );
308
309 $this->assertContains(
310 "\n" . Html::inlineScript( "\nlet foo = \"bar\";\n", $op->getCSPNonce() ) . "\n" .
311 Html::inlineScript( "\nalert( foo );\n", $op->getCSPNonce() ) . "\n",
312 "\n" . $op->getBottomScripts() . "\n"
313 );
314 }
315
316 // @todo How to test filterModules(), warnModuleTargetFilter(), getModules(), etc.?
317
318 /**
319 * @covers OutputPage::getTarget
320 * @covers OutputPage::setTarget
321 */
322 public function testSetTarget() {
323 $op = $this->newInstance();
324 $op->setTarget( 'foo' );
325
326 $this->assertSame( 'foo', $op->getTarget() );
327 // @todo What else? Test some actual effect?
328 }
329
330 // @todo How to test addContentOverride(Callback)?
331
332 /**
333 * @covers OutputPage::getHeadItemsArray
334 * @covers OutputPage::addHeadItem
335 * @covers OutputPage::addHeadItems
336 * @covers OutputPage::hasHeadItem
337 */
338 public function testHeadItems() {
339 $op = $this->newInstance();
340 $op->addHeadItem( 'a', 'b' );
341 $op->addHeadItems( [ 'c' => '<d>&amp;', 'e' => 'f', 'a' => 'q' ] );
342 $op->addHeadItem( 'e', 'g' );
343 $op->addHeadItems( 'x' );
344
345 $this->assertSame( [ 'a' => 'q', 'c' => '<d>&amp;', 'e' => 'g', 'x' ],
346 $op->getHeadItemsArray() );
347
348 $this->assertTrue( $op->hasHeadItem( 'a' ) );
349 $this->assertTrue( $op->hasHeadItem( 'c' ) );
350 $this->assertTrue( $op->hasHeadItem( 'e' ) );
351 $this->assertTrue( $op->hasHeadItem( '0' ) );
352
353 $this->assertContains( "\nq\n<d>&amp;\ng\nx\n",
354 '' . $op->headElement( $op->getContext()->getSkin() ) );
355 }
356
357 /**
358 * @covers OutputPage::getHeadItemsArray
359 * @covers OutputPage::addParserOutputMetadata
360 * @covers OutputPage::addParserOutput
361 */
362 public function testHeadItemsParserOutput() {
363 $op = $this->newInstance();
364 $stubPO1 = $this->createParserOutputStub( 'getHeadItems', [ 'a' => 'b' ] );
365 $op->addParserOutputMetadata( $stubPO1 );
366 $stubPO2 = $this->createParserOutputStub( 'getHeadItems',
367 [ 'c' => '<d>&amp;', 'e' => 'f', 'a' => 'q' ] );
368 $op->addParserOutputMetadata( $stubPO2 );
369 $stubPO3 = $this->createParserOutputStub( 'getHeadItems', [ 'e' => 'g' ] );
370 $op->addParserOutput( $stubPO3 );
371 $stubPO4 = $this->createParserOutputStub( 'getHeadItems', [ 'x' ] );
372 $op->addParserOutputMetadata( $stubPO4 );
373
374 $this->assertSame( [ 'a' => 'q', 'c' => '<d>&amp;', 'e' => 'g', 'x' ],
375 $op->getHeadItemsArray() );
376
377 $this->assertTrue( $op->hasHeadItem( 'a' ) );
378 $this->assertTrue( $op->hasHeadItem( 'c' ) );
379 $this->assertTrue( $op->hasHeadItem( 'e' ) );
380 $this->assertTrue( $op->hasHeadItem( '0' ) );
381 $this->assertFalse( $op->hasHeadItem( 'b' ) );
382
383 $this->assertContains( "\nq\n<d>&amp;\ng\nx\n",
384 '' . $op->headElement( $op->getContext()->getSkin() ) );
385 }
386
387 /**
388 * @covers OutputPage::addBodyClasses
389 */
390 public function testAddBodyClasses() {
391 $op = $this->newInstance();
392 $op->addBodyClasses( 'a' );
393 $op->addBodyClasses( 'mediawiki' );
394 $op->addBodyClasses( 'b c' );
395 $op->addBodyClasses( [ 'd', 'e' ] );
396 $op->addBodyClasses( 'a' );
397
398 $this->assertContains( '"a mediawiki b c d e ltr',
399 '' . $op->headElement( $op->getContext()->getSkin() ) );
400 }
401
402 /**
403 * @covers OutputPage::setArticleBodyOnly
404 * @covers OutputPage::getArticleBodyOnly
405 */
406 public function testArticleBodyOnly() {
407 $op = $this->newInstance();
408 $this->assertFalse( $op->getArticleBodyOnly() );
409
410 $op->setArticleBodyOnly( true );
411 $this->assertTrue( $op->getArticleBodyOnly() );
412
413 $op->addHTML( '<b>a</b>' );
414
415 $this->assertSame( '<b>a</b>', $op->output( true ) );
416 }
417
418 /**
419 * @covers OutputPage::setProperty
420 * @covers OutputPage::getProperty
421 */
422 public function testProperties() {
423 $op = $this->newInstance();
424
425 $this->assertNull( $op->getProperty( 'foo' ) );
426
427 $op->setProperty( 'foo', 'bar' );
428 $op->setProperty( 'baz', 'quz' );
429
430 $this->assertSame( 'bar', $op->getProperty( 'foo' ) );
431 $this->assertSame( 'quz', $op->getProperty( 'baz' ) );
432 }
433
434 /**
435 * @dataProvider provideCheckLastModified
436 *
437 * @covers OutputPage::checkLastModified
438 * @covers OutputPage::getCdnCacheEpoch
439 */
440 public function testCheckLastModified(
441 $timestamp, $ifModifiedSince, $expected, $config = [], $callback = null
442 ) {
443 $request = new FauxRequest();
444 if ( $ifModifiedSince ) {
445 if ( is_numeric( $ifModifiedSince ) ) {
446 // Unix timestamp
447 $ifModifiedSince = date( 'D, d M Y H:i:s', $ifModifiedSince ) . ' GMT';
448 }
449 $request->setHeader( 'If-Modified-Since', $ifModifiedSince );
450 }
451
452 if ( !isset( $config['CacheEpoch'] ) ) {
453 // Make sure it's not too recent
454 $config['CacheEpoch'] = '20000101000000';
455 }
456
457 $op = $this->newInstance( $config, $request );
458
459 if ( $callback ) {
460 $callback( $op, $this );
461 }
462
463 // Avoid a complaint about not being able to disable compression
464 Wikimedia\suppressWarnings();
465 try {
466 $this->assertEquals( $expected, $op->checkLastModified( $timestamp ) );
467 } finally {
468 Wikimedia\restoreWarnings();
469 }
470 }
471
472 public function provideCheckLastModified() {
473 $lastModified = time() - 3600;
474 return [
475 'Timestamp 0' =>
476 [ '0', $lastModified, false ],
477 'Timestamp Unix epoch' =>
478 [ '19700101000000', $lastModified, false ],
479 'Timestamp same as If-Modified-Since' =>
480 [ $lastModified, $lastModified, true ],
481 'Timestamp one second after If-Modified-Since' =>
482 [ $lastModified + 1, $lastModified, false ],
483 'No If-Modified-Since' =>
484 [ $lastModified + 1, null, false ],
485 'Malformed If-Modified-Since' =>
486 [ $lastModified + 1, 'GIBBERING WOMBATS !!!', false ],
487 'Non-standard IE-style If-Modified-Since' =>
488 [ $lastModified, date( 'D, d M Y H:i:s', $lastModified ) . ' GMT; length=5202',
489 true ],
490 // @todo Should we fix this behavior to match the spec? Probably no reason to.
491 'If-Modified-Since not per spec but we accept it anyway because strtotime does' =>
492 [ $lastModified, "@$lastModified", true ],
493 '$wgCachePages = false' =>
494 [ $lastModified, $lastModified, false, [ 'CachePages' => false ] ],
495 '$wgCacheEpoch' =>
496 [ $lastModified, $lastModified, false,
497 [ 'CacheEpoch' => wfTimestamp( TS_MW, $lastModified + 1 ) ] ],
498 'Recently-touched user' =>
499 [ $lastModified, $lastModified, false, [],
500 function ( $op ) {
501 $op->getContext()->setUser( $this->getTestUser()->getUser() );
502 } ],
503 'After CDN expiry' =>
504 [ $lastModified, $lastModified, false,
505 [ 'UseCdn' => true, 'CdnMaxAge' => 3599 ] ],
506 'Hook allows cache use' =>
507 [ $lastModified + 1, $lastModified, true, [],
508 function ( $op, $that ) {
509 $that->setTemporaryHook( 'OutputPageCheckLastModified',
510 function ( &$modifiedTimes ) {
511 $modifiedTimes = [ 1 ];
512 }
513 );
514 } ],
515 'Hooks prohibits cache use' =>
516 [ $lastModified, $lastModified, false, [],
517 function ( $op, $that ) {
518 $that->setTemporaryHook( 'OutputPageCheckLastModified',
519 function ( &$modifiedTimes ) {
520 $modifiedTimes = [ max( $modifiedTimes ) + 1 ];
521 }
522 );
523 } ],
524 ];
525 }
526
527 /**
528 * @dataProvider provideCdnCacheEpoch
529 *
530 * @covers OutputPage::getCdnCacheEpoch
531 */
532 public function testCdnCacheEpoch( $params ) {
533 $out = TestingAccessWrapper::newFromObject( $this->newInstance() );
534 $reqTime = strtotime( $params['reqTime'] );
535 $pageTime = strtotime( $params['pageTime'] );
536 $actual = max( $pageTime, $out->getCdnCacheEpoch( $reqTime, $params['maxAge'] ) );
537
538 $this->assertEquals(
539 $params['expect'],
540 gmdate( DateTime::ATOM, $actual ),
541 'cdn epoch'
542 );
543 }
544
545 public static function provideCdnCacheEpoch() {
546 $base = [
547 'pageTime' => '2011-04-01T12:00:00+00:00',
548 'maxAge' => 24 * 3600,
549 ];
550 return [
551 'after 1s' => [ $base + [
552 'reqTime' => '2011-04-01T12:00:01+00:00',
553 'expect' => '2011-04-01T12:00:00+00:00',
554 ] ],
555 'after 23h' => [ $base + [
556 'reqTime' => '2011-04-02T11:00:00+00:00',
557 'expect' => '2011-04-01T12:00:00+00:00',
558 ] ],
559 'after 24h and a bit' => [ $base + [
560 'reqTime' => '2011-04-02T12:34:56+00:00',
561 'expect' => '2011-04-01T12:34:56+00:00',
562 ] ],
563 'after a year' => [ $base + [
564 'reqTime' => '2012-05-06T00:12:07+00:00',
565 'expect' => '2012-05-05T00:12:07+00:00',
566 ] ],
567 ];
568 }
569
570 // @todo How to test setLastModified?
571
572 /**
573 * @covers OutputPage::setRobotPolicy
574 * @covers OutputPage::getHeadLinksArray
575 */
576 public function testSetRobotPolicy() {
577 $op = $this->newInstance();
578 $op->setRobotPolicy( 'noindex, nofollow' );
579
580 $links = $op->getHeadLinksArray();
581 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
582 }
583
584 /**
585 * @covers OutputPage::setIndexPolicy
586 * @covers OutputPage::setFollowPolicy
587 * @covers OutputPage::getHeadLinksArray
588 */
589 public function testSetIndexFollowPolicies() {
590 $op = $this->newInstance();
591 $op->setIndexPolicy( 'noindex' );
592 $op->setFollowPolicy( 'nofollow' );
593
594 $links = $op->getHeadLinksArray();
595 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
596 }
597
598 private function extractHTMLTitle( OutputPage $op ) {
599 $html = $op->headElement( $op->getContext()->getSkin() );
600
601 // OutputPage should always output the title in a nice format such that regexes will work
602 // fine. If it doesn't, we'll fail the tests.
603 preg_match_all( '!<title>(.*?)</title>!', $html, $matches );
604
605 $this->assertLessThanOrEqual( 1, count( $matches[1] ), 'More than one <title>!' );
606
607 if ( !count( $matches[1] ) ) {
608 return null;
609 }
610
611 return $matches[1][0];
612 }
613
614 /**
615 * Shorthand for getting the text of a message, in content language.
616 */
617 private static function getMsgText( $op, ...$msgParams ) {
618 return $op->msg( ...$msgParams )->inContentLanguage()->text();
619 }
620
621 /**
622 * @covers OutputPage::setHTMLTitle
623 * @covers OutputPage::getHTMLTitle
624 */
625 public function testHTMLTitle() {
626 $op = $this->newInstance();
627
628 // Default
629 $this->assertSame( '', $op->getHTMLTitle() );
630 $this->assertSame( '', $op->getPageTitle() );
631 $this->assertSame(
632 $this->getMsgText( $op, 'pagetitle', '' ),
633 $this->extractHTMLTitle( $op )
634 );
635
636 // Set to string
637 $op->setHTMLTitle( 'Potatoes will eat me' );
638
639 $this->assertSame( 'Potatoes will eat me', $op->getHTMLTitle() );
640 $this->assertSame( 'Potatoes will eat me', $this->extractHTMLTitle( $op ) );
641 // Shouldn't have changed the page title
642 $this->assertSame( '', $op->getPageTitle() );
643
644 // Set to message
645 $msg = $op->msg( 'mainpage' );
646
647 $op->setHTMLTitle( $msg );
648 $this->assertSame( $msg->text(), $op->getHTMLTitle() );
649 $this->assertSame( $msg->text(), $this->extractHTMLTitle( $op ) );
650 $this->assertSame( '', $op->getPageTitle() );
651 }
652
653 /**
654 * @covers OutputPage::setRedirectedFrom
655 */
656 public function testSetRedirectedFrom() {
657 $op = $this->newInstance();
658
659 $op->setRedirectedFrom( Title::newFromText( 'Talk:Some page' ) );
660 $this->assertSame( 'Talk:Some_page', $op->getJSVars()['wgRedirectedFrom'] );
661 }
662
663 /**
664 * @covers OutputPage::setPageTitle
665 * @covers OutputPage::getPageTitle
666 */
667 public function testPageTitle() {
668 // We don't test the actual HTML output anywhere, because that's up to the skin.
669 $op = $this->newInstance();
670
671 // Test default
672 $this->assertSame( '', $op->getPageTitle() );
673 $this->assertSame( '', $op->getHTMLTitle() );
674
675 // Test set to plain text
676 $op->setPageTitle( 'foobar' );
677
678 $this->assertSame( 'foobar', $op->getPageTitle() );
679 // HTML title should change as well
680 $this->assertSame( $this->getMsgText( $op, 'pagetitle', 'foobar' ), $op->getHTMLTitle() );
681
682 // Test set to text with good and bad HTML. We don't try to be comprehensive here, that
683 // belongs in Sanitizer tests.
684 $op->setPageTitle( '<script>a</script>&amp;<i>b</i>' );
685
686 $this->assertSame( '&lt;script&gt;a&lt;/script&gt;&amp;<i>b</i>', $op->getPageTitle() );
687 $this->assertSame(
688 $this->getMsgText( $op, 'pagetitle', '<script>a</script>&b' ),
689 $op->getHTMLTitle()
690 );
691
692 // Test set to message
693 $text = $this->getMsgText( $op, 'mainpage' );
694
695 $op->setPageTitle( $op->msg( 'mainpage' )->inContentLanguage() );
696 $this->assertSame( $text, $op->getPageTitle() );
697 $this->assertSame( $this->getMsgText( $op, 'pagetitle', $text ), $op->getHTMLTitle() );
698 }
699
700 /**
701 * @covers OutputPage::setTitle
702 */
703 public function testSetTitle() {
704 $op = $this->newInstance();
705
706 $this->assertSame( 'My test page', $op->getTitle()->getPrefixedText() );
707
708 $op->setTitle( Title::newFromText( 'Another test page' ) );
709
710 $this->assertSame( 'Another test page', $op->getTitle()->getPrefixedText() );
711 }
712
713 /**
714 * @covers OutputPage::setSubtitle
715 * @covers OutputPage::clearSubtitle
716 * @covers OutputPage::addSubtitle
717 * @covers OutputPage::getSubtitle
718 */
719 public function testSubtitle() {
720 $op = $this->newInstance();
721
722 $this->assertSame( '', $op->getSubtitle() );
723
724 $op->addSubtitle( '<b>foo</b>' );
725
726 $this->assertSame( '<b>foo</b>', $op->getSubtitle() );
727
728 $op->addSubtitle( $op->msg( 'mainpage' )->inContentLanguage() );
729
730 $this->assertSame(
731 "<b>foo</b><br />\n\t\t\t\t" . $this->getMsgText( $op, 'mainpage' ),
732 $op->getSubtitle()
733 );
734
735 $op->setSubtitle( 'There can be only one' );
736
737 $this->assertSame( 'There can be only one', $op->getSubtitle() );
738
739 $op->clearSubtitle();
740
741 $this->assertSame( '', $op->getSubtitle() );
742 }
743
744 /**
745 * @dataProvider provideBacklinkSubtitle
746 *
747 * @covers OutputPage::buildBacklinkSubtitle
748 */
749 public function testBuildBacklinkSubtitle( $titles, $queries, $contains, $notContains ) {
750 if ( count( $titles ) > 1 ) {
751 // Not applicable
752 $this->assertTrue( true );
753 return;
754 }
755
756 $title = Title::newFromText( $titles[0] );
757 $query = $queries[0];
758
759 $this->editPage( 'Page 1', '' );
760 $this->editPage( 'Page 2', '#REDIRECT [[Page 1]]' );
761
762 $str = OutputPage::buildBacklinkSubtitle( $title, $query )->text();
763
764 foreach ( $contains as $substr ) {
765 $this->assertContains( $substr, $str );
766 }
767
768 foreach ( $notContains as $substr ) {
769 $this->assertNotContains( $substr, $str );
770 }
771 }
772
773 /**
774 * @dataProvider provideBacklinkSubtitle
775 *
776 * @covers OutputPage::addBacklinkSubtitle
777 * @covers OutputPage::getSubtitle
778 */
779 public function testAddBacklinkSubtitle( $titles, $queries, $contains, $notContains ) {
780 $this->editPage( 'Page 1', '' );
781 $this->editPage( 'Page 2', '#REDIRECT [[Page 1]]' );
782
783 $op = $this->newInstance();
784 foreach ( $titles as $i => $unused ) {
785 $op->addBacklinkSubtitle( Title::newFromText( $titles[$i] ), $queries[$i] );
786 }
787
788 $str = $op->getSubtitle();
789
790 foreach ( $contains as $substr ) {
791 $this->assertContains( $substr, $str );
792 }
793
794 foreach ( $notContains as $substr ) {
795 $this->assertNotContains( $substr, $str );
796 }
797 }
798
799 public function provideBacklinkSubtitle() {
800 return [
801 [
802 [ 'Page 1' ],
803 [ [] ],
804 [ 'Page 1' ],
805 [ 'redirect', 'Page 2' ],
806 ],
807 [
808 [ 'Page 2' ],
809 [ [] ],
810 [ 'redirect=no' ],
811 [ 'Page 1' ],
812 ],
813 [
814 [ 'Page 1' ],
815 [ [ 'action' => 'edit' ] ],
816 [ 'action=edit' ],
817 [],
818 ],
819 [
820 [ 'Page 1', 'Page 2' ],
821 [ [], [] ],
822 [ 'Page 1', 'Page 2', "<br />\n\t\t\t\t" ],
823 [],
824 ],
825 // @todo Anything else to test?
826 ];
827 }
828
829 /**
830 * @covers OutputPage::setPrintable
831 * @covers OutputPage::isPrintable
832 */
833 public function testPrintable() {
834 $op = $this->newInstance();
835
836 $this->assertFalse( $op->isPrintable() );
837
838 $op->setPrintable();
839
840 $this->assertTrue( $op->isPrintable() );
841 }
842
843 /**
844 * @covers OutputPage::disable
845 * @covers OutputPage::isDisabled
846 */
847 public function testDisable() {
848 $op = $this->newInstance();
849
850 $this->assertFalse( $op->isDisabled() );
851 $this->assertNotSame( '', $op->output( true ) );
852
853 $op->disable();
854
855 $this->assertTrue( $op->isDisabled() );
856 $this->assertSame( '', $op->output( true ) );
857 }
858
859 /**
860 * @covers OutputPage::showNewSectionLink
861 * @covers OutputPage::addParserOutputMetadata
862 * @covers OutputPage::addParserOutput
863 */
864 public function testShowNewSectionLink() {
865 $op = $this->newInstance();
866
867 $this->assertFalse( $op->showNewSectionLink() );
868
869 $pOut1 = $this->createParserOutputStub( 'getNewSection', true );
870 $op->addParserOutputMetadata( $pOut1 );
871 $this->assertTrue( $op->showNewSectionLink() );
872
873 $pOut2 = $this->createParserOutputStub( 'getNewSection', false );
874 $op->addParserOutput( $pOut2 );
875 $this->assertFalse( $op->showNewSectionLink() );
876 }
877
878 /**
879 * @covers OutputPage::forceHideNewSectionLink
880 * @covers OutputPage::addParserOutputMetadata
881 * @covers OutputPage::addParserOutput
882 */
883 public function testForceHideNewSectionLink() {
884 $op = $this->newInstance();
885
886 $this->assertFalse( $op->forceHideNewSectionLink() );
887
888 $pOut1 = $this->createParserOutputStub( 'getHideNewSection', true );
889 $op->addParserOutputMetadata( $pOut1 );
890 $this->assertTrue( $op->forceHideNewSectionLink() );
891
892 $pOut2 = $this->createParserOutputStub( 'getHideNewSection', false );
893 $op->addParserOutput( $pOut2 );
894 $this->assertFalse( $op->forceHideNewSectionLink() );
895 }
896
897 /**
898 * @covers OutputPage::setSyndicated
899 * @covers OutputPage::isSyndicated
900 */
901 public function testSetSyndicated() {
902 $op = $this->newInstance( [ 'Feed' => true ] );
903 $this->assertFalse( $op->isSyndicated() );
904
905 $op->setSyndicated();
906 $this->assertTrue( $op->isSyndicated() );
907
908 $op->setSyndicated( false );
909 $this->assertFalse( $op->isSyndicated() );
910
911 $op = $this->newInstance(); // Feed => false by default
912 $this->assertFalse( $op->isSyndicated() );
913
914 $op->setSyndicated();
915 $this->assertFalse( $op->isSyndicated() );
916 }
917
918 /**
919 * @covers OutputPage::isSyndicated
920 * @covers OutputPage::setFeedAppendQuery
921 * @covers OutputPage::addFeedLink
922 * @covers OutputPage::getSyndicationLinks()
923 */
924 public function testFeedLinks() {
925 $op = $this->newInstance( [ 'Feed' => true ] );
926 $this->assertSame( [], $op->getSyndicationLinks() );
927
928 $op->addFeedLink( 'not a supported format', 'abc' );
929 $this->assertFalse( $op->isSyndicated() );
930 $this->assertSame( [], $op->getSyndicationLinks() );
931
932 $feedTypes = $op->getConfig()->get( 'AdvertisedFeedTypes' );
933
934 $op->addFeedLink( $feedTypes[0], 'def' );
935 $this->assertTrue( $op->isSyndicated() );
936 $this->assertSame( [ $feedTypes[0] => 'def' ], $op->getSyndicationLinks() );
937
938 $op->setFeedAppendQuery( false );
939 $expected = [];
940 foreach ( $feedTypes as $type ) {
941 $expected[$type] = $op->getTitle()->getLocalURL( "feed=$type" );
942 }
943 $this->assertSame( $expected, $op->getSyndicationLinks() );
944
945 $op->setFeedAppendQuery( 'apples=oranges' );
946 foreach ( $feedTypes as $type ) {
947 $expected[$type] = $op->getTitle()->getLocalURL( "feed=$type&apples=oranges" );
948 }
949 $this->assertSame( $expected, $op->getSyndicationLinks() );
950
951 $op = $this->newInstance(); // Feed => false by default
952 $this->assertSame( [], $op->getSyndicationLinks() );
953
954 $op->addFeedLink( $feedTypes[0], 'def' );
955 $this->assertFalse( $op->isSyndicated() );
956 $this->assertSame( [], $op->getSyndicationLinks() );
957 }
958
959 /**
960 * @covers OutputPage::setArticleFlag
961 * @covers OutputPage::isArticle
962 * @covers OutputPage::setArticleRelated
963 * @covers OutputPage::isArticleRelated
964 */
965 function testArticleFlags() {
966 $op = $this->newInstance();
967 $this->assertFalse( $op->isArticle() );
968 $this->assertTrue( $op->isArticleRelated() );
969
970 $op->setArticleRelated( false );
971 $this->assertFalse( $op->isArticle() );
972 $this->assertFalse( $op->isArticleRelated() );
973
974 $op->setArticleFlag( true );
975 $this->assertTrue( $op->isArticle() );
976 $this->assertTrue( $op->isArticleRelated() );
977
978 $op->setArticleFlag( false );
979 $this->assertFalse( $op->isArticle() );
980 $this->assertTrue( $op->isArticleRelated() );
981
982 $op->setArticleFlag( true );
983 $op->setArticleRelated( false );
984 $this->assertFalse( $op->isArticle() );
985 $this->assertFalse( $op->isArticleRelated() );
986 }
987
988 /**
989 * @covers OutputPage::addLanguageLinks
990 * @covers OutputPage::setLanguageLinks
991 * @covers OutputPage::getLanguageLinks
992 * @covers OutputPage::addParserOutputMetadata
993 * @covers OutputPage::addParserOutput
994 */
995 function testLanguageLinks() {
996 $op = $this->newInstance();
997 $this->assertSame( [], $op->getLanguageLinks() );
998
999 $op->addLanguageLinks( [ 'fr:A', 'it:B' ] );
1000 $this->assertSame( [ 'fr:A', 'it:B' ], $op->getLanguageLinks() );
1001
1002 $op->addLanguageLinks( [ 'de:C', 'es:D' ] );
1003 $this->assertSame( [ 'fr:A', 'it:B', 'de:C', 'es:D' ], $op->getLanguageLinks() );
1004
1005 $op->setLanguageLinks( [ 'pt:E' ] );
1006 $this->assertSame( [ 'pt:E' ], $op->getLanguageLinks() );
1007
1008 $pOut1 = $this->createParserOutputStub( 'getLanguageLinks', [ 'he:F', 'ar:G' ] );
1009 $op->addParserOutputMetadata( $pOut1 );
1010 $this->assertSame( [ 'pt:E', 'he:F', 'ar:G' ], $op->getLanguageLinks() );
1011
1012 $pOut2 = $this->createParserOutputStub( 'getLanguageLinks', [ 'pt:H' ] );
1013 $op->addParserOutput( $pOut2 );
1014 $this->assertSame( [ 'pt:E', 'he:F', 'ar:G', 'pt:H' ], $op->getLanguageLinks() );
1015 }
1016
1017 // @todo Are these category links tests too abstract and complicated for what they test? Would
1018 // it make sense to just write out all the tests by hand with maybe some copy-and-paste?
1019
1020 /**
1021 * @dataProvider provideGetCategories
1022 *
1023 * @covers OutputPage::addCategoryLinks
1024 * @covers OutputPage::getCategories
1025 * @covers OutputPage::getCategoryLinks
1026 *
1027 * @param array $args Array of form [ category name => sort key ]
1028 * @param array $fakeResults Array of form [ category name => value to return from mocked
1029 * LinkBatch ]
1030 * @param callable $variantLinkCallback Callback to replace findVariantLink() call
1031 * @param array $expectedNormal Expected return value of getCategoryLinks['normal']
1032 * @param array $expectedHidden Expected return value of getCategoryLinks['hidden']
1033 */
1034 public function testAddCategoryLinks(
1035 array $args, array $fakeResults, callable $variantLinkCallback = null,
1036 array $expectedNormal, array $expectedHidden
1037 ) {
1038 $expectedNormal = $this->extractExpectedCategories( $expectedNormal, 'add' );
1039 $expectedHidden = $this->extractExpectedCategories( $expectedHidden, 'add' );
1040
1041 $op = $this->setupCategoryTests( $fakeResults, $variantLinkCallback );
1042
1043 $op->addCategoryLinks( $args );
1044
1045 $this->doCategoryAsserts( $op, $expectedNormal, $expectedHidden );
1046 $this->doCategoryLinkAsserts( $op, $expectedNormal, $expectedHidden );
1047 }
1048
1049 /**
1050 * @dataProvider provideGetCategories
1051 *
1052 * @covers OutputPage::addCategoryLinks
1053 * @covers OutputPage::getCategories
1054 * @covers OutputPage::getCategoryLinks
1055 */
1056 public function testAddCategoryLinksOneByOne(
1057 array $args, array $fakeResults, callable $variantLinkCallback = null,
1058 array $expectedNormal, array $expectedHidden
1059 ) {
1060 if ( count( $args ) <= 1 ) {
1061 // @todo Should this be skipped instead of passed?
1062 $this->assertTrue( true );
1063 return;
1064 }
1065
1066 $expectedNormal = $this->extractExpectedCategories( $expectedNormal, 'onebyone' );
1067 $expectedHidden = $this->extractExpectedCategories( $expectedHidden, 'onebyone' );
1068
1069 $op = $this->setupCategoryTests( $fakeResults, $variantLinkCallback );
1070
1071 foreach ( $args as $key => $val ) {
1072 $op->addCategoryLinks( [ $key => $val ] );
1073 }
1074
1075 $this->doCategoryAsserts( $op, $expectedNormal, $expectedHidden );
1076 $this->doCategoryLinkAsserts( $op, $expectedNormal, $expectedHidden );
1077 }
1078
1079 /**
1080 * @dataProvider provideGetCategories
1081 *
1082 * @covers OutputPage::setCategoryLinks
1083 * @covers OutputPage::getCategories
1084 * @covers OutputPage::getCategoryLinks
1085 */
1086 public function testSetCategoryLinks(
1087 array $args, array $fakeResults, callable $variantLinkCallback = null,
1088 array $expectedNormal, array $expectedHidden
1089 ) {
1090 $expectedNormal = $this->extractExpectedCategories( $expectedNormal, 'set' );
1091 $expectedHidden = $this->extractExpectedCategories( $expectedHidden, 'set' );
1092
1093 $op = $this->setupCategoryTests( $fakeResults, $variantLinkCallback );
1094
1095 $op->setCategoryLinks( [ 'Initial page' => 'Initial page' ] );
1096 $op->setCategoryLinks( $args );
1097
1098 // We don't reset the categories, for some reason, only the links
1099 $expectedNormalCats = array_merge( [ 'Initial page' ], $expectedNormal );
1100 $expectedCats = array_merge( $expectedHidden, $expectedNormalCats );
1101
1102 $this->doCategoryAsserts( $op, $expectedNormalCats, $expectedHidden );
1103 $this->doCategoryLinkAsserts( $op, $expectedNormal, $expectedHidden );
1104 }
1105
1106 /**
1107 * @dataProvider provideGetCategories
1108 *
1109 * @covers OutputPage::addParserOutputMetadata
1110 * @covers OutputPage::addParserOutput
1111 * @covers OutputPage::getCategories
1112 * @covers OutputPage::getCategoryLinks
1113 */
1114 public function testParserOutputCategoryLinks(
1115 array $args, array $fakeResults, callable $variantLinkCallback = null,
1116 array $expectedNormal, array $expectedHidden
1117 ) {
1118 $expectedNormal = $this->extractExpectedCategories( $expectedNormal, 'pout' );
1119 $expectedHidden = $this->extractExpectedCategories( $expectedHidden, 'pout' );
1120
1121 $op = $this->setupCategoryTests( $fakeResults, $variantLinkCallback );
1122
1123 $stubPO = $this->createParserOutputStub( 'getCategories', $args );
1124
1125 // addParserOutput and addParserOutputMetadata should behave identically for us, so
1126 // alternate to get coverage for both without adding extra tests
1127 static $idx = 0;
1128 $idx++;
1129 $method = [ 'addParserOutputMetadata', 'addParserOutput' ][$idx % 2];
1130 $op->$method( $stubPO );
1131
1132 $this->doCategoryAsserts( $op, $expectedNormal, $expectedHidden );
1133 $this->doCategoryLinkAsserts( $op, $expectedNormal, $expectedHidden );
1134 }
1135
1136 /**
1137 * We allow different expectations for different tests as an associative array, like
1138 * [ 'set' => [ ... ], 'default' => [ ... ] ] if setCategoryLinks() will give a different
1139 * result.
1140 */
1141 private function extractExpectedCategories( array $expected, $key ) {
1142 if ( !$expected || isset( $expected[0] ) ) {
1143 return $expected;
1144 }
1145 return $expected[$key] ?? $expected['default'];
1146 }
1147
1148 private function setupCategoryTests(
1149 array $fakeResults, callable $variantLinkCallback = null
1150 ) : OutputPage {
1151 $this->setMwGlobals( 'wgUsePigLatinVariant', true );
1152
1153 $op = $this->getMockBuilder( OutputPage::class )
1154 ->setConstructorArgs( [ new RequestContext() ] )
1155 ->setMethods( [ 'addCategoryLinksToLBAndGetResult', 'getTitle' ] )
1156 ->getMock();
1157
1158 $title = Title::newFromText( 'My test page' );
1159 $op->expects( $this->any() )
1160 ->method( 'getTitle' )
1161 ->will( $this->returnValue( $title ) );
1162
1163 $op->expects( $this->any() )
1164 ->method( 'addCategoryLinksToLBAndGetResult' )
1165 ->will( $this->returnCallback( function ( array $categories ) use ( $fakeResults ) {
1166 $return = [];
1167 foreach ( $categories as $category => $unused ) {
1168 if ( isset( $fakeResults[$category] ) ) {
1169 $return[] = $fakeResults[$category];
1170 }
1171 }
1172 return new FakeResultWrapper( $return );
1173 } ) );
1174
1175 if ( $variantLinkCallback ) {
1176 $mockContLang = $this->getMockBuilder( Language::class )
1177 ->setConstructorArgs( [ 'en' ] )
1178 ->setMethods( [ 'findVariantLink' ] )
1179 ->getMock();
1180 $mockContLang->expects( $this->any() )
1181 ->method( 'findVariantLink' )
1182 ->will( $this->returnCallback( $variantLinkCallback ) );
1183 $this->setContentLang( $mockContLang );
1184 }
1185
1186 $this->assertSame( [], $op->getCategories() );
1187
1188 return $op;
1189 }
1190
1191 private function doCategoryAsserts( $op, $expectedNormal, $expectedHidden ) {
1192 $this->assertSame( array_merge( $expectedHidden, $expectedNormal ), $op->getCategories() );
1193 $this->assertSame( $expectedNormal, $op->getCategories( 'normal' ) );
1194 $this->assertSame( $expectedHidden, $op->getCategories( 'hidden' ) );
1195 }
1196
1197 private function doCategoryLinkAsserts( $op, $expectedNormal, $expectedHidden ) {
1198 $catLinks = $op->getCategoryLinks();
1199 $this->assertSame( (bool)$expectedNormal + (bool)$expectedHidden, count( $catLinks ) );
1200 if ( $expectedNormal ) {
1201 $this->assertSame( count( $expectedNormal ), count( $catLinks['normal'] ) );
1202 }
1203 if ( $expectedHidden ) {
1204 $this->assertSame( count( $expectedHidden ), count( $catLinks['hidden'] ) );
1205 }
1206
1207 foreach ( $expectedNormal as $i => $name ) {
1208 $this->assertContains( $name, $catLinks['normal'][$i] );
1209 }
1210 foreach ( $expectedHidden as $i => $name ) {
1211 $this->assertContains( $name, $catLinks['hidden'][$i] );
1212 }
1213 }
1214
1215 public function provideGetCategories() {
1216 return [
1217 'No categories' => [ [], [], null, [], [] ],
1218 'Simple test' => [
1219 [ 'Test1' => 'Some sortkey', 'Test2' => 'A different sortkey' ],
1220 [ 'Test1' => (object)[ 'pp_value' => 1, 'page_title' => 'Test1' ],
1221 'Test2' => (object)[ 'page_title' => 'Test2' ] ],
1222 null,
1223 [ 'Test2' ],
1224 [ 'Test1' ],
1225 ],
1226 'Invalid title' => [
1227 [ '[' => '[', 'Test' => 'Test' ],
1228 [ 'Test' => (object)[ 'page_title' => 'Test' ] ],
1229 null,
1230 [ 'Test' ],
1231 [],
1232 ],
1233 'Variant link' => [
1234 [ 'Test' => 'Test', 'Estay' => 'Estay' ],
1235 [ 'Test' => (object)[ 'page_title' => 'Test' ] ],
1236 function ( &$link, &$title ) {
1237 if ( $link === 'Estay' ) {
1238 $link = 'Test';
1239 $title = Title::makeTitleSafe( NS_CATEGORY, $link );
1240 }
1241 },
1242 // For adding one by one, the variant gets added as well as the original category,
1243 // but if you add them all together the second time gets skipped.
1244 [ 'onebyone' => [ 'Test', 'Test' ], 'default' => [ 'Test' ] ],
1245 [],
1246 ],
1247 ];
1248 }
1249
1250 /**
1251 * @covers OutputPage::getCategories
1252 */
1253 public function testGetCategoriesInvalid() {
1254 $this->setExpectedException( InvalidArgumentException::class,
1255 'Invalid category type given: hiddne' );
1256
1257 $op = $this->newInstance();
1258 $op->getCategories( 'hiddne' );
1259 }
1260
1261 // @todo Should we test addCategoryLinksToLBAndGetResult? If so, how? Insert some test rows in
1262 // the DB?
1263
1264 /**
1265 * @covers OutputPage::setIndicators
1266 * @covers OutputPage::getIndicators
1267 * @covers OutputPage::addParserOutputMetadata
1268 * @covers OutputPage::addParserOutput
1269 */
1270 public function testIndicators() {
1271 $op = $this->newInstance();
1272 $this->assertSame( [], $op->getIndicators() );
1273
1274 $op->setIndicators( [] );
1275 $this->assertSame( [], $op->getIndicators() );
1276
1277 // Test sorting alphabetically
1278 $op->setIndicators( [ 'b' => 'x', 'a' => 'y' ] );
1279 $this->assertSame( [ 'a' => 'y', 'b' => 'x' ], $op->getIndicators() );
1280
1281 // Test overwriting existing keys
1282 $op->setIndicators( [ 'c' => 'z', 'a' => 'w' ] );
1283 $this->assertSame( [ 'a' => 'w', 'b' => 'x', 'c' => 'z' ], $op->getIndicators() );
1284
1285 // Test with addParserOutputMetadata
1286 $pOut1 = $this->createParserOutputStub( 'getIndicators', [ 'c' => 'u', 'd' => 'v' ] );
1287 $op->addParserOutputMetadata( $pOut1 );
1288 $this->assertSame( [ 'a' => 'w', 'b' => 'x', 'c' => 'u', 'd' => 'v' ],
1289 $op->getIndicators() );
1290
1291 // Test with addParserOutput
1292 $pOut2 = $this->createParserOutputStub( 'getIndicators', [ 'a' => '!!!' ] );
1293 $op->addParserOutput( $pOut2 );
1294 $this->assertSame( [ 'a' => '!!!', 'b' => 'x', 'c' => 'u', 'd' => 'v' ],
1295 $op->getIndicators() );
1296 }
1297
1298 /**
1299 * @covers OutputPage::addHelpLink
1300 * @covers OutputPage::getIndicators
1301 */
1302 public function testAddHelpLink() {
1303 $op = $this->newInstance();
1304
1305 $op->addHelpLink( 'Manual:PHP unit testing' );
1306 $indicators = $op->getIndicators();
1307 $this->assertSame( [ 'mw-helplink' ], array_keys( $indicators ) );
1308 $this->assertContains( 'Manual:PHP_unit_testing', $indicators['mw-helplink'] );
1309
1310 $op->addHelpLink( 'https://phpunit.de', true );
1311 $indicators = $op->getIndicators();
1312 $this->assertSame( [ 'mw-helplink' ], array_keys( $indicators ) );
1313 $this->assertContains( 'https://phpunit.de', $indicators['mw-helplink'] );
1314 $this->assertNotContains( 'mediawiki', $indicators['mw-helplink'] );
1315 $this->assertNotContains( 'Manual:PHP', $indicators['mw-helplink'] );
1316 }
1317
1318 /**
1319 * @covers OutputPage::prependHTML
1320 * @covers OutputPage::addHTML
1321 * @covers OutputPage::addElement
1322 * @covers OutputPage::clearHTML
1323 * @covers OutputPage::getHTML
1324 */
1325 public function testBodyHTML() {
1326 $op = $this->newInstance();
1327 $this->assertSame( '', $op->getHTML() );
1328
1329 $op->addHTML( 'a' );
1330 $this->assertSame( 'a', $op->getHTML() );
1331
1332 $op->addHTML( 'b' );
1333 $this->assertSame( 'ab', $op->getHTML() );
1334
1335 $op->prependHTML( 'c' );
1336 $this->assertSame( 'cab', $op->getHTML() );
1337
1338 $op->addElement( 'p', [ 'id' => 'foo' ], 'd' );
1339 $this->assertSame( 'cab<p id="foo">d</p>', $op->getHTML() );
1340
1341 $op->clearHTML();
1342 $this->assertSame( '', $op->getHTML() );
1343 }
1344
1345 /**
1346 * @dataProvider provideRevisionId
1347 * @covers OutputPage::setRevisionId
1348 * @covers OutputPage::getRevisionId
1349 */
1350 public function testRevisionId( $newVal, $expected ) {
1351 $op = $this->newInstance();
1352
1353 $this->assertNull( $op->setRevisionId( $newVal ) );
1354 $this->assertSame( $expected, $op->getRevisionId() );
1355 $this->assertSame( $expected, $op->setRevisionId( null ) );
1356 $this->assertNull( $op->getRevisionId() );
1357 }
1358
1359 public function provideRevisionId() {
1360 return [
1361 [ null, null ],
1362 [ 7, 7 ],
1363 [ -1, -1 ],
1364 [ 3.2, 3 ],
1365 [ '0', 0 ],
1366 [ '32% finished', 32 ],
1367 [ false, 0 ],
1368 ];
1369 }
1370
1371 /**
1372 * @covers OutputPage::setRevisionTimestamp
1373 * @covers OutputPage::getRevisionTimestamp
1374 */
1375 public function testRevisionTimestamp() {
1376 $op = $this->newInstance();
1377 $this->assertNull( $op->getRevisionTimestamp() );
1378
1379 $this->assertNull( $op->setRevisionTimestamp( 'abc' ) );
1380 $this->assertSame( 'abc', $op->getRevisionTimestamp() );
1381 $this->assertSame( 'abc', $op->setRevisionTimestamp( null ) );
1382 $this->assertNull( $op->getRevisionTimestamp() );
1383 }
1384
1385 /**
1386 * @covers OutputPage::setFileVersion
1387 * @covers OutputPage::getFileVersion
1388 */
1389 public function testFileVersion() {
1390 $op = $this->newInstance();
1391 $this->assertNull( $op->getFileVersion() );
1392
1393 $stubFile = $this->createMock( File::class );
1394 $stubFile->method( 'exists' )->willReturn( true );
1395 $stubFile->method( 'getTimestamp' )->willReturn( '12211221123321' );
1396 $stubFile->method( 'getSha1' )->willReturn( 'bf3ffa7047dc080f5855377a4f83cd18887e3b05' );
1397
1398 $op->setFileVersion( $stubFile );
1399
1400 $this->assertEquals(
1401 [ 'time' => '12211221123321', 'sha1' => 'bf3ffa7047dc080f5855377a4f83cd18887e3b05' ],
1402 $op->getFileVersion()
1403 );
1404
1405 $stubMissingFile = $this->createMock( File::class );
1406 $stubMissingFile->method( 'exists' )->willReturn( false );
1407
1408 $op->setFileVersion( $stubMissingFile );
1409 $this->assertNull( $op->getFileVersion() );
1410
1411 $op->setFileVersion( $stubFile );
1412 $this->assertNotNull( $op->getFileVersion() );
1413
1414 $op->setFileVersion( null );
1415 $this->assertNull( $op->getFileVersion() );
1416 }
1417
1418 /**
1419 * Call either with arguments $methodName, $returnValue; or an array
1420 * [ $methodName => $returnValue, $methodName => $returnValue, ... ]
1421 */
1422 private function createParserOutputStub( ...$args ) {
1423 if ( count( $args ) === 0 ) {
1424 $retVals = [];
1425 } elseif ( count( $args ) === 1 ) {
1426 $retVals = $args[0];
1427 } elseif ( count( $args ) === 2 ) {
1428 $retVals = [ $args[0] => $args[1] ];
1429 }
1430 $pOut = $this->getMock( ParserOutput::class );
1431 foreach ( $retVals as $method => $retVal ) {
1432 $pOut->method( $method )->willReturn( $retVal );
1433 }
1434
1435 $arrayReturningMethods = [
1436 'getCategories',
1437 'getFileSearchOptions',
1438 'getHeadItems',
1439 'getIndicators',
1440 'getLanguageLinks',
1441 'getOutputHooks',
1442 'getTemplateIds',
1443 ];
1444
1445 foreach ( $arrayReturningMethods as $method ) {
1446 $pOut->method( $method )->willReturn( [] );
1447 }
1448
1449 return $pOut;
1450 }
1451
1452 /**
1453 * @covers OutputPage::getTemplateIds
1454 * @covers OutputPage::addParserOutputMetadata
1455 * @covers OutputPage::addParserOutput
1456 */
1457 public function testTemplateIds() {
1458 $op = $this->newInstance();
1459 $this->assertSame( [], $op->getTemplateIds() );
1460
1461 // Test with no template id's
1462 $stubPOEmpty = $this->createParserOutputStub();
1463 $op->addParserOutputMetadata( $stubPOEmpty );
1464 $this->assertSame( [], $op->getTemplateIds() );
1465
1466 // Test with some arbitrary template id's
1467 $ids = [
1468 NS_MAIN => [ 'A' => 3, 'B' => 17 ],
1469 NS_TALK => [ 'C' => 31 ],
1470 NS_MEDIA => [ 'D' => -1 ],
1471 ];
1472
1473 $stubPO1 = $this->createParserOutputStub( 'getTemplateIds', $ids );
1474
1475 $op->addParserOutputMetadata( $stubPO1 );
1476 $this->assertSame( $ids, $op->getTemplateIds() );
1477
1478 // Test merging with a second set of id's
1479 $stubPO2 = $this->createParserOutputStub( 'getTemplateIds', [
1480 NS_MAIN => [ 'E' => 1234 ],
1481 NS_PROJECT => [ 'F' => 5678 ],
1482 ] );
1483
1484 $finalIds = [
1485 NS_MAIN => [ 'E' => 1234, 'A' => 3, 'B' => 17 ],
1486 NS_TALK => [ 'C' => 31 ],
1487 NS_MEDIA => [ 'D' => -1 ],
1488 NS_PROJECT => [ 'F' => 5678 ],
1489 ];
1490
1491 $op->addParserOutput( $stubPO2 );
1492 $this->assertSame( $finalIds, $op->getTemplateIds() );
1493
1494 // Test merging with an empty set of id's
1495 $op->addParserOutputMetadata( $stubPOEmpty );
1496 $this->assertSame( $finalIds, $op->getTemplateIds() );
1497 }
1498
1499 /**
1500 * @covers OutputPage::getFileSearchOptions
1501 * @covers OutputPage::addParserOutputMetadata
1502 * @covers OutputPage::addParserOutput
1503 */
1504 public function testFileSearchOptions() {
1505 $op = $this->newInstance();
1506 $this->assertSame( [], $op->getFileSearchOptions() );
1507
1508 // Test with no files
1509 $stubPOEmpty = $this->createParserOutputStub();
1510
1511 $op->addParserOutputMetadata( $stubPOEmpty );
1512 $this->assertSame( [], $op->getFileSearchOptions() );
1513
1514 // Test with some arbitrary files
1515 $files1 = [
1516 'A' => [ 'time' => null, 'sha1' => '' ],
1517 'B' => [
1518 'time' => '12211221123321',
1519 'sha1' => 'bf3ffa7047dc080f5855377a4f83cd18887e3b05',
1520 ],
1521 ];
1522
1523 $stubPO1 = $this->createParserOutputStub( 'getFileSearchOptions', $files1 );
1524
1525 $op->addParserOutput( $stubPO1 );
1526 $this->assertSame( $files1, $op->getFileSearchOptions() );
1527
1528 // Test merging with a second set of files
1529 $files2 = [
1530 'C' => [ 'time' => null, 'sha1' => '' ],
1531 'B' => [ 'time' => null, 'sha1' => '' ],
1532 ];
1533
1534 $stubPO2 = $this->createParserOutputStub( 'getFileSearchOptions', $files2 );
1535
1536 $op->addParserOutputMetadata( $stubPO2 );
1537 $this->assertSame( array_merge( $files1, $files2 ), $op->getFileSearchOptions() );
1538
1539 // Test merging with an empty set of files
1540 $op->addParserOutput( $stubPOEmpty );
1541 $this->assertSame( array_merge( $files1, $files2 ), $op->getFileSearchOptions() );
1542 }
1543
1544 /**
1545 * @dataProvider provideAddWikiText
1546 * @covers OutputPage::addWikiTextAsInterface
1547 * @covers OutputPage::wrapWikiTextAsInterface
1548 * @covers OutputPage::addWikiTextAsContent
1549 * @covers OutputPage::getHTML
1550 */
1551 public function testAddWikiText( $method, array $args, $expected ) {
1552 $op = $this->newInstance();
1553 $this->assertSame( '', $op->getHTML() );
1554
1555 if ( in_array(
1556 $method,
1557 [ 'addWikiTextAsInterface', 'addWikiTextAsContent' ]
1558 ) && count( $args ) >= 3 && $args[2] === null ) {
1559 // Special placeholder because we can't get the actual title in the provider
1560 $args[2] = $op->getTitle();
1561 }
1562
1563 $op->$method( ...$args );
1564 $this->assertSame( $expected, $op->getHTML() );
1565 }
1566
1567 public function provideAddWikiText() {
1568 $tests = [
1569 'addWikiTextAsInterface' => [
1570 'Simple wikitext' => [
1571 [ "'''Bold'''" ],
1572 "<p><b>Bold</b>\n</p>",
1573 ], 'Untidy wikitext' => [
1574 [ "<b>Bold" ],
1575 "<p><b>Bold\n</b></p>",
1576 ], 'List at start' => [
1577 [ '* List' ],
1578 "<ul><li>List</li></ul>\n",
1579 ], 'List not at start' => [
1580 [ '* Not a list', false ],
1581 '<p>* Not a list</p>',
1582 ], 'No section edit links' => [
1583 [ '== Title ==' ],
1584 "<h2><span class=\"mw-headline\" id=\"Title\">Title</span></h2>",
1585 ], 'With title at start' => [
1586 [ '* {{PAGENAME}}', true, Title::newFromText( 'Talk:Some page' ) ],
1587 "<ul><li>Some page</li></ul>\n",
1588 ], 'With title at start' => [
1589 [ '* {{PAGENAME}}', false, Title::newFromText( 'Talk:Some page' ), false ],
1590 "<p>* Some page</p>",
1591 ], 'Untidy input' => [
1592 [ '<b>{{PAGENAME}}', true, Title::newFromText( 'Talk:Some page' ) ],
1593 "<p><b>Some page\n</b></p>",
1594 ],
1595 ],
1596 'addWikiTextAsContent' => [
1597 'SpecialNewimages' => [
1598 [ "<p lang='en' dir='ltr'>\nMy message" ],
1599 '<p lang="en" dir="ltr">' . "\nMy message</p>"
1600 ], 'List at start' => [
1601 [ '* List' ],
1602 "<ul><li>List</li></ul>",
1603 ], 'List not at start' => [
1604 [ '* <b>Not a list', false ],
1605 '<p>* <b>Not a list</b></p>',
1606 ], 'With title at start' => [
1607 [ '* {{PAGENAME}}', true, Title::newFromText( 'Talk:Some page' ) ],
1608 "<ul><li>Some page</li></ul>\n",
1609 ], 'With title at start' => [
1610 [ '* {{PAGENAME}}', false, Title::newFromText( 'Talk:Some page' ), false ],
1611 "<p>* Some page</p>",
1612 ], 'EditPage' => [
1613 [ "<div class='mw-editintro'>{{PAGENAME}}", true, Title::newFromText( 'Talk:Some page' ) ],
1614 '<div class="mw-editintro">' . "Some page</div>"
1615 ],
1616 ],
1617 'wrapWikiTextAsInterface' => [
1618 'Simple' => [
1619 [ 'wrapperClass', 'text' ],
1620 "<div class=\"wrapperClass\"><p>text\n</p></div>"
1621 ], 'Spurious </div>' => [
1622 [ 'wrapperClass', 'text</div><div>more' ],
1623 "<div class=\"wrapperClass\"><p>text</p><div>more</div></div>"
1624 ], 'Extra newlines would break <p> wrappers' => [
1625 [ 'two classes', "1\n\n2\n\n3" ],
1626 "<div class=\"two classes\"><p>1\n</p><p>2\n</p><p>3\n</p></div>"
1627 ], 'Other unclosed tags' => [
1628 [ 'error', 'a<b>c<i>d' ],
1629 "<div class=\"error\"><p>a<b>c<i>d\n</i></b></p></div>"
1630 ],
1631 ],
1632 ];
1633
1634 // We have to reformat our array to match what PHPUnit wants
1635 $ret = [];
1636 foreach ( $tests as $key => $subarray ) {
1637 foreach ( $subarray as $subkey => $val ) {
1638 $val = array_merge( [ $key ], $val );
1639 $ret[$subkey] = $val;
1640 }
1641 }
1642
1643 return $ret;
1644 }
1645
1646 /**
1647 * @covers OutputPage::addWikiTextAsInterface
1648 */
1649 public function testAddWikiTextAsInterfaceNoTitle() {
1650 $this->setExpectedException( MWException::class, 'Title is null' );
1651
1652 $op = $this->newInstance( [], null, 'notitle' );
1653 $op->addWikiTextAsInterface( 'a' );
1654 }
1655
1656 /**
1657 * @covers OutputPage::addWikiTextAsContent
1658 */
1659 public function testAddWikiTextAsContentNoTitle() {
1660 $this->setExpectedException( MWException::class, 'Title is null' );
1661
1662 $op = $this->newInstance( [], null, 'notitle' );
1663 $op->addWikiTextAsContent( 'a' );
1664 }
1665
1666 /**
1667 * @covers OutputPage::addWikiMsg
1668 */
1669 public function testAddWikiMsg() {
1670 $msg = wfMessage( 'parentheses' );
1671 $this->assertSame( '(a)', $msg->rawParams( 'a' )->plain() );
1672
1673 $op = $this->newInstance();
1674 $this->assertSame( '', $op->getHTML() );
1675 $op->addWikiMsg( 'parentheses', "<b>a" );
1676 // The input is bad unbalanced HTML, but the output is tidied
1677 $this->assertSame( "<p>(<b>a)\n</b></p>", $op->getHTML() );
1678 }
1679
1680 /**
1681 * @covers OutputPage::wrapWikiMsg
1682 */
1683 public function testWrapWikiMsg() {
1684 $msg = wfMessage( 'parentheses' );
1685 $this->assertSame( '(a)', $msg->rawParams( 'a' )->plain() );
1686
1687 $op = $this->newInstance();
1688 $this->assertSame( '', $op->getHTML() );
1689 $op->wrapWikiMsg( '[$1]', [ 'parentheses', "<b>a" ] );
1690 // The input is bad unbalanced HTML, but the output is tidied
1691 $this->assertSame( "<p>[(<b>a)]\n</b></p>", $op->getHTML() );
1692 }
1693
1694 /**
1695 * @covers OutputPage::addParserOutputMetadata
1696 * @covers OutputPage::addParserOutput
1697 */
1698 public function testNoGallery() {
1699 $op = $this->newInstance();
1700 $this->assertFalse( $op->mNoGallery );
1701
1702 $stubPO1 = $this->createParserOutputStub( 'getNoGallery', true );
1703 $op->addParserOutputMetadata( $stubPO1 );
1704 $this->assertTrue( $op->mNoGallery );
1705
1706 $stubPO2 = $this->createParserOutputStub( 'getNoGallery', false );
1707 $op->addParserOutput( $stubPO2 );
1708 $this->assertFalse( $op->mNoGallery );
1709 }
1710
1711 private static $parserOutputHookCalled;
1712
1713 /**
1714 * @covers OutputPage::addParserOutputMetadata
1715 */
1716 public function testParserOutputHooks() {
1717 $op = $this->newInstance();
1718 $pOut = $this->createParserOutputStub( 'getOutputHooks', [
1719 [ 'myhook', 'banana' ],
1720 [ 'yourhook', 'kumquat' ],
1721 [ 'theirhook', 'hippopotamus' ],
1722 ] );
1723
1724 self::$parserOutputHookCalled = [];
1725
1726 $this->setMwGlobals( 'wgParserOutputHooks', [
1727 'myhook' => function ( OutputPage $innerOp, ParserOutput $innerPOut, $data )
1728 use ( $op, $pOut ) {
1729 $this->assertSame( $op, $innerOp );
1730 $this->assertSame( $pOut, $innerPOut );
1731 $this->assertSame( 'banana', $data );
1732 self::$parserOutputHookCalled[] = 'closure';
1733 },
1734 'yourhook' => [ $this, 'parserOutputHookCallback' ],
1735 'theirhook' => [ __CLASS__, 'parserOutputHookCallbackStatic' ],
1736 'uncalled' => function () {
1737 $this->assertTrue( false );
1738 },
1739 ] );
1740
1741 $op->addParserOutputMetadata( $pOut );
1742
1743 $this->assertSame( [ 'closure', 'callback', 'static' ], self::$parserOutputHookCalled );
1744 }
1745
1746 public function parserOutputHookCallback(
1747 OutputPage $op, ParserOutput $pOut, $data
1748 ) {
1749 $this->assertSame( 'kumquat', $data );
1750
1751 self::$parserOutputHookCalled[] = 'callback';
1752 }
1753
1754 public static function parserOutputHookCallbackStatic(
1755 OutputPage $op, ParserOutput $pOut, $data
1756 ) {
1757 // All the assert methods are actually static, who knew!
1758 self::assertSame( 'hippopotamus', $data );
1759
1760 self::$parserOutputHookCalled[] = 'static';
1761 }
1762
1763 // @todo Make sure to test the following in addParserOutputMetadata() as well when we add tests
1764 // for them:
1765 // * addModules()
1766 // * addModuleStyles()
1767 // * addJsConfigVars()
1768 // * enableOOUI()
1769 // Otherwise those lines of addParserOutputMetadata() will be reported as covered, but we won't
1770 // be testing they actually work.
1771
1772 /**
1773 * @covers OutputPage::addParserOutputText
1774 */
1775 public function testAddParserOutputText() {
1776 $op = $this->newInstance();
1777 $this->assertSame( '', $op->getHTML() );
1778
1779 $pOut = $this->createParserOutputStub( 'getText', '<some text>' );
1780
1781 $op->addParserOutputMetadata( $pOut );
1782 $this->assertSame( '', $op->getHTML() );
1783
1784 $op->addParserOutputText( $pOut );
1785 $this->assertSame( '<some text>', $op->getHTML() );
1786 }
1787
1788 /**
1789 * @covers OutputPage::addParserOutput
1790 */
1791 public function testAddParserOutput() {
1792 $op = $this->newInstance();
1793 $this->assertSame( '', $op->getHTML() );
1794 $this->assertFalse( $op->showNewSectionLink() );
1795
1796 $pOut = $this->createParserOutputStub( [
1797 'getText' => '<some text>',
1798 'getNewSection' => true,
1799 ] );
1800
1801 $op->addParserOutput( $pOut );
1802 $this->assertSame( '<some text>', $op->getHTML() );
1803 $this->assertTrue( $op->showNewSectionLink() );
1804 }
1805
1806 /**
1807 * @covers OutputPage::addTemplate
1808 */
1809 public function testAddTemplate() {
1810 $template = $this->getMock( QuickTemplate::class );
1811 $template->method( 'getHTML' )->willReturn( '<abc>&def;' );
1812
1813 $op = $this->newInstance();
1814 $op->addTemplate( $template );
1815
1816 $this->assertSame( '<abc>&def;', $op->getHTML() );
1817 }
1818
1819 /**
1820 * @dataProvider provideParse
1821 * @covers OutputPage::parse
1822 * @param array $args To pass to parse()
1823 * @param string $expectedHTML Expected return value for parse()
1824 * @param string $expectedHTML Expected return value for parseInline(), if different
1825 */
1826 public function testParse( array $args, $expectedHTML ) {
1827 $this->hideDeprecated( 'OutputPage::parse' );
1828 $op = $this->newInstance();
1829 $this->assertSame( $expectedHTML, $op->parse( ...$args ) );
1830 }
1831
1832 /**
1833 * @dataProvider provideParse
1834 * @covers OutputPage::parseInline
1835 */
1836 public function testParseInline( array $args, $expectedHTML, $expectedHTMLInline = null ) {
1837 if ( count( $args ) > 3 ) {
1838 // $language param not supported
1839 $this->assertTrue( true );
1840 return;
1841 }
1842 $this->hideDeprecated( 'OutputPage::parseInline' );
1843 $op = $this->newInstance();
1844 $this->assertSame( $expectedHTMLInline ?? $expectedHTML, $op->parseInline( ...$args ) );
1845 }
1846
1847 public function provideParse() {
1848 return [
1849 'List at start of line (content)' => [
1850 [ '* List', true, false ],
1851 "<div class=\"mw-parser-output\"><ul><li>List</li></ul></div>",
1852 "<ul><li>List</li></ul>",
1853 ],
1854 'List at start of line (interface)' => [
1855 [ '* List', true, true ],
1856 "<ul><li>List</li></ul>",
1857 ],
1858 'List not at start (content)' => [
1859 [ "* ''Not'' list", false, false ],
1860 '<div class="mw-parser-output">* <i>Not</i> list</div>',
1861 '* <i>Not</i> list',
1862 ],
1863 'List not at start (interface)' => [
1864 [ "* ''Not'' list", false, true ],
1865 '* <i>Not</i> list',
1866 ],
1867 'Interface message' => [
1868 [ "''Italic''", true, true ],
1869 "<p><i>Italic</i>\n</p>",
1870 '<i>Italic</i>',
1871 ],
1872 'formatnum (content)' => [
1873 [ '{{formatnum:123456.789}}', true, false ],
1874 "<div class=\"mw-parser-output\"><p>123,456.789\n</p></div>",
1875 "123,456.789",
1876 ],
1877 'formatnum (interface)' => [
1878 [ '{{formatnum:123456.789}}', true, true ],
1879 "<p>123,456.789\n</p>",
1880 "123,456.789",
1881 ],
1882 'Language (content)' => [
1883 [ '{{formatnum:123456.789}}', true, false, Language::factory( 'is' ) ],
1884 "<div class=\"mw-parser-output\"><p>123.456,789\n</p></div>",
1885 ],
1886 'Language (interface)' => [
1887 [ '{{formatnum:123456.789}}', true, true, Language::factory( 'is' ) ],
1888 "<p>123.456,789\n</p>",
1889 '123.456,789',
1890 ],
1891 'No section edit links' => [
1892 [ '== Header ==' ],
1893 '<div class="mw-parser-output"><h2><span class="mw-headline" id="Header">' .
1894 "Header</span></h2></div>",
1895 '<h2><span class="mw-headline" id="Header">Header</span></h2>',
1896 ]
1897 ];
1898 }
1899
1900 /**
1901 * @dataProvider provideParseAs
1902 * @covers OutputPage::parseAsContent
1903 * @param array $args To pass to parse()
1904 * @param string $expectedHTML Expected return value for parseAsContent()
1905 * @param string $expectedHTML Expected return value for parseInlineAsInterface(), if different
1906 */
1907 public function testParseAsContent(
1908 array $args, $expectedHTML, $expectedHTMLInline = null
1909 ) {
1910 $op = $this->newInstance();
1911 $this->assertSame( $expectedHTML, $op->parseAsContent( ...$args ) );
1912 }
1913
1914 /**
1915 * @dataProvider provideParseAs
1916 * @covers OutputPage::parseAsInterface
1917 * @param array $args To pass to parse()
1918 * @param string $expectedHTML Expected return value for parseAsInterface()
1919 * @param string $expectedHTML Expected return value for parseInlineAsInterface(), if different
1920 */
1921 public function testParseAsInterface(
1922 array $args, $expectedHTML, $expectedHTMLInline = null
1923 ) {
1924 $op = $this->newInstance();
1925 $this->assertSame( $expectedHTML, $op->parseAsInterface( ...$args ) );
1926 }
1927
1928 /**
1929 * @dataProvider provideParseAs
1930 * @covers OutputPage::parseInlineAsInterface
1931 */
1932 public function testParseInlineAsInterface(
1933 array $args, $expectedHTML, $expectedHTMLInline = null
1934 ) {
1935 $op = $this->newInstance();
1936 $this->assertSame(
1937 $expectedHTMLInline ?? $expectedHTML,
1938 $op->parseInlineAsInterface( ...$args )
1939 );
1940 }
1941
1942 public function provideParseAs() {
1943 return [
1944 'List at start of line' => [
1945 [ '* List', true ],
1946 "<ul><li>List</li></ul>",
1947 ],
1948 'List not at start' => [
1949 [ "* ''Not'' list", false ],
1950 '<p>* <i>Not</i> list</p>',
1951 '* <i>Not</i> list',
1952 ],
1953 'Italics' => [
1954 [ "''Italic''", true ],
1955 "<p><i>Italic</i>\n</p>",
1956 '<i>Italic</i>',
1957 ],
1958 'formatnum' => [
1959 [ '{{formatnum:123456.789}}', true ],
1960 "<p>123,456.789\n</p>",
1961 "123,456.789",
1962 ],
1963 'No section edit links' => [
1964 [ '== Header ==' ],
1965 '<h2><span class="mw-headline" id="Header">Header</span></h2>',
1966 ]
1967 ];
1968 }
1969
1970 /**
1971 * @covers OutputPage::parse
1972 */
1973 public function testParseNullTitle() {
1974 $this->hideDeprecated( 'OutputPage::parse' );
1975 $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
1976 $op = $this->newInstance( [], null, 'notitle' );
1977 $op->parse( '' );
1978 }
1979
1980 /**
1981 * @covers OutputPage::parseInline
1982 */
1983 public function testParseInlineNullTitle() {
1984 $this->hideDeprecated( 'OutputPage::parseInline' );
1985 $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
1986 $op = $this->newInstance( [], null, 'notitle' );
1987 $op->parseInline( '' );
1988 }
1989
1990 /**
1991 * @covers OutputPage::parseAsContent
1992 */
1993 public function testParseAsContentNullTitle() {
1994 $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
1995 $op = $this->newInstance( [], null, 'notitle' );
1996 $op->parseAsContent( '' );
1997 }
1998
1999 /**
2000 * @covers OutputPage::parseAsInterface
2001 */
2002 public function testParseAsInterfaceNullTitle() {
2003 $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
2004 $op = $this->newInstance( [], null, 'notitle' );
2005 $op->parseAsInterface( '' );
2006 }
2007
2008 /**
2009 * @covers OutputPage::parseInlineAsInterface
2010 */
2011 public function testParseInlineAsInterfaceNullTitle() {
2012 $this->setExpectedException( MWException::class, 'Empty $mTitle in OutputPage::parseInternal' );
2013 $op = $this->newInstance( [], null, 'notitle' );
2014 $op->parseInlineAsInterface( '' );
2015 }
2016
2017 /**
2018 * @covers OutputPage::setCdnMaxage
2019 * @covers OutputPage::lowerCdnMaxage
2020 */
2021 public function testCdnMaxage() {
2022 $op = $this->newInstance();
2023 $wrapper = TestingAccessWrapper::newFromObject( $op );
2024 $this->assertSame( 0, $wrapper->mCdnMaxage );
2025
2026 $op->setCdnMaxage( -1 );
2027 $this->assertSame( -1, $wrapper->mCdnMaxage );
2028
2029 $op->setCdnMaxage( 120 );
2030 $this->assertSame( 120, $wrapper->mCdnMaxage );
2031
2032 $op->setCdnMaxage( 60 );
2033 $this->assertSame( 60, $wrapper->mCdnMaxage );
2034
2035 $op->setCdnMaxage( 180 );
2036 $this->assertSame( 180, $wrapper->mCdnMaxage );
2037
2038 $op->lowerCdnMaxage( 240 );
2039 $this->assertSame( 180, $wrapper->mCdnMaxage );
2040
2041 $op->setCdnMaxage( 300 );
2042 $this->assertSame( 240, $wrapper->mCdnMaxage );
2043
2044 $op->lowerCdnMaxage( 120 );
2045 $this->assertSame( 120, $wrapper->mCdnMaxage );
2046
2047 $op->setCdnMaxage( 180 );
2048 $this->assertSame( 120, $wrapper->mCdnMaxage );
2049
2050 $op->setCdnMaxage( 60 );
2051 $this->assertSame( 60, $wrapper->mCdnMaxage );
2052
2053 $op->setCdnMaxage( 240 );
2054 $this->assertSame( 120, $wrapper->mCdnMaxage );
2055 }
2056
2057 /** @var int Faked time to set for tests that need it */
2058 private static $fakeTime;
2059
2060 /**
2061 * @dataProvider provideAdaptCdnTTL
2062 * @covers OutputPage::adaptCdnTTL
2063 * @param array $args To pass to adaptCdnTTL()
2064 * @param int $expected Expected new value of mCdnMaxageLimit
2065 * @param array $options Associative array:
2066 * initialMaxage => Maxage to set before calling adaptCdnTTL() (default 86400)
2067 */
2068 public function testAdaptCdnTTL( array $args, $expected, array $options = [] ) {
2069 try {
2070 MWTimestamp::setFakeTime( self::$fakeTime );
2071
2072 $op = $this->newInstance();
2073 // Set a high maxage so that it will get reduced by adaptCdnTTL(). The default maxage
2074 // is 0, so adaptCdnTTL() won't mutate the object at all.
2075 $initial = $options['initialMaxage'] ?? 86400;
2076 $op->setCdnMaxage( $initial );
2077
2078 $op->adaptCdnTTL( ...$args );
2079 } finally {
2080 MWTimestamp::setFakeTime( false );
2081 }
2082
2083 $wrapper = TestingAccessWrapper::newFromObject( $op );
2084
2085 // Special rules for false/null
2086 if ( $args[0] === null || $args[0] === false ) {
2087 $this->assertSame( $initial, $wrapper->mCdnMaxage, 'member value' );
2088 $op->setCdnMaxage( $expected + 1 );
2089 $this->assertSame( $expected + 1, $wrapper->mCdnMaxage, 'member value after new set' );
2090 return;
2091 }
2092
2093 $this->assertSame( $expected, $wrapper->mCdnMaxageLimit, 'limit value' );
2094
2095 if ( $initial >= $expected ) {
2096 $this->assertSame( $expected, $wrapper->mCdnMaxage, 'member value' );
2097 } else {
2098 $this->assertSame( $initial, $wrapper->mCdnMaxage, 'member value' );
2099 }
2100
2101 $op->setCdnMaxage( $expected + 1 );
2102 $this->assertSame( $expected, $wrapper->mCdnMaxage, 'member value after new set' );
2103 }
2104
2105 public function provideAdaptCdnTTL() {
2106 global $wgCdnMaxAge;
2107 $now = time();
2108 self::$fakeTime = $now;
2109 return [
2110 'Five minutes ago' => [ [ $now - 300 ], 270 ],
2111 'Now' => [ [ +0 ], IExpiringStore::TTL_MINUTE ],
2112 'Five minutes from now' => [ [ $now + 300 ], IExpiringStore::TTL_MINUTE ],
2113 'Five minutes ago, initial maxage four minutes' =>
2114 [ [ $now - 300 ], 270, [ 'initialMaxage' => 240 ] ],
2115 'A very long time ago' => [ [ $now - 1000000000 ], $wgCdnMaxAge ],
2116 'Initial maxage zero' => [ [ $now - 300 ], 270, [ 'initialMaxage' => 0 ] ],
2117
2118 'false' => [ [ false ], IExpiringStore::TTL_MINUTE ],
2119 'null' => [ [ null ], IExpiringStore::TTL_MINUTE ],
2120 "'0'" => [ [ '0' ], IExpiringStore::TTL_MINUTE ],
2121 'Empty string' => [ [ '' ], IExpiringStore::TTL_MINUTE ],
2122 // @todo These give incorrect results due to timezones, how to test?
2123 //"'now'" => [ [ 'now' ], IExpiringStore::TTL_MINUTE ],
2124 //"'parse error'" => [ [ 'parse error' ], IExpiringStore::TTL_MINUTE ],
2125
2126 'Now, minTTL 0' => [ [ $now, 0 ], IExpiringStore::TTL_MINUTE ],
2127 'Now, minTTL 0.000001' => [ [ $now, 0.000001 ], 0 ],
2128 'A very long time ago, maxTTL even longer' =>
2129 [ [ $now - 1000000000, 0, 1000000001 ], 900000000 ],
2130 ];
2131 }
2132
2133 /**
2134 * @covers OutputPage::enableClientCache
2135 * @covers OutputPage::addParserOutputMetadata
2136 * @covers OutputPage::addParserOutput
2137 */
2138 public function testClientCache() {
2139 $op = $this->newInstance();
2140
2141 // Test initial value
2142 $this->assertSame( true, $op->enableClientCache( null ) );
2143 // Test that calling with null doesn't change the value
2144 $this->assertSame( true, $op->enableClientCache( null ) );
2145
2146 // Test setting to false
2147 $this->assertSame( true, $op->enableClientCache( false ) );
2148 $this->assertSame( false, $op->enableClientCache( null ) );
2149 // Test that calling with null doesn't change the value
2150 $this->assertSame( false, $op->enableClientCache( null ) );
2151
2152 // Test that a cacheable ParserOutput doesn't set to true
2153 $pOutCacheable = $this->createParserOutputStub( 'isCacheable', true );
2154 $op->addParserOutputMetadata( $pOutCacheable );
2155 $this->assertSame( false, $op->enableClientCache( null ) );
2156
2157 // Test setting back to true
2158 $this->assertSame( false, $op->enableClientCache( true ) );
2159 $this->assertSame( true, $op->enableClientCache( null ) );
2160
2161 // Test that an uncacheable ParserOutput does set to false
2162 $pOutUncacheable = $this->createParserOutputStub( 'isCacheable', false );
2163 $op->addParserOutput( $pOutUncacheable );
2164 $this->assertSame( false, $op->enableClientCache( null ) );
2165 }
2166
2167 /**
2168 * @covers OutputPage::getCacheVaryCookies
2169 */
2170 public function testGetCacheVaryCookies() {
2171 global $wgCookiePrefix, $wgDBname;
2172 $op = $this->newInstance();
2173 $prefix = $wgCookiePrefix !== false ? $wgCookiePrefix : $wgDBname;
2174 $expectedCookies = [
2175 "{$prefix}Token",
2176 "{$prefix}LoggedOut",
2177 "{$prefix}_session",
2178 'forceHTTPS',
2179 'cookie1',
2180 'cookie2',
2181 ];
2182
2183 // We have to reset the cookies because getCacheVaryCookies may have already been called
2184 TestingAccessWrapper::newFromClass( OutputPage::class )->cacheVaryCookies = null;
2185
2186 $this->setMwGlobals( 'wgCacheVaryCookies', [ 'cookie1' ] );
2187 $this->setTemporaryHook( 'GetCacheVaryCookies',
2188 function ( $innerOP, &$cookies ) use ( $op, $expectedCookies ) {
2189 $this->assertSame( $op, $innerOP );
2190 $cookies[] = 'cookie2';
2191 $this->assertSame( $expectedCookies, $cookies );
2192 }
2193 );
2194
2195 $this->assertSame( $expectedCookies, $op->getCacheVaryCookies() );
2196 }
2197
2198 /**
2199 * @covers OutputPage::haveCacheVaryCookies
2200 */
2201 public function testHaveCacheVaryCookies() {
2202 $request = new FauxRequest();
2203 $op = $this->newInstance( [], $request );
2204
2205 // No cookies are set.
2206 $this->assertFalse( $op->haveCacheVaryCookies() );
2207
2208 // 'Token' is present but empty, so it shouldn't count.
2209 $request->setCookie( 'Token', '' );
2210 $this->assertFalse( $op->haveCacheVaryCookies() );
2211
2212 // 'Token' present and nonempty.
2213 $request->setCookie( 'Token', '123' );
2214 $this->assertTrue( $op->haveCacheVaryCookies() );
2215 }
2216
2217 /**
2218 * @dataProvider provideVaryHeaders
2219 *
2220 * @covers OutputPage::addVaryHeader
2221 * @covers OutputPage::getVaryHeader
2222 *
2223 * @param array[] $calls For each array, call addVaryHeader() with those arguments
2224 * @param string[] $cookies Array of cookie names to vary on
2225 * @param string $vary Text of expected Vary header (including the 'Vary: ')
2226 */
2227 public function testVaryHeaders( array $calls, array $cookies, $vary ) {
2228 // Get rid of default Vary fields
2229 $op = $this->getMockBuilder( OutputPage::class )
2230 ->setConstructorArgs( [ new RequestContext() ] )
2231 ->setMethods( [ 'getCacheVaryCookies' ] )
2232 ->getMock();
2233 $op->expects( $this->any() )
2234 ->method( 'getCacheVaryCookies' )
2235 ->will( $this->returnValue( $cookies ) );
2236 TestingAccessWrapper::newFromObject( $op )->mVaryHeader = [];
2237
2238 $this->hideDeprecated( 'addVaryHeader $option is ignored' );
2239 foreach ( $calls as $call ) {
2240 $op->addVaryHeader( ...$call );
2241 }
2242 $this->assertEquals( $vary, $op->getVaryHeader(), 'Vary:' );
2243 }
2244
2245 public function provideVaryHeaders() {
2246 return [
2247 'No header' => [
2248 [],
2249 [],
2250 'Vary: ',
2251 ],
2252 'Single header' => [
2253 [
2254 [ 'Cookie' ],
2255 ],
2256 [],
2257 'Vary: Cookie',
2258 ],
2259 'Non-unique headers' => [
2260 [
2261 [ 'Cookie' ],
2262 [ 'Accept-Language' ],
2263 [ 'Cookie' ],
2264 ],
2265 [],
2266 'Vary: Cookie, Accept-Language',
2267 ],
2268 'Two headers with single options' => [
2269 // Options are deprecated since 1.34
2270 [
2271 [ 'Cookie', [ 'param=phpsessid' ] ],
2272 [ 'Accept-Language', [ 'substr=en' ] ],
2273 ],
2274 [],
2275 'Vary: Cookie, Accept-Language',
2276 ],
2277 'One header with multiple options' => [
2278 // Options are deprecated since 1.34
2279 [
2280 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
2281 ],
2282 [],
2283 'Vary: Cookie',
2284 ],
2285 'Duplicate option' => [
2286 // Options are deprecated since 1.34
2287 [
2288 [ 'Cookie', [ 'param=phpsessid' ] ],
2289 [ 'Cookie', [ 'param=phpsessid' ] ],
2290 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
2291 ],
2292 [],
2293 'Vary: Cookie, Accept-Language',
2294 ],
2295 'Same header, different options' => [
2296 // Options are deprecated since 1.34
2297 [
2298 [ 'Cookie', [ 'param=phpsessid' ] ],
2299 [ 'Cookie', [ 'param=userId' ] ],
2300 ],
2301 [],
2302 'Vary: Cookie',
2303 ],
2304 'No header, vary cookies' => [
2305 [],
2306 [ 'cookie1', 'cookie2' ],
2307 'Vary: Cookie',
2308 ],
2309 'Cookie header with option plus vary cookies' => [
2310 // Options are deprecated since 1.34
2311 [
2312 [ 'Cookie', [ 'param=cookie1' ] ],
2313 ],
2314 [ 'cookie2', 'cookie3' ],
2315 'Vary: Cookie',
2316 ],
2317 'Non-cookie header plus vary cookies' => [
2318 [
2319 [ 'Accept-Language' ],
2320 ],
2321 [ 'cookie' ],
2322 'Vary: Accept-Language, Cookie',
2323 ],
2324 'Cookie and non-cookie headers plus vary cookies' => [
2325 // Options are deprecated since 1.34
2326 [
2327 [ 'Cookie', [ 'param=cookie1' ] ],
2328 [ 'Accept-Language' ],
2329 ],
2330 [ 'cookie2' ],
2331 'Vary: Cookie, Accept-Language',
2332 ],
2333 ];
2334 }
2335
2336 /**
2337 * @covers OutputPage::getVaryHeader
2338 */
2339 public function testVaryHeaderDefault() {
2340 $op = $this->newInstance();
2341 $this->assertSame( 'Vary: Accept-Encoding, Cookie', $op->getVaryHeader() );
2342 }
2343
2344 /**
2345 * @dataProvider provideLinkHeaders
2346 *
2347 * @covers OutputPage::addLinkHeader
2348 * @covers OutputPage::getLinkHeader
2349 */
2350 public function testLinkHeaders( array $headers, $result ) {
2351 $op = $this->newInstance();
2352
2353 foreach ( $headers as $header ) {
2354 $op->addLinkHeader( $header );
2355 }
2356
2357 $this->assertEquals( $result, $op->getLinkHeader() );
2358 }
2359
2360 public function provideLinkHeaders() {
2361 return [
2362 [
2363 [],
2364 false
2365 ],
2366 [
2367 [ '<https://foo/bar.jpg>;rel=preload;as=image' ],
2368 'Link: <https://foo/bar.jpg>;rel=preload;as=image',
2369 ],
2370 [
2371 [
2372 '<https://foo/bar.jpg>;rel=preload;as=image',
2373 '<https://foo/baz.jpg>;rel=preload;as=image'
2374 ],
2375 'Link: <https://foo/bar.jpg>;rel=preload;as=image,<https://foo/baz.jpg>;' .
2376 'rel=preload;as=image',
2377 ],
2378 ];
2379 }
2380
2381 /**
2382 * @dataProvider provideAddAcceptLanguage
2383 * @covers OutputPage::addAcceptLanguage
2384 */
2385 public function testAddAcceptLanguage(
2386 $code, array $variants, $expected, array $options = []
2387 ) {
2388 $req = new FauxRequest( in_array( 'varianturl', $options ) ? [ 'variant' => 'x' ] : [] );
2389 $op = $this->newInstance( [], $req, in_array( 'notitle', $options ) ? 'notitle' : null );
2390
2391 if ( !in_array( 'notitle', $options ) ) {
2392 $mockLang = $this->getMock( Language::class );
2393
2394 if ( in_array( 'varianturl', $options ) ) {
2395 $mockLang->expects( $this->never() )->method( $this->anything() );
2396 } else {
2397 $mockLang->method( 'hasVariants' )->willReturn( count( $variants ) > 1 );
2398 $mockLang->method( 'getVariants' )->willReturn( $variants );
2399 $mockLang->method( 'getCode' )->willReturn( $code );
2400 }
2401
2402 $mockTitle = $this->getMock( Title::class );
2403 $mockTitle->method( 'getPageLanguage' )->willReturn( $mockLang );
2404
2405 $op->setTitle( $mockTitle );
2406 }
2407
2408 // This will run addAcceptLanguage()
2409 $op->sendCacheControl();
2410 $this->assertSame( "Vary: $expected", $op->getVaryHeader() );
2411 }
2412
2413 public function provideAddAcceptLanguage() {
2414 return [
2415 'No variants' => [
2416 'en',
2417 [ 'en' ],
2418 'Accept-Encoding, Cookie',
2419 ],
2420 'One simple variant' => [
2421 'en',
2422 [ 'en', 'en-x-piglatin' ],
2423 'Accept-Encoding, Cookie, Accept-Language',
2424 ],
2425 'Multiple variants with BCP47 alternatives' => [
2426 'zh',
2427 [ 'zh', 'zh-hans', 'zh-cn', 'zh-tw' ],
2428 'Accept-Encoding, Cookie, Accept-Language',
2429 ],
2430 'No title' => [
2431 'en',
2432 [ 'en', 'en-x-piglatin' ],
2433 'Accept-Encoding, Cookie',
2434 [ 'notitle' ]
2435 ],
2436 'Variant in URL' => [
2437 'en',
2438 [ 'en', 'en-x-piglatin' ],
2439 'Accept-Encoding, Cookie',
2440 [ 'varianturl' ]
2441 ],
2442 ];
2443 }
2444
2445 /**
2446 * @covers OutputPage::preventClickjacking
2447 * @covers OutputPage::allowClickjacking
2448 * @covers OutputPage::getPreventClickjacking
2449 * @covers OutputPage::addParserOutputMetadata
2450 * @covers OutputPage::addParserOutput
2451 */
2452 public function testClickjacking() {
2453 $op = $this->newInstance();
2454 $this->assertTrue( $op->getPreventClickjacking() );
2455
2456 $op->allowClickjacking();
2457 $this->assertFalse( $op->getPreventClickjacking() );
2458
2459 $op->preventClickjacking();
2460 $this->assertTrue( $op->getPreventClickjacking() );
2461
2462 $op->preventClickjacking( false );
2463 $this->assertFalse( $op->getPreventClickjacking() );
2464
2465 $pOut1 = $this->createParserOutputStub( 'preventClickjacking', true );
2466 $op->addParserOutputMetadata( $pOut1 );
2467 $this->assertTrue( $op->getPreventClickjacking() );
2468
2469 // The ParserOutput can't allow, only prevent
2470 $pOut2 = $this->createParserOutputStub( 'preventClickjacking', false );
2471 $op->addParserOutputMetadata( $pOut2 );
2472 $this->assertTrue( $op->getPreventClickjacking() );
2473
2474 // Reset to test with addParserOutput()
2475 $op->allowClickjacking();
2476 $this->assertFalse( $op->getPreventClickjacking() );
2477
2478 $op->addParserOutput( $pOut1 );
2479 $this->assertTrue( $op->getPreventClickjacking() );
2480
2481 $op->addParserOutput( $pOut2 );
2482 $this->assertTrue( $op->getPreventClickjacking() );
2483 }
2484
2485 /**
2486 * @dataProvider provideGetFrameOptions
2487 * @covers OutputPage::getFrameOptions
2488 * @covers OutputPage::preventClickjacking
2489 */
2490 public function testGetFrameOptions(
2491 $breakFrames, $preventClickjacking, $editPageFrameOptions, $expected
2492 ) {
2493 $op = $this->newInstance( [
2494 'BreakFrames' => $breakFrames,
2495 'EditPageFrameOptions' => $editPageFrameOptions,
2496 ] );
2497 $op->preventClickjacking( $preventClickjacking );
2498
2499 $this->assertSame( $expected, $op->getFrameOptions() );
2500 }
2501
2502 public function provideGetFrameOptions() {
2503 return [
2504 'BreakFrames true' => [ true, false, false, 'DENY' ],
2505 'Allow clickjacking locally' => [ false, false, 'DENY', false ],
2506 'Allow clickjacking globally' => [ false, true, false, false ],
2507 'DENY globally' => [ false, true, 'DENY', 'DENY' ],
2508 'SAMEORIGIN' => [ false, true, 'SAMEORIGIN', 'SAMEORIGIN' ],
2509 'BreakFrames with SAMEORIGIN' => [ true, true, 'SAMEORIGIN', 'DENY' ],
2510 ];
2511 }
2512
2513 /**
2514 * See ResourceLoaderClientHtmlTest for full coverage.
2515 *
2516 * @dataProvider provideMakeResourceLoaderLink
2517 *
2518 * @covers OutputPage::makeResourceLoaderLink
2519 */
2520 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
2521 $this->setMwGlobals( [
2522 'wgResourceLoaderDebug' => false,
2523 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
2524 'wgCSPReportOnlyHeader' => true,
2525 ] );
2526 $class = new ReflectionClass( OutputPage::class );
2527 $method = $class->getMethod( 'makeResourceLoaderLink' );
2528 $method->setAccessible( true );
2529 $ctx = new RequestContext();
2530 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
2531 $ctx->setSkin( $skinFactory->makeSkin( 'fallback' ) );
2532 $ctx->setLanguage( 'en' );
2533 $out = new OutputPage( $ctx );
2534 $nonce = $class->getProperty( 'CSPNonce' );
2535 $nonce->setAccessible( true );
2536 $nonce->setValue( $out, 'secret' );
2537 $rl = $out->getResourceLoader();
2538 $rl->setMessageBlobStore( $this->createMock( MessageBlobStore::class ) );
2539 $rl->register( [
2540 'test.foo' => new ResourceLoaderTestModule( [
2541 'script' => 'mw.test.foo( { a: true } );',
2542 'styles' => '.mw-test-foo { content: "style"; }',
2543 ] ),
2544 'test.bar' => new ResourceLoaderTestModule( [
2545 'script' => 'mw.test.bar( { a: true } );',
2546 'styles' => '.mw-test-bar { content: "style"; }',
2547 ] ),
2548 'test.baz' => new ResourceLoaderTestModule( [
2549 'script' => 'mw.test.baz( { a: true } );',
2550 'styles' => '.mw-test-baz { content: "style"; }',
2551 ] ),
2552 'test.quux' => new ResourceLoaderTestModule( [
2553 'script' => 'mw.test.baz( { token: 123 } );',
2554 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
2555 'group' => 'private',
2556 ] ),
2557 'test.noscript' => new ResourceLoaderTestModule( [
2558 'styles' => '.stuff { color: red; }',
2559 'group' => 'noscript',
2560 ] ),
2561 'test.group.foo' => new ResourceLoaderTestModule( [
2562 'script' => 'mw.doStuff( "foo" );',
2563 'group' => 'foo',
2564 ] ),
2565 'test.group.bar' => new ResourceLoaderTestModule( [
2566 'script' => 'mw.doStuff( "bar" );',
2567 'group' => 'bar',
2568 ] ),
2569 ] );
2570 $links = $method->invokeArgs( $out, $args );
2571 $actualHtml = strval( $links );
2572 $this->assertEquals( $expectedHtml, $actualHtml );
2573 }
2574
2575 public static function provideMakeResourceLoaderLink() {
2576 // phpcs:disable Generic.Files.LineLength
2577 return [
2578 // Single only=scripts load
2579 [
2580 [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
2581 "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
2582 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?lang=en\u0026modules=test.foo\u0026only=scripts");'
2583 . "});</script>"
2584 ],
2585 // Multiple only=styles load
2586 [
2587 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
2588
2589 '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?lang=en&amp;modules=test.bar%2Cbaz%2Cfoo&amp;only=styles"/>'
2590 ],
2591 // Private embed (only=scripts)
2592 [
2593 [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
2594 "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
2595 . "mw.test.baz({token:123});\nmw.loader.state({\"test.quux\":\"ready\"});"
2596 . "});</script>"
2597 ],
2598 // Load private module (combined)
2599 [
2600 [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ],
2601 "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
2602 . "mw.loader.implement(\"test.quux@1ev0ijv\",function($,jQuery,require,module){"
2603 . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}"
2604 . "\"]});});</script>"
2605 ],
2606 // Load no modules
2607 [
2608 [ [], ResourceLoaderModule::TYPE_COMBINED ],
2609 '',
2610 ],
2611 // noscript group
2612 [
2613 [ 'test.noscript', ResourceLoaderModule::TYPE_STYLES ],
2614 '<noscript><link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?lang=en&amp;modules=test.noscript&amp;only=styles"/></noscript>'
2615 ],
2616 // Load two modules in separate groups
2617 [
2618 [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ],
2619 "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
2620 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?lang=en\u0026modules=test.group.bar");'
2621 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?lang=en\u0026modules=test.group.foo");'
2622 . "});</script>"
2623 ],
2624 ];
2625 // phpcs:enable
2626 }
2627
2628 /**
2629 * @dataProvider provideBuildExemptModules
2630 *
2631 * @covers OutputPage::buildExemptModules
2632 */
2633 public function testBuildExemptModules( array $exemptStyleModules, $expect ) {
2634 $this->setMwGlobals( [
2635 'wgResourceLoaderDebug' => false,
2636 'wgLoadScript' => '/w/load.php',
2637 // Stub wgCacheEpoch as it influences getVersionHash used for the
2638 // urls in the expected HTML
2639 'wgCacheEpoch' => '20140101000000',
2640 ] );
2641
2642 // Set up stubs
2643 $ctx = new RequestContext();
2644 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
2645 $ctx->setSkin( $skinFactory->makeSkin( 'fallback' ) );
2646 $ctx->setLanguage( 'en' );
2647 $op = $this->getMockBuilder( OutputPage::class )
2648 ->setConstructorArgs( [ $ctx ] )
2649 ->setMethods( [ 'buildCssLinksArray' ] )
2650 ->getMock();
2651 $op->expects( $this->any() )
2652 ->method( 'buildCssLinksArray' )
2653 ->willReturn( [] );
2654 $rl = $op->getResourceLoader();
2655 $rl->setMessageBlobStore( $this->createMock( MessageBlobStore::class ) );
2656
2657 // Register custom modules
2658 $rl->register( [
2659 'example.site.a' => new ResourceLoaderTestModule( [ 'group' => 'site' ] ),
2660 'example.site.b' => new ResourceLoaderTestModule( [ 'group' => 'site' ] ),
2661 'example.user' => new ResourceLoaderTestModule( [ 'group' => 'user' ] ),
2662 ] );
2663
2664 $op = TestingAccessWrapper::newFromObject( $op );
2665 $op->rlExemptStyleModules = $exemptStyleModules;
2666 $this->assertEquals(
2667 $expect,
2668 strval( $op->buildExemptModules() )
2669 );
2670 }
2671
2672 public static function provideBuildExemptModules() {
2673 // phpcs:disable Generic.Files.LineLength
2674 return [
2675 'empty' => [
2676 'exemptStyleModules' => [],
2677 '<meta name="ResourceLoaderDynamicStyles" content=""/>',
2678 ],
2679 'empty sets' => [
2680 'exemptStyleModules' => [ 'site' => [], 'noscript' => [], 'private' => [], 'user' => [] ],
2681 '<meta name="ResourceLoaderDynamicStyles" content=""/>',
2682 ],
2683 'default logged-out' => [