2022-07-20 18:14:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\Tests;
|
|
|
|
|
2022-07-23 09:33:40 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2022-07-20 18:14:11 +00:00
|
|
|
use MediaWiki\Extension\Math\MathWikibaseConnector;
|
2022-07-21 08:02:56 +00:00
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
use MWException;
|
|
|
|
use Site;
|
2022-07-20 18:14:11 +00:00
|
|
|
use TestLogger;
|
|
|
|
use Wikibase\Client\RepoLinker;
|
|
|
|
use Wikibase\DataAccess\DatabaseEntitySource;
|
|
|
|
use Wikibase\DataAccess\EntitySourceDefinitions;
|
2022-07-21 08:02:56 +00:00
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser;
|
|
|
|
use Wikibase\Lib\Store\EntityRevisionLookup;
|
|
|
|
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
|
2022-07-20 18:14:11 +00:00
|
|
|
use Wikibase\Lib\SubEntityTypesMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \MediaWiki\Extension\Math\MathWikibaseConnector
|
|
|
|
*/
|
2022-07-21 08:02:56 +00:00
|
|
|
class MathWikibaseConnectorTest extends MediaWikiUnitTestCase {
|
2022-07-20 18:14:11 +00:00
|
|
|
|
|
|
|
private const EXAMPLE_URL = 'https://example.com/';
|
|
|
|
|
|
|
|
public function testGetUrl() {
|
|
|
|
$mathWikibase = $this->getWikibaseConnector();
|
|
|
|
$this->assertEquals( self::EXAMPLE_URL . 'wiki/Special:EntityPage/Q42',
|
|
|
|
$mathWikibase->buildURL( 'Q42' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFetchInvalidLanguage() {
|
2022-07-21 08:02:56 +00:00
|
|
|
$languageFactory = $this->createMock( LanguageFactory::class );
|
|
|
|
$languageFactory->method( 'getLanguage' )
|
|
|
|
->willThrowException( new MWException( 'Invalid code' ) );
|
|
|
|
$mathWikibase = $this->getWikibaseConnector( $languageFactory );
|
|
|
|
|
2022-07-20 18:14:11 +00:00
|
|
|
$this->expectException( 'InvalidArgumentException' );
|
|
|
|
$this->expectErrorMessage( 'Invalid language code specified.' );
|
|
|
|
$mathWikibase->fetchWikibaseFromId( 'Q1', '&' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFetchNonExistingId() {
|
|
|
|
$mathWikibase = $this->getWikibaseConnector();
|
|
|
|
$this->expectException( 'InvalidArgumentException' );
|
|
|
|
$this->expectErrorMessage( 'Non-existing Wikibase ID.' );
|
|
|
|
$mathWikibase->fetchWikibaseFromId( 'Q1', 'en' );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function newConnector(): RepoLinker {
|
|
|
|
return new RepoLinker(
|
|
|
|
new EntitySourceDefinitions(
|
|
|
|
[
|
|
|
|
new DatabaseEntitySource(
|
|
|
|
'test',
|
|
|
|
'testdb',
|
|
|
|
[ 'item' => [ 'namespaceId' => 123, 'slot' => 'main' ] ],
|
|
|
|
self::EXAMPLE_URL . 'entity',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
''
|
|
|
|
)
|
|
|
|
],
|
|
|
|
new SubEntityTypesMapper( [] )
|
|
|
|
),
|
|
|
|
self::EXAMPLE_URL,
|
|
|
|
'/wiki/$1',
|
|
|
|
'' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-21 08:02:56 +00:00
|
|
|
* @param LanguageFactory|null $languageFactory
|
2022-07-20 18:14:11 +00:00
|
|
|
* @return MathWikibaseConnector
|
|
|
|
*/
|
2022-07-21 08:02:56 +00:00
|
|
|
public function getWikibaseConnector( LanguageFactory $languageFactory = null ): MathWikibaseConnector {
|
|
|
|
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
|
|
|
|
$labelDescriptionLookupFactory = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
|
|
|
|
$languageFactory = $languageFactory ?: $this->createMock( LanguageFactory::class );
|
2022-07-23 09:33:40 +00:00
|
|
|
return new MathWikibaseConnector(
|
|
|
|
new ServiceOptions( MathWikibaseConnector::CONSTRUCTOR_OPTIONS, [
|
2022-07-21 08:02:56 +00:00
|
|
|
'MathWikibasePropertyIdHasPart' => 'P1',
|
|
|
|
'MathWikibasePropertyIdDefiningFormula' => 'P2',
|
|
|
|
'MathWikibasePropertyIdQuantitySymbol' => 'P3'
|
2022-07-23 09:33:40 +00:00
|
|
|
] ),
|
2022-07-21 08:02:56 +00:00
|
|
|
$this->newConnector(),
|
|
|
|
$languageFactory,
|
2022-07-23 09:33:40 +00:00
|
|
|
$entityRevisionLookup,
|
|
|
|
$labelDescriptionLookupFactory,
|
|
|
|
new Site(),
|
|
|
|
new BasicEntityIdParser(),
|
2022-07-21 08:02:56 +00:00
|
|
|
new TestLogger() );
|
2022-07-20 18:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|