diff --git a/tests/phpunit/CiteHooksTest.php b/tests/phpunit/CiteHooksTest.php new file mode 100644 index 000000000..e9e2b3125 --- /dev/null +++ b/tests/phpunit/CiteHooksTest.php @@ -0,0 +1,79 @@ +setMwGlobals( [ + 'wgCiteStoreReferencesData' => true, + ] ); + } + + /** + * @covers ::onLinksUpdateComplete + */ + public function testOnLinksUpdateComplete() { + $cache = $this->getMockBuilder( WANObjectCache::class ) + ->setConstructorArgs( [ [ 'cache' => new HashBagOStuff() ] ] ) + ->setMethods( [ 'makeKey', 'relayPurge' ] ) + ->getMock(); + $cache->method( 'makeKey' ) + ->willReturn( '' ); + // What we actually want to check here is if WANObjectCache::delete() is called, but it's + // final and can't be mocked. + $cache->expects( $this->once() ) + ->method( 'relayPurge' ) + ->with( + 'WANCache:v:', + WANObjectCache::MAX_COMMIT_DELAY, + WANObjectCache::HOLDOFF_TTL_NONE + ); + $this->setService( 'MainWANObjectCache', $cache ); + + $linksUpdate = $this->createMock( LinksUpdate::class ); + $linksUpdate->method( 'getAddedProperties' ) + ->willReturn( [ 'references-1' => true ] ); + $linksUpdate->method( 'getTitle' ) + ->willReturn( $this->createMock( Title::class ) ); + + CiteHooks::onLinksUpdateComplete( $linksUpdate ); + } + + /** + * @covers ::onResourceLoaderGetConfigVars + */ + public function testOnResourceLoaderGetConfigVars() { + $vars = []; + + CiteHooks::onResourceLoaderGetConfigVars( $vars ); + + $this->assertArrayHasKey( 'wgCiteVisualEditorOtherGroup', $vars ); + $this->assertArrayHasKey( 'wgCiteResponsiveReferences', $vars ); + } + + /** + * @covers ::onAPIQuerySiteInfoGeneralInfo + */ + public function testOnAPIQuerySiteInfoGeneralInfo() { + $api = $this->createMock( ApiQuerySiteinfo::class ); + $data = []; + + CiteHooks::onAPIQuerySiteInfoGeneralInfo( $api, $data ); + + $this->assertArrayHasKey( 'citeresponsivereferences', $data ); + } + +} diff --git a/tests/phpunit/unit/CiteHooksTest.php b/tests/phpunit/unit/CiteHooksTest.php new file mode 100644 index 000000000..c8b53d784 --- /dev/null +++ b/tests/phpunit/unit/CiteHooksTest.php @@ -0,0 +1,105 @@ +createMock( Parser::class ); + $parser->expects( $this->exactly( 2 ) ) + ->method( 'setHook' ) + ->withConsecutive( + [ 'ref', $this->isType( 'callable' ) ], + [ 'references', $this->isType( 'callable' ) ] + ); + + CiteHooks::onParserFirstCallInit( $parser ); + + $this->assertInstanceOf( Cite::class, $parser->extCite ); + } + + /** + * @covers ::onContentHandlerDefaultModelFor + */ + public function testOnContentHandlerDefaultModelFor() { + $title = $this->createMock( Title::class ); + $title->method( 'inNamespace' ) + ->willReturn( true ); + $title->method( 'getText' ) + ->willReturn( 'Cite-tool-definition.json' ); + + CiteHooks::onContentHandlerDefaultModelFor( $title, $model ); + + $this->assertSame( CONTENT_MODEL_JSON, $model ); + } + + /** + * @covers ::onResourceLoaderTestModules + */ + public function testOnResourceLoaderTestModules() { + $testModules = []; + $resourceLoader = $this->createMock( ResourceLoader::class ); + $resourceLoader->method( 'getConfig' ) + ->willReturn( new HashConfig( [ + 'ResourceModules' => [ 'ext.visualEditor.mediawiki' => true ], + ] ) ); + + CiteHooks::onResourceLoaderTestModules( $testModules, $resourceLoader ); + + $this->assertArrayHasKey( 'ext.cite.visualEditor.test', $testModules['qunit'] ); + } + + /** + * @covers ::onResourceLoaderRegisterModules + */ + public function testOnResourceLoaderRegisterModules() { + $resourceLoader = $this->createMock( ResourceLoader::class ); + $resourceLoader->expects( $this->atLeastOnce() ) + ->method( 'register' ); + + CiteHooks::onResourceLoaderRegisterModules( $resourceLoader ); + } + + /** + * @covers ::onLinksUpdate + */ + public function testOnLinksUpdate() { + $parserOutput = $this->createMock( ParserOutput::class ); + $parserOutput->method( 'getExtensionData' ) + ->willReturn( true ); + $parserOutput->expects( $this->once() ) + ->method( 'setExtensionData' ) + ->with( Cite::EXT_DATA_KEY, null ); + + $linksUpdate = $this->createMock( LinksUpdate::class ); + $linksUpdate->method( 'getParserOutput' ) + ->willReturn( $parserOutput ); + + CiteHooks::onLinksUpdate( $linksUpdate ); + + $this->assertArrayHasKey( 'references-1', $linksUpdate->mProperties ); + } + +}