mediawiki-extensions-Gadgets/tests/phpunit/unit/GadgetTest.php

353 lines
13 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\Extension\Gadgets\Gadget;
use MediaWiki\ResourceLoader\Module;
use MediaWiki\User\User;
/**
* @covers \MediaWiki\Extension\Gadgets\Gadget
* @covers \MediaWiki\Extension\Gadgets\GadgetResourceLoaderModule
* @covers \MediaWiki\Extension\Gadgets\MediaWikiGadgetsDefinitionRepo
* @group Gadgets
*/
class GadgetTest extends MediaWikiUnitTestCase {
use GadgetTestTrait;
public function testToArray() {
$g = $this->makeGadget( '*bar[ResourceLoader|rights=test]|bar.js|foo.css | foo.json' );
$gNewFromSerialized = new Gadget( $g->toArray() );
$this->assertArrayEquals( $g->toArray(), $gNewFromSerialized->toArray() );
}
public function testGadgetFromDefinitionContent() {
$gArray = Gadget::serializeDefinition( 'bar', [
'settings' => [
'rights' => [],
'default' => true,
'package' => true,
'hidden' => false,
'actions' => [],
'skins' => [],
'namespaces' => [],
'categories' => [],
'contentModels' => [],
'category' => 'misc',
'supportsUrlLoad' => false,
'requiresES6' => false,
],
'module' => [
'pages' => [ 'foo.js', 'bar.css' ],
'datas' => [],
'dependencies' => [ 'moment' ],
'peers' => [],
'messages' => [ 'blanknamespace' ],
'type' => 'general',
]
] );
$g = new Gadget( $gArray );
$this->assertTrue( $g->isOnByDefault() );
$this->assertTrue( $g->isPackaged() );
$this->assertFalse( $g->isHidden() );
$this->assertFalse( $g->supportsUrlLoad() );
$this->assertTrue( $g->supportsResourceLoader() );
$this->assertCount( 1, $g->getScripts() );
$this->assertCount( 1, $g->getStyles() );
$this->assertCount( 0, $g->getJSONs() );
$this->assertCount( 1, $g->getDependencies() );
$this->assertCount( 1, $g->getMessages() );
// Ensure parity and internal consistency
// between Gadget::serializeDefinition and Gadget::toArray
$arr = $g->toArray();
unset( $arr['definition'] );
$this->assertSame( $arr, $gArray );
}
public function testInvalidLines() {
$this->assertFalse( $this->makeGadget( '' ) );
$this->assertFalse( $this->makeGadget( '<foo|bar>' ) );
}
public function testSimpleCases() {
$g = $this->makeGadget( '* foo bar| foo.css|foo.js|foo.json|foo.bar' );
$this->assertEquals( 'foo_bar', $g->getName() );
$this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.json' ], $g->getJSONs() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css', 'MediaWiki:Gadget-foo.json' ],
$g->getScriptsAndStyles() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() );
$this->assertFalse( $g->supportsResourceLoader() );
$this->assertTrue( $g->hasModule() );
}
public function testRLtag() {
$g = $this->makeGadget( '*foo [ResourceLoader]|foo.js|foo.css' );
$this->assertEquals( 'foo', $g->getName() );
$this->assertTrue( $g->supportsResourceLoader() );
$this->assertCount( 0, $g->getLegacyScripts() );
}
public function testPackaged() {
$g = $this->makeGadget( '* foo bar[ResourceLoader|package]| foo.css|foo.js|foo.bar|foo.json' );
$this->assertEquals( 'foo_bar', $g->getName() );
$this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.json' ], $g->getJSONs() );
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css', 'MediaWiki:Gadget-foo.json' ],
$g->getScriptsAndStyles() );
$this->assertEquals( [], $g->getLegacyScripts() );
$this->assertTrue( $g->supportsResourceLoader() );
$this->assertTrue( $g->hasModule() );
}
public function testSupportsUrlLoad() {
$directLoadAllowedByDefault = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$directLoadAllowed1 = $this->makeGadget( '*bar[ResourceLoader|supportsUrlLoad]|bar.js' );
$directLoadAllowed2 = $this->makeGadget( '*bar[ResourceLoader|supportsUrlLoad=true]|bar.js' );
$directLoadNotAllowed = $this->makeGadget( '*baz[ResourceLoader|supportsUrlLoad=false]|baz.js' );
$this->assertFalse( $directLoadAllowedByDefault->supportsUrlLoad() );
$this->assertTrue( $directLoadAllowed1->supportsUrlLoad() );
$this->assertTrue( $directLoadAllowed2->supportsUrlLoad() );
$this->assertFalse( $directLoadNotAllowed->supportsUrlLoad() );
}
public function testIsAllowed() {
$user = $this->getMockBuilder( User::class )
->onlyMethods( [ 'isAllowedAll' ] )
->getMock();
$user->method( 'isAllowedAll' )
->willReturnCallback(
static function ( ...$rights ) {
return array_diff( $rights, [ 'test' ] ) === [];
}
);
/** @var User $user */
$gUnset = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$gAllowed = $this->makeGadget( '*bar[ResourceLoader|rights=test]|bar.js' );
$gNotAllowed = $this->makeGadget( '*baz[ResourceLoader|rights=nope]|baz.js' );
$this->assertTrue( $gUnset->isAllowed( $user ) );
$this->assertTrue( $gAllowed->isAllowed( $user ) );
$this->assertFalse( $gNotAllowed->isAllowed( $user ) );
}
public function testSkinsTag() {
$gUnset = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$gSkinSupported = $this->makeGadget( '*bar[ResourceLoader|skins=fallback]|bar.js' );
$gSkinNotSupported = $this->makeGadget( '*baz[ResourceLoader|skins=bar]|baz.js' );
$skin = new SkinFallback();
$this->assertTrue( $gUnset->isSkinSupported( $skin ) );
$this->assertTrue( $gSkinSupported->isSkinSupported( $skin ) );
$this->assertFalse( $gSkinNotSupported->isSkinSupported( $skin ) );
}
public function testActionsTag() {
$gUnset = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$gActionSupported = $this->makeGadget( '*bar[ResourceLoader|actions=edit]|bar.js' );
$gActionNotSupported = $this->makeGadget( '*baz[ResourceLoader|actions=history]|baz.js' );
$this->assertTrue( $gUnset->isActionSupported( 'edit' ) );
$this->assertTrue( $gActionSupported->isActionSupported( 'edit' ) );
$this->assertFalse( $gActionNotSupported->isActionSupported( 'edit' ) );
// special case
$this->assertTrue( $gActionSupported->isActionSupported( 'submit' ) );
$gMultiActions = $this->makeGadget( '*bar[ResourceLoader|actions=unknown,history]|bar.js' );
$this->assertTrue( $gMultiActions->isActionSupported( 'history' ) );
$this->assertFalse( $gMultiActions->isActionSupported( 'view' ) );
}
public function testNamespacesTag() {
$gUnsetNamespace = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$gNamespace0 = $this->makeGadget( '*bar[ResourceLoader|namespaces=0]|bar.js' );
$gNamespace1 = $this->makeGadget( '*bar[ResourceLoader|namespaces=1]|bar.js' );
$gMultiNamespace = $this->makeGadget( '*bar[ResourceLoader|namespaces=1,2,3,4]|bar.js' );
$this->assertTrue( $gUnsetNamespace->isNamespaceSupported( 5 ) );
$this->assertTrue( $gNamespace0->isNamespaceSupported( 0 ) );
$this->assertFalse( $gNamespace0->isNamespaceSupported( 2 ) );
$this->assertTrue( $gNamespace1->isNamespaceSupported( 1 ) );
$this->assertFalse( $gNamespace1->isNamespaceSupported( 2 ) );
$this->assertTrue( $gMultiNamespace->isNamespaceSupported( 1 ) );
$this->assertTrue( $gMultiNamespace->isNamespaceSupported( 2 ) );
$this->assertFalse( $gMultiNamespace->isNamespaceSupported( 5 ) );
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
$this->assertSame( [ '1', '2', '3', '4' ], $gMultiNamespace->getRequiredNamespaces() );
}
public function testCategoriesTag() {
$gUnsetCategory = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$gCategoryFoo = $this->makeGadget( '*foo[ResourceLoader|categories=Foo]|foo.js' );
$gMultiCategory = $this->makeGadget( '*foo[ResourceLoader|categories=Foo,Bar baz,quux]|foo.js' );
$this->assertTrue( $gUnsetCategory->isCategorySupported( [ 'Foo' ] ) );
$this->assertTrue( $gCategoryFoo->isCategorySupported( [ 'Foo' ] ) );
$this->assertFalse( $gCategoryFoo->isCategorySupported( [ 'Bar' ] ) );
$this->assertFalse( $gCategoryFoo->isCategorySupported( [ 'Bar baz' ] ) );
$this->assertTrue( $gMultiCategory->isCategorySupported( [ 'Foo' ] ) );
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Bar' ] ) );
$this->assertTrue( $gMultiCategory->isCategorySupported( [ 'Spam', 'Bar baz', 'Eggs' ] ) );
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Spam', 'Eggs' ] ) );
// Load condition must use title text form
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'foo' ] ) );
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Bar_baz' ] ) );
// Definition must use title text form, too
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Quux' ] ) );
}
public function testContentModelsTags() {
$gUnsetModel = $this->makeGadget( '*foo[ResourceLoader]|foo.js' );
$gModelWikitext = $this->makeGadget( '*bar[ResourceLoader|contentModels=wikitext]|bar.js' );
$gModelCode = $this->makeGadget( '*bar[ResourceLoader|contentModels=javascript,css]|bar.js' );
$this->assertTrue( $gUnsetModel->isContentModelSupported( 'wikitext' ) );
$this->assertTrue( $gModelWikitext->isContentModelSupported( 'wikitext' ) );
$this->assertFalse( $gModelWikitext->isContentModelSupported( 'javascript' ) );
$this->assertTrue( $gModelCode->isContentModelSupported( 'javascript' ) );
$this->assertTrue( $gModelCode->isContentModelSupported( 'css' ) );
$this->assertFalse( $gModelCode->isContentModelSupported( 'wikitext' ) );
}
public function testDependencies() {
$g = $this->makeGadget( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
$this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() );
$this->assertTrue( $g->supportsResourceLoader() );
$this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() );
}
public function testES6() {
$es6gadget = $this->makeGadget( '* foo[ResourceLoader|requiresES6]|bar.js' );
$es5gadget = $this->makeGadget( '* foo[ResourceLoader]|bar.js' );
$this->assertTrue( $es6gadget->requiresES6() );
$this->assertFalse( $es5gadget->requiresES6() );
}
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
public static function provideGetType() {
return [
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'Default (mixed)',
'* foo[ResourceLoader]|bar.css|bar.js',
'general',
Module::LOAD_GENERAL,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'Default (styles only)',
'* foo[ResourceLoader]|bar.css',
'styles',
Module::LOAD_STYLES,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'Default (scripts only)',
'* foo[ResourceLoader]|bar.js',
'general',
Module::LOAD_GENERAL,
],
[
'Default (styles only with dependencies)',
'* foo[ResourceLoader|dependencies=jquery.ui]|bar.css',
'general',
Module::LOAD_GENERAL,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'Styles type (mixed)',
'* foo[ResourceLoader|type=styles]|bar.css|bar.js',
'styles',
Module::LOAD_STYLES,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'Styles type (styles only)',
'* foo[ResourceLoader|type=styles]|bar.css',
'styles',
Module::LOAD_STYLES,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'Styles type (scripts only)',
'* foo[ResourceLoader|type=styles]|bar.js',
'styles',
Module::LOAD_STYLES,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'General type (mixed)',
'* foo[ResourceLoader|type=general]|bar.css|bar.js',
'general',
Module::LOAD_GENERAL,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'General type (styles only)',
'* foo[ResourceLoader|type=general]|bar.css',
'general',
Module::LOAD_GENERAL,
],
[
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'General type (scripts only)',
'* foo[ResourceLoader|type=general]|bar.js',
'general',
Module::LOAD_GENERAL,
],
];
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
}
/**
* @dataProvider provideGetType
*/
public function testType( $message, $definition, $gType, $mType ) {
$g = $this->makeGadget( $definition );
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
$this->assertEquals( $gType, $g->getType(), "Gadget: $message" );
$this->assertEquals( $mType, $this->makeGadgetModule( $g )->getType(), "Module: $message" );
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
}
public function testIsHidden() {
$g = $this->makeGadget( '* foo[hidden]|bar.js' );
$this->assertTrue( $g->isHidden() );
$g = $this->makeGadget( '* foo[ResourceLoader|hidden]|bar.js' );
$this->assertTrue( $g->isHidden() );
$g = $this->makeGadget( '* foo[ResourceLoader]|bar.js' );
$this->assertFalse( $g->isHidden() );
}
public static function provideWarnings() {
return [
[
'* foo[ResourceLoader|package]|foo.css',
[ 'gadgets-validate-noentrypoint' ]
],
[
'* foo[ResourceLoader]|foo.js,foo.json',
[ 'gadgets-validate-json' ]
],
[
'* foo[ResourceLoader|type=styles]|foo.js|foo.css',
[ 'gadgets-validate-scriptsnotallowed' ]
],
[
'* foo[ResourceLoader|type=styles|peers=bar]|foo.js|foo.json|foo.css',
[ 'gadgets-validate-scriptsnotallowed', 'gadgets-validate-stylepeers', 'gadgets-validate-json' ]
]
];
}
/**
* @dataProvider provideWarnings
*/
public function testGadgetWarnings( $definition, $expectedMsgKeys ) {
$g = $this->makeGadget( $definition );
$msgKeys = $g->getValidationWarnings();
$this->assertArrayEquals( $expectedMsgKeys, $msgKeys );
}
2011-05-21 11:23:06 +00:00
}