| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Extension\Gadgets\Gadget; |
| 4 |
use MediaWiki\ResourceLoader\Module; |
| 5 |
use MediaWiki\User\User; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class GadgetTest extends MediaWikiUnitTestCase { |
| 14 |
use GadgetTestTrait; |
| 15 |
|
| 16 |
public function testToArray() { |
| 17 |
$g = $this->makeGadget( '*bar[ResourceLoader|rights=test]|bar.js|foo.css | foo.json' ); |
| 18 |
$gNewFromSerialized = new Gadget( $g->toArray() ); |
| 19 |
$this->assertArrayEquals( $g->toArray(), $gNewFromSerialized->toArray() ); |
| 20 |
} |
| 21 |
|
| 22 |
public function testGadgetFromDefinitionContent() { |
| 23 |
$gArray = Gadget::serializeDefinition( 'bar', [ |
| 24 |
'settings' => [ |
| 25 |
'rights' => [], |
| 26 |
'default' => true, |
| 27 |
'package' => true, |
| 28 |
'hidden' => false, |
| 29 |
'actions' => [], |
| 30 |
'skins' => [], |
| 31 |
'namespaces' => [], |
| 32 |
'categories' => [], |
| 33 |
'contentModels' => [], |
| 34 |
'category' => 'misc', |
| 35 |
'supportsUrlLoad' => false, |
| 36 |
'requiresES6' => false, |
| 37 |
], |
| 38 |
'module' => [ |
| 39 |
'pages' => [ 'foo.js', 'bar.css' ], |
| 40 |
'datas' => [], |
| 41 |
'dependencies' => [ 'moment' ], |
| 42 |
'peers' => [], |
| 43 |
'messages' => [ 'blanknamespace' ], |
| 44 |
'type' => 'general', |
| 45 |
] |
| 46 |
] ); |
| 47 |
|
| 48 |
$g = new Gadget( $gArray ); |
| 49 |
|
| 50 |
$this->assertTrue( $g->isOnByDefault() ); |
| 51 |
$this->assertTrue( $g->isPackaged() ); |
| 52 |
$this->assertFalse( $g->isHidden() ); |
| 53 |
$this->assertFalse( $g->supportsUrlLoad() ); |
| 54 |
$this->assertTrue( $g->supportsResourceLoader() ); |
| 55 |
$this->assertCount( 1, $g->getScripts() ); |
| 56 |
$this->assertCount( 1, $g->getStyles() ); |
| 57 |
$this->assertCount( 0, $g->getJSONs() ); |
| 58 |
$this->assertCount( 1, $g->getDependencies() ); |
| 59 |
$this->assertCount( 1, $g->getMessages() ); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
$arr = $g->toArray(); |
| 64 |
unset( $arr['definition'] ); |
| 65 |
$this->assertSame( $arr, $gArray ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testInvalidLines() { |
| 69 |
$this->assertFalse( $this->makeGadget( '' ) ); |
| 70 |
$this->assertFalse( $this->makeGadget( '<foo|bar>' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testSimpleCases() { |
| 74 |
$g = $this->makeGadget( '* foo bar| foo.css|foo.js|foo.json|foo.bar' ); |
| 75 |
$this->assertEquals( 'foo_bar', $g->getName() ); |
| 76 |
$this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) ); |
| 77 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() ); |
| 78 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() ); |
| 79 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.json' ], $g->getJSONs() ); |
| 80 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css', 'MediaWiki:Gadget-foo.json' ], |
| 81 |
$g->getScriptsAndStyles() ); |
| 82 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() ); |
| 83 |
$this->assertFalse( $g->supportsResourceLoader() ); |
| 84 |
$this->assertTrue( $g->hasModule() ); |
| 85 |
} |
| 86 |
|
| 87 |
public function testRLtag() { |
| 88 |
$g = $this->makeGadget( '*foo [ResourceLoader]|foo.js|foo.css' ); |
| 89 |
$this->assertEquals( 'foo', $g->getName() ); |
| 90 |
$this->assertTrue( $g->supportsResourceLoader() ); |
| 91 |
$this->assertCount( 0, $g->getLegacyScripts() ); |
| 92 |
} |
| 93 |
|
| 94 |
public function testPackaged() { |
| 95 |
$g = $this->makeGadget( '* foo bar[ResourceLoader|package]| foo.css|foo.js|foo.bar|foo.json' ); |
| 96 |
$this->assertEquals( 'foo_bar', $g->getName() ); |
| 97 |
$this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) ); |
| 98 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() ); |
| 99 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() ); |
| 100 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.json' ], $g->getJSONs() ); |
| 101 |
$this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css', 'MediaWiki:Gadget-foo.json' ], |
| 102 |
$g->getScriptsAndStyles() ); |
| 103 |
$this->assertEquals( [], $g->getLegacyScripts() ); |
| 104 |
$this->assertTrue( $g->supportsResourceLoader() ); |
| 105 |
$this->assertTrue( $g->hasModule() ); |
| 106 |
} |
| 107 |
|
| 108 |
public function testSupportsUrlLoad() { |
| 109 |
$directLoadAllowedByDefault = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 110 |
$directLoadAllowed1 = $this->makeGadget( '*bar[ResourceLoader|supportsUrlLoad]|bar.js' ); |
| 111 |
$directLoadAllowed2 = $this->makeGadget( '*bar[ResourceLoader|supportsUrlLoad=true]|bar.js' ); |
| 112 |
$directLoadNotAllowed = $this->makeGadget( '*baz[ResourceLoader|supportsUrlLoad=false]|baz.js' ); |
| 113 |
|
| 114 |
$this->assertFalse( $directLoadAllowedByDefault->supportsUrlLoad() ); |
| 115 |
$this->assertTrue( $directLoadAllowed1->supportsUrlLoad() ); |
| 116 |
$this->assertTrue( $directLoadAllowed2->supportsUrlLoad() ); |
| 117 |
$this->assertFalse( $directLoadNotAllowed->supportsUrlLoad() ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testIsAllowed() { |
| 121 |
$user = $this->getMockBuilder( User::class ) |
| 122 |
->onlyMethods( [ 'isAllowedAll' ] ) |
| 123 |
->getMock(); |
| 124 |
$user->method( 'isAllowedAll' ) |
| 125 |
->willReturnCallback( |
| 126 |
static function ( ...$rights ) { |
| 127 |
return array_diff( $rights, [ 'test' ] ) === []; |
| 128 |
} |
| 129 |
); |
| 130 |
|
| 131 |
|
| 132 |
$gUnset = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 133 |
$gAllowed = $this->makeGadget( '*bar[ResourceLoader|rights=test]|bar.js' ); |
| 134 |
$gNotAllowed = $this->makeGadget( '*baz[ResourceLoader|rights=nope]|baz.js' ); |
| 135 |
$this->assertTrue( $gUnset->isAllowed( $user ) ); |
| 136 |
$this->assertTrue( $gAllowed->isAllowed( $user ) ); |
| 137 |
$this->assertFalse( $gNotAllowed->isAllowed( $user ) ); |
| 138 |
} |
| 139 |
|
| 140 |
public function testSkinsTag() { |
| 141 |
$gUnset = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 142 |
$gSkinSupported = $this->makeGadget( '*bar[ResourceLoader|skins=fallback]|bar.js' ); |
| 143 |
$gSkinNotSupported = $this->makeGadget( '*baz[ResourceLoader|skins=bar]|baz.js' ); |
| 144 |
$skin = new SkinFallback(); |
| 145 |
$this->assertTrue( $gUnset->isSkinSupported( $skin ) ); |
| 146 |
$this->assertTrue( $gSkinSupported->isSkinSupported( $skin ) ); |
| 147 |
$this->assertFalse( $gSkinNotSupported->isSkinSupported( $skin ) ); |
| 148 |
} |
| 149 |
|
| 150 |
public function testActionsTag() { |
| 151 |
$gUnset = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 152 |
$gActionSupported = $this->makeGadget( '*bar[ResourceLoader|actions=edit]|bar.js' ); |
| 153 |
$gActionNotSupported = $this->makeGadget( '*baz[ResourceLoader|actions=history]|baz.js' ); |
| 154 |
$this->assertTrue( $gUnset->isActionSupported( 'edit' ) ); |
| 155 |
$this->assertTrue( $gActionSupported->isActionSupported( 'edit' ) ); |
| 156 |
$this->assertFalse( $gActionNotSupported->isActionSupported( 'edit' ) ); |
| 157 |
|
| 158 |
|
| 159 |
$this->assertTrue( $gActionSupported->isActionSupported( 'submit' ) ); |
| 160 |
|
| 161 |
$gMultiActions = $this->makeGadget( '*bar[ResourceLoader|actions=unknown,history]|bar.js' ); |
| 162 |
$this->assertTrue( $gMultiActions->isActionSupported( 'history' ) ); |
| 163 |
$this->assertFalse( $gMultiActions->isActionSupported( 'view' ) ); |
| 164 |
} |
| 165 |
|
| 166 |
public function testNamespacesTag() { |
| 167 |
$gUnsetNamespace = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 168 |
$gNamespace0 = $this->makeGadget( '*bar[ResourceLoader|namespaces=0]|bar.js' ); |
| 169 |
$gNamespace1 = $this->makeGadget( '*bar[ResourceLoader|namespaces=1]|bar.js' ); |
| 170 |
$gMultiNamespace = $this->makeGadget( '*bar[ResourceLoader|namespaces=1,2,3,4]|bar.js' ); |
| 171 |
|
| 172 |
$this->assertTrue( $gUnsetNamespace->isNamespaceSupported( 5 ) ); |
| 173 |
|
| 174 |
$this->assertTrue( $gNamespace0->isNamespaceSupported( 0 ) ); |
| 175 |
$this->assertFalse( $gNamespace0->isNamespaceSupported( 2 ) ); |
| 176 |
|
| 177 |
$this->assertTrue( $gNamespace1->isNamespaceSupported( 1 ) ); |
| 178 |
$this->assertFalse( $gNamespace1->isNamespaceSupported( 2 ) ); |
| 179 |
|
| 180 |
$this->assertTrue( $gMultiNamespace->isNamespaceSupported( 1 ) ); |
| 181 |
$this->assertTrue( $gMultiNamespace->isNamespaceSupported( 2 ) ); |
| 182 |
$this->assertFalse( $gMultiNamespace->isNamespaceSupported( 5 ) ); |
| 183 |
|
| 184 |
$this->assertSame( [ '1', '2', '3', '4' ], $gMultiNamespace->getRequiredNamespaces() ); |
| 185 |
} |
| 186 |
|
| 187 |
public function testCategoriesTag() { |
| 188 |
$gUnsetCategory = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 189 |
$gCategoryFoo = $this->makeGadget( '*foo[ResourceLoader|categories=Foo]|foo.js' ); |
| 190 |
$gMultiCategory = $this->makeGadget( '*foo[ResourceLoader|categories=Foo,Bar baz,quux]|foo.js' ); |
| 191 |
|
| 192 |
$this->assertTrue( $gUnsetCategory->isCategorySupported( [ 'Foo' ] ) ); |
| 193 |
|
| 194 |
$this->assertTrue( $gCategoryFoo->isCategorySupported( [ 'Foo' ] ) ); |
| 195 |
$this->assertFalse( $gCategoryFoo->isCategorySupported( [ 'Bar' ] ) ); |
| 196 |
$this->assertFalse( $gCategoryFoo->isCategorySupported( [ 'Bar baz' ] ) ); |
| 197 |
|
| 198 |
$this->assertTrue( $gMultiCategory->isCategorySupported( [ 'Foo' ] ) ); |
| 199 |
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Bar' ] ) ); |
| 200 |
$this->assertTrue( $gMultiCategory->isCategorySupported( [ 'Spam', 'Bar baz', 'Eggs' ] ) ); |
| 201 |
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Spam', 'Eggs' ] ) ); |
| 202 |
|
| 203 |
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'foo' ] ) ); |
| 204 |
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Bar_baz' ] ) ); |
| 205 |
|
| 206 |
$this->assertFalse( $gMultiCategory->isCategorySupported( [ 'Quux' ] ) ); |
| 207 |
} |
| 208 |
|
| 209 |
public function testContentModelsTags() { |
| 210 |
$gUnsetModel = $this->makeGadget( '*foo[ResourceLoader]|foo.js' ); |
| 211 |
$gModelWikitext = $this->makeGadget( '*bar[ResourceLoader|contentModels=wikitext]|bar.js' ); |
| 212 |
$gModelCode = $this->makeGadget( '*bar[ResourceLoader|contentModels=javascript,css]|bar.js' ); |
| 213 |
|
| 214 |
$this->assertTrue( $gUnsetModel->isContentModelSupported( 'wikitext' ) ); |
| 215 |
|
| 216 |
$this->assertTrue( $gModelWikitext->isContentModelSupported( 'wikitext' ) ); |
| 217 |
$this->assertFalse( $gModelWikitext->isContentModelSupported( 'javascript' ) ); |
| 218 |
|
| 219 |
$this->assertTrue( $gModelCode->isContentModelSupported( 'javascript' ) ); |
| 220 |
$this->assertTrue( $gModelCode->isContentModelSupported( 'css' ) ); |
| 221 |
$this->assertFalse( $gModelCode->isContentModelSupported( 'wikitext' ) ); |
| 222 |
} |
| 223 |
|
| 224 |
public function testDependencies() { |
| 225 |
$g = $this->makeGadget( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' ); |
| 226 |
$this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() ); |
| 227 |
$this->assertTrue( $g->supportsResourceLoader() ); |
| 228 |
$this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() ); |
| 229 |
} |
| 230 |
|
| 231 |
public function testES6() { |
| 232 |
$es6gadget = $this->makeGadget( '* foo[ResourceLoader|requiresES6]|bar.js' ); |
| 233 |
$es5gadget = $this->makeGadget( '* foo[ResourceLoader]|bar.js' ); |
| 234 |
$this->assertTrue( $es6gadget->requiresES6() ); |
| 235 |
$this->assertFalse( $es5gadget->requiresES6() ); |
| 236 |
} |
| 237 |
|
| 238 |
public static function provideGetType() { |
| 239 |
return [ |
| 240 |
[ |
| 241 |
'Default (mixed)', |
| 242 |
'* foo[ResourceLoader]|bar.css|bar.js', |
| 243 |
'general', |
| 244 |
Module::LOAD_GENERAL, |
| 245 |
], |
| 246 |
[ |
| 247 |
'Default (styles only)', |
| 248 |
'* foo[ResourceLoader]|bar.css', |
| 249 |
'styles', |
| 250 |
Module::LOAD_STYLES, |
| 251 |
], |
| 252 |
[ |
| 253 |
'Default (scripts only)', |
| 254 |
'* foo[ResourceLoader]|bar.js', |
| 255 |
'general', |
| 256 |
Module::LOAD_GENERAL, |
| 257 |
], |
| 258 |
[ |
| 259 |
'Default (styles only with dependencies)', |
| 260 |
'* foo[ResourceLoader|dependencies=jquery.ui]|bar.css', |
| 261 |
'general', |
| 262 |
Module::LOAD_GENERAL, |
| 263 |
], |
| 264 |
[ |
| 265 |
'Styles type (mixed)', |
| 266 |
'* foo[ResourceLoader|type=styles]|bar.css|bar.js', |
| 267 |
'styles', |
| 268 |
Module::LOAD_STYLES, |
| 269 |
], |
| 270 |
[ |
| 271 |
'Styles type (styles only)', |
| 272 |
'* foo[ResourceLoader|type=styles]|bar.css', |
| 273 |
'styles', |
| 274 |
Module::LOAD_STYLES, |
| 275 |
], |
| 276 |
[ |
| 277 |
'Styles type (scripts only)', |
| 278 |
'* foo[ResourceLoader|type=styles]|bar.js', |
| 279 |
'styles', |
| 280 |
Module::LOAD_STYLES, |
| 281 |
], |
| 282 |
[ |
| 283 |
'General type (mixed)', |
| 284 |
'* foo[ResourceLoader|type=general]|bar.css|bar.js', |
| 285 |
'general', |
| 286 |
Module::LOAD_GENERAL, |
| 287 |
], |
| 288 |
[ |
| 289 |
'General type (styles only)', |
| 290 |
'* foo[ResourceLoader|type=general]|bar.css', |
| 291 |
'general', |
| 292 |
Module::LOAD_GENERAL, |
| 293 |
], |
| 294 |
[ |
| 295 |
'General type (scripts only)', |
| 296 |
'* foo[ResourceLoader|type=general]|bar.js', |
| 297 |
'general', |
| 298 |
Module::LOAD_GENERAL, |
| 299 |
], |
| 300 |
]; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public function testType( $message, $definition, $gType, $mType ) { |
| 307 |
$g = $this->makeGadget( $definition ); |
| 308 |
$this->assertEquals( $gType, $g->getType(), "Gadget: $message" ); |
| 309 |
$this->assertEquals( $mType, $this->makeGadgetModule( $g )->getType(), "Module: $message" ); |
| 310 |
} |
| 311 |
|
| 312 |
public function testIsHidden() { |
| 313 |
$g = $this->makeGadget( '* foo[hidden]|bar.js' ); |
| 314 |
$this->assertTrue( $g->isHidden() ); |
| 315 |
|
| 316 |
$g = $this->makeGadget( '* foo[ResourceLoader|hidden]|bar.js' ); |
| 317 |
$this->assertTrue( $g->isHidden() ); |
| 318 |
|
| 319 |
$g = $this->makeGadget( '* foo[ResourceLoader]|bar.js' ); |
| 320 |
$this->assertFalse( $g->isHidden() ); |
| 321 |
} |
| 322 |
|
| 323 |
public static function provideWarnings() { |
| 324 |
return [ |
| 325 |
[ |
| 326 |
'* foo[ResourceLoader|package]|foo.css', |
| 327 |
[ 'gadgets-validate-noentrypoint' ] |
| 328 |
], |
| 329 |
[ |
| 330 |
'* foo[ResourceLoader]|foo.js,foo.json', |
| 331 |
[ 'gadgets-validate-json' ] |
| 332 |
], |
| 333 |
[ |
| 334 |
'* foo[ResourceLoader|type=styles]|foo.js|foo.css', |
| 335 |
[ 'gadgets-validate-scriptsnotallowed' ] |
| 336 |
], |
| 337 |
[ |
| 338 |
'* foo[ResourceLoader|type=styles|peers=bar]|foo.js|foo.json|foo.css', |
| 339 |
[ 'gadgets-validate-scriptsnotallowed', 'gadgets-validate-stylepeers', 'gadgets-validate-json' ] |
| 340 |
] |
| 341 |
]; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function testGadgetWarnings( $definition, $expectedMsgKeys ) { |
| 348 |
$g = $this->makeGadget( $definition ); |
| 349 |
$msgKeys = $g->getValidationWarnings(); |
| 350 |
$this->assertArrayEquals( $expectedMsgKeys, $msgKeys ); |
| 351 |
} |
| 352 |
} |