MediaWiki REL1_32
GadgetTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
12 protected $user;
13
14 public function setUp() {
16
17 parent::setUp();
18
19 $wgGroupPermissions['unittesters'] = [
20 'test' => true,
21 ];
22 $this->user = $this->getTestUser( [ 'unittesters' ] )->getUser();
23 }
24
25 public function tearDown() {
27 parent::tearDown();
28 }
29
34 private function create( $line ) {
36 $g = $repo->newFromDefinition( $line, 'misc' );
37 $this->assertInstanceOf( Gadget::class, $g );
38 return $g;
39 }
40
41 private function getModule( Gadget $g ) {
42 $module = TestingAccessWrapper::newFromObject(
43 new GadgetResourceLoaderModule( [ 'id' => null ] )
44 );
45 $module->gadget = $g;
46 return $module;
47 }
48
52 public function testInvalidLines() {
54 $this->assertFalse( $repo->newFromDefinition( '', 'misc' ) );
55 $this->assertFalse( $repo->newFromDefinition( '<foo|bar>', 'misc' ) );
56 }
57
62 public function testSimpleCases() {
63 $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' );
64 $this->assertEquals( 'foo_bar', $g->getName() );
65 $this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
66 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
67 $this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
68 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css' ],
69 $g->getScriptsAndStyles() );
70 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() );
71 $this->assertFalse( $g->supportsResourceLoader() );
72 $this->assertTrue( $g->hasModule() );
73 }
74
80 public function testRLtag() {
81 $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' );
82 $this->assertEquals( 'foo', $g->getName() );
83 $this->assertTrue( $g->supportsResourceLoader() );
84 $this->assertEquals( 0, count( $g->getLegacyScripts() ) );
85 }
86
91 public function testisAllowed() {
92 $gUnset = $this->create( '*foo[ResourceLoader]|foo.js' );
93 $gAllowed = $this->create( '*bar[ResourceLoader|rights=test]|bar.js' );
94 $gNotAllowed = $this->create( '*baz[ResourceLoader|rights=nope]|baz.js' );
95 $this->assertTrue( $gUnset->isAllowed( $this->user ) );
96 $this->assertTrue( $gAllowed->isAllowed( $this->user ) );
97 $this->assertFalse( $gNotAllowed->isAllowed( $this->user ) );
98 }
99
104 public function testSkinsTag() {
105 $gUnset = $this->create( '*foo[ResourceLoader]|foo.js' );
106 $gSkinSupported = $this->create( '*bar[ResourceLoader|skins=fallback]|bar.js' );
107 $gSkinNotSupported = $this->create( '*baz[ResourceLoader|skins=bar]|baz.js' );
108 $skin = new SkinFallback();
109 $this->assertTrue( $gUnset->isSkinSupported( $skin ) );
110 $this->assertTrue( $gSkinSupported->isSkinSupported( $skin ) );
111 $this->assertFalse( $gSkinNotSupported->isSkinSupported( $skin ) );
112 }
113
118 public function testDependencies() {
119 $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
120 $this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() );
121 $this->assertTrue( $g->supportsResourceLoader() );
122 $this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() );
123 }
124
125 public static function provideGetType() {
126 return [
127 [
128 'Default (mixed)',
129 '* foo[ResourceLoader]|bar.css|bar.js',
130 'general',
132 ],
133 [
134 'Default (styles only)',
135 '* foo[ResourceLoader]|bar.css',
136 'styles',
138 ],
139 [
140 'Default (scripts only)',
141 '* foo[ResourceLoader]|bar.js',
142 'general',
144 ],
145 [
146 'Default (styles only with dependencies)',
147 '* foo[ResourceLoader|dependencies=jquery.ui]|bar.css',
148 'general',
150 ],
151 [
152 'Styles type (mixed)',
153 '* foo[ResourceLoader|type=styles]|bar.css|bar.js',
154 'styles',
156 ],
157 [
158 'Styles type (styles only)',
159 '* foo[ResourceLoader|type=styles]|bar.css',
160 'styles',
162 ],
163 [
164 'Styles type (scripts only)',
165 '* foo[ResourceLoader|type=styles]|bar.js',
166 'styles',
168 ],
169 [
170 'General type (mixed)',
171 '* foo[ResourceLoader|type=general]|bar.css|bar.js',
172 'general',
174 ],
175 [
176 'General type (styles only)',
177 '* foo[ResourceLoader|type=general]|bar.css',
178 'general',
180 ],
181 [
182 'General type (scripts only)',
183 '* foo[ResourceLoader|type=general]|bar.js',
184 'general',
186 ],
187 ];
188 }
189
196 public function testType( $message, $definition, $gType, $mType ) {
197 $g = $this->create( $definition );
198 $this->assertEquals( $gType, $g->getType(), "Gadget: $message" );
199 $this->assertEquals( $mType, $this->getModule( $g )->getType(), "Module: $message" );
200 }
201
206 public function testIsHidden() {
207 $g = $this->create( '* foo[hidden]|bar.js' );
208 $this->assertTrue( $g->isHidden() );
209
210 $g = $this->create( '* foo[ResourceLoader|hidden]|bar.js' );
211 $this->assertTrue( $g->isHidden() );
212
213 $g = $this->create( '* foo[ResourceLoader]|bar.js' );
214 $this->assertFalse( $g->isHidden() );
215 }
216
223 public function testPreferences() {
224 $prefs = [];
225 $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
226 // Force usage of a MediaWikiGadgetsDefinitionRepo
228
229 $gadgets = $repo->fetchStructuredList( '* foo | foo.js
230==keep-section1==
231* bar| bar.js
232==remove-section==
233* baz [rights=embezzle] |baz.js
234==keep-section2==
235* quux [rights=test] | quux.js' );
236 $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
237
238 $repo->definitionCache = $gadgets;
239 $this->assertTrue( GadgetHooks::getPreferences( $this->user, $prefs ),
240 'GetPrefences hook should return true' );
241
242 $options = $prefs['gadgets']['options'];
243 $this->assertArrayNotHasKey( '⧼gadget-section-remove-section⧽', $options,
244 'Must not show empty sections' );
245 $this->assertArrayHasKey( '⧼gadget-section-keep-section1⧽', $options );
246 $this->assertArrayHasKey( '⧼gadget-section-keep-section2⧽', $options );
247 }
248}
$wgGroupPermissions['sysop']['replacetext']
$line
Definition cdb.php:59
static getPreferences( $user, &$preferences)
GetPreferences hook handler.
static setSingleton( $repo=null)
Should only be used by unit tests.
Class representing a list of resources for one gadget, basically a wrapper around the Gadget class.
Gadgets.
Definition GadgetTest.php:8
testDependencies()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::getDependencies.
testPreferences()
Gadget GadgetHooks::getPreferences GadgetRepo MediaWikiGadgetsDefinitionRepo.
testType( $message, $definition, $gType, $mType)
provideGetType MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::getType GadgetResourceLoader...
testInvalidLines()
MediaWikiGadgetsDefinitionRepo::newFromDefinition.
create( $line)
testRLtag()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::supportsResourceLoader Gadget::getLegacyScr...
testSkinsTag()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::isSkinSupported.
getModule(Gadget $g)
static provideGetType()
testSimpleCases()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget.
testisAllowed()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::isAllowed.
testIsHidden()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::isHidden.
Wrapper for one gadget.
Definition Gadget.php:17
static getModuleName( $id)
Definition Gadget.php:150
Gadgets repo powered by MediaWiki:Gadgets-definition.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
SkinTemplate class for the fallback skin.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050