2019-11-07 19:57:00 +00:00
|
|
|
<?php
|
|
|
|
|
2019-11-12 10:45:13 +00:00
|
|
|
namespace Cite\Tests;
|
2019-11-07 19:57:00 +00:00
|
|
|
|
|
|
|
use ApiQuerySiteinfo;
|
2019-11-19 14:12:11 +00:00
|
|
|
use Cite\Hooks\CiteHooks;
|
2024-09-06 12:21:58 +00:00
|
|
|
use Cite\ReferencePreviews\ReferencePreviewsGadgetsIntegration;
|
2024-04-26 18:05:40 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
|
|
|
use MediaWiki\ResourceLoader\ResourceLoader;
|
2023-11-29 17:55:07 +00:00
|
|
|
use MediaWiki\User\Options\StaticUserOptionsLookup;
|
2024-09-06 12:21:58 +00:00
|
|
|
use MediaWiki\User\User;
|
2019-11-07 19:57:00 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-14 01:53:19 +00:00
|
|
|
* @covers \Cite\Hooks\CiteHooks
|
2019-11-19 10:31:08 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2019-11-07 19:57:00 +00:00
|
|
|
*/
|
|
|
|
class CiteHooksTest extends \MediaWikiIntegrationTestCase {
|
|
|
|
|
2024-04-26 18:05:40 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideBooleans
|
|
|
|
*/
|
|
|
|
public function testOnResourceLoaderGetConfigVars( bool $enabled ) {
|
2019-11-07 19:57:00 +00:00
|
|
|
$vars = [];
|
|
|
|
|
2024-04-26 18:05:40 +00:00
|
|
|
$config = new HashConfig( [
|
|
|
|
'CiteVisualEditorOtherGroup' => $enabled,
|
|
|
|
'CiteResponsiveReferences' => $enabled,
|
|
|
|
'CiteBookReferencing' => $enabled,
|
|
|
|
] );
|
2023-06-21 14:40:12 +00:00
|
|
|
|
2024-09-06 14:25:26 +00:00
|
|
|
( new CiteHooks(
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
2024-09-06 12:21:58 +00:00
|
|
|
$this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
|
2024-09-06 14:25:26 +00:00
|
|
|
new StaticUserOptionsLookup( [] )
|
|
|
|
) )
|
|
|
|
->onResourceLoaderGetConfigVars( $vars, 'vector', $config );
|
2019-11-07 19:57:00 +00:00
|
|
|
|
2024-04-26 18:05:40 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
'wgCiteVisualEditorOtherGroup' => $enabled,
|
|
|
|
'wgCiteResponsiveReferences' => $enabled,
|
|
|
|
'wgCiteBookReferencing' => $enabled,
|
|
|
|
], $vars );
|
2019-11-07 19:57:00 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 18:05:40 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideBooleans
|
|
|
|
*/
|
|
|
|
public function testOnResourceLoaderRegisterModules( bool $enabled ) {
|
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'Popups' );
|
|
|
|
|
|
|
|
$resourceLoader = $this->createMock( ResourceLoader::class );
|
|
|
|
$resourceLoader->method( 'getConfig' )
|
|
|
|
->willReturn( new HashConfig( [ 'CiteReferencePreviews' => $enabled ] ) );
|
|
|
|
$resourceLoader->expects( $this->exactly( (int)$enabled ) )
|
|
|
|
->method( 'register' );
|
|
|
|
|
2024-09-06 14:25:26 +00:00
|
|
|
( new CiteHooks(
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
2024-09-06 12:21:58 +00:00
|
|
|
$this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
|
2024-09-06 14:25:26 +00:00
|
|
|
new StaticUserOptionsLookup( [] )
|
|
|
|
) )
|
|
|
|
->onResourceLoaderRegisterModules( $resourceLoader );
|
2024-04-26 18:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideBooleans
|
|
|
|
*/
|
|
|
|
public function testOnAPIQuerySiteInfoGeneralInfo( bool $enabled ) {
|
2019-11-07 19:57:00 +00:00
|
|
|
$api = $this->createMock( ApiQuerySiteinfo::class );
|
2020-12-28 15:40:47 +00:00
|
|
|
$api->expects( $this->once() )
|
|
|
|
->method( 'getConfig' )
|
2024-04-26 18:05:40 +00:00
|
|
|
->willReturn( new HashConfig( [ 'CiteResponsiveReferences' => $enabled ] ) );
|
2020-12-28 15:40:47 +00:00
|
|
|
|
2019-11-07 19:57:00 +00:00
|
|
|
$data = [];
|
|
|
|
|
2024-09-06 14:25:26 +00:00
|
|
|
( new CiteHooks(
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
2024-09-06 12:21:58 +00:00
|
|
|
$this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
|
2024-09-06 14:25:26 +00:00
|
|
|
new StaticUserOptionsLookup( [] )
|
|
|
|
) )
|
|
|
|
->onAPIQuerySiteInfoGeneralInfo( $api, $data );
|
2019-11-07 19:57:00 +00:00
|
|
|
|
2024-04-26 18:05:40 +00:00
|
|
|
$this->assertSame( [ 'citeresponsivereferences' => $enabled ], $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideBooleans() {
|
|
|
|
yield [ true ];
|
|
|
|
yield [ false ];
|
2019-11-07 19:57:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 12:21:58 +00:00
|
|
|
public function testOnGetPreferences_noConflicts() {
|
|
|
|
$expected = [
|
|
|
|
'popups-reference-previews' => [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'popups-refpreview-user-preference-label',
|
|
|
|
'help-message' => 'popups-prefs-conflicting-gadgets-info',
|
|
|
|
'section' => 'rendering/reading'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$gadgetsIntegrationMock = $this->createMock( ReferencePreviewsGadgetsIntegration::class );
|
|
|
|
$prefs = [];
|
|
|
|
( new CiteHooks(
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
|
|
|
$gadgetsIntegrationMock,
|
|
|
|
new StaticUserOptionsLookup( [] )
|
|
|
|
) )
|
|
|
|
->onGetPreferences( $this->createMock( User::class ), $prefs );
|
|
|
|
$this->assertEquals( $expected, $prefs );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnGetPreferences_conflictingGadget() {
|
|
|
|
$expected = [
|
|
|
|
'popups-reference-previews' => [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'popups-refpreview-user-preference-label',
|
|
|
|
// 'help-message' => 'popups-prefs-conflicting-gadgets-info',
|
|
|
|
'help-message' => [
|
|
|
|
'popups-prefs-navpopups-gadget-conflict-info',
|
|
|
|
'Special:Preferences#mw-prefsection-gadgets',
|
|
|
|
],
|
|
|
|
'section' => 'rendering/reading',
|
|
|
|
'disabled' => true
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$gadgetsIntegrationMock = $this->createMock( ReferencePreviewsGadgetsIntegration::class );
|
|
|
|
$gadgetsIntegrationMock->expects( $this->once() )
|
|
|
|
->method( 'isNavPopupsGadgetEnabled' )
|
|
|
|
->willReturn( true );
|
|
|
|
$prefs = [];
|
|
|
|
( new CiteHooks(
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
|
|
|
$gadgetsIntegrationMock,
|
|
|
|
new StaticUserOptionsLookup( [] )
|
|
|
|
) )
|
|
|
|
->onGetPreferences( $this->createMock( User::class ), $prefs );
|
|
|
|
$this->assertEquals( $expected, $prefs );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnGetPreferences_redundantPreference() {
|
|
|
|
$prefs = [
|
|
|
|
'popups-reference-previews' => [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'from-another-extension',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$expected = $prefs;
|
|
|
|
( new CiteHooks(
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
|
|
|
$this->getServiceContainer()->getService( 'Cite.GadgetsIntegration' ),
|
|
|
|
new StaticUserOptionsLookup( [] )
|
|
|
|
) )
|
|
|
|
->onGetPreferences( $this->createMock( User::class ), $prefs );
|
|
|
|
$this->assertEquals( $expected, $prefs );
|
|
|
|
}
|
|
|
|
|
2019-11-07 19:57:00 +00:00
|
|
|
}
|