diff --git a/repo/tests/phpunit/includes/FederatedProperties/Hooks/LabelPrefetchHookHandlerTest.php b/repo/tests/phpunit/includes/FederatedProperties/Hooks/LabelPrefetchHookHandlerTest.php deleted file mode 100644 index 3a77f74a19..0000000000 --- a/repo/tests/phpunit/includes/FederatedProperties/Hooks/LabelPrefetchHookHandlerTest.php +++ /dev/null @@ -1,98 +0,0 @@ - NS_MAIN, - 'rc_title' => 'Q1', - 'rc_comment_text' => "/* wbsetclaim-update:1||1 */ [[Property:P1]]: asdf", - ], - (object)[ - 'rc_namespace' => NS_MAIN, - 'rc_title' => 'Q2', - 'rc_comment_text' => "/* wbsetclaim-update:1||1 */ [[Property:P2]]: asdf", - ], - (object)[ - 'rc_namespace' => NS_MAIN, - 'rc_title' => 'Q3', - 'rc_comment_text' => "/* wbsetclaim-update:1||1 */ [[Property:P3]]: asdf", - ], - ]; - - $expectedTermTypes = [ 'label', 'description' ]; - $expectedLanguageCodes = [ 'de', 'en', 'it' ]; - - $itemOneId = new ItemId( 'Q1' ); - $itemOne = new Item( $itemOneId ); - - $itemOne->getStatements()->addStatement( new Statement( new PropertyNoValueSnak( 1 ) ) ); - $itemOne->getStatements()->addStatement( new Statement( new PropertyNoValueSnak( 2 ) ) ); - - $itemTwoId = new ItemId( 'Q2' ); - $itemTwo = new Item( $itemTwoId ); - - $itemTwo->getStatements()->addStatement( new Statement( new PropertyNoValueSnak( 1 ) ) ); - $itemTwo->getStatements()->addStatement( new Statement( new PropertyNoValueSnak( 3 ) ) ); - - $itemThreeNotFoundId = new ItemId( 'Q3' ); - - $expectedItemIds = [ - $itemOneId, - $itemTwoId, - $itemThreeNotFoundId, - ]; - - $expectedPropertyIds = [ - new NumericPropertyId( 'P1' ), - new NumericPropertyId( 'P2' ), - new NumericPropertyId( 'P3' ), - ]; - - $prefetchingTermLookup = $this->createMock( PrefetchingTermLookup::class ); - $prefetchingTermLookup->expects( $this->once() ) - ->method( 'prefetchTerms' ) - ->willReturnCallback( $this->getPrefetchTermsCallback( - $expectedPropertyIds, - $expectedTermTypes, - $expectedLanguageCodes - ) ); - - $linkBeginHookHandler = $this->getLabelPrefetchHookHandlers( - $this->getPrefetchTermsCallback( $expectedItemIds, $expectedTermTypes, $expectedLanguageCodes ), - $expectedTermTypes, - $expectedLanguageCodes, - $prefetchingTermLookup - ); - - /** @var ChangesList $changesList */ - $changesList = $this->createMock( ChangesList::class ); - - $linkBeginHookHandler->onChangesListInitRows( - $changesList, - $rows - ); - } -} diff --git a/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTest.php b/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTest.php index ea4899376a..c7b17aa4a0 100644 --- a/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTest.php +++ b/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTest.php @@ -3,8 +3,21 @@ namespace Wikibase\Repo\Tests\Hooks; use ChangesList; +use MediaWiki\Title\Title; +use MediaWiki\Title\TitleFactory; +use PHPUnit\Framework\TestCase; +use Wikibase\DataModel\Entity\BasicEntityIdParser; +use Wikibase\DataModel\Entity\EntityId; +use Wikibase\DataModel\Entity\EntityIdParsingException; use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\Entity\NumericPropertyId; +use Wikibase\DataModel\Services\Term\TermBuffer; +use Wikibase\DataModel\Term\TermTypes; +use Wikibase\Lib\LanguageFallbackChainFactory; +use Wikibase\Lib\Store\EntityIdLookup; +use Wikibase\Lib\TermLanguageFallbackChain; +use Wikibase\Repo\Hooks\LabelPrefetchHookHandler; +use Wikibase\Repo\Hooks\SummaryParsingPrefetchHelper; /** * @covers \Wikibase\Repo\Hooks\LabelPrefetchHookHandler @@ -17,29 +30,156 @@ * @author Katie Filbert < aude.wiki@gmail.com > * @author Daniel Kinzler */ -class LabelPrefetchHookHandlerTest extends LabelPrefetchHookHandlerTestBase { +class LabelPrefetchHookHandlerTest extends TestCase { + + /** + * @param Title[] $titles + * + * @return EntityId[] + */ + public function titlesToIds( array $titles ) { + $entityIds = []; + $idParser = new BasicEntityIdParser(); + + foreach ( $titles as $title ) { + try { + // Pretend the article ID is the numeric entity ID. + $entityId = $idParser->parse( $title->getBaseText() ); + $key = $entityId->getNumericId(); + + $entityIds[$key] = $entityId; + } catch ( EntityIdParsingException $ex ) { + // skip + } + } + + return $entityIds; + } + + /** + * @param callable $prefetchTerms + * @param string[] $termTypes + * @param string[] $languageCodes + * @param callable $prefetchSummaryTerms + * @return LabelPrefetchHookHandler + */ + protected function getLabelPrefetchHookHandlers( + $prefetchTerms, + array $termTypes, + array $languageCodes, + $prefetchSummaryTerms + ) { + $termBuffer = $this->createMock( TermBuffer::class ); + $termBuffer->expects( $this->atLeastOnce() ) + ->method( 'prefetchTerms' ) + ->willReturnCallback( $prefetchTerms ); + + $termBufferSummaries = $this->createMock( TermBuffer::class ); + $termBufferSummaries->expects( $this->atLeastOnce() ) + ->method( 'prefetchTerms' ) + ->willReturnCallback( $prefetchSummaryTerms ); + + $idLookup = $this->createMock( EntityIdLookup::class ); + $idLookup->expects( $this->atLeastOnce() ) + ->method( 'getEntityIds' ) + ->willReturnCallback( [ $this, 'titlesToIds' ] ); + + $titleFactory = $this->createMock( TitleFactory::class ); + $titleFactory->expects( $this->atLeastOnce() ) + ->method( 'makeTitle' ) + ->willReturnCallback( function ( int $ns, string $titleText ) { + $title = $this->createMock( Title::class ); + $title->expects( $this->once() ) + ->method( 'getBaseText' ) + ->willReturn( $titleText ); + return $title; + } ); + + $fallbackChain = $this->createMock( TermLanguageFallbackChain::class ); + $fallbackChain->method( 'getFetchLanguageCodes' ) + ->willReturn( $languageCodes ); + + $fallbackChainFactory = $this->createMock( LanguageFallbackChainFactory::class ); + $fallbackChainFactory->method( 'newFromContext' ) + ->willReturn( $fallbackChain ); + + return new LabelPrefetchHookHandler( + $termBuffer, + $idLookup, + $titleFactory, + $termTypes, + $fallbackChainFactory, + new SummaryParsingPrefetchHelper( $termBufferSummaries ) + ); + } + + protected function getPrefetchTermsCallback( $expectedIds, $expectedTermTypes, $expectedLanguageCodes ) { + $prefetchTerms = function ( + array $entityIds, + array $termTypes = null, + array $languageCodes = null + ) use ( + $expectedIds, + $expectedTermTypes, + $expectedLanguageCodes + ) { + $expectedIdStrings = array_map( function( EntityId $id ) { + return $id->getSerialization(); + }, $expectedIds ); + $entityIdStrings = array_map( function( EntityId $id ) { + return $id->getSerialization(); + }, $entityIds ); + + sort( $expectedIdStrings ); + sort( $entityIdStrings ); + + $this->assertEquals( $expectedIdStrings, $entityIdStrings ); + $this->assertEquals( $expectedTermTypes, $termTypes ); + $this->assertEquals( $expectedLanguageCodes, $languageCodes ); + }; + return $prefetchTerms; + } public function testDoChangesListInitRows() { $rows = [ - (object)[ 'rc_namespace' => NS_MAIN, 'rc_title' => 'XYZ', 'rc_comment_text' => '' ], - (object)[ 'rc_namespace' => NS_MAIN, 'rc_title' => 'Q23', 'rc_comment_text' => 'foo' ], - (object)[ 'rc_namespace' => NS_MAIN, 'rc_title' => 'P55', 'rc_comment_text' => '' ], + (object)[ + 'rc_namespace' => NS_MAIN, + 'rc_title' => 'Q1', + 'rc_comment_text' => "/* wbsetclaim-update:1||1 */ [[Property:P100]]: asdf", + ], + (object)[ + 'rc_namespace' => NS_MAIN, + 'rc_title' => 'P2', + 'rc_comment_text' => "/* wbsetclaim-update:1||1 */ [[Property:P200]]: [[Q2013]]", + ], + (object)[ + 'rc_namespace' => NS_MAIN, + 'rc_title' => 'Q3', + 'rc_comment_text' => "/* wbsetclaim-update:1||1 */ [[Property:P300]]: asdf", + ], ]; - $expectedTermTypes = [ 'label', 'description' ]; + $expectedTermTypes = [ TermTypes::TYPE_LABEL, TermTypes::TYPE_DESCRIPTION ]; $expectedLanguageCodes = [ 'de', 'en', 'it' ]; - $expectedIds = [ - new ItemId( 'Q23' ), - new NumericPropertyId( 'P55' ), + $expectedEditedEntityIds = [ + new ItemId( 'Q1' ), + new NumericPropertyId( 'P2' ), + new ItemId( 'Q3' ), ]; - $prefetchTerms = $this->getPrefetchTermsCallback( $expectedIds, $expectedTermTypes, $expectedLanguageCodes ); + $expectedSummaryEntityIds = [ + new NumericPropertyId( 'P100' ), + new NumericPropertyId( 'P200' ), + new ItemId( 'Q2013' ), + new NumericPropertyId( 'P300' ), + ]; $linkBeginHookHandler = $this->getLabelPrefetchHookHandlers( - $prefetchTerms, + $this->getPrefetchTermsCallback( $expectedEditedEntityIds, $expectedTermTypes, $expectedLanguageCodes ), $expectedTermTypes, - $expectedLanguageCodes + $expectedLanguageCodes, + $this->getPrefetchTermsCallback( $expectedSummaryEntityIds, $expectedTermTypes, $expectedLanguageCodes ) ); /** @var ChangesList $changesList */ diff --git a/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTestBase.php b/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTestBase.php deleted file mode 100644 index d951c2728a..0000000000 --- a/repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTestBase.php +++ /dev/null @@ -1,124 +0,0 @@ -parse( $title->getText() ); - $key = $entityId->getNumericId(); - - $entityIds[$key] = $entityId; - } catch ( EntityIdParsingException $ex ) { - // skip - } - } - - return $entityIds; - } - - /** - * @param callable $prefetchTerms - * @param string[] $termTypes - * @param string[] $languageCodes - * @param PrefetchingTermLookup|null $prefetchingTermLookup - * @return LabelPrefetchHookHandler - */ - protected function getLabelPrefetchHookHandlers( - $prefetchTerms, - array $termTypes, - array $languageCodes, - PrefetchingTermLookup $prefetchingTermLookup = null - ) { - $prefetchingTermLookup ??= $this->createMock( PrefetchingTermLookup::class ); - - $termBuffer = $this->createMock( TermBuffer::class ); - $termBuffer->expects( $this->atLeastOnce() ) - ->method( 'prefetchTerms' ) - ->willReturnCallback( $prefetchTerms ); - - $idLookup = $this->createMock( EntityIdLookup::class ); - $idLookup->expects( $this->atLeastOnce() ) - ->method( 'getEntityIds' ) - ->willReturnCallback( [ $this, 'titlesToIds' ] ); - - // TODO add a mock instead - $titleFactory = MediaWikiServices::getInstance()->getTitleFactory(); - - $fallbackChain = $this->createMock( TermLanguageFallbackChain::class ); - $fallbackChain->method( 'getFetchLanguageCodes' ) - ->willReturn( $languageCodes ); - - $fallbackChainFactory = $this->createMock( LanguageFallbackChainFactory::class ); - $fallbackChainFactory->method( 'newFromContext' ) - ->willReturn( $fallbackChain ); - - return new LabelPrefetchHookHandler( - $termBuffer, - $idLookup, - $titleFactory, - $termTypes, - $fallbackChainFactory, - new SummaryParsingPrefetchHelper( $prefetchingTermLookup ) - ); - } - - protected function getPrefetchTermsCallback( $expectedIds, $expectedTermTypes, $expectedLanguageCodes ) { - $prefetchTerms = function ( - array $entityIds, - array $termTypes = null, - array $languageCodes = null - ) use ( - $expectedIds, - $expectedTermTypes, - $expectedLanguageCodes - ) { - $expectedIdStrings = array_map( function( EntityId $id ) { - return $id->getSerialization(); - }, $expectedIds ); - $entityIdStrings = array_map( function( EntityId $id ) { - return $id->getSerialization(); - }, $entityIds ); - - sort( $expectedIdStrings ); - sort( $entityIdStrings ); - - $this->assertEquals( $expectedIdStrings, $entityIdStrings ); - $this->assertEquals( $expectedTermTypes, $termTypes ); - $this->assertEquals( $expectedLanguageCodes, $languageCodes ); - }; - return $prefetchTerms; - } -}