MediaWiki REL1_30
GadgetTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
14 private function create( $line ) {
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() {
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 'general',
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 'general',
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
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 ),
164 'GetPrefences hook should return true' );
165
166 $options = $prefs['gadgets']['options'];
167 $this->assertArrayNotHasKey( '⧼gadget-section-remove-section⧽', $options,
168 'Must not show empty sections' );
169 $this->assertArrayHasKey( '⧼gadget-section-keep-section1⧽', $options );
170 $this->assertArrayHasKey( '⧼gadget-section-keep-section2⧽', $options );
171 }
172
173 public function tearDown() {
175 parent::tearDown();
176 }
177}
$line
Definition cdb.php:58
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.
Wrapper for one gadget.
static getModuleName( $id)
Gadgets.
Definition GadgetTest.php:9
static provideGetType()
create( $line)
testType( $message, $definition, $gType, $mType)
provideGetType
getModule(Gadget $g)
Gadgets repo powered by MediaWiki:Gadgets-definition.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
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:1971