diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/SemanticMediaWikiProvidedHookInterfaceIntegrationTest.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/SemanticMediaWikiProvidedHookInterfaceIntegrationTest.php | 471 |
1 files changed, 471 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/SemanticMediaWikiProvidedHookInterfaceIntegrationTest.php b/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/SemanticMediaWikiProvidedHookInterfaceIntegrationTest.php new file mode 100644 index 00000000..4a74f0ff --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/SemanticMediaWikiProvidedHookInterfaceIntegrationTest.php @@ -0,0 +1,471 @@ +<?php + +namespace SMW\Tests\Integration; + +use RuntimeException; +use SMW\ApplicationFactory; +use SMW\DIWikiPage; +use SMW\Tests\TestEnvironment; + +/** + * @group semantic-mediawiki + * + * @license GNU GPL v2+ + * @since 2.1 + * + * @author mwjames + */ +class SemanticMediaWikiProvidedHookInterfaceIntegrationTest extends \PHPUnit_Framework_TestCase { + + private $mwHooksHandler; + private $applicationFactory; + private $spyLogger; + + protected function setUp() { + parent::setUp(); + + $this->testEnvironment = new TestEnvironment(); + $this->spyLogger = $this->testEnvironment->newSpyLogger(); + + $this->mwHooksHandler = $this->testEnvironment->getUtilityFactory()->newMwHooksHandler(); + $this->mwHooksHandler->deregisterListedHooks(); + + $this->applicationFactory = ApplicationFactory::getInstance(); + } + + protected function tearDown() { + $this->mwHooksHandler->restoreListedHooks(); + $this->applicationFactory->clear(); + $this->testEnvironment->tearDown(); + parent::tearDown(); + } + + /** + * @dataProvider storeClassProvider + */ + public function testUnregisteredQueryResultHook( $storeClass ) { + + $query = $this->getMockBuilder( '\SMWQuery' ) + ->disableOriginalConstructor() + ->getMock(); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'fetchQueryResult' ] ) + ->getMock(); + + $store->expects( $this->once() ) + ->method( 'fetchQueryResult' ); + + $store->getQueryResult( $query ); + } + + /** + * @dataProvider storeClassProvider + */ + public function testRegisteredStoreBeforeQueryResultLookupCompleteHookToPreFetchQueryResult( $storeClass ) { + + $query = $this->getMockBuilder( '\SMWQuery' ) + ->disableOriginalConstructor() + ->getMock(); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'fetchQueryResult' ] ) + ->getMock(); + + $store->expects( $this->once() ) + ->method( 'fetchQueryResult' ); + + $this->mwHooksHandler->register( 'SMW::Store::BeforeQueryResultLookupComplete', function( $store, $query, &$queryResult ) { + $queryResult = 'Foo'; + return true; + } ); + + $this->assertNotEquals( + 'Foo', + $store->getQueryResult( $query ) + ); + } + + /** + * @dataProvider storeClassProvider + */ + public function testRegisteredStoreBeforeQueryResultLookupCompleteHookToSuppressDefaultQueryResultFetch( $storeClass ) { + + $query = $this->getMockBuilder( '\SMWQuery' ) + ->disableOriginalConstructor() + ->getMock(); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'fetchQueryResult' ] ) + ->getMock(); + + $store->expects( $this->never() ) + ->method( 'fetchQueryResult' ); + + $this->mwHooksHandler->register( 'SMW::Store::BeforeQueryResultLookupComplete', function( $store, $query, &$queryResult ) { + + $queryResult = 'Foo'; + + // Return false to suppress additional calls to fetchQueryResult + return false; + } ); + + $this->assertEquals( + 'Foo', + $store->getQueryResult( $query ) + ); + } + + /** + * @dataProvider storeClassProvider + */ + public function testRegisteredStoreAfterQueryResultLookupComplete( $storeClass ) { + + $queryResult = $this->getMockBuilder( '\SMWQueryResult' ) + ->disableOriginalConstructor() + ->getMock(); + + $query = $this->getMockBuilder( '\SMWQuery' ) + ->disableOriginalConstructor() + ->getMock(); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'fetchQueryResult' ] ) + ->getMock(); + + $store->expects( $this->once() ) + ->method( 'fetchQueryResult' ) + ->will( $this->returnValue( $queryResult ) ); + + $this->mwHooksHandler->register( 'SMW::Store::AfterQueryResultLookupComplete', function( $store, &$queryResult ) { + + if ( !$queryResult instanceof \SMWQueryResult ) { + throw new RuntimeException( 'Expected a SMWQueryResult instance' ); + } + + return true; + } ); + + $store->getQueryResult( $query ); + } + + /** + * @dataProvider storeClassProvider + */ + public function testRegisteredFactboxBeforeContentGenerationToSuppressDefaultTableCreation( $storeClass ) { + + $this->applicationFactory->getSettings()->set( 'smwgShowFactbox', SMW_FACTBOX_NONEMPTY ); + + $connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' ) + ->disableOriginalConstructor() + ->getMock(); + + $semanticData = $this->getMockBuilder( '\SMW\SemanticData' ) + ->disableOriginalConstructor() + ->getMock(); + + $semanticData->expects( $this->once() ) + ->method( 'getSubject' ) + ->will( $this->returnValue( new DIWikiPage( 'Bar', NS_MAIN ) ) ); + + $semanticData->expects( $this->any() ) + ->method( 'hasVisibleProperties' ) + ->will( $this->returnValue( true ) ); + + $store = $this->getMockBuilder( $storeClass ) + ->disableOriginalConstructor() + ->setMethods( [ 'getSemanticData', 'getConnection' ] ) + ->getMock(); + + $store->expects( $this->any() ) + ->method( 'getConnection' ) + ->will( $this->returnValue( $connection ) ); + + $store->expects( $this->once() ) + ->method( 'getSemanticData' ) + ->will( $this->returnValue( $semanticData ) ); + + $this->applicationFactory->registerObject( 'Store', $store ); + + $this->mwHooksHandler->register( 'SMW::Factbox::BeforeContentGeneration', function( &$text, $semanticData ) { + $text = $semanticData->getSubject()->getTitle()->getText(); + return false; + } ); + + $title = $this->getMockBuilder( '\Title' ) + ->disableOriginalConstructor() + ->getMock(); + + $title->expects( $this->any() ) + ->method( 'getNamespace' ) + ->will( $this->returnValue( NS_MAIN ) ); + + $title->expects( $this->once() ) + ->method( 'getDBKey' ) + ->will( $this->returnValue( 'Foo' ) ); + + $instance = $this->applicationFactory->singleton( 'FactboxFactory' )->newFactbox( $title, new \ParserOutput() ); + $instance->doBuild(); + + $this->assertEquals( + 'Bar', + $instance->getContent() + ); + } + + public function testRegisteredSQLStoreBeforeChangeTitleComplete() { + + // To make this work with SPARQLStore, need to inject the basestore + $storeClass = '\SMWSQLStore3'; + + $title = \Title::newFromText( __METHOD__ ); + + $idGenerator = $this->getMockBuilder( '\SMWSql3SmwIds' ) + ->disableOriginalConstructor() + ->getMock(); + + $idGenerator->expects( $this->any() ) + ->method( 'getSMWPropertyID' ) + ->will( $this->returnValue( 42 ) ); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'getObjectIds', 'getPropertyTables' ] ) + ->getMock(); + + $store->expects( $this->any() ) + ->method( 'getObjectIds' ) + ->will( $this->returnValue( $idGenerator ) ); + + $store->expects( $this->any() ) + ->method( 'getPropertyTables' ) + ->will( $this->returnValue( [] ) ); + + $null = 0; + + $this->mwHooksHandler->register( 'SMW::SQLStore::BeforeChangeTitleComplete', function( $store, $oldTitle, $newTitle, $pageId, $redirectId ) { + return $store->reachedTheBeforeChangeTitleCompleteHook = true; + } ); + + $store->changeTitle( $title, $title, $null, $null ); + + $this->assertTrue( + $store->reachedTheBeforeChangeTitleCompleteHook + ); + } + + public function testRegisteredSQLStoreBeforeDeleteSubjectComplete() { + + // To make this work with SPARQLStore, need to inject the basestore + $storeClass = '\SMWSQLStore3'; + + $title = \Title::newFromText( __METHOD__ ); + + $idGenerator = $this->getMockBuilder( '\SMWSql3SmwIds' ) + ->disableOriginalConstructor() + ->getMock(); + + $idGenerator->expects( $this->any() ) + ->method( 'findAllEntitiesThatMatch' ) + ->will( $this->returnValue( [ 42 ] ) ); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'getPropertyTables', 'getObjectIds' ] ) + ->getMock(); + + $store->expects( $this->any() ) + ->method( 'getObjectIds' ) + ->will( $this->returnValue( $idGenerator ) ); + + $store->expects( $this->any() ) + ->method( 'getPropertyTables' ) + ->will( $this->returnValue( [] ) ); + + $this->mwHooksHandler->register( 'SMW::SQLStore::BeforeDeleteSubjectComplete', function( $store, $title ) { + return $store->reachedTheBeforeDeleteSubjectCompleteHook = true; + } ); + + $store->deleteSubject( $title ); + + $this->assertTrue( + $store->reachedTheBeforeDeleteSubjectCompleteHook + ); + } + + public function testRegisteredSQLStoreAfterDeleteSubjectComplete() { + + // To make this work with SPARQLStore, need to inject the basestore + $storeClass = '\SMWSQLStore3'; + + $title = \Title::newFromText( __METHOD__ ); + + $idGenerator = $this->getMockBuilder( '\SMWSql3SmwIds' ) + ->disableOriginalConstructor() + ->getMock(); + + $idGenerator->expects( $this->any() ) + ->method( 'findAllEntitiesThatMatch' ) + ->will( $this->returnValue( [ 42 ] ) ); + + $store = $this->getMockBuilder( $storeClass ) + ->setMethods( [ 'getPropertyTables', 'getObjectIds' ] ) + ->getMock(); + + $store->expects( $this->any() ) + ->method( 'getObjectIds' ) + ->will( $this->returnValue( $idGenerator ) ); + + $store->expects( $this->any() ) + ->method( 'getPropertyTables' ) + ->will( $this->returnValue( [] ) ); + + $this->mwHooksHandler->register( 'SMW::SQLStore::AfterDeleteSubjectComplete', function( $store, $title ) { + return $store->reachedTheAfterDeleteSubjectCompleteHook = true; + } ); + + $store->deleteSubject( $title ); + + $this->assertTrue( + $store->reachedTheAfterDeleteSubjectCompleteHook + ); + } + + public function testRegisteredParserBeforeMagicWordsFinder() { + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $title = \Title::newFromText( __METHOD__ ); + + $semanticData = $this->getMockBuilder( '\SMW\SemanticData' ) + ->disableOriginalConstructor() + ->getMock(); + + $parserData = $this->getMockBuilder( '\SMW\ParserData' ) + ->disableOriginalConstructor() + ->getMock(); + + $parserData->expects( $this->any() ) + ->method( 'getOutput' ) + ->will( $this->returnValue( $parserOutput ) ); + + $parserData->expects( $this->any() ) + ->method( 'getSubject' ) + ->will( $this->returnValue( DIWikiPage::newFromTitle( $title ) ) ); + + $parserData->expects( $this->any() ) + ->method( 'getSemanticData' ) + ->will( $this->returnValue( $semanticData ) ); + + $parserData->expects( $this->any() ) + ->method( 'getTitle' ) + ->will( $this->returnValue( $title ) ); + + $magicWordsFinder = $this->getMockBuilder( '\SMW\MediaWiki\MagicWordsFinder' ) + ->disableOriginalConstructor() + ->getMock(); + + $magicWordsFinder->expects( $this->once() ) + ->method( 'findMagicWordInText' ) + ->with( + $this->equalTo( 'Foo' ), + $this->anything() ) + ->will( $this->returnValue( [] ) ); + + $linksProcessor = $this->getMockBuilder( '\SMW\Parser\LinksProcessor' ) + ->disableOriginalConstructor() + ->getMock(); + + $redirectTargetFinder = $this->getMockBuilder( '\SMW\MediaWiki\RedirectTargetFinder' ) + ->disableOriginalConstructor() + ->getMock(); + + $inTextAnnotationParser = $this->getMockBuilder( '\SMW\Parser\InTextAnnotationParser' ) + ->setConstructorArgs( [ $parserData, $linksProcessor, $magicWordsFinder, $redirectTargetFinder ] ) + ->setMethods( null ) + ->getMock(); + + $this->mwHooksHandler->register( 'SMW::Parser::BeforeMagicWordsFinder', function( &$magicWords ) { + $magicWords = [ 'Foo' ]; + + // Just to make MW 1.19 happy, otherwise it is not really needed + return true; + } ); + + $text = ''; + + $inTextAnnotationParser->parse( $text ); + } + + public function testRegisteredAddCustomFixedPropertyTables() { + + $store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' ) + ->setMethods( null ) + ->getMock(); + + $this->mwHooksHandler->register( 'SMW::SQLStore::AddCustomFixedPropertyTables', function( &$customFixedProperties, &$fixedPropertyTablePrefix ) { + + // Standard table prefix + $customFixedProperties['Foo'] = '_Bar'; + + // Custom table prefix + $customFixedProperties['Foobar'] = '_Foooo'; + $fixedPropertyTablePrefix['Foobar'] = 'smw_ext'; + + return true; + } ); + + $this->assertEquals( + 'smw_fpt_bar', + $store->findPropertyTableID( new \SMW\DIProperty( 'Foo' ) ) + ); + + $this->assertEquals( + 'smw_ext_foooo', + $store->findPropertyTableID( new \SMW\DIProperty( 'Foobar' ) ) + ); + } + + public function testRegisteredAfterDataUpdateComplete() { + + $test = $this->getMockBuilder( '\stdClass' ) + ->setMethods( [ 'is' ] ) + ->getMock(); + + $test->expects( $this->once() ) + ->method( 'is' ) + ->with( $this->equalTo( [] ) ); + + $store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' ) + ->setMethods( [ 'getPropertyTables' ] ) + ->getMock(); + + $store->expects( $this->any() ) + ->method( 'getPropertyTables' ) + ->will( $this->returnValue( [] ) ); + + $store->setOption( 'smwgSemanticsEnabled', true ); + $store->setOption( 'smwgAutoRefreshSubject', true ); + + $store->setLogger( $this->spyLogger ); + + $this->mwHooksHandler->register( 'SMW::SQLStore::AfterDataUpdateComplete', function( $store, $semanticData, $changeOp ) use ( $test ){ + $test->is( $changeOp->getChangedEntityIdSummaryList() ); + + return true; + } ); + + $store->updateData( + new \SMW\SemanticData( DIWikiPage::newFromText( 'Foo' ) ) + ); + } + + public function storeClassProvider() { + + $provider[] = [ '\SMWSQLStore3' ]; + $provider[] = [ '\SMW\SPARQLStore\SPARQLStore' ]; + + return $provider; + } + +} |