Fix DatabaseSqlite IDEA warnings
[mediawiki.git] / tests / phpunit / includes / OutputPageTest.php
blobc637d34a5ae92465e144536ee2d9be804081125d
1 <?php
3 /**
5 * @author Matthew Flaschen
7 * @group Output
9 * @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)';
16 /**
17 * Tests a particular case of transformCssMedia, using the given input, globals,
18 * expected return, and message
20 * Asserts that $expectedReturn is returned.
22 * options['printableQuery'] - value of query string for printable, or omitted for none
23 * options['handheldQuery'] - value of query string for handheld, or omitted for none
24 * options['media'] - passed into the method under the same name
25 * options['expectedReturn'] - expected return value
26 * options['message'] - PHPUnit message for assertion
28 * @param array $args Key-value array of arguments as shown above
30 protected function assertTransformCssMediaCase( $args ) {
31 $queryData = [];
32 if ( isset( $args['printableQuery'] ) ) {
33 $queryData['printable'] = $args['printableQuery'];
36 if ( isset( $args['handheldQuery'] ) ) {
37 $queryData['handheld'] = $args['handheldQuery'];
40 $fauxRequest = new FauxRequest( $queryData, false );
41 $this->setMwGlobals( [
42 'wgRequest' => $fauxRequest,
43 ] );
45 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
49 /**
50 * Tests print requests
51 * @covers OutputPage::transformCssMedia
53 public function testPrintRequests() {
54 $this->assertTransformCssMediaCase( [
55 'printableQuery' => '1',
56 'media' => 'screen',
57 'expectedReturn' => null,
58 'message' => 'On printable request, screen returns null'
59 ] );
61 $this->assertTransformCssMediaCase( [
62 'printableQuery' => '1',
63 'media' => self::SCREEN_MEDIA_QUERY,
64 'expectedReturn' => null,
65 'message' => 'On printable request, screen media query returns null'
66 ] );
68 $this->assertTransformCssMediaCase( [
69 'printableQuery' => '1',
70 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
71 'expectedReturn' => null,
72 'message' => 'On printable request, screen media query with only returns null'
73 ] );
75 $this->assertTransformCssMediaCase( [
76 'printableQuery' => '1',
77 'media' => 'print',
78 'expectedReturn' => '',
79 'message' => 'On printable request, media print returns empty string'
80 ] );
83 /**
84 * Tests screen requests, without either query parameter set
85 * @covers OutputPage::transformCssMedia
87 public function testScreenRequests() {
88 $this->assertTransformCssMediaCase( [
89 'media' => 'screen',
90 'expectedReturn' => 'screen',
91 'message' => 'On screen request, screen media type is preserved'
92 ] );
94 $this->assertTransformCssMediaCase( [
95 'media' => 'handheld',
96 'expectedReturn' => 'handheld',
97 'message' => 'On screen request, handheld media type is preserved'
98 ] );
100 $this->assertTransformCssMediaCase( [
101 'media' => self::SCREEN_MEDIA_QUERY,
102 'expectedReturn' => self::SCREEN_MEDIA_QUERY,
103 'message' => 'On screen request, screen media query is preserved.'
104 ] );
106 $this->assertTransformCssMediaCase( [
107 'media' => self::SCREEN_ONLY_MEDIA_QUERY,
108 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
109 'message' => 'On screen request, screen media query with only is preserved.'
110 ] );
112 $this->assertTransformCssMediaCase( [
113 'media' => 'print',
114 'expectedReturn' => 'print',
115 'message' => 'On screen request, print media type is preserved'
116 ] );
120 * Tests handheld behavior
121 * @covers OutputPage::transformCssMedia
123 public function testHandheld() {
124 $this->assertTransformCssMediaCase( [
125 'handheldQuery' => '1',
126 'media' => 'handheld',
127 'expectedReturn' => '',
128 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
129 ] );
131 $this->assertTransformCssMediaCase( [
132 'handheldQuery' => '1',
133 'media' => 'screen',
134 'expectedReturn' => null,
135 'message' => 'On request with handheld querystring and media is screen, returns null'
136 ] );
139 public static function provideMakeResourceLoaderLink() {
140 // @codingStandardsIgnoreStart Generic.Files.LineLength
141 return [
142 // Single only=scripts load
144 [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
145 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
146 . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
147 . "});</script>"
149 // Multiple only=styles load
151 [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
153 '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.bar%2Cbaz%2Cfoo&amp;only=styles&amp;skin=fallback"/>'
155 // Private embed (only=scripts)
157 [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
158 "<script>(window.RLQ=window.RLQ||[]).push(function(){"
159 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
160 . "});</script>"
163 // @codingStandardsIgnoreEnd
167 * See ResourceLoaderClientHtmlTest for full coverage.
169 * @dataProvider provideMakeResourceLoaderLink
170 * @covers OutputPage::makeResourceLoaderLink
172 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
173 $this->setMwGlobals( [
174 'wgResourceLoaderDebug' => false,
175 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
176 ] );
177 $class = new ReflectionClass( 'OutputPage' );
178 $method = $class->getMethod( 'makeResourceLoaderLink' );
179 $method->setAccessible( true );
180 $ctx = new RequestContext();
181 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
182 $ctx->setLanguage( 'en' );
183 $out = new OutputPage( $ctx );
184 $rl = $out->getResourceLoader();
185 $rl->setMessageBlobStore( new NullMessageBlobStore() );
186 $rl->register( [
187 'test.foo' => new ResourceLoaderTestModule( [
188 'script' => 'mw.test.foo( { a: true } );',
189 'styles' => '.mw-test-foo { content: "style"; }',
190 ] ),
191 'test.bar' => new ResourceLoaderTestModule( [
192 'script' => 'mw.test.bar( { a: true } );',
193 'styles' => '.mw-test-bar { content: "style"; }',
194 ] ),
195 'test.baz' => new ResourceLoaderTestModule( [
196 'script' => 'mw.test.baz( { a: true } );',
197 'styles' => '.mw-test-baz { content: "style"; }',
198 ] ),
199 'test.quux' => new ResourceLoaderTestModule( [
200 'script' => 'mw.test.baz( { token: 123 } );',
201 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
202 'group' => 'private',
203 ] ),
204 ] );
205 $links = $method->invokeArgs( $out, $args );
206 $actualHtml = strval( $links );
207 $this->assertEquals( $expectedHtml, $actualHtml );
211 * @dataProvider provideVaryHeaders
212 * @covers OutputPage::addVaryHeader
213 * @covers OutputPage::getVaryHeader
214 * @covers OutputPage::getKeyHeader
216 public function testVaryHeaders( $calls, $vary, $key ) {
217 // get rid of default Vary fields
218 $outputPage = $this->getMockBuilder( 'OutputPage' )
219 ->setConstructorArgs( [ new RequestContext() ] )
220 ->setMethods( [ 'getCacheVaryCookies' ] )
221 ->getMock();
222 $outputPage->expects( $this->any() )
223 ->method( 'getCacheVaryCookies' )
224 ->will( $this->returnValue( [] ) );
225 TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = [];
227 foreach ( $calls as $call ) {
228 call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
230 $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
231 $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
234 public function provideVaryHeaders() {
235 // note: getKeyHeader() automatically adds Vary: Cookie
236 return [
237 [ // single header
239 [ 'Cookie' ],
241 'Vary: Cookie',
242 'Key: Cookie',
244 [ // non-unique headers
246 [ 'Cookie' ],
247 [ 'Accept-Language' ],
248 [ 'Cookie' ],
250 'Vary: Cookie, Accept-Language',
251 'Key: Cookie,Accept-Language',
253 [ // two headers with single options
255 [ 'Cookie', [ 'param=phpsessid' ] ],
256 [ 'Accept-Language', [ 'substr=en' ] ],
258 'Vary: Cookie, Accept-Language',
259 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
261 [ // one header with multiple options
263 [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
265 'Vary: Cookie',
266 'Key: Cookie;param=phpsessid;param=userId',
268 [ // Duplicate option
270 [ 'Cookie', [ 'param=phpsessid' ] ],
271 [ 'Cookie', [ 'param=phpsessid' ] ],
272 [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
274 'Vary: Cookie, Accept-Language',
275 'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
277 [ // Same header, different options
279 [ 'Cookie', [ 'param=phpsessid' ] ],
280 [ 'Cookie', [ 'param=userId' ] ],
282 'Vary: Cookie',
283 'Key: Cookie;param=phpsessid;param=userId',
289 * @covers OutputPage::haveCacheVaryCookies
291 function testHaveCacheVaryCookies() {
292 $request = new FauxRequest();
293 $context = new RequestContext();
294 $context->setRequest( $request );
295 $outputPage = new OutputPage( $context );
297 // No cookies are set.
298 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
300 // 'Token' is present but empty, so it shouldn't count.
301 $request->setCookie( 'Token', '' );
302 $this->assertFalse( $outputPage->haveCacheVaryCookies() );
304 // 'Token' present and nonempty.
305 $request->setCookie( 'Token', '123' );
306 $this->assertTrue( $outputPage->haveCacheVaryCookies() );
311 * MessageBlobStore that doesn't do anything
313 class NullMessageBlobStore extends MessageBlobStore {
314 public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
315 return [];
318 public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
319 return false;
322 public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
325 public function updateMessage( $key ) {
328 public function clear() {