MediaWiki REL1_31
GadgetTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
9
10 public function tearDown() {
12 parent::tearDown();
13 }
14
19 private function create( $line ) {
21 $g = $repo->newFromDefinition( $line, 'misc' );
22 $this->assertInstanceOf( Gadget::class, $g );
23 return $g;
24 }
25
26 private function getModule( Gadget $g ) {
27 $module = TestingAccessWrapper::newFromObject(
28 new GadgetResourceLoaderModule( [ 'id' => null ] )
29 );
30 $module->gadget = $g;
31 return $module;
32 }
33
37 public function testInvalidLines() {
39 $this->assertFalse( $repo->newFromDefinition( '', 'misc' ) );
40 $this->assertFalse( $repo->newFromDefinition( '<foo|bar>', 'misc' ) );
41 }
42
49 public function testSimpleCases() {
50 $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' );
51 $this->assertEquals( 'foo_bar', $g->getName() );
52 $this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
53 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
54 $this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
55 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css' ],
56 $g->getScriptsAndStyles() );
57 $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() );
58 $this->assertFalse( $g->supportsResourceLoader() );
59 $this->assertTrue( $g->hasModule() );
60 }
61
67 public function testRLtag() {
68 $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' );
69 $this->assertEquals( 'foo', $g->getName() );
70 $this->assertTrue( $g->supportsResourceLoader() );
71 $this->assertEquals( 0, count( $g->getLegacyScripts() ) );
72 }
73
78 public function testDependencies() {
79 $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
80 $this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() );
81 $this->assertTrue( $g->supportsResourceLoader() );
82 $this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() );
83 }
84
85 public static function provideGetType() {
86 return [
87 [
88 'Default (mixed)',
89 '* foo[ResourceLoader]|bar.css|bar.js',
90 'general',
92 ],
93 [
94 'Default (styles only)',
95 '* foo[ResourceLoader]|bar.css',
96 'styles',
98 ],
99 [
100 'Default (scripts only)',
101 '* foo[ResourceLoader]|bar.js',
102 'general',
104 ],
105 [
106 'Default (styles only with dependencies)',
107 '* foo[ResourceLoader|dependencies=jquery.ui]|bar.css',
108 'general',
110 ],
111 [
112 'Styles type (mixed)',
113 '* foo[ResourceLoader|type=styles]|bar.css|bar.js',
114 'styles',
116 ],
117 [
118 'Styles type (styles only)',
119 '* foo[ResourceLoader|type=styles]|bar.css',
120 'styles',
122 ],
123 [
124 'Styles type (scripts only)',
125 '* foo[ResourceLoader|type=styles]|bar.js',
126 'styles',
128 ],
129 [
130 'General type (mixed)',
131 '* foo[ResourceLoader|type=general]|bar.css|bar.js',
132 'general',
134 ],
135 [
136 'General type (styles only)',
137 '* foo[ResourceLoader|type=general]|bar.css',
138 'general',
140 ],
141 [
142 'General type (scripts only)',
143 '* foo[ResourceLoader|type=general]|bar.js',
144 'general',
146 ],
147 ];
148 }
149
156 public function testType( $message, $definition, $gType, $mType ) {
157 $g = $this->create( $definition );
158 $this->assertEquals( $gType, $g->getType(), "Gadget: $message" );
159 $this->assertEquals( $mType, $this->getModule( $g )->getType(), "Module: $message" );
160 }
161
166 public function testIsHidden() {
167 $g = $this->create( '* foo[hidden]|bar.js' );
168 $this->assertTrue( $g->isHidden() );
169
170 $g = $this->create( '* foo[ResourceLoader|hidden]|bar.js' );
171 $this->assertTrue( $g->isHidden() );
172
173 $g = $this->create( '* foo[ResourceLoader]|bar.js' );
174 $this->assertFalse( $g->isHidden() );
175 }
176
181 public function testPreferences() {
182 $prefs = [];
183 $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
184 // Force usage of a MediaWikiGadgetsDefinitionRepo
186
187 $gadgets = $repo->fetchStructuredList( '* foo | foo.js
188==keep-section1==
189* bar| bar.js
190==remove-section==
191* baz [rights=embezzle] |baz.js
192==keep-section2==
193* quux [rights=read] | quux.js' );
194 $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
195
196 $repo->definitionCache = $gadgets;
197 $this->assertTrue( GadgetHooks::getPreferences( new User, $prefs ),
198 'GetPrefences hook should return true' );
199
200 $options = $prefs['gadgets']['options'];
201 $this->assertArrayNotHasKey( '⧼gadget-section-remove-section⧽', $options,
202 'Must not show empty sections' );
203 $this->assertArrayHasKey( '⧼gadget-section-keep-section1⧽', $options );
204 $this->assertArrayHasKey( '⧼gadget-section-keep-section2⧽', $options );
205 }
206}
$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()
MediaWikiGadgetsDefinitionRepo::fetchStructuredList GadgetHooks::getPreferences.
testType( $message, $definition, $gType, $mType)
provideGetType MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::getType GadgetResourceLoader...
testInvalidLines()
MediaWikiGadgetsDefinitionRepo::newFromDefinition.
create( $line)
testRLtag()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::supportsResourceLoader Gadget::getLegacyScr...
getModule(Gadget $g)
static provideGetType()
testSimpleCases()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::__construct Gadget::getName Gadget::getModu...
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.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
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:2001