| 1 |
<?php |
| 2 |
|
| 3 |
use DataValues\StringValue; |
| 4 |
use MediaWiki\Extension\Math\MathMLRdfBuilder; |
| 5 |
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait; |
| 6 |
use Wikibase\DataModel\Entity\NumericPropertyId; |
| 7 |
use Wikibase\DataModel\Snak\PropertyValueSnak; |
| 8 |
use Wikimedia\Purtle\NTriplesRdfWriter; |
| 9 |
use Wikimedia\Rdbms\IDatabase; |
| 10 |
use Wikimedia\Rdbms\LBFactory; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class MathMLRdfBuilderTest extends MediaWikiIntegrationTestCase { |
| 20 |
use MathMockHttpTrait; |
| 21 |
|
| 22 |
private const ACME_PREFIX_URL = 'http://acme/'; |
| 23 |
private const ACME_REF = 'testing'; |
| 24 |
|
| 25 |
protected function setUp(): void { |
| 26 |
$this->markTestSkippedIfExtensionNotLoaded( 'WikibaseClient' ); |
| 27 |
$db = $this->createMock( IDatabase::class ); |
| 28 |
$db->method( 'selectRow' )->willReturn( false ); |
| 29 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 30 |
$lbFactory->method( 'getReplicaDatabase' )->willReturn( $db ); |
| 31 |
$this->setService( 'DBLoadBalancerFactory', $lbFactory ); |
| 32 |
parent::setUp(); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private function makeCase( $test ) { |
| 40 |
$builder = new MathMLRdfBuilder(); |
| 41 |
$writer = new NTriplesRdfWriter(); |
| 42 |
$writer->prefix( 'www', "http://www/" ); |
| 43 |
$writer->prefix( 'acme', self::ACME_PREFIX_URL ); |
| 44 |
|
| 45 |
$writer->start(); |
| 46 |
$writer->about( 'www', 'Q1' ); |
| 47 |
|
| 48 |
$snak = new PropertyValueSnak( new NumericPropertyId( 'P1' ), new StringValue( $test ) ); |
| 49 |
$builder->addValue( $writer, 'acme', self::ACME_REF, 'DUMMY', '', $snak ); |
| 50 |
|
| 51 |
return trim( $writer->drain() ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testValidInput() { |
| 55 |
$this->setupGoodMathRestBaseMockHttp(); |
| 56 |
|
| 57 |
$triples = $this->makeCase( '\sin x^2' ); |
| 58 |
$this->assertStringContainsString( |
| 59 |
self::ACME_PREFIX_URL . self::ACME_REF . '> "<math', |
| 60 |
$triples |
| 61 |
); |
| 62 |
$this->assertStringContainsString( '<mi>sin</mi>\n', $triples ); |
| 63 |
$this->assertStringContainsString( '<mn>2</mn>\n', $triples ); |
| 64 |
$this->assertStringContainsString( 'x^{2}', $triples ); |
| 65 |
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testInvalidInput() { |
| 69 |
$this->setupBadMathRestBaseMockHttp(); |
| 70 |
|
| 71 |
$triples = $this->makeCase( '\sin\newcommand' ); |
| 72 |
$this->assertStringContainsString( '<math', $triples ); |
| 73 |
$this->assertStringContainsString( 'unknown function', $triples ); |
| 74 |
$this->assertStringContainsString( 'newcommand', $triples ); |
| 75 |
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples ); |
| 76 |
} |
| 77 |
} |