MediaWiki  1.29.1
GadgetTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
14  private function create( $line ) {
15  $repo = new MediaWikiGadgetsDefinitionRepo();
16  $g = $repo->newFromDefinition( $line, 'misc' );
17  $this->assertInstanceOf( 'Gadget', $g );
18  return $g;
19  }
20 
21  private function getModule( Gadget $g ) {
22  $module = TestingAccessWrapper::newFromObject(
23  new GadgetResourceLoaderModule( [ 'id' => null ] )
24  );
25  $module->gadget = $g;
26  return $module;
27  }
28 
29  public function testInvalidLines() {
30  $repo = new MediaWikiGadgetsDefinitionRepo();
31  $this->assertFalse( $repo->newFromDefinition( '', 'misc' ) );
32  $this->assertFalse( $repo->newFromDefinition( '<foo|bar>', 'misc' ) );
33  }
34 
35  public function testSimpleCases() {
36  $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' );
37  $this->assertEquals( 'foo_bar', $g->getName() );
38  $this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
39  $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
40  $this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
41  $this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css' ],
42  $g->getScriptsAndStyles() );
43  $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() );
44  $this->assertFalse( $g->supportsResourceLoader() );
45  $this->assertTrue( $g->hasModule() );
46  }
47 
48  public function testRLtag() {
49  $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' );
50  $this->assertEquals( 'foo', $g->getName() );
51  $this->assertTrue( $g->supportsResourceLoader() );
52  $this->assertEquals( 0, count( $g->getLegacyScripts() ) );
53  }
54 
55  public function testDependencies() {
56  $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
57  $this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() );
58  $this->assertTrue( $g->supportsResourceLoader() );
59  $this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() );
60  }
61 
62  public static function provideGetType() {
63  return [
64  [
65  'Default (mixed)',
66  '* foo[ResourceLoader]|bar.css|bar.js',
67  '',
69  ],
70  [
71  'Default (styles only)',
72  '* foo[ResourceLoader]|bar.css',
73  'styles',
75  ],
76  [
77  'Default (scripts only)',
78  '* foo[ResourceLoader]|bar.js',
79  'general',
81  ],
82  [
83  'Default (styles only with dependencies)',
84  '* foo[ResourceLoader|dependencies=jquery.ui]|bar.css',
85  '',
87  ],
88  [
89  'Styles type (mixed)',
90  '* foo[ResourceLoader|type=styles]|bar.css|bar.js',
91  'styles',
93  ],
94  [
95  'Styles type (styles only)',
96  '* foo[ResourceLoader|type=styles]|bar.css',
97  'styles',
99  ],
100  [
101  'Styles type (scripts only)',
102  '* foo[ResourceLoader|type=styles]|bar.js',
103  'styles',
105  ],
106  [
107  'General type (mixed)',
108  '* foo[ResourceLoader|type=general]|bar.css|bar.js',
109  'general',
111  ],
112  [
113  'General type (styles only)',
114  '* foo[ResourceLoader|type=general]|bar.css',
115  'general',
117  ],
118  [
119  'General type (scripts only)',
120  '* foo[ResourceLoader|type=general]|bar.js',
121  'general',
123  ],
124  ];
125  }
126 
130  public function testType( $message, $definition, $gType, $mType ) {
131  $g = $this->create( $definition );
132  $this->assertEquals( $gType, $g->getType(), "Gadget: $message" );
133  $this->assertEquals( $mType, $this->getModule( $g )->getType(), "Module: $message" );
134  }
135 
136  public function testIsHidden() {
137  $g = $this->create( '* foo[hidden]|bar.js' );
138  $this->assertTrue( $g->isHidden() );
139 
140  $g = $this->create( '* foo[ResourceLoader|hidden]|bar.js' );
141  $this->assertTrue( $g->isHidden() );
142 
143  $g = $this->create( '* foo[ResourceLoader]|bar.js' );
144  $this->assertFalse( $g->isHidden() );
145  }
146 
147  public function testPreferences() {
148  $prefs = [];
149  $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
150  // Force usage of a MediaWikiGadgetsDefinitionRepo
151  GadgetRepo::setSingleton( $repo );
152 
153  $gadgets = $repo->fetchStructuredList( '* foo | foo.js
154 ==keep-section1==
155 * bar| bar.js
156 ==remove-section==
157 * baz [rights=embezzle] |baz.js
158 ==keep-section2==
159 * quux [rights=read] | quux.js' );
160  $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
161 
162  $repo->definitionCache = $gadgets;
163  $this->assertTrue( GadgetHooks::getPreferences( new User, $prefs ), 'GetPrefences hook should return true' );
164 
165  $options = $prefs['gadgets']['options'];
166  $this->assertFalse( isset( $options['⧼gadget-section-remove-section⧽'] ), 'Must not show empty sections' );
167  $this->assertTrue( isset( $options['⧼gadget-section-keep-section1⧽'] ) );
168  $this->assertTrue( isset( $options['⧼gadget-section-keep-section2⧽'] ) );
169  }
170 
171  public function tearDown() {
173  parent::tearDown();
174  }
175 }
ResourceLoaderModule\LOAD_GENERAL
const LOAD_GENERAL
Definition: ResourceLoaderModule.php:44
GadgetsTest\testSimpleCases
testSimpleCases()
Definition: GadgetTest.php:35
Gadget\getModuleName
static getModuleName( $id)
Definition: Gadgets_body.php:153
captcha-old.count
count
Definition: captcha-old.py:225
GadgetsTest\testPreferences
testPreferences()
Definition: GadgetTest.php:147
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
GadgetsTest\testInvalidLines
testInvalidLines()
Definition: GadgetTest.php:29
GadgetsTest\getModule
getModule(Gadget $g)
Definition: GadgetTest.php:21
GadgetsTest\create
create( $line)
Definition: GadgetTest.php:14
GadgetsTest\provideGetType
static provideGetType()
Definition: GadgetTest.php:62
MediaWikiGadgetsDefinitionRepo
Gadgets repo powered by MediaWiki:Gadgets-definition.
Definition: MediaWikiGadgetsDefinitionRepo.php:7
GadgetsTest\testType
testType( $message, $definition, $gType, $mType)
provideGetType
Definition: GadgetTest.php:130
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
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
Gadget
Wrapper for one gadget.
Definition: Gadgets_body.php:18
GadgetsTest\testRLtag
testRLtag()
Definition: GadgetTest.php:48
GadgetsTest\tearDown
tearDown()
Definition: GadgetTest.php:171
$line
$line
Definition: cdb.php:58
GadgetHooks\getPreferences
static getPreferences( $user, &$preferences)
GetPreferences hook handler.
Definition: GadgetHooks.php:77
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:42
GadgetsTest\testDependencies
testDependencies()
Definition: GadgetTest.php:55
GadgetRepo\setSingleton
static setSingleton( $repo=null)
Should only be used by unit tests.
Definition: GadgetRepo.php:66
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
GadgetsTest
Gadgets.
Definition: GadgetTest.php:9
GadgetsTest\testIsHidden
testIsHidden()
Definition: GadgetTest.php:136
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049