2013-01-14 03:26:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Matthew Flaschen
|
|
|
|
|
*
|
|
|
|
|
* @group Output
|
|
|
|
|
*
|
2013-10-24 19:35:04 +00:00
|
|
|
* @todo factor tests in this class into providers and test methods
|
|
|
|
|
*
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
class OutputPageTest extends MediaWikiTestCase {
|
|
|
|
|
const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
|
|
|
|
|
const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests a particular case of transformCssMedia, using the given input, globals,
|
|
|
|
|
* expected return, and message
|
|
|
|
|
*
|
|
|
|
|
* Asserts that $expectedReturn is returned.
|
|
|
|
|
*
|
|
|
|
|
* options['printableQuery'] - value of query string for printable, or omitted for none
|
|
|
|
|
* options['handheldQuery'] - value of query string for handheld, or omitted for none
|
|
|
|
|
* options['media'] - passed into the method under the same name
|
|
|
|
|
* options['expectedReturn'] - expected return value
|
|
|
|
|
* options['message'] - PHPUnit message for assertion
|
|
|
|
|
*
|
|
|
|
|
* @param array $args key-value array of arguments as shown above
|
|
|
|
|
*/
|
2013-02-14 11:22:13 +00:00
|
|
|
protected function assertTransformCssMediaCase( $args ) {
|
2013-01-14 03:26:15 +00:00
|
|
|
$queryData = array();
|
|
|
|
|
if ( isset( $args['printableQuery'] ) ) {
|
|
|
|
|
$queryData['printable'] = $args['printableQuery'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $args['handheldQuery'] ) ) {
|
|
|
|
|
$queryData['handheld'] = $args['handheldQuery'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fauxRequest = new FauxRequest( $queryData, false );
|
|
|
|
|
$this->setMWGlobals( array(
|
2013-02-14 11:22:13 +00:00
|
|
|
'wgRequest' => $fauxRequest,
|
2013-01-14 03:26:15 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$actualReturn = OutputPage::transformCssMedia( $args['media'] );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests print requests
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testPrintRequests() {
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen returns null'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen media query returns null'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen media query with only returns null'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => 'print',
|
|
|
|
|
'expectedReturn' => '',
|
|
|
|
|
'message' => 'On printable request, media print returns empty string'
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests screen requests, without either query parameter set
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testScreenRequests() {
|
|
|
|
|
$this->assertTransformCssMediaCase( array(
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => 'screen',
|
2013-05-11 19:05:43 +00:00
|
|
|
'message' => 'On screen request, screen media type is preserved'
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$this->assertTransformCssMediaCase( array(
|
|
|
|
|
'media' => 'handheld',
|
|
|
|
|
'expectedReturn' => 'handheld',
|
|
|
|
|
'message' => 'On screen request, handheld media type is preserved'
|
2013-01-14 03:26:15 +00:00
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'message' => 'On screen request, screen media query is preserved.'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'message' => 'On screen request, screen media query with only is preserved.'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => 'print',
|
|
|
|
|
'expectedReturn' => 'print',
|
|
|
|
|
'message' => 'On screen request, print media type is preserved'
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-11 19:05:43 +00:00
|
|
|
* Tests handheld behavior
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testHandheld() {
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'handheldQuery' => '1',
|
|
|
|
|
'media' => 'handheld',
|
|
|
|
|
'expectedReturn' => '',
|
|
|
|
|
'message' => 'On request with handheld querystring and media is handheld, returns empty string'
|
|
|
|
|
) );
|
|
|
|
|
|
2013-05-11 19:05:43 +00:00
|
|
|
$this->assertTransformCssMediaCase( array(
|
2013-01-14 03:26:15 +00:00
|
|
|
'handheldQuery' => '1',
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On request with handheld querystring and media is screen, returns null'
|
|
|
|
|
) );
|
|
|
|
|
}
|
2014-06-28 20:40:22 +00:00
|
|
|
|
|
|
|
|
public static function provideMakeResourceLoaderLink() {
|
|
|
|
|
return array(
|
|
|
|
|
// Load module script only
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ),
|
|
|
|
|
'<script src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=scripts&skin=vector&*"></script>
|
|
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load module styles only
|
|
|
|
|
// This also tests the order the modules are put into the url
|
|
|
|
|
array(
|
|
|
|
|
array( array( 'test.baz', 'test.foo', 'test.bar' ), ResourceLoaderModule::TYPE_STYLES ),
|
|
|
|
|
'<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=vector&*">
|
|
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load private module (combined)
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.quux', ResourceLoaderModule::TYPE_COMBINED ),
|
|
|
|
|
'<script>if(window.mw){
|
|
|
|
|
mw.loader.implement("test.quux",function($,jQuery){mw.test.baz({token:123});},{"css":[".mw-icon{transition:none}\n/* cache key: wiki:resourceloader:filter:minify-css:7:fd8ea20b3336b2bfb230c789d430067a */"]},{});
|
|
|
|
|
/* cache key: wiki:resourceloader:filter:minify-js:7:274ccee17be73cd5f4dda5dc2a819188 */
|
|
|
|
|
}</script>
|
|
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load module script with with ESI
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS, true ),
|
|
|
|
|
'<script><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=scripts&skin=vector&*" /></script>
|
|
|
|
|
'
|
|
|
|
|
),
|
|
|
|
|
// Load module styles with with ESI
|
|
|
|
|
array(
|
|
|
|
|
array( 'test.foo', ResourceLoaderModule::TYPE_STYLES, true ),
|
|
|
|
|
'<style><esi:include src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=styles&skin=vector&*" /></style>
|
|
|
|
|
',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMakeResourceLoaderLink
|
|
|
|
|
* @covers OutputPage::makeResourceLoaderLink
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeResourceLoaderLink( $args, $expectedHtml) {
|
|
|
|
|
$this->setMwGlobals( array(
|
2014-07-11 14:35:14 +00:00
|
|
|
'wgResourceLoaderDebug' => false,
|
2014-06-28 20:40:22 +00:00
|
|
|
'wgResourceLoaderUseESI' => true,
|
|
|
|
|
'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
|
|
|
|
|
// Affects whether CDATA is inserted
|
|
|
|
|
'wgWellFormedXml' => false,
|
|
|
|
|
// Cache key is based on database name, and affects output;
|
|
|
|
|
// this test should not touch the database anyways.
|
|
|
|
|
'wgDBname' => 'wiki',
|
|
|
|
|
'wgDBprefix' => '',
|
|
|
|
|
) );
|
|
|
|
|
$class = new ReflectionClass( 'OutputPage' );
|
|
|
|
|
$method = $class->getMethod( 'makeResourceLoaderLink' );
|
|
|
|
|
$method->setAccessible( true );
|
|
|
|
|
$ctx = new RequestContext();
|
2014-07-11 14:35:14 +00:00
|
|
|
$ctx->setLanguage( 'en' );
|
2014-06-28 20:40:22 +00:00
|
|
|
$out = new OutputPage( $ctx );
|
|
|
|
|
$rl = $out->getResourceLoader();
|
|
|
|
|
$rl->register( array(
|
|
|
|
|
'test.foo' => new ResourceLoaderTestModule(array(
|
|
|
|
|
'script' => 'mw.test.foo( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-foo { content: "style"; }',
|
|
|
|
|
)),
|
|
|
|
|
'test.bar' => new ResourceLoaderTestModule(array(
|
|
|
|
|
'script' => 'mw.test.bar( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-bar { content: "style"; }',
|
|
|
|
|
)),
|
|
|
|
|
'test.baz' => new ResourceLoaderTestModule(array(
|
|
|
|
|
'script' => 'mw.test.baz( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-baz { content: "style"; }',
|
|
|
|
|
)),
|
|
|
|
|
'test.quux' => new ResourceLoaderTestModule(array(
|
|
|
|
|
'script' => 'mw.test.baz( { token: 123 } );',
|
|
|
|
|
'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
|
|
|
|
|
'group' => 'private',
|
|
|
|
|
)),
|
|
|
|
|
) );
|
|
|
|
|
$links = $method->invokeArgs( $out, $args );
|
|
|
|
|
$this->assertEquals( $expectedHtml, $links['html'] );
|
|
|
|
|
}
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|