2019-07-22 08:34:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-06 02:05:20 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
2022-02-06 18:54:47 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\Hooks as GadgetHooks;
|
|
|
|
use MediaWiki\Extension\Gadgets\MediaWikiGadgetsDefinitionRepo;
|
2019-07-22 08:34:21 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Gadgets
|
2023-08-06 20:55:00 +00:00
|
|
|
* @group Database
|
2019-07-22 08:34:21 +00:00
|
|
|
*/
|
|
|
|
class GadgetHooksTest extends MediaWikiIntegrationTestCase {
|
2021-07-22 09:18:47 +00:00
|
|
|
public function tearDown(): void {
|
2019-07-22 08:34:21 +00:00
|
|
|
GadgetRepo::setSingleton();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-06 18:54:47 +00:00
|
|
|
* @covers \MediaWiki\Extension\Gadgets\Gadget
|
2021-12-06 10:34:17 +00:00
|
|
|
* @covers \MediaWiki\Extension\Gadgets\Hooks::onGetPreferences
|
2022-02-06 18:54:47 +00:00
|
|
|
* @covers \MediaWiki\Extension\Gadgets\GadgetRepo
|
|
|
|
* @covers \MediaWiki\Extension\Gadgets\MediaWikiGadgetsDefinitionRepo
|
2019-07-22 08:34:21 +00:00
|
|
|
*/
|
|
|
|
public function testPreferences() {
|
|
|
|
$prefs = [];
|
2023-07-17 21:11:25 +00:00
|
|
|
$testRightAllowed = 'gadget-test-right-allowed';
|
|
|
|
$testRightNotAllowed = 'gadget-test-right-notallowed';
|
2019-07-22 08:34:21 +00:00
|
|
|
$repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
|
|
|
|
// Force usage of a MediaWikiGadgetsDefinitionRepo
|
|
|
|
GadgetRepo::setSingleton( $repo );
|
|
|
|
|
2023-07-17 21:11:25 +00:00
|
|
|
$gadgetsDef = <<<EOT
|
|
|
|
* foo | foo.js
|
2019-07-22 08:34:21 +00:00
|
|
|
==keep-section1==
|
|
|
|
* bar| bar.js
|
|
|
|
==remove-section==
|
2023-07-17 21:11:25 +00:00
|
|
|
* baz [rights=$testRightNotAllowed] |baz.js
|
2019-07-22 08:34:21 +00:00
|
|
|
==keep-section2==
|
2023-07-17 21:11:25 +00:00
|
|
|
* quux [rights=$testRightAllowed] | quux.js
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
/** @var MediaWikiGadgetsDefinitionRepo $repo */
|
|
|
|
$gadgets = $repo->fetchStructuredList( $gadgetsDef );
|
2019-07-22 08:34:21 +00:00
|
|
|
$this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
|
|
|
|
|
2022-04-05 19:00:51 +00:00
|
|
|
$repo->definitions = $gadgets;
|
2023-07-17 21:11:25 +00:00
|
|
|
|
|
|
|
$user = $this->createMock( User::class );
|
|
|
|
$user->method( 'isAllowedAll' )
|
|
|
|
->willReturnCallback( static function ( ...$rights ) use ( $testRightNotAllowed ): bool {
|
|
|
|
return !in_array( $testRightNotAllowed, $rights, true );
|
|
|
|
} );
|
|
|
|
( new GadgetHooks() )->onGetPreferences( $user, $prefs );
|
2019-07-22 08:34:21 +00:00
|
|
|
|
2021-11-05 22:12:36 +00:00
|
|
|
$this->assertArrayHasKey( 'gadget-bar', $prefs );
|
|
|
|
$this->assertArrayNotHasKey( 'gadget-baz', $prefs,
|
|
|
|
'Must not show unavailable gadgets' );
|
|
|
|
$this->assertEquals( 'gadgets/gadget-section-keep-section2', $prefs['gadget-quux']['section'] );
|
2019-07-22 08:34:21 +00:00
|
|
|
}
|
|
|
|
}
|