mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-23 14:36:51 +00:00
[refactor] Rewrite context as service
Puts the ReferencePreviewsContext behind a service to improve testability. Bug: T363162 Change-Id: I63aa4144081a2625da1b89f4d56adb612741c8fc
This commit is contained in:
parent
878a29b727
commit
8e4c83bfc6
|
@ -39,6 +39,7 @@
|
|||
"main": {
|
||||
"class": "Cite\\Hooks\\CiteHooks",
|
||||
"services": [
|
||||
"Cite.ReferencePreviewsContext",
|
||||
"UserOptionsLookup"
|
||||
]
|
||||
},
|
||||
|
@ -239,6 +240,9 @@
|
|||
"localBasePath": "modules",
|
||||
"remoteExtPath": "Cite/modules"
|
||||
},
|
||||
"ServiceWiringFiles": [
|
||||
"src/ServiceWiring.php"
|
||||
],
|
||||
"QUnitTestModule": {
|
||||
"localBasePath": "",
|
||||
"remoteExtPath": "Cite",
|
||||
|
|
|
@ -35,9 +35,14 @@ class CiteHooks implements
|
|||
EditPage__showEditForm_initialHook
|
||||
{
|
||||
|
||||
private ReferencePreviewsContext $referencePreviewsContext;
|
||||
private UserOptionsLookup $userOptionsLookup;
|
||||
|
||||
public function __construct( UserOptionsLookup $userOptionsLookup ) {
|
||||
public function __construct(
|
||||
ReferencePreviewsContext $referencePreviewsContext,
|
||||
UserOptionsLookup $userOptionsLookup
|
||||
) {
|
||||
$this->referencePreviewsContext = $referencePreviewsContext;
|
||||
$this->userOptionsLookup = $userOptionsLookup;
|
||||
}
|
||||
|
||||
|
@ -66,11 +71,7 @@ class CiteHooks implements
|
|||
* @param OutputPage $out
|
||||
*/
|
||||
public function onMakeGlobalVariablesScript( &$vars, $out ): void {
|
||||
$referencePreviewsContext = new ReferencePreviewsContext(
|
||||
$out->getConfig(),
|
||||
$this->userOptionsLookup
|
||||
);
|
||||
$vars['wgCiteReferencePreviewsActive'] = $referencePreviewsContext->isReferencePreviewsEnabled(
|
||||
$vars['wgCiteReferencePreviewsActive'] = $this->referencePreviewsContext->isReferencePreviewsEnabled(
|
||||
$out->getUser(),
|
||||
$out->getSkin()
|
||||
);
|
||||
|
|
16
src/ServiceWiring.php
Normal file
16
src/ServiceWiring.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
use Cite\ReferencePreviews\ReferencePreviewsContext;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
return [
|
||||
'Cite.ReferencePreviewsContext' => static function ( MediaWikiServices $services ): ReferencePreviewsContext {
|
||||
return new ReferencePreviewsContext(
|
||||
$services->getMainConfig(),
|
||||
$services->getUserOptionsLookup()
|
||||
);
|
||||
},
|
||||
];
|
|
@ -26,8 +26,11 @@ class CiteHooksTest extends \MediaWikiIntegrationTestCase {
|
|||
'CiteBookReferencing' => $enabled,
|
||||
] );
|
||||
|
||||
$citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) );
|
||||
$citeHooks->onResourceLoaderGetConfigVars( $vars, 'vector', $config );
|
||||
( new CiteHooks(
|
||||
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
||||
new StaticUserOptionsLookup( [] )
|
||||
) )
|
||||
->onResourceLoaderGetConfigVars( $vars, 'vector', $config );
|
||||
|
||||
$this->assertSame( [
|
||||
'wgCiteVisualEditorOtherGroup' => $enabled,
|
||||
|
@ -48,8 +51,11 @@ class CiteHooksTest extends \MediaWikiIntegrationTestCase {
|
|||
$resourceLoader->expects( $this->exactly( (int)$enabled ) )
|
||||
->method( 'register' );
|
||||
|
||||
$citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) );
|
||||
$citeHooks->onResourceLoaderRegisterModules( $resourceLoader );
|
||||
( new CiteHooks(
|
||||
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
||||
new StaticUserOptionsLookup( [] )
|
||||
) )
|
||||
->onResourceLoaderRegisterModules( $resourceLoader );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,8 +69,11 @@ class CiteHooksTest extends \MediaWikiIntegrationTestCase {
|
|||
|
||||
$data = [];
|
||||
|
||||
$citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) );
|
||||
$citeHooks->onAPIQuerySiteInfoGeneralInfo( $api, $data );
|
||||
( new CiteHooks(
|
||||
$this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ),
|
||||
new StaticUserOptionsLookup( [] )
|
||||
) )
|
||||
->onAPIQuerySiteInfoGeneralInfo( $api, $data );
|
||||
|
||||
$this->assertSame( [ 'citeresponsivereferences' => $enabled ], $data );
|
||||
}
|
||||
|
|
|
@ -31,8 +31,6 @@ class ReferencePreviewsContextTest extends MediaWikiIntegrationTestCase {
|
|||
] );
|
||||
$userOptLookup = $this->createNoOpMock( UserOptionsLookup::class );
|
||||
|
||||
$context = new ReferencePreviewsContext( $config, $userOptLookup );
|
||||
|
||||
$user = $this->createMock( User::class );
|
||||
$user->method( 'isNamed' )->willReturn( false );
|
||||
|
||||
|
@ -40,7 +38,11 @@ class ReferencePreviewsContextTest extends MediaWikiIntegrationTestCase {
|
|||
$skin->method( 'getSkinName' )->willReturn( $skinName );
|
||||
|
||||
$this->assertSame( $expected,
|
||||
$context->isReferencePreviewsEnabled( $user, $skin ),
|
||||
( new ReferencePreviewsContext(
|
||||
$config,
|
||||
$userOptLookup
|
||||
) )
|
||||
->isReferencePreviewsEnabled( $user, $skin ),
|
||||
( $expected ? 'A' : 'No' ) . ' module is sent to the user.' );
|
||||
}
|
||||
|
||||
|
@ -73,12 +75,14 @@ class ReferencePreviewsContextTest extends MediaWikiIntegrationTestCase {
|
|||
'CiteReferencePreviewsConflictingRefTooltipsGadgetName' => '',
|
||||
] );
|
||||
|
||||
$context = new ReferencePreviewsContext( $config, $userOptLookup );
|
||||
|
||||
$skin = $this->createMock( Skin::class );
|
||||
|
||||
$this->assertSame( $expected,
|
||||
$context->isReferencePreviewsEnabled( $user, $skin ),
|
||||
( new ReferencePreviewsContext(
|
||||
$config,
|
||||
$userOptLookup
|
||||
) )
|
||||
->isReferencePreviewsEnabled( $user, $skin ),
|
||||
( $expected ? 'A' : 'No' ) . ' module is sent to the user.' );
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Cite\Tests\Unit;
|
||||
|
||||
use Cite\Hooks\CiteHooks;
|
||||
use Cite\ReferencePreviews\ReferencePreviewsContext;
|
||||
use MediaWiki\Title\Title;
|
||||
use MediaWiki\User\Options\StaticUserOptionsLookup;
|
||||
|
||||
|
@ -19,8 +20,11 @@ class CiteHooksUnitTest extends \MediaWikiUnitTestCase {
|
|||
$title->method( 'getText' )
|
||||
->willReturn( 'Cite-tool-definition.json' );
|
||||
|
||||
$citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) );
|
||||
$citeHooks->onContentHandlerDefaultModelFor( $title, $model );
|
||||
( new CiteHooks(
|
||||
$this->createMock( ReferencePreviewsContext::class ),
|
||||
new StaticUserOptionsLookup( [] )
|
||||
) )
|
||||
->onContentHandlerDefaultModelFor( $title, $model );
|
||||
|
||||
$this->assertSame( CONTENT_MODEL_JSON, $model );
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue