2022-07-08 05:13:08 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\Tests;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Extension\Math\Rest\Popup;
|
|
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
2022-11-18 19:26:02 +00:00
|
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2022-07-08 05:13:08 +00:00
|
|
|
|
use MediaWiki\Rest\Handler;
|
|
|
|
|
use MediaWiki\Rest\HttpException;
|
|
|
|
|
use MediaWiki\Rest\RequestData;
|
|
|
|
|
use MediaWiki\Tests\Rest\Handler\HandlerTestTrait;
|
|
|
|
|
use Title;
|
|
|
|
|
use Wikibase\DataModel\Entity\Item;
|
|
|
|
|
use Wikibase\Lib\Store\EntityRevision;
|
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Extension\Math\Rest\Popup
|
|
|
|
|
*/
|
|
|
|
|
class PopupTest extends MathWikibaseConnectorTestFactory {
|
|
|
|
|
|
|
|
|
|
use HandlerTestTrait;
|
|
|
|
|
|
|
|
|
|
public function testNonExistingId() {
|
|
|
|
|
$popupHandler = $this->getPopup();
|
|
|
|
|
$response = $this->executeHandler( $popupHandler, $this->getRequest( '1', 'en' ) );
|
|
|
|
|
$this->assertEquals( 400, $response->getStatusCode() );
|
|
|
|
|
$data = json_decode( $response->getBody(), true );
|
|
|
|
|
$this->assertEquals( 'Non-existing Wikibase ID.', $data[ 'message' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testParameterSettingsSetup() {
|
|
|
|
|
$popupHandler = $this->getPopup();
|
|
|
|
|
$this->assertSame( [ 'qid' => [
|
|
|
|
|
HANDLER::PARAM_SOURCE => 'path',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
|
] ], $popupHandler->getParamSettings() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidationExceptionForMalformedId() {
|
|
|
|
|
$popupHandler = $this->getPopup();
|
|
|
|
|
$this->initHandler( $popupHandler, $this->getRequest( 'Q1', 'en' ) );
|
|
|
|
|
$this->expectException( HttpException::class );
|
|
|
|
|
$this->validateHandler( $popupHandler );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvalidLanguage() {
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$languageNameUtilsMock = $this->createMock( LanguageNameUtils::class );
|
|
|
|
|
$languageNameUtilsMock->expects( $this->once() )
|
|
|
|
|
->method( 'isValidCode' )
|
2022-07-08 05:13:08 +00:00
|
|
|
|
->with( 'tmp' )
|
2022-11-18 19:26:02 +00:00
|
|
|
|
->willReturn( false );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$popupHandler = $this->getPopup( null, $languageNameUtilsMock );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$response = $this->executeHandler( $popupHandler, $this->getRequest( '1', 'tmp' ) );
|
|
|
|
|
$this->assertEquals( 400, $response->getStatusCode() );
|
|
|
|
|
$data = json_decode( $response->getBody(), true );
|
|
|
|
|
$this->assertEquals( 'Invalid language code.', $data[ 'message' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideItemSetups
|
|
|
|
|
*/
|
|
|
|
|
public function testExistingId( Item $item ) {
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$popupHandler = $this->getPopup( null, null, $item );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
|
|
|
|
$request = $this->getRequest( '1', 'en' );
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $popupHandler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'title', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'canonicalurl', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'fullurl', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'contentmodel', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'pagelanguagedir', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'pagelanguage', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'pagelanguagehtmlcode', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'extract', $data );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 'mass–energy equivalence', $data[ 'title' ] );
|
|
|
|
|
$this->assertEquals( 'special/Q1', $data[ 'canonicalurl' ] );
|
|
|
|
|
$this->assertEquals( 'special/Q1', $data[ 'fullurl' ] );
|
|
|
|
|
$this->assertEquals( 'html', $data[ 'contentmodel' ] );
|
|
|
|
|
|
|
|
|
|
$html = $data[ 'extract' ];
|
|
|
|
|
|
|
|
|
|
// popup contains formula label and description
|
|
|
|
|
$this->assertStringContainsString( self::TEST_ITEMS[ 'Q1' ][0], $html );
|
|
|
|
|
$this->assertStringContainsString( self::TEST_ITEMS[ 'Q1' ][1], $html );
|
|
|
|
|
|
|
|
|
|
// popup contains formula and labels of each element
|
|
|
|
|
foreach ( self::TEST_ITEMS as $key => $part ) {
|
|
|
|
|
if ( $key === 'Q1' ) {
|
|
|
|
|
// the formula itself is not shown in the popup, only its elements
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->assertStringContainsString( $part[0], $html );
|
|
|
|
|
$this->assertStringContainsString( self::getExpectedMathML( $part[2] ), $html );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getRequest( $id, $lang ): RequestData {
|
|
|
|
|
return new RequestData( [
|
|
|
|
|
'pathParams' => [ 'qid' => $id ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Accept-Language' => $lang
|
|
|
|
|
]
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getPopup(
|
|
|
|
|
LanguageFactory $languageFactoryMock = null,
|
2022-11-18 19:26:02 +00:00
|
|
|
|
LanguageNameUtils $languageNameUtilsMock = null,
|
2022-07-08 05:13:08 +00:00
|
|
|
|
Item $item = null
|
|
|
|
|
): Popup {
|
|
|
|
|
$languageFactoryMock = $languageFactoryMock ?: $this->createMock( LanguageFactory::class );
|
2022-11-18 19:26:02 +00:00
|
|
|
|
if ( !$languageNameUtilsMock ) {
|
|
|
|
|
$languageNameUtilsMock = self::createMock( LanguageNameUtils::class );
|
|
|
|
|
$languageNameUtilsMock->method( 'isValidCode' )->willReturn( true );
|
|
|
|
|
}
|
2022-07-08 05:13:08 +00:00
|
|
|
|
$mathWikibaseConnectorMock = $item ?
|
|
|
|
|
$this->getWikibaseConnectorWithExistingItems( new EntityRevision( $item ) ) :
|
2022-11-18 19:26:02 +00:00
|
|
|
|
$this->getWikibaseConnector( $languageFactoryMock, $languageNameUtilsMock );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
|
|
|
|
|
$titleMock = $this->createMock( Title::class );
|
|
|
|
|
$titleMock->method( 'getLocalURL' )->willReturn( 'special/Q1' );
|
|
|
|
|
$titleMock->method( 'getFullURL' )->willReturn( 'special/Q1' );
|
|
|
|
|
$titleFactoryMock = $this->createMock( \TitleFactory::class );
|
|
|
|
|
$titleFactoryMock->expects( $this->once() )
|
|
|
|
|
->method( 'newFromText' )
|
|
|
|
|
->willReturn( $titleMock );
|
|
|
|
|
|
2022-11-18 19:26:02 +00:00
|
|
|
|
return new Popup( $mathWikibaseConnectorMock, $languageFactoryMock, $languageNameUtilsMock, $titleFactoryMock );
|
2022-07-08 05:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideItemSetups(): array {
|
|
|
|
|
return [
|
|
|
|
|
[ $this->setupMassEnergyEquivalenceItem( true ) ],
|
|
|
|
|
[ $this->setupMassEnergyEquivalenceItem( false ) ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|