Skip to content

Commit

Permalink
Merge "Merge and simplify the LabelPrefetchHookHandler tests"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Dec 13, 2024
2 parents c8f0172 + fd15248 commit bc9191b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 233 deletions.

This file was deleted.

162 changes: 151 additions & 11 deletions repo/tests/phpunit/includes/Hooks/LabelPrefetchHookHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,29 +30,156 @@
* @author Katie Filbert < [email protected] >
* @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 */
Expand Down
Loading

0 comments on commit bc9191b

Please sign in to comment.