5 * @author Matthew Flaschen
9 * @todo factor tests in this class into providers and test methods
11 class OutputPageTest
extends MediaWikiTestCase
{
12 const SCREEN_MEDIA_QUERY
= 'screen and (min-width: 982px)';
13 const SCREEN_ONLY_MEDIA_QUERY
= 'only screen and (min-width: 982px)';
16 * @covers OutputPage::addMeta
17 * @covers OutputPage::getMetaTags
18 * @covers OutputPage::getHeadLinksArray
20 public function testMetaTags() {
21 $outputPage = $this->newInstance();
22 $outputPage->addMeta( 'http:expires', '0' );
23 $outputPage->addMeta( 'keywords', 'first' );
24 $outputPage->addMeta( 'keywords', 'second' );
25 $outputPage->addMeta( 'og:title', 'Ta-duh' );
28 [ 'http:expires', '0' ],
29 [ 'keywords', 'first' ],
30 [ 'keywords', 'second' ],
31 [ 'og:title', 'Ta-duh' ],
33 $this->assertSame( $expected, $outputPage->getMetaTags() );
35 $links = $outputPage->getHeadLinksArray();
36 $this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
37 $this->assertContains( '<meta name="keywords" content="first"/>', $links );
38 $this->assertContains( '<meta name="keywords" content="second"/>', $links );
39 $this->assertContains( '<meta property="og:title" content="Ta-duh"/>', $links );
40 $this->assertArrayNotHasKey( 'meta-robots', $links );
44 * @covers OutputPage::setIndexPolicy
45 * @covers OutputPage::setFollowPolicy
46 * @covers OutputPage::getHeadLinksArray
48 public function testRobotsPolicies() {
49 $outputPage = $this->newInstance();
50 $outputPage->setIndexPolicy( 'noindex' );
51 $outputPage->setFollowPolicy( 'nofollow' );
53 $links = $outputPage->getHeadLinksArray();
54 $this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
58 * Tests a particular case of transformCssMedia, using the given input, globals,
59 * expected return, and message
61 * Asserts that $expectedReturn is returned.
63 * options['printableQuery'] - value of query string for printable, or omitted for none
64 * options['handheldQuery'] - value of query string for handheld, or omitted for none
65 * options['media'] - passed into the method under the same name
66 * options['expectedReturn'] - expected return value
67 * options['message'] - PHPUnit message for assertion
69 * @param array $args Key-value array of arguments as shown above
71 protected function assertTransformCssMediaCase( $args ) {
73 if ( isset( $args['printableQuery'] ) ) {
74 $queryData['printable'] = $args['printableQuery'];
77 if ( isset( $args['handheldQuery'] ) ) {
78 $queryData['handheld'] = $args['handheldQuery'];
81 $fauxRequest = new FauxRequest( $queryData, false );
82 $this->setMwGlobals( [
83 'wgRequest' => $fauxRequest,
86 $actualReturn = OutputPage
::transformCssMedia( $args['media'] );
87 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
91 * Tests print requests
92 * @covers OutputPage::transformCssMedia
94 public function testPrintRequests() {
95 $this->assertTransformCssMediaCase( [
96 'printableQuery' => '1',
98 'expectedReturn' => null,
99 'message' => 'On printable request, screen returns null'
102 $this->assertTransformCssMediaCase( [
103 'printableQuery' => '1',
104 'media' => self
::SCREEN_MEDIA_QUERY
,
105 'expectedReturn' => null,
106 'message' => 'On printable request, screen media query returns null'
109 $this->assertTransformCssMediaCase( [
110 'printableQuery' => '1',
111 'media' => self
::SCREEN_ONLY_MEDIA_QUERY
,
112 'expectedReturn' => null,
113 'message' => 'On printable request, screen media query with only returns null'
116 $this->assertTransformCssMediaCase( [
117 'printableQuery' => '1',
119 'expectedReturn' => '',
120 'message' => 'On printable request, media print returns empty string'
125 * Tests screen requests, without either query parameter set
126 * @covers OutputPage::transformCssMedia
128 public function testScreenRequests() {
129 $this->assertTransformCssMediaCase( [
131 'expectedReturn' => 'screen',
132 'message' => 'On screen request, screen media type is preserved'
135 $this->assertTransformCssMediaCase( [
136 'media' => 'handheld',
137 'expectedReturn' => 'handheld',
138 'message' => 'On screen request, handheld media type is preserved'
141 $this->assertTransformCssMediaCase( [
142 'media' => self
::SCREEN_MEDIA_QUERY
,
143 'expectedReturn' => self
::SCREEN_MEDIA_QUERY
,
144 'message' => 'On screen request, screen media query is preserved.'
147 $this->assertTransformCssMediaCase( [
148 'media' => self
::SCREEN_ONLY_MEDIA_QUERY
,
149 'expectedReturn' => self
::SCREEN_ONLY_MEDIA_QUERY
,
150 'message' => 'On screen request, screen media query with only is preserved.'
153 $this->assertTransformCssMediaCase( [
155 'expectedReturn' => 'print',
156 'message' => 'On screen request, print media type is preserved'
161 * Tests handheld behavior
162 * @covers OutputPage::transformCssMedia
164 public function testHandheld() {
165 $this->assertTransformCssMediaCase( [
166 'handheldQuery' => '1',
167 'media' => 'handheld',
168 'expectedReturn' => '',
169 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
172 $this->assertTransformCssMediaCase( [
173 'handheldQuery' => '1',
175 'expectedReturn' => null,
176 'message' => 'On request with handheld querystring and media is screen, returns null'
180 public static function provideTransformFilePath() {
181 $baseDir = dirname( __DIR__
) . '/data/media';
183 // File that matches basePath, and exists. Hash found and appended.
184 [ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/test.jpg', '/w/test.jpg?edcf2' ],
185 // File that matches basePath, but not found on disk. Empty query.
186 [ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/unknown.png', '/w/unknown.png?' ],
187 // File not matching basePath. Ignored.
188 [ 'baseDir' => $baseDir, 'basePath' => '/w', '/files/test.jpg' ],
189 // Empty string. Ignored.
190 [ 'baseDir' => $baseDir, 'basePath' => '/w', '', '' ],
191 // Similar path, but with domain component. Ignored.
192 [ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/w/test.jpg' ],
193 [ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/w/test.jpg' ],
194 // Unrelated path with domain component. Ignored.
195 [ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/files/test.jpg' ],
196 [ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/files/test.jpg' ],
197 // Unrelated path with domain, and empty base path (root mw install). Ignored.
198 [ 'baseDir' => $baseDir, 'basePath' => '', 'https://example.org/files/test.jpg' ],
199 [ 'baseDir' => $baseDir, 'basePath' => '', '//example.org/files/test.jpg' ], // T155310
204 * @dataProvider provideTransformFilePath
205 * @covers OutputPage::transformFilePath
206 * @covers OutputPage::transformResourcePath
208 public function testTransformResourcePath( $baseDir, $basePath, $path, $expected = null ) {
209 $this->setMwGlobals( 'IP', $baseDir );
210 $conf = new HashConfig( [ 'ResourceBasePath' => $basePath ] );
212 MediaWiki\
suppressWarnings();
213 $actual = OutputPage
::transformResourcePath( $conf, $path );
214 MediaWiki\restoreWarnings
();
216 $this->assertEquals( $expected ?
: $path, $actual );
219 public static function provideMakeResourceLoaderLink() {
220 // @codingStandardsIgnoreStart Generic.Files.LineLength
222 // Single only=scripts load
224 [ 'test.foo', ResourceLoaderModule
::TYPE_SCRIPTS
],
225 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
226 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
229 // Multiple only=styles load
231 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule
::TYPE_STYLES
],
233 '<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"/>'
235 // Private embed (only=scripts)
237 [ 'test.quux', ResourceLoaderModule
::TYPE_SCRIPTS
],
238 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
239 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
243 // @codingStandardsIgnoreEnd
247 * See ResourceLoaderClientHtmlTest for full coverage.
249 * @dataProvider provideMakeResourceLoaderLink
250 * @covers OutputPage::makeResourceLoaderLink
252 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
253 $this->setMwGlobals( [
254 'wgResourceLoaderDebug' => false,
255 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
257 $class = new ReflectionClass( 'OutputPage' );
258 $method = $class->getMethod( 'makeResourceLoaderLink' );
259 $method->setAccessible( true );
260 $ctx = new RequestContext();
261 $ctx->setSkin( SkinFactory
::getDefaultInstance()->makeSkin( 'fallback' ) );
262 $ctx->setLanguage( 'en' );
263 $out = new OutputPage( $ctx );
264 $rl = $out->getResourceLoader();
265 $rl->setMessageBlobStore( new NullMessageBlobStore() );
267 'test.foo' => new ResourceLoaderTestModule( [
268 'script' => 'mw.test.foo( { a: true } );',
269 'styles' => '.mw-test-foo { content: "style"; }',
271 'test.bar' => new ResourceLoaderTestModule( [
272 'script' => 'mw.test.bar( { a: true } );',
273 'styles' => '.mw-test-bar { content: "style"; }',
275 'test.baz' => new ResourceLoaderTestModule( [
276 'script' => 'mw.test.baz( { a: true } );',
277 'styles' => '.mw-test-baz { content: "style"; }',
279 'test.quux' => new ResourceLoaderTestModule( [
280 'script' => 'mw.test.baz( { token: 123 } );',
281 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
282 'group' => 'private',
285 $links = $method->invokeArgs( $out, $args );
286 $actualHtml = strval( $links );
287 $this->assertEquals( $expectedHtml, $actualHtml );
291 * @dataProvider provideVaryHeaders
292 * @covers OutputPage::addVaryHeader
293 * @covers OutputPage::getVaryHeader
294 * @covers OutputPage::getKeyHeader
296 public function testVaryHeaders( $calls, $vary, $key ) {
297 // get rid of default Vary fields
298 $outputPage = $this->getMockBuilder( 'OutputPage' )
299 ->setConstructorArgs( [ new RequestContext() ] )
300 ->setMethods( [ 'getCacheVaryCookies' ] )
302 $outputPage->expects( $this->any() )
303 ->method( 'getCacheVaryCookies' )
304 ->will( $this->returnValue( [] ) );
305 TestingAccessWrapper
::newFromObject( $outputPage )->mVaryHeader
= [];
307 foreach ( $calls as $call ) {
308 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
310 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
311 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
314 public function provideVaryHeaders() {
315 // note: getKeyHeader() automatically adds Vary: Cookie
324 [ // non-unique headers
327 [ 'Accept-Language' ],
330 'Vary: Cookie, Accept-Language',
331 'Key: Cookie,Accept-Language',
333 [ // two headers with single options
335 [ 'Cookie', [ 'param=phpsessid' ] ],
336 [ 'Accept-Language', [ 'substr=en' ] ],
338 'Vary: Cookie, Accept-Language',
339 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
341 [ // one header with multiple options
343 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
346 'Key: Cookie;param=phpsessid;param=userId',
348 [ // Duplicate option
350 [ 'Cookie', [ 'param=phpsessid' ] ],
351 [ 'Cookie', [ 'param=phpsessid' ] ],
352 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
354 'Vary: Cookie, Accept-Language',
355 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
357 [ // Same header, different options
359 [ 'Cookie', [ 'param=phpsessid' ] ],
360 [ 'Cookie', [ 'param=userId' ] ],
363 'Key: Cookie;param=phpsessid;param=userId',
369 * @covers OutputPage::haveCacheVaryCookies
371 public function testHaveCacheVaryCookies() {
372 $request = new FauxRequest();
373 $context = new RequestContext();
374 $context->setRequest( $request );
375 $outputPage = new OutputPage( $context );
377 // No cookies are set.
378 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
380 // 'Token' is present but empty, so it shouldn't count.
381 $request->setCookie( 'Token', '' );
382 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
384 // 'Token' present and nonempty.
385 $request->setCookie( 'Token', '123' );
386 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
390 * @covers OutputPage::addCategoryLinks
391 * @covers OutputPage::getCategories
393 public function testGetCategories() {
394 $fakeResultWrapper = new FakeResultWrapper( [
397 'page_title' => 'Test'
400 'page_title' => 'Test2'
403 $outputPage = $this->getMockBuilder( 'OutputPage' )
404 ->setConstructorArgs( [ new RequestContext() ] )
405 ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
407 $outputPage->expects( $this->any() )
408 ->method( 'addCategoryLinksToLBAndGetResult' )
409 ->will( $this->returnValue( $fakeResultWrapper ) );
411 $outputPage->addCategoryLinks( [
415 $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
416 $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
417 $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
423 private function newInstance() {
424 $context = new RequestContext();
426 $context->setConfig( new HashConfig( [
427 'AppleTouchIcon' => false,
428 'DisableLangConversion' => true,
429 'EnableAPI' => false,
430 'EnableCanonicalServerLink' => false,
433 'LanguageCode' => false,
434 'ReferrerPolicy' => false,
435 'RightsPage' => false,
436 'RightsUrl' => false,
437 'UniversalEditButton' => false,
440 return new OutputPage( $context );
445 * MessageBlobStore that doesn't do anything
447 class NullMessageBlobStore
extends MessageBlobStore
{
448 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
452 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
456 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
459 public function updateMessage( $key ) {
462 public function clear() {