mediawiki-extensions-Gadgets/tests/phpunit/integration/GadgetHooksTest.php
Daimona Eaytoy b6e76eeb3a Add GadgetHooksTest to the Database group
The test uses black magic to override the behaviour of
MediaWikiGadgetsDefinitionRepo. However, this magic doesn't work well
with I2ac659a4, which disabled gadget loading in non-database tests.
This test is the only one that needs that code to be run, although it
won't actually make any queries because of said black magic.

Add the test to the Database group as a temporary (TM) workaround to
replacing the TestingAccessWrapper magic with something else.

Change-Id: I7d9fd612dd685a5728d3a7a9ced97fd5ac45f491
2023-08-06 22:55:00 +02:00

61 lines
1.9 KiB
PHP

<?php
use MediaWiki\Extension\Gadgets\GadgetRepo;
use MediaWiki\Extension\Gadgets\Hooks as GadgetHooks;
use MediaWiki\Extension\Gadgets\MediaWikiGadgetsDefinitionRepo;
use Wikimedia\TestingAccessWrapper;
/**
* @group Gadgets
* @group Database
*/
class GadgetHooksTest extends MediaWikiIntegrationTestCase {
public function tearDown(): void {
GadgetRepo::setSingleton();
parent::tearDown();
}
/**
* @covers \MediaWiki\Extension\Gadgets\Gadget
* @covers \MediaWiki\Extension\Gadgets\Hooks::onGetPreferences
* @covers \MediaWiki\Extension\Gadgets\GadgetRepo
* @covers \MediaWiki\Extension\Gadgets\MediaWikiGadgetsDefinitionRepo
*/
public function testPreferences() {
$prefs = [];
$testRightAllowed = 'gadget-test-right-allowed';
$testRightNotAllowed = 'gadget-test-right-notallowed';
$repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
// Force usage of a MediaWikiGadgetsDefinitionRepo
GadgetRepo::setSingleton( $repo );
$gadgetsDef = <<<EOT
* foo | foo.js
==keep-section1==
* bar| bar.js
==remove-section==
* baz [rights=$testRightNotAllowed] |baz.js
==keep-section2==
* quux [rights=$testRightAllowed] | quux.js
EOT;
/** @var MediaWikiGadgetsDefinitionRepo $repo */
$gadgets = $repo->fetchStructuredList( $gadgetsDef );
$this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
$repo->definitions = $gadgets;
$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 );
$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'] );
}
}