mediawiki-extensions-Math/tests/phpunit/MathMLRdfBuilderTest.php
Jakob Warkotsch 950b150e8c FP: Change PropertyId to NumericPropertyId
See ADR 22 for more info.

Bug: T289125
Change-Id: Id6293612a102d2785f5fee984af431e1219b64c0
2021-09-17 10:47:14 +02:00

71 lines
2.1 KiB
PHP

<?php
use DataValues\StringValue;
use MediaWiki\Extension\Math\MathMLRdfBuilder;
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
use Wikibase\DataModel\Entity\NumericPropertyId;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikimedia\Purtle\NTriplesRdfWriter;
/**
* Test the MathML RDF formatter
*
* @group Math
* @covers \MediaWiki\Extension\Math\MathMLRdfBuilder
* @author Moritz Schubotz (physikerwelt)
*/
class MathMLRdfBuilderTest extends MediaWikiTestCase {
use MathMockHttpTrait;
private const ACME_PREFIX_URL = 'http://acme/';
private const ACME_REF = 'testing';
protected function setUp(): void {
$this->markTestSkippedIfExtensionNotLoaded( 'WikibaseClient' );
parent::setUp();
}
/**
* @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 NumericPropertyId( 'P1' ), new StringValue( $test ) );
$builder->addValue( $writer, 'acme', self::ACME_REF, 'DUMMY', '', $snak );
return trim( $writer->drain() );
}
public function testValidInput() {
$this->setupGoodMathRestBaseMockHttp();
$triples = $this->makeCase( '\sin x^2' );
$this->assertStringContainsString(
self::ACME_PREFIX_URL . self::ACME_REF . '> "<math',
$triples
);
$this->assertStringContainsString( '<mi>sin</mi>\n', $triples );
$this->assertStringContainsString( '<mn>2</mn>\n', $triples );
$this->assertStringContainsString( 'x^{2}', $triples );
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
}
public function testInvalidInput() {
$this->setupBadMathRestBaseMockHttp();
$triples = $this->makeCase( '\sin\newcommand' );
$this->assertStringContainsString( '<math', $triples );
$this->assertStringContainsString( 'unknown function', $triples );
$this->assertStringContainsString( 'newcommand', $triples );
$this->assertStringContainsString( '^^<http://www.w3.org/1998/Math/MathML> .', $triples );
}
}