2016-02-09 19:53:51 +00:00
|
|
|
<?php
|
2018-03-10 12:17:05 +00:00
|
|
|
|
|
|
|
use DataValues\StringValue;
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMLRdfBuilder;
|
2021-08-11 13:38:51 +00:00
|
|
|
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
2018-03-10 12:17:05 +00:00
|
|
|
use Wikibase\DataModel\Entity\PropertyId;
|
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak;
|
|
|
|
use Wikimedia\Purtle\NTriplesRdfWriter;
|
|
|
|
|
2016-02-09 19:53:51 +00:00
|
|
|
/**
|
|
|
|
* Test the MathML RDF formatter
|
|
|
|
*
|
|
|
|
* @group Math
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathMLRdfBuilder
|
2016-02-09 19:53:51 +00:00
|
|
|
* @author Moritz Schubotz (physikerwelt)
|
|
|
|
*/
|
|
|
|
class MathMLRdfBuilderTest extends MediaWikiTestCase {
|
2021-08-11 13:38:51 +00:00
|
|
|
use MathMockHttpTrait;
|
|
|
|
|
2020-05-30 01:13:31 +00:00
|
|
|
private const ACME_PREFIX_URL = 'http://acme/';
|
|
|
|
private const ACME_REF = 'testing';
|
2016-02-09 19:53:51 +00:00
|
|
|
|
2021-08-12 12:54:49 +00:00
|
|
|
protected function setUp(): void {
|
|
|
|
$this->markTestSkippedIfExtensionNotLoaded( 'WikibaseClient' );
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2016-02-09 19:53:51 +00:00
|
|
|
/**
|
|
|
|
* @param string $test
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function makeCase( $test ) {
|
|
|
|
$builder = new MathMLRdfBuilder();
|
|
|
|
$writer = new NTriplesRdfWriter();
|
|
|
|
$writer->prefix( 'www', "http://www/" );
|
|
|
|
$writer->prefix( 'acme', self::ACME_PREFIX_URL );
|
|
|
|
|
|
|
|
$writer->start();
|
|
|
|
$writer->about( 'www', 'Q1' );
|
|
|
|
|
|
|
|
$snak = new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( $test ) );
|
2020-01-18 13:54:59 +00:00
|
|
|
$builder->addValue( $writer, 'acme', self::ACME_REF, 'DUMMY', '', $snak );
|
2016-02-09 19:53:51 +00:00
|
|
|
|
|
|
|
return trim( $writer->drain() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidInput() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
|
|
|
|
$triples = $this->makeCase( '\sin x^2' );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString(
|
|
|
|
self::ACME_PREFIX_URL . self::ACME_REF . '> "<math',
|
|
|
|
$triples
|
|
|
|
);
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->assertStringContainsString( '<mi>sin</mi>\n', $triples );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '<mn>2</mn>\n', $triples );
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->assertStringContainsString( 'x^{2}', $triples );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
|
2016-02-09 19:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidInput() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupBadMathRestBaseMockHttp();
|
|
|
|
|
|
|
|
$triples = $this->makeCase( '\sin\newcommand' );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '<math', $triples );
|
|
|
|
$this->assertStringContainsString( 'unknown function', $triples );
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->assertStringContainsString( 'newcommand', $triples );
|
2020-04-05 11:38:45 +00:00
|
|
|
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
|
2016-02-09 19:53:51 +00:00
|
|
|
}
|
|
|
|
}
|