5 * @author Matthew Flaschen
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)';
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 ) {
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( array(
42 'wgRequest' => $fauxRequest,
45 $actualReturn = OutputPage
::transformCssMedia( $args['media'] );
46 $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
50 * Tests print requests
51 * @covers OutputPage::transformCssMedia
53 public function testPrintRequests() {
54 $this->assertTransformCssMediaCase( array(
55 'printableQuery' => '1',
57 'expectedReturn' => null,
58 'message' => 'On printable request, screen returns null'
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'
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'
75 $this->assertTransformCssMediaCase( array(
76 'printableQuery' => '1',
78 'expectedReturn' => '',
79 'message' => 'On printable request, media print returns empty string'
84 * Tests screen requests, without either query parameter set
85 * @covers OutputPage::transformCssMedia
87 public function testScreenRequests() {
88 $this->assertTransformCssMediaCase( array(
90 'expectedReturn' => 'screen',
91 'message' => 'On screen request, screen media type is preserved'
94 $this->assertTransformCssMediaCase( array(
95 'media' => 'handheld',
96 'expectedReturn' => 'handheld',
97 'message' => 'On screen request, handheld media type is preserved'
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.'
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.'
112 $this->assertTransformCssMediaCase( array(
114 'expectedReturn' => 'print',
115 'message' => 'On screen request, print media type is preserved'
120 * Tests handheld behavior
121 * @covers OutputPage::transformCssMedia
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'
131 $this->assertTransformCssMediaCase( array(
132 'handheldQuery' => '1',
134 'expectedReturn' => null,
135 'message' => 'On request with handheld querystring and media is screen, returns null'
139 public static function provideMakeResourceLoaderLink() {
141 // Load module script only
143 array( 'test.foo', ResourceLoaderModule
::TYPE_SCRIPTS
),
144 "<script>var RLQ = RLQ || []; RLQ.push( function () {\n"
145 . 'document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?'
146 . 'debug=false\u0026amp;lang=en\u0026amp;modules=test.foo\u0026amp;only'
147 . '=scripts\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");'
151 // Don't condition wrap raw modules (like the startup module)
152 array( 'test.raw', ResourceLoaderModule
::TYPE_SCRIPTS
),
153 '<script src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.raw&only=scripts&skin=fallback&*"></script>'
155 // Load module styles only
156 // This also tests the order the modules are put into the url
158 array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule
::TYPE_STYLES
),
160 '<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&*">'
162 // Load private module (only=scripts)
164 array( 'test.quux', ResourceLoaderModule
::TYPE_SCRIPTS
),
165 "<script>var RLQ = RLQ || []; RLQ.push( function () {\n"
166 . "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});\n"
169 // Load private module (combined)
171 array( 'test.quux', ResourceLoaderModule
::TYPE_COMBINED
),
172 "<script>var RLQ = RLQ || []; RLQ.push( function () {\n"
173 . "mw.loader.implement(\"test.quux\",function($,jQuery){"
174 . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}\\n"
175 . "\"]});\n\n} );</script>"
177 // Load module script with ESI
179 array( 'test.foo', ResourceLoaderModule
::TYPE_SCRIPTS
, true ),
180 '<script><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=scripts&skin=fallback&*" /></script>'
182 // Load module styles with ESI
184 array( 'test.foo', ResourceLoaderModule
::TYPE_STYLES
, true ),
185 '<style><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=styles&skin=fallback&*" /></style>',
189 array( array(), ResourceLoaderModule
::TYPE_COMBINED
),
194 array( 'test.noscript', ResourceLoaderModule
::TYPE_STYLES
),
195 '<noscript><link rel=stylesheet href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.noscript&only=styles&skin=fallback&*"></noscript>'
197 // Load two modules in separate groups
199 array( array( 'test.group.foo', 'test.group.bar' ), ResourceLoaderModule
::TYPE_COMBINED
),
200 "<script>var RLQ = RLQ || []; RLQ.push( function () {\n"
201 . 'document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.group.bar\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");'
202 . "\n} );</script>\n"
203 . "<script>var RLQ = RLQ || []; RLQ.push( function () {\n"
204 . 'document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.group.foo\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");'
211 * @dataProvider provideMakeResourceLoaderLink
212 * @covers OutputPage::makeResourceLoaderLink
213 * @covers ResourceLoader::makeLoaderImplementScript
214 * @covers ResourceLoader::makeModuleResponse
215 * @covers ResourceLoader::makeInlineScript
216 * @covers ResourceLoader::makeLoaderStateScript
217 * @covers ResourceLoader::createLoaderURL
219 public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
220 $this->setMwGlobals( array(
221 'wgResourceLoaderDebug' => false,
222 'wgResourceLoaderUseESI' => true,
223 'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
224 // Affects whether CDATA is inserted
225 'wgWellFormedXml' => false,
227 $class = new ReflectionClass( 'OutputPage' );
228 $method = $class->getMethod( 'makeResourceLoaderLink' );
229 $method->setAccessible( true );
230 $ctx = new RequestContext();
231 $ctx->setSkin( SkinFactory
::getDefaultInstance()->makeSkin( 'fallback' ) );
232 $ctx->setLanguage( 'en' );
233 $out = new OutputPage( $ctx );
234 $rl = $out->getResourceLoader();
235 $rl->setMessageBlobStore( new NullMessageBlobStore() );
236 $rl->register( array(
237 'test.foo' => new ResourceLoaderTestModule( array(
238 'script' => 'mw.test.foo( { a: true } );',
239 'styles' => '.mw-test-foo { content: "style"; }',
241 'test.bar' => new ResourceLoaderTestModule( array(
242 'script' => 'mw.test.bar( { a: true } );',
243 'styles' => '.mw-test-bar { content: "style"; }',
245 'test.baz' => new ResourceLoaderTestModule( array(
246 'script' => 'mw.test.baz( { a: true } );',
247 'styles' => '.mw-test-baz { content: "style"; }',
249 'test.quux' => new ResourceLoaderTestModule( array(
250 'script' => 'mw.test.baz( { token: 123 } );',
251 'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
252 'group' => 'private',
254 'test.raw' => new ResourceLoaderTestModule( array(
255 'script' => 'mw.test.baz( { token: 123 } );',
258 'test.noscript' => new ResourceLoaderTestModule( array(
259 'styles' => '.mw-test-noscript { content: "style"; }',
260 'group' => 'noscript',
262 'test.group.bar' => new ResourceLoaderTestModule( array(
263 'styles' => '.mw-group-bar { content: "style"; }',
266 'test.group.foo' => new ResourceLoaderTestModule( array(
267 'styles' => '.mw-group-foo { content: "style"; }',
271 $links = $method->invokeArgs( $out, $args );
272 // Strip comments to avoid variation due to wgDBname in WikiID and cache key
273 $actualHtml = preg_replace( '#/\*[^*]+\*/#', '', implode( "\n", $links['html'] ) );
274 $this->assertEquals( $expectedHtml, $actualHtml );
279 * MessageBlobStore that doesn't do anything
281 class NullMessageBlobStore
extends MessageBlobStore
{
282 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
286 public function insertMessageBlob( $name, ResourceLoaderModule
$module, $lang ) {
290 public function updateModule( $name, ResourceLoaderModule
$module, $lang ) {
294 public function updateMessage( $key ) {
296 public function clear() {