2022-07-08 05:13:08 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\Tests;
|
|
|
|
|
|
|
|
|
|
use DataValues\StringValue;
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
|
use MediaWiki\Extension\Math\MathFormatter;
|
|
|
|
|
use MediaWiki\Extension\Math\MathWikibaseConnector;
|
2024-10-20 07:27:34 +00:00
|
|
|
|
use MediaWiki\Language\Language;
|
2022-07-08 05:13:08 +00:00
|
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
2022-11-18 19:26:02 +00:00
|
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2024-10-20 07:27:34 +00:00
|
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2024-01-06 15:30:26 +00:00
|
|
|
|
use MediaWiki\Site\Site;
|
2022-07-08 05:13:08 +00:00
|
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use TestLogger;
|
|
|
|
|
use Wikibase\Client\RepoLinker;
|
|
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser;
|
|
|
|
|
use Wikibase\DataModel\Entity\EntityId;
|
|
|
|
|
use Wikibase\DataModel\Entity\EntityIdParser;
|
|
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue;
|
|
|
|
|
use Wikibase\DataModel\Entity\Item;
|
|
|
|
|
use Wikibase\DataModel\Entity\ItemId;
|
|
|
|
|
use Wikibase\DataModel\Entity\NumericPropertyId;
|
|
|
|
|
use Wikibase\DataModel\SiteLink;
|
|
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak;
|
|
|
|
|
use Wikibase\DataModel\Snak\SnakList;
|
|
|
|
|
use Wikibase\DataModel\Statement\Statement;
|
|
|
|
|
use Wikibase\DataModel\Statement\StatementList;
|
|
|
|
|
use Wikibase\DataModel\Term\Term;
|
|
|
|
|
use Wikibase\Lib\Store\EntityRevision;
|
|
|
|
|
use Wikibase\Lib\Store\EntityRevisionLookup;
|
|
|
|
|
use Wikibase\Lib\Store\FallbackLabelDescriptionLookup;
|
|
|
|
|
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
|
|
|
|
|
use Wikibase\Lib\Store\StorageException;
|
|
|
|
|
|
|
|
|
|
class MathWikibaseConnectorTestFactory extends MediaWikiUnitTestCase {
|
|
|
|
|
public const EXAMPLE_URL = 'https://example.com/';
|
|
|
|
|
|
|
|
|
|
public const TEST_ITEMS = [
|
|
|
|
|
'Q1' => [ 'mass–energy equivalence', 'physical law relating mass to energy', 'E = mc^2' ],
|
|
|
|
|
'Q2' => [ 'energy', 'measure for the ability of a system to do work', 'E' ],
|
|
|
|
|
'Q3' => [
|
|
|
|
|
'speed of light',
|
|
|
|
|
'speed at which all massless particles and associated fields travel in vacuum',
|
|
|
|
|
'c'
|
|
|
|
|
],
|
|
|
|
|
'Q4' => [
|
|
|
|
|
'mass',
|
|
|
|
|
'property of matter to resist changes of the state of motion and to attract other bodies',
|
|
|
|
|
'm'
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2024-04-08 12:45:40 +00:00
|
|
|
|
public static function setUpBeforeClass(): void {
|
|
|
|
|
ExtensionRegistry::enableForTest();
|
|
|
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) {
|
|
|
|
|
self::markTestSkipped( 'WikibaseClient is not installed. Skipping tests.' );
|
|
|
|
|
}
|
|
|
|
|
ExtensionRegistry::disableForTest();
|
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 05:13:08 +00:00
|
|
|
|
public function getWikibaseConnectorWithExistingItems(
|
|
|
|
|
EntityRevision $entityRevision,
|
|
|
|
|
bool $storageExceptionOnQ3 = false,
|
2024-10-26 13:15:18 +00:00
|
|
|
|
?LoggerInterface $logger = null,
|
|
|
|
|
?EntityIdParser $parser = null
|
2022-07-08 05:13:08 +00:00
|
|
|
|
): MathWikibaseConnector {
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$revisionLookupMock = $this->createMock( EntityRevisionLookup::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$revisionLookupMock->method( 'getEntityRevision' )->willReturnCallback(
|
|
|
|
|
static function ( EntityId $entityId ) use ( $entityRevision, $storageExceptionOnQ3 ) {
|
|
|
|
|
if ( $storageExceptionOnQ3 && $entityId->getSerialization() === 'Q3' ) {
|
|
|
|
|
throw new StorageException( 'Test Exception' );
|
|
|
|
|
} else {
|
|
|
|
|
return $entityRevision;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
$revisionLookupMock->expects( self::atLeastOnce() )
|
|
|
|
|
->method( 'getEntityRevision' );
|
|
|
|
|
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$fallbackLabelDescriptionLookupFactoryMock = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
|
|
|
|
|
$languageMock = $this->createMock( Language::class );
|
|
|
|
|
$languageFactoryMock = $this->createMock( LanguageFactory::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$languageFactoryMock->method( 'getLanguage' )
|
|
|
|
|
->with( 'en' )
|
|
|
|
|
->willReturn( $languageMock );
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$languageNameUtilsMock = $this->createMock( LanguageNameUtils::class );
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$languageNameUtilsMock->method( 'isValidCode' )
|
|
|
|
|
->with( 'en' )
|
|
|
|
|
->willReturn( true );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$fallbackLabelDescriptionLookupFactoryMock->method( 'newLabelDescriptionLookup' )
|
|
|
|
|
->with( $languageMock )
|
|
|
|
|
->willReturnCallback( [ $this, 'newLabelDescriptionLookup' ] );
|
|
|
|
|
|
|
|
|
|
return self::getWikibaseConnector(
|
|
|
|
|
$languageFactoryMock,
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$languageNameUtilsMock,
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$fallbackLabelDescriptionLookupFactoryMock,
|
|
|
|
|
$revisionLookupMock,
|
|
|
|
|
$logger,
|
|
|
|
|
$parser
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getWikibaseConnector(
|
2024-10-26 13:15:18 +00:00
|
|
|
|
?LanguageFactory $languageFactory = null,
|
|
|
|
|
?LanguageNameUtils $languageNameUtils = null,
|
|
|
|
|
?FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory = null,
|
|
|
|
|
?EntityRevisionLookup $entityRevisionLookupMock = null,
|
|
|
|
|
?LoggerInterface $logger = null,
|
|
|
|
|
?EntityIdParser $parser = null
|
2022-07-08 05:13:08 +00:00
|
|
|
|
): MathWikibaseConnector {
|
|
|
|
|
$labelDescriptionLookupFactory = $labelDescriptionLookupFactory ?:
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$this->createMock( FallbackLabelDescriptionLookupFactory::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
|
|
|
|
$entityRevisionLookup = $entityRevisionLookupMock ?:
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$this->createMock( EntityRevisionLookup::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$languageFactory = $languageFactory ?: $this->createMock( LanguageFactory::class );
|
2022-11-18 19:26:02 +00:00
|
|
|
|
if ( !$languageNameUtils ) {
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$languageNameUtils = $this->createMock( LanguageNameUtils::class );
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$languageNameUtils->method( 'isValidCode' )->willReturn( true );
|
|
|
|
|
}
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$site = $this->createMock( Site::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$site->method( 'getGlobalId' )->willReturn( '' );
|
|
|
|
|
$site->method( 'getPageUrl' )->willReturn( self::EXAMPLE_URL );
|
|
|
|
|
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$repoConnector = $this->createMock( RepoLinker::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$repoConnector->method( 'getEntityUrl' )
|
|
|
|
|
->willReturnCallback( static function ( ItemId $itemId ) {
|
2023-09-07 14:48:52 +00:00
|
|
|
|
return self::EXAMPLE_URL . 'wiki/Special:EntityPage/' . $itemId->getSerialization();
|
2022-07-08 05:13:08 +00:00
|
|
|
|
} );
|
|
|
|
|
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$mathFormatter = $this->createMock( MathFormatter::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$mathFormatter->method( 'format' )
|
|
|
|
|
->willReturnCallback( static function ( StringValue $value ) {
|
|
|
|
|
return self::getExpectedMathML( $value->getValue() );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return new MathWikibaseConnector(
|
|
|
|
|
new ServiceOptions( MathWikibaseConnector::CONSTRUCTOR_OPTIONS, [
|
|
|
|
|
'MathWikibasePropertyIdHasPart' => 'P1',
|
|
|
|
|
'MathWikibasePropertyIdDefiningFormula' => 'P2',
|
|
|
|
|
'MathWikibasePropertyIdQuantitySymbol' => 'P3',
|
|
|
|
|
'MathWikibasePropertyIdInDefiningFormula' => 'P4',
|
|
|
|
|
'MathWikibasePropertyIdSymbolRepresents' => 'P5'
|
|
|
|
|
] ),
|
|
|
|
|
$repoConnector,
|
|
|
|
|
$languageFactory,
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$languageNameUtils,
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$entityRevisionLookup,
|
|
|
|
|
$labelDescriptionLookupFactory,
|
|
|
|
|
$site,
|
|
|
|
|
$parser ?: new BasicEntityIdParser(),
|
|
|
|
|
$mathFormatter,
|
|
|
|
|
$logger ?: new TestLogger()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setupMassEnergyEquivalenceItem(
|
|
|
|
|
bool $hasPartMode
|
|
|
|
|
) {
|
|
|
|
|
$partPropertyId = new NumericPropertyId( $hasPartMode ? 'P1' : 'P4' );
|
|
|
|
|
$symbolPropertyId = new NumericPropertyId( $hasPartMode ? 'P3' : 'P5' );
|
|
|
|
|
$items = [];
|
|
|
|
|
$statements = [];
|
|
|
|
|
foreach ( self::TEST_ITEMS as $key => $itemInfo ) {
|
|
|
|
|
$itemId = new ItemId( $key );
|
|
|
|
|
$items[ $key ] = new Item( $itemId );
|
|
|
|
|
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$siteLinkMock = $this->createMock( SiteLink::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$siteLinkMock->method( 'getSiteId' )->willReturn( '' );
|
|
|
|
|
$siteLinkMock->method( 'getPageName' )->willReturn( '' );
|
|
|
|
|
$items[ $key ]->addSiteLink( $siteLinkMock );
|
|
|
|
|
|
|
|
|
|
if ( $key === 'Q1' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$partSnak = new PropertyValueSnak(
|
|
|
|
|
$partPropertyId,
|
|
|
|
|
$hasPartMode ? new EntityIdValue( $items[ $key ]->getId() ) : new StringValue( $itemInfo[2] )
|
|
|
|
|
);
|
|
|
|
|
$partQualifier = new PropertyValueSnak(
|
|
|
|
|
$symbolPropertyId,
|
|
|
|
|
$hasPartMode ? new StringValue( $itemInfo[2] ) : new EntityIdValue( $items[ $key ]->getId() )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$statement = new Statement( $partSnak );
|
|
|
|
|
$statement->setQualifiers( new SnakList( [ $partQualifier ] ) );
|
|
|
|
|
$statements[] = $statement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mainFormulaValue = new StringValue( self::TEST_ITEMS[ 'Q1' ][2] );
|
|
|
|
|
$definingFormulaStatement = new Statement( new PropertyValueSnak(
|
|
|
|
|
new NumericPropertyId( 'P2' ),
|
|
|
|
|
$mainFormulaValue
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$statementList = new StatementList( ...$statements );
|
|
|
|
|
$statementList->addStatement( $definingFormulaStatement );
|
|
|
|
|
$items[ 'Q1' ]->setStatements( $statementList );
|
|
|
|
|
return $items[ 'Q1' ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function newLabelDescriptionLookup(): FallbackLabelDescriptionLookup {
|
2023-05-23 19:23:27 +00:00
|
|
|
|
$lookup = $this->createMock( FallbackLabelDescriptionLookup::class );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
|
|
|
|
$lookup->method( 'getLabel' )
|
|
|
|
|
->willReturnCallback( static function ( EntityId $entityId ) {
|
|
|
|
|
if ( self::TEST_ITEMS[ $entityId->getSerialization() ] !== null ) {
|
|
|
|
|
return new Term( 'en', self::TEST_ITEMS[ $entityId->getSerialization() ][0] );
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$lookup->method( 'getDescription' )
|
|
|
|
|
->willReturnCallback( static function ( EntityId $entityId ) {
|
|
|
|
|
if ( self::TEST_ITEMS[ $entityId->getSerialization() ] !== null ) {
|
|
|
|
|
return new Term( 'en', self::TEST_ITEMS[ $entityId->getSerialization() ][1] );
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return $lookup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getExpectedMathML( $str ) {
|
|
|
|
|
return '<math>' . $str . '</math>';
|
|
|
|
|
}
|
|
|
|
|
}
|