| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Extension\Gadgets\Gadget; |
| 5 |
use MediaWiki\Extension\Gadgets\Hooks as GadgetHooks; |
| 6 |
use MediaWiki\Extension\Gadgets\StaticGadgetRepo; |
| 7 |
use MediaWiki\Output\OutputPage; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class GadgetHooksTest extends MediaWikiIntegrationTestCase { |
| 17 |
|
| 18 |
public function testDefaultGadget() { |
| 19 |
$services = $this->getServiceContainer(); |
| 20 |
$repo = new StaticGadgetRepo( [ |
| 21 |
'g1' => new Gadget( [ 'name' => 'g1', 'onByDefault' => true, 'pages' => [ 'test.css' ] ] ), |
| 22 |
] ); |
| 23 |
$hooks = new GadgetHooks( $repo, $services->getUserOptionsLookup(), null ); |
| 24 |
$out = new OutputPage( RequestContext::getMain() ); |
| 25 |
$out->setTitle( Title::newMainPage() ); |
| 26 |
$skin = $this->createMock( Skin::class ); |
| 27 |
$hooks->onBeforePageDisplay( $out, $skin ); |
| 28 |
$this->assertArrayEquals( [ 'ext.gadget.g1' ], $out->getModuleStyles() ); |
| 29 |
} |
| 30 |
|
| 31 |
public function testEnabledGadget() { |
| 32 |
$services = $this->getServiceContainer(); |
| 33 |
$repo = new StaticGadgetRepo( [ |
| 34 |
'g1' => new Gadget( [ 'name' => 'g1', 'pages' => [ 'test.js' ], 'resourceLoaded' => true ] ), |
| 35 |
] ); |
| 36 |
$hooks = new GadgetHooks( $repo, $services->getUserOptionsLookup(), null ); |
| 37 |
$context = RequestContext::getMain(); |
| 38 |
$out = new OutputPage( $context ); |
| 39 |
$out->setTitle( Title::newMainPage() ); |
| 40 |
$user = $this->getTestUser()->getUser(); |
| 41 |
$context->setUser( $user ); |
| 42 |
$skin = $this->createMock( Skin::class ); |
| 43 |
|
| 44 |
$hooks->onBeforePageDisplay( $out, $skin ); |
| 45 |
$this->assertArrayEquals( [], $out->getModules() ); |
| 46 |
|
| 47 |
$services->getUserOptionsManager()->setOption( $user, 'gadget-g1', true ); |
| 48 |
$services->getUserOptionsManager()->saveOptions( $user ); |
| 49 |
$hooks->onBeforePageDisplay( $out, $skin ); |
| 50 |
$this->assertArrayEquals( [ 'ext.gadget.g1' ], $out->getModules() ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testDefaultUserOptions() { |
| 54 |
$repo = new StaticGadgetRepo( [ |
| 55 |
'g1' => new Gadget( [ 'name' => 'g1', 'pages' => [ 'test.css' ], 'onByDefault' => true ] ), |
| 56 |
'g2' => new Gadget( [ 'name' => 'g2', 'pages' => [ 'test.css' ] ] ), |
| 57 |
'g3' => new Gadget( [ 'name' => 'g3', 'pages' => [ 'test.css' ], 'hidden' => true ] ), |
| 58 |
] ); |
| 59 |
$this->setService( 'GadgetsRepo', $repo ); |
| 60 |
$optionsLookup = $this->getServiceContainer()->getUserOptionsLookup(); |
| 61 |
$user = $this->getTestUser()->getUser(); |
| 62 |
$this->assertSame( 1, $optionsLookup->getOption( $user, 'gadget-g1' ) ); |
| 63 |
$this->assertSame( 0, $optionsLookup->getOption( $user, 'gadget-g2' ) ); |
| 64 |
$this->assertNull( $optionsLookup->getOption( $user, 'gadget-g3' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
public function testPreferences() { |
| 68 |
$services = $this->getServiceContainer(); |
| 69 |
$repo = new StaticGadgetRepo( [ |
| 70 |
'foo' => new Gadget( [ 'name' => 'foo', 'pages' => [ 'foo.css' ] ] ), |
| 71 |
'bar' => new Gadget( [ 'name' => 'bar', 'pages' => [ 'bar.css' ], |
| 72 |
'category' => 'keep-section1' ] ), |
| 73 |
'baz' => new Gadget( [ 'name' => 'baz', 'pages' => [ 'baz.css' ], 'requiredRights' => [ 'delete' ], |
| 74 |
'category' => 'remove-section' ] ), |
| 75 |
'quux' => new Gadget( [ 'name' => 'quux', 'pages' => [ 'quux.css' ], 'requiredRights' => [ 'read' ], |
| 76 |
'category' => 'keep-section2' ] ), |
| 77 |
] ); |
| 78 |
$hooks = new GadgetHooks( $repo, $services->getUserOptionsLookup(), null ); |
| 79 |
|
| 80 |
$user = $this->getTestUser()->getUser(); |
| 81 |
$hooks->onGetPreferences( $user, $prefs ); |
| 82 |
|
| 83 |
|
| 84 |
$this->assertEquals( 'check', $prefs['gadget-bar']['type'] ); |
| 85 |
$this->assertEquals( 'api', $prefs['gadget-baz']['type'], |
| 86 |
'Must not show unavailable gadgets' ); |
| 87 |
$this->assertEquals( 'gadgets/gadget-section-keep-section2', $prefs['gadget-quux']['section'] ); |
| 88 |
|
| 89 |
$services->getUserGroupManager()->addUserToGroup( $user, 'sysop' ); |
| 90 |
$hooks->onGetPreferences( $user, $prefs ); |
| 91 |
$this->assertEquals( 'check', $prefs['gadget-bar']['type'] ); |
| 92 |
|
| 93 |
$this->assertEquals( 'check', $prefs['gadget-baz']['type'] ); |
| 94 |
} |
| 95 |
} |