5 * @author Matthew Flaschen
10 * @todo factor tests in this class into providers and test methods
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)';
17 * @covers OutputPage::addMeta
18 * @covers OutputPage::getMetaTags
19 * @covers OutputPage::getHeadLinksArray
21 public function testMetaTags() {
22 $outputPage = $this->newInstance();
23 $outputPage->addMeta( 'http:expires', '0' );
24 $outputPage->addMeta( 'keywords', 'first' );
25 $outputPage->addMeta( 'keywords', 'second' );
26 $outputPage->addMeta( 'og:title', 'Ta-duh' );
29 [ 'http:expires', '0' ],
30 [ 'keywords', 'first' ],
31 [ 'keywords', 'second' ],
32 [ 'og:title', 'Ta-duh' ],
34 $this->assertSame( $expected, $outputPage->getMetaTags() );
36 $links = $outputPage->getHeadLinksArray();
37 $this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
38 $this->assertContains( '<meta name="keywords" content="first"/>', $links );
39 $this->assertContains( '<meta name="keywords" content="second"/>', $links );
40 $this->assertContains( '<meta property="og:title" content="Ta-duh"/>', $links );
41 $this->assertArrayNotHasKey( 'meta-robots', $links );
45 * @covers OutputPage::setIndexPolicy
46 * @covers OutputPage::setFollowPolicy
47 * @covers OutputPage::getHeadLinksArray
49 public function testRobotsPolicies() {
50 $outputPage = $this->newInstance();
51 $outputPage->setIndexPolicy( 'noindex' );
52 $outputPage->setFollowPolicy( 'nofollow' );
54 $links = $outputPage->getHeadLinksArray();
55 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
59 * Tests a particular case of transformCssMedia, using the given input, globals,
60 * expected return, and message
62 * Asserts that $expectedReturn is returned.
64 * options['printableQuery'] - value of query string for printable, or omitted for none
65 * options['handheldQuery'] - value of query string for handheld, or omitted for none
66 * options['media'] - passed into the method under the same name
67 * options['expectedReturn'] - expected return value
68 * options['message'] - PHPUnit message for assertion
70 * @param array $args Key-value array of arguments as shown above
72 protected function assertTransformCssMediaCase( $args ) {
74 if ( isset( $args['printableQuery'] ) ) {
75 $queryData['printable'] = $args['printableQuery'];
78 if ( isset( $args['handheldQuery'] ) ) {
79 $queryData['handheld'] = $args['handheldQuery'];
82 $fauxRequest = new FauxRequest( $queryData, false );
83 $this->setMwGlobals( [
84 'wgRequest' => $fauxRequest,
87 $actualReturn = OutputPage
::transformCssMedia( $args['media'] );
88 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
92 * Tests print requests
93 * @covers OutputPage::transformCssMedia
95 public function testPrintRequests() {
96 $this->assertTransformCssMediaCase( [
97 'printableQuery' => '1',
99 'expectedReturn' => null,
100 'message' => 'On printable request, screen returns null'
103 $this->assertTransformCssMediaCase( [
104 'printableQuery' => '1',
105 'media' => self
::SCREEN_MEDIA_QUERY
,
106 'expectedReturn' => null,
107 'message' => 'On printable request, screen media query returns null'
110 $this->assertTransformCssMediaCase( [
111 'printableQuery' => '1',
112 'media' => self
::SCREEN_ONLY_MEDIA_QUERY
,
113 'expectedReturn' => null,
114 'message' => 'On printable request, screen media query with only returns null'
117 $this->assertTransformCssMediaCase( [
118 'printableQuery' => '1',
120 'expectedReturn' => '',
121 'message' => 'On printable request, media print returns empty string'
126 * Tests screen requests, without either query parameter set
127 * @covers OutputPage::transformCssMedia
129 public function testScreenRequests() {
130 $this->assertTransformCssMediaCase( [
132 'expectedReturn' => 'screen',
133 'message' => 'On screen request, screen media type is preserved'
136 $this->assertTransformCssMediaCase( [
137 'media' => 'handheld',
138 'expectedReturn' => 'handheld',
139 'message' => 'On screen request, handheld media type is preserved'
142 $this->assertTransformCssMediaCase( [
143 'media' => self
::SCREEN_MEDIA_QUERY
,
144 'expectedReturn' => self
::SCREEN_MEDIA_QUERY
,
145 'message' => 'On screen request, screen media query is preserved.'
148 $this->assertTransformCssMediaCase( [
149 'media' => self
::SCREEN_ONLY_MEDIA_QUERY
,
150 'expectedReturn' => self
::SCREEN_ONLY_MEDIA_QUERY
,
151 'message' => 'On screen request, screen media query with only is preserved.'
154 $this->assertTransformCssMediaCase( [
156 'expectedReturn' => 'print',
157 'message' => 'On screen request, print media type is preserved'
162 * Tests handheld behavior
163 * @covers OutputPage::transformCssMedia
165 public function testHandheld() {
166 $this->assertTransformCssMediaCase( [
167 'handheldQuery' => '1',
168 'media' => 'handheld',
169 'expectedReturn' => '',
170 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
173 $this->assertTransformCssMediaCase( [
174 'handheldQuery' => '1',
176 'expectedReturn' => null,
177 'message' => 'On request with handheld querystring and media is screen, returns null'
181 public static function provideTransformFilePath() {
182 $baseDir = dirname( __DIR__
) . '/data/media';
184 // File that matches basePath, and exists. Hash found and appended.
186 'baseDir' => $baseDir, 'basePath' => '/w',
190 // File that matches basePath, but not found on disk. Empty query.
192 'baseDir' => $baseDir, 'basePath' => '/w',
196 // File not matching basePath. Ignored.
198 'baseDir' => $baseDir, 'basePath' => '/w',
201 // Empty string. Ignored.
203 'baseDir' => $baseDir, 'basePath' => '/w',
207 // Similar path, but with domain component. Ignored.
209 'baseDir' => $baseDir, 'basePath' => '/w',
210 '//example.org/w/test.jpg'
213 'baseDir' => $baseDir, 'basePath' => '/w',
214 'https://example.org/w/test.jpg'
216 // Unrelated path with domain component. Ignored.
218 'baseDir' => $baseDir, 'basePath' => '/w',
219 'https://example.org/files/test.jpg'
222 'baseDir' => $baseDir, 'basePath' => '/w',
223 '//example.org/files/test.jpg'
225 // Unrelated path with domain, and empty base path (root mw install). Ignored.
227 'baseDir' => $baseDir, 'basePath' => '',
228 'https://example.org/files/test.jpg'
231 'baseDir' => $baseDir, 'basePath' => '',
233 '//example.org/files/test.jpg'
235 // Check UploadPath before ResourceBasePath (T155146)
237 'baseDir' => dirname( $baseDir ), 'basePath' => '',
238 'uploadDir' => $baseDir, 'uploadPath' => '/images',
240 '/images/test.jpg?edcf2'
246 * @dataProvider provideTransformFilePath
247 * @covers OutputPage::transformFilePath
248 * @covers OutputPage::transformResourcePath
250 public function testTransformResourcePath( $baseDir, $basePath, $uploadDir = null,
251 $uploadPath = null, $path = null, $expected = null
253 if ( $path === null ) {
254 // Skip optional $uploadDir and $uploadPath
256 $expected = $uploadPath;
257 $uploadDir = "$baseDir/images";
258 $uploadPath = "$basePath/images";
260 $this->setMwGlobals( 'IP', $baseDir );
261 $conf = new HashConfig( [
262 'ResourceBasePath' => $basePath,
263 'UploadDirectory' => $uploadDir,
264 'UploadPath' => $uploadPath,
267 MediaWiki\
suppressWarnings();
268 $actual = OutputPage
::transformResourcePath( $conf, $path );
269 MediaWiki\restoreWarnings
();
271 $this->assertEquals( $expected ?
: $path, $actual );
274 public static function provideMakeResourceLoaderLink() {
275 // @codingStandardsIgnoreStart Generic.Files.LineLength
277 // Single only=scripts load
279 [ 'test.foo', ResourceLoaderModule
::TYPE_SCRIPTS
],
280 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
281 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
284 // Multiple only=styles load
286 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule
::TYPE_STYLES
],
288 '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.bar%2Cbaz%2Cfoo&only=styles&skin=fallback"/>'
290 // Private embed (only=scripts)
292 [ 'test.quux', ResourceLoaderModule
::TYPE_SCRIPTS
],
293 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
294 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
298 // @codingStandardsIgnoreEnd
302 * See ResourceLoaderClientHtmlTest for full coverage.
304 * @dataProvider provideMakeResourceLoaderLink
305 * @covers OutputPage::makeResourceLoaderLink
307 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
308 $this->setMwGlobals( [
309 'wgResourceLoaderDebug' => false,
310 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
312 $class = new ReflectionClass( 'OutputPage' );
313 $method = $class->getMethod( 'makeResourceLoaderLink' );
314 $method->setAccessible( true );
315 $ctx = new RequestContext();
316 $ctx->setSkin( SkinFactory
::getDefaultInstance()->makeSkin( 'fallback' ) );
317 $ctx->setLanguage( 'en' );
318 $out = new OutputPage( $ctx );
319 $rl = $out->getResourceLoader();
320 $rl->setMessageBlobStore( new NullMessageBlobStore() );
322 'test.foo' => new ResourceLoaderTestModule( [
323 'script' => 'mw.test.foo( { a: true } );',
324 'styles' => '.mw-test-foo { content: "style"; }',
326 'test.bar' => new ResourceLoaderTestModule( [
327 'script' => 'mw.test.bar( { a: true } );',
328 'styles' => '.mw-test-bar { content: "style"; }',
330 'test.baz' => new ResourceLoaderTestModule( [
331 'script' => 'mw.test.baz( { a: true } );',
332 'styles' => '.mw-test-baz { content: "style"; }',
334 'test.quux' => new ResourceLoaderTestModule( [
335 'script' => 'mw.test.baz( { token: 123 } );',
336 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
337 'group' => 'private',
340 $links = $method->invokeArgs( $out, $args );
341 $actualHtml = strval( $links );
342 $this->assertEquals( $expectedHtml, $actualHtml );
346 * @dataProvider provideVaryHeaders
347 * @covers OutputPage::addVaryHeader
348 * @covers OutputPage::getVaryHeader
349 * @covers OutputPage::getKeyHeader
351 public function testVaryHeaders( $calls, $vary, $key ) {
352 // get rid of default Vary fields
353 $outputPage = $this->getMockBuilder( 'OutputPage' )
354 ->setConstructorArgs( [ new RequestContext() ] )
355 ->setMethods( [ 'getCacheVaryCookies' ] )
357 $outputPage->expects( $this->any() )
358 ->method( 'getCacheVaryCookies' )
359 ->will( $this->returnValue( [] ) );
360 TestingAccessWrapper
::newFromObject( $outputPage )->mVaryHeader
= [];
362 foreach ( $calls as $call ) {
363 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
365 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
366 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
369 public function provideVaryHeaders() {
370 // note: getKeyHeader() automatically adds Vary: Cookie
379 [ // non-unique headers
382 [ 'Accept-Language' ],
385 'Vary: Cookie, Accept-Language',
386 'Key: Cookie,Accept-Language',
388 [ // two headers with single options
390 [ 'Cookie', [ 'param=phpsessid' ] ],
391 [ 'Accept-Language', [ 'substr=en' ] ],
393 'Vary: Cookie, Accept-Language',
394 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
396 [ // one header with multiple options
398 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
401 'Key: Cookie;param=phpsessid;param=userId',
403 [ // Duplicate option
405 [ 'Cookie', [ 'param=phpsessid' ] ],
406 [ 'Cookie', [ 'param=phpsessid' ] ],
407 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
409 'Vary: Cookie, Accept-Language',
410 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
412 [ // Same header, different options
414 [ 'Cookie', [ 'param=phpsessid' ] ],
415 [ 'Cookie', [ 'param=userId' ] ],
418 'Key: Cookie;param=phpsessid;param=userId',
424 * @covers OutputPage::haveCacheVaryCookies
426 public function testHaveCacheVaryCookies() {
427 $request = new FauxRequest();
428 $context = new RequestContext();
429 $context->setRequest( $request );
430 $outputPage = new OutputPage( $context );
432 // No cookies are set.
433 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
435 // 'Token' is present but empty, so it shouldn't count.
436 $request->setCookie( 'Token', '' );
437 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
439 // 'Token' present and nonempty.
440 $request->setCookie( 'Token', '123' );
441 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
445 * @covers OutputPage::addCategoryLinks
446 * @covers OutputPage::getCategories
448 public function testGetCategories() {
449 $fakeResultWrapper = new FakeResultWrapper( [
452 'page_title' => 'Test'
455 'page_title' => 'Test2'
458 $outputPage = $this->getMockBuilder( 'OutputPage' )
459 ->setConstructorArgs( [ new RequestContext() ] )
460 ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
462 $outputPage->expects( $this->any() )
463 ->method( 'addCategoryLinksToLBAndGetResult' )
464 ->will( $this->returnValue( $fakeResultWrapper ) );
466 $outputPage->addCategoryLinks( [
470 $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
471 $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
472 $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
478 private function newInstance() {
479 $context = new RequestContext();
481 $context->setConfig( new HashConfig( [
482 'AppleTouchIcon' => false,
483 'DisableLangConversion' => true,
484 'EnableAPI' => false,
485 'EnableCanonicalServerLink' => false,
488 'LanguageCode' => false,
489 'ReferrerPolicy' => false,
490 'RightsPage' => false,
491 'RightsUrl' => false,
492 'UniversalEditButton' => false,
495 return new OutputPage( $context );
500 * MessageBlobStore that doesn't do anything
502 class NullMessageBlobStore
extends MessageBlobStore
{
503 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
507 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
511 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
514 public function updateMessage( $key ) {
517 public function clear() {