MediaWiki  1.32.5
GadgetTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
12  protected $user;
13 
14  public function setUp() {
15  global $wgGroupPermissions;
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 ) {
35  $repo = new MediaWikiGadgetsDefinitionRepo();
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() {
53  $repo = new MediaWikiGadgetsDefinitionRepo();
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
227  GadgetRepo::setSingleton( $repo );
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 }
GadgetTest\setUp
setUp()
Definition: GadgetTest.php:14
ResourceLoaderModule\LOAD_GENERAL
const LOAD_GENERAL
Definition: ResourceLoaderModule.php:45
Gadget\getModuleName
static getModuleName( $id)
Definition: Gadget.php:150
GadgetTest
Gadgets.
Definition: GadgetTest.php:8
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:179
GadgetTest\testPreferences
testPreferences()
Gadget GadgetHooks::getPreferences GadgetRepo MediaWikiGadgetsDefinitionRepo.
Definition: GadgetTest.php:223
captcha-old.count
count
Definition: captcha-old.py:249
GadgetTest\getModule
getModule(Gadget $g)
Definition: GadgetTest.php:41
GadgetTest\testInvalidLines
testInvalidLines()
MediaWikiGadgetsDefinitionRepo::newFromDefinition.
Definition: GadgetTest.php:52
GadgetTest\$user
User $user
Definition: GadgetTest.php:12
GadgetTest\testIsHidden
testIsHidden()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::isHidden.
Definition: GadgetTest.php:206
GadgetTest\tearDown
tearDown()
Definition: GadgetTest.php:25
MediaWikiGadgetsDefinitionRepo
Gadgets repo powered by MediaWiki:Gadgets-definition.
Definition: MediaWikiGadgetsDefinitionRepo.php:10
GadgetTest\testisAllowed
testisAllowed()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::isAllowed.
Definition: GadgetTest.php:91
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
GadgetTest\testSkinsTag
testSkinsTag()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::isSkinSupported.
Definition: GadgetTest.php:104
GadgetTest\testRLtag
testRLtag()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::supportsResourceLoader Gadget::getLegacyScr...
Definition: GadgetTest.php:80
user
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
SkinFallback
SkinTemplate class for the fallback skin.
Definition: SkinFallback.php:12
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Gadget
Wrapper for one gadget.
Definition: Gadget.php:17
GadgetTest\create
create( $line)
Definition: GadgetTest.php:34
GadgetTest\testType
testType( $message, $definition, $gType, $mType)
provideGetType MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::getType GadgetResourceLoader...
Definition: GadgetTest.php:196
$line
$line
Definition: cdb.php:59
GadgetHooks\getPreferences
static getPreferences( $user, &$preferences)
GetPreferences hook handler.
Definition: GadgetHooks.php:72
GadgetResourceLoaderModule
Class representing a list of resources for one gadget, basically a wrapper around the Gadget class.
Definition: GadgetResourceLoaderModule.php:7
ResourceLoaderModule\LOAD_STYLES
const LOAD_STYLES
Definition: ResourceLoaderModule.php:43
GadgetTest\testDependencies
testDependencies()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget::getDependencies.
Definition: GadgetTest.php:118
$options
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:2044
GadgetRepo\setSingleton
static setSingleton( $repo=null)
Should only be used by unit tests.
Definition: GadgetRepo.php:101
GadgetTest\testSimpleCases
testSimpleCases()
MediaWikiGadgetsDefinitionRepo::newFromDefinition Gadget.
Definition: GadgetTest.php:62
$skin
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 noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned $skin
Definition: hooks.txt:2044
$wgGroupPermissions
$wgGroupPermissions['sysop']['replacetext']
Definition: ReplaceText.php:56
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
GadgetTest\provideGetType
static provideGetType()
Definition: GadgetTest.php:125