89d1de7bf31a164c7a75b62e89cc566635d8f937
[lhc/web/wiklou.git] / tests / phpunit / includes / OutputPageTest.php
1 <?php
2
3 /**
4 *
5 * @author Matthew Flaschen
6 *
7 * @group Output
8 *
9 * @todo factor tests in this class into providers and test methods
10 *
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 /**
17 * Tests a particular case of transformCssMedia, using the given input, globals,
18 * expected return, and message
19 *
20 * Asserts that $expectedReturn is returned.
21 *
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
27 *
28 * @param array $args Key-value array of arguments as shown above
29 */
30 protected function assertTransformCssMediaCase( $args ) {
31 $queryData = array();
32 if ( isset( $args['printableQuery'] ) ) {
33 $queryData['printable'] = $args['printableQuery'];
34 }
35
36 if ( isset( $args['handheldQuery'] ) ) {
37 $queryData['handheld'] = $args['handheldQuery'];
38 }
39
40 $fauxRequest = new FauxRequest( $queryData, false );
41 $this->setMwGlobals( array(
42 'wgRequest' => $fauxRequest,
43 ) );
44
45 $actualReturn = OutputPage::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
47 }
48
49 /**
50 * Tests print requests
51 * @covers OutputPage::transformCssMedia
52 */
53 public function testPrintRequests() {
54 $this->assertTransformCssMediaCase( array(
55 'printableQuery' => '1',
56 'media' => 'screen',
57 'expectedReturn' => null,
58 'message' => 'On printable request, screen returns null'
59 ) );
60
61 $this->assertTransformCssMediaCase( array(
62 'printableQuery' => '1',
63 'media' => self::SCREEN_MEDIA_QUERY,
64 'expectedReturn' => null,
65 'message' => 'On printable request, screen media query returns null'
66 ) );
67
68 $this->assertTransformCssMediaCase( array(
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 ) );
74
75 $this->assertTransformCssMediaCase( array(
76 'printableQuery' => '1',
77 'media' => 'print',
78 'expectedReturn' => '',
79 'message' => 'On printable request, media print returns empty string'
80 ) );
81 }
82
83 /**
84 * Tests screen requests, without either query parameter set
85 * @covers OutputPage::transformCssMedia
86 */
87 public function testScreenRequests() {
88 $this->assertTransformCssMediaCase( array(
89 'media' => 'screen',
90 'expectedReturn' => 'screen',
91 'message' => 'On screen request, screen media type is preserved'
92 ) );
93
94 $this->assertTransformCssMediaCase( array(
95 'media' => 'handheld',
96 'expectedReturn' => 'handheld',
97 'message' => 'On screen request, handheld media type is preserved'
98 ) );
99
100 $this->assertTransformCssMediaCase( array(
101 'media' => self::SCREEN_MEDIA_QUERY,
102 'expectedReturn' => self::SCREEN_MEDIA_QUERY,
103 'message' => 'On screen request, screen media query is preserved.'
104 ) );
105
106 $this->assertTransformCssMediaCase( array(
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 ) );
111
112 $this->assertTransformCssMediaCase( array(
113 'media' => 'print',
114 'expectedReturn' => 'print',
115 'message' => 'On screen request, print media type is preserved'
116 ) );
117 }
118
119 /**
120 * Tests handheld behavior
121 * @covers OutputPage::transformCssMedia
122 */
123 public function testHandheld() {
124 $this->assertTransformCssMediaCase( array(
125 'handheldQuery' => '1',
126 'media' => 'handheld',
127 'expectedReturn' => '',
128 'message' => 'On request with handheld querystring and media is handheld, returns empty string'
129 ) );
130
131 $this->assertTransformCssMediaCase( array(
132 'handheldQuery' => '1',
133 'media' => 'screen',
134 'expectedReturn' => null,
135 'message' => 'On request with handheld querystring and media is screen, returns null'
136 ) );
137 }
138
139 public static function provideMakeResourceLoaderLink() {
140 return array(
141 // Load module script only
142 array(
143 array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ),
144 '<script>if(window.mw){
145 document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.foo\u0026amp;only=scripts\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");
146 }</script>
147 '
148 ),
149 array(
150 // Don't condition wrap raw modules (like the startup module)
151 array( 'test.raw', ResourceLoaderModule::TYPE_SCRIPTS ),
152 '<script src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.raw&amp;only=scripts&amp;skin=fallback&amp;*"></script>
153 '
154 ),
155 // Load module styles only
156 // This also tests the order the modules are put into the url
157 array(
158 array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule::TYPE_STYLES ),
159 '<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&amp;*">
160 '
161 ),
162 // Load private module (only=scripts)
163 array(
164 array( 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ),
165 '<script>if(window.mw){
166 mw.test.baz({token:123});mw.loader.state({"test.quux":"ready"});
167
168 }</script>
169 '
170 ),
171 // Load private module (combined)
172 array(
173 array( 'test.quux', ResourceLoaderModule::TYPE_COMBINED ),
174 '<script>if(window.mw){
175 mw.loader.implement("test.quux",function($,jQuery){mw.test.baz({token:123});},{"css":[".mw-icon{transition:none}\n"]},{},{});
176
177 }</script>
178 '
179 ),
180 // Load module script with with ESI
181 array(
182 array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS, true ),
183 '<script><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.foo&amp;only=scripts&amp;skin=fallback&amp;*" /></script>
184 '
185 ),
186 // Load module styles with with ESI
187 array(
188 array( 'test.foo', ResourceLoaderModule::TYPE_STYLES, true ),
189 '<style><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.foo&amp;only=styles&amp;skin=fallback&amp;*" /></style>
190 ',
191 ),
192 // Load no modules
193 array(
194 array( array(), ResourceLoaderModule::TYPE_COMBINED ),
195 '',
196 ),
197 // noscript group
198 array(
199 array( 'test.noscript', ResourceLoaderModule::TYPE_STYLES ),
200 '<noscript><link rel=stylesheet href="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.noscript&amp;only=styles&amp;skin=fallback&amp;*"></noscript>
201 '
202 ),
203 // Load two modules in separate groups
204 array(
205 array( array( 'test.group.foo', 'test.group.bar' ), ResourceLoaderModule::TYPE_COMBINED ),
206 '<script src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.group.bar&amp;skin=fallback&amp;*"></script>
207 <script src="http://127.0.0.1:8080/w/load.php?debug=false&amp;lang=en&amp;modules=test.group.foo&amp;skin=fallback&amp;*"></script>
208 ',
209 ),
210 );
211 }
212
213 /**
214 * @dataProvider provideMakeResourceLoaderLink
215 * @covers OutputPage::makeResourceLoaderLink
216 */
217 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
218 $this->setMwGlobals( array(
219 'wgResourceLoaderDebug' => false,
220 'wgResourceLoaderUseESI' => true,
221 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
222 // Affects whether CDATA is inserted
223 'wgWellFormedXml' => false,
224 ) );
225 $class = new ReflectionClass( 'OutputPage' );
226 $method = $class->getMethod( 'makeResourceLoaderLink' );
227 $method->setAccessible( true );
228 $ctx = new RequestContext();
229 $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
230 $ctx->setLanguage( 'en' );
231 $out = new OutputPage( $ctx );
232 $rl = $out->getResourceLoader();
233 $rl->register( array(
234 'test.foo' => new ResourceLoaderTestModule( array(
235 'script' => 'mw.test.foo( { a: true } );',
236 'styles' => '.mw-test-foo { content: "style"; }',
237 )),
238 'test.bar' => new ResourceLoaderTestModule( array(
239 'script' => 'mw.test.bar( { a: true } );',
240 'styles' => '.mw-test-bar { content: "style"; }',
241 )),
242 'test.baz' => new ResourceLoaderTestModule( array(
243 'script' => 'mw.test.baz( { a: true } );',
244 'styles' => '.mw-test-baz { content: "style"; }',
245 )),
246 'test.quux' => new ResourceLoaderTestModule( array(
247 'script' => 'mw.test.baz( { token: 123 } );',
248 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
249 'group' => 'private',
250 )),
251 'test.raw' => new ResourceLoaderTestModule( array(
252 'script' => 'mw.test.baz( { token: 123 } );',
253 'isRaw' => true,
254 )),
255 'test.noscript' => new ResourceLoaderTestModule( array(
256 'styles' => '.mw-test-noscript { content: "style"; }',
257 'group' => 'noscript',
258 )),
259 'test.group.bar' => new ResourceLoaderTestModule( array(
260 'styles' => '.mw-group-bar { content: "style"; }',
261 'group' => 'bar',
262 )),
263 'test.group.foo' => new ResourceLoaderTestModule( array(
264 'styles' => '.mw-group-foo { content: "style"; }',
265 'group' => 'foo',
266 )),
267 ) );
268 $links = $method->invokeArgs( $out, $args );
269 // Strip comments to avoid variation due to wgDBname in WikiID and cache key
270 $actualHtml = preg_replace( '#/\*[^*]+\*/#', '', $links['html'] );
271 $this->assertEquals( $expectedHtml, $actualHtml );
272 }
273 }