MediaWiki REL1_34
GadgetTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
8class GadgetTest extends MediaWikiUnitTestCase {
13 private function create( $line ) {
15 $g = $repo->newFromDefinition( $line, 'misc' );
16 $this->assertInstanceOf( Gadget::class, $g );
17 return $g;
18 }
19
20 private function getModule( Gadget $g ) {
21 $module = TestingAccessWrapper::newFromObject(
22 new GadgetResourceLoaderModule( [ 'id' => null ] )
23 );
24 $module->gadget = $g;
25 return $module;
26 }
27
31 public function testInvalidLines() {
33 $this->assertFalse( $repo->newFromDefinition( '', 'misc' ) );
34 $this->assertFalse( $repo->newFromDefinition( '<foo|bar>', 'misc' ) );
35 }
36
41 public function testSimpleCases() {
42 $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' );
43 $this->assertEquals( 'foo_bar', $g->getName() );
44 $this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
45 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
46 $this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
47 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css' ],
48 $g->getScriptsAndStyles() );
49 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() );
50 $this->assertFalse( $g->supportsResourceLoader() );
51 $this->assertTrue( $g->hasModule() );
52 }
53
59 public function testRLtag() {
60 $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' );
61 $this->assertEquals( 'foo', $g->getName() );
62 $this->assertTrue( $g->supportsResourceLoader() );
63 $this->assertEquals( 0, count( $g->getLegacyScripts() ) );
64 }
65
70 public function testIsAllowed() {
71 $user = $this->getMockBuilder( User::class )
72 ->setMethods( [ 'isAllowedAll' ] )
73 ->getMock();
74 $user->method( 'isAllowedAll' )
75 ->willReturnCallback(
76 function ( ...$rights ) {
77 return array_diff( $rights, [ 'test' ] ) === [];
78 }
79 );
80
82 $gUnset = $this->create( '*foo[ResourceLoader]|foo.js' );
83 $gAllowed = $this->create( '*bar[ResourceLoader|rights=test]|bar.js' );
84 $gNotAllowed = $this->create( '*baz[ResourceLoader|rights=nope]|baz.js' );
85 $this->assertTrue( $gUnset->isAllowed( $user ) );
86 $this->assertTrue( $gAllowed->isAllowed( $user ) );
87 $this->assertFalse( $gNotAllowed->isAllowed( $user ) );
88 }
89
94 public function testSkinsTag() {
95 $gUnset = $this->create( '*foo[ResourceLoader]|foo.js' );
96 $gSkinSupported = $this->create( '*bar[ResourceLoader|skins=fallback]|bar.js' );
97 $gSkinNotSupported = $this->create( '*baz[ResourceLoader|skins=bar]|baz.js' );
98 $skin = new SkinFallback();
99 $this->assertTrue( $gUnset->isSkinSupported( $skin ) );
100 $this->assertTrue( $gSkinSupported->isSkinSupported( $skin ) );
101 $this->assertFalse( $gSkinNotSupported->isSkinSupported( $skin ) );
102 }
103
108 public function testDependencies() {
109 $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
110 $this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() );
111 $this->assertTrue( $g->supportsResourceLoader() );
112 $this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() );
113 }
114
115 public static function provideGetType() {
116 return [
117 [
118 'Default (mixed)',
119 '* foo[ResourceLoader]|bar.css|bar.js',
120 'general',
121 ResourceLoaderModule::LOAD_GENERAL,
122 ],
123 [
124 'Default (styles only)',
125 '* foo[ResourceLoader]|bar.css',
126 'styles',
127 ResourceLoaderModule::LOAD_STYLES,
128 ],
129 [
130 'Default (scripts only)',
131 '* foo[ResourceLoader]|bar.js',
132 'general',
133 ResourceLoaderModule::LOAD_GENERAL,
134 ],
135 [
136 'Default (styles only with dependencies)',
137 '* foo[ResourceLoader|dependencies=jquery.ui]|bar.css',
138 'general',
139 ResourceLoaderModule::LOAD_GENERAL,
140 ],
141 [
142 'Styles type (mixed)',
143 '* foo[ResourceLoader|type=styles]|bar.css|bar.js',
144 'styles',
145 ResourceLoaderModule::LOAD_STYLES,
146 ],
147 [
148 'Styles type (styles only)',
149 '* foo[ResourceLoader|type=styles]|bar.css',
150 'styles',
151 ResourceLoaderModule::LOAD_STYLES,
152 ],
153 [
154 'Styles type (scripts only)',
155 '* foo[ResourceLoader|type=styles]|bar.js',
156 'styles',
157 ResourceLoaderModule::LOAD_STYLES,
158 ],
159 [
160 'General type (mixed)',
161 '* foo[ResourceLoader|type=general]|bar.css|bar.js',
162 'general',
163 ResourceLoaderModule::LOAD_GENERAL,
164 ],
165 [
166 'General type (styles only)',
167 '* foo[ResourceLoader|type=general]|bar.css',
168 'general',
169 ResourceLoaderModule::LOAD_GENERAL,
170 ],
171 [
172 'General type (scripts only)',
173 '* foo[ResourceLoader|type=general]|bar.js',
174 'general',
175 ResourceLoaderModule::LOAD_GENERAL,
176 ],
177 ];
178 }
179
186 public function testType( $message, $definition, $gType, $mType ) {
187 $g = $this->create( $definition );
188 $this->assertEquals( $gType, $g->getType(), "Gadget: $message" );
189 $this->assertEquals( $mType, $this->getModule( $g )->getType(), "Module: $message" );
190 }
191
196 public function testIsHidden() {
197 $g = $this->create( '* foo[hidden]|bar.js' );
198 $this->assertTrue( $g->isHidden() );
199
200 $g = $this->create( '* foo[ResourceLoader|hidden]|bar.js' );
201 $this->assertTrue( $g->isHidden() );
202
203 $g = $this->create( '* foo[ResourceLoader]|bar.js' );
204 $this->assertFalse( $g->isHidden() );
205 }
206}
$line
Definition cdb.php:59
Class representing a list of resources for one gadget, basically a wrapper around the Gadget class.
@group Gadgets
Definition GadgetTest.php:8
testDependencies()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget::getDependencies
testType( $message, $definition, $gType, $mType)
@dataProvider provideGetType @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget...
testInvalidLines()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
testIsAllowed()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget::isAllowed
create( $line)
testRLtag()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget::supportsResourceLoader @cov...
testSkinsTag()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget::isSkinSupported
getModule(Gadget $g)
static provideGetType()
testSimpleCases()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget
testIsHidden()
@covers MediaWikiGadgetsDefinitionRepo::newFromDefinition @covers Gadget::isHidden
Wrapper for one gadget.
Definition Gadget.php:17
static getModuleName( $id)
Definition Gadget.php:150
Gadgets repo powered by MediaWiki:Gadgets-definition.
SkinTemplate class for the fallback skin.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
isAllowed( $action='')
Internal mechanics of testing a permission.
Definition User.php:3694