2014-10-09 13:49:26 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2014-10-09 13:49:26 +00:00
|
|
|
/**
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathLaTeXML
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
|
|
|
* @group Math
|
2018-09-04 12:27:15 +00:00
|
|
|
* @group Database
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2018-04-13 14:06:39 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2016-02-12 16:57:37 +00:00
|
|
|
*/
|
2014-10-09 13:49:26 +00:00
|
|
|
class MathLaTeXMLDatabaseTest extends MediaWikiTestCase {
|
|
|
|
public $renderer;
|
|
|
|
const SOME_TEX = "a+b";
|
|
|
|
const SOME_HTML = "a<sub>b</sub>";
|
|
|
|
const SOME_MATHML = "iℏ∂_tΨ=H^Ψ<mrow><\ci>";
|
|
|
|
const SOME_LOG = "Sample Log Text.";
|
|
|
|
const SOME_TIMESTAMP = 1272509157;
|
|
|
|
const SOME_SVG = "<?xml </svg >>%%LIKE;'\" DROP TABLE math;";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to test protected/private Methods
|
2018-06-20 09:10:23 +00:00
|
|
|
* @param string $name
|
2014-10-09 13:49:26 +00:00
|
|
|
* @return ReflectionMethod
|
|
|
|
*/
|
2015-09-21 16:14:01 +00:00
|
|
|
protected static function getMethod( $name ) {
|
2018-06-06 10:13:04 +00:00
|
|
|
$class = new ReflectionClass( MathLaTeXML::class );
|
2015-09-21 16:14:01 +00:00
|
|
|
$method = $class->getMethod( $name );
|
|
|
|
$method->setAccessible( true );
|
2014-10-09 13:49:26 +00:00
|
|
|
return $method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a new database connection and a new math renderer
|
|
|
|
* TODO: Check if there is a way to get database access without creating
|
|
|
|
* the connection to the database explicitly
|
|
|
|
* function addDBData() {
|
|
|
|
* $this->tablesUsed[] = 'math';
|
|
|
|
* }
|
|
|
|
* was not sufficient.
|
|
|
|
*/
|
2020-01-28 20:18:17 +00:00
|
|
|
protected function setUp() : void {
|
2014-10-09 13:49:26 +00:00
|
|
|
parent::setUp();
|
|
|
|
// TODO: figure out why this is necessary
|
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
|
|
|
// Create a new instance of MathSource
|
|
|
|
$this->renderer = new MathLaTeXML( self::SOME_TEX );
|
|
|
|
self::setupTestDB( $this->db, "mathtest" );
|
2015-09-21 16:14:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 13:49:26 +00:00
|
|
|
/**
|
|
|
|
* Checks the tex and hash functions
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathRenderer::getInputHash
|
2014-10-09 13:49:26 +00:00
|
|
|
*/
|
|
|
|
public function testInputHash() {
|
|
|
|
$expectedhash = $this->db->encodeBlob( pack( "H32", md5( self::SOME_TEX ) ) );
|
|
|
|
$this->assertEquals( $expectedhash, $this->renderer->getInputHash() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to set the current state of the sample renderer instance to the test values
|
|
|
|
*/
|
|
|
|
public function setValues() {
|
|
|
|
// set some values
|
|
|
|
$this->renderer->setTex( self::SOME_TEX );
|
|
|
|
$this->renderer->setMathml( self::SOME_MATHML );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathLaTeXML::getMathTableName
|
2014-10-09 13:49:26 +00:00
|
|
|
*/
|
|
|
|
public function testTableName() {
|
|
|
|
$fnGetMathTableName = self::getMethod( 'getMathTableName' );
|
|
|
|
$obj = new MathLaTeXML();
|
2016-04-12 20:53:25 +00:00
|
|
|
$tableName = $fnGetMathTableName->invokeArgs( $obj, [] );
|
2014-10-09 13:49:26 +00:00
|
|
|
$this->assertEquals( $tableName, "mathlatexml", "Wrong latexml table name" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-12 19:40:34 +00:00
|
|
|
* Checks the creation of the math table.
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathHooks::onLoadExtensionSchemaUpdates
|
2014-10-09 13:49:26 +00:00
|
|
|
*/
|
|
|
|
public function testCreateTable() {
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->setMwGlobals( 'wgMathValidModes', [ 'latexml' ] );
|
2014-10-09 13:49:26 +00:00
|
|
|
$this->db->dropTable( "mathlatexml", __METHOD__ );
|
|
|
|
$dbu = DatabaseUpdater::newForDB( $this->db );
|
2016-04-12 20:53:25 +00:00
|
|
|
$dbu->doUpdates( [ "extensions" ] );
|
2014-10-09 13:49:26 +00:00
|
|
|
$this->expectOutputRegex( '/(.*)Creating mathlatexml table(.*)/' );
|
|
|
|
$this->setValues();
|
|
|
|
$this->renderer->writeToDatabase();
|
|
|
|
$res = $this->db->select( "mathlatexml", "*" );
|
|
|
|
$row = $res->fetchRow();
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertEquals( 12, count( $row ) );
|
2014-10-09 13:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks database access. Writes an entry and reads it back.
|
|
|
|
* @depends testCreateTable
|
2019-11-26 09:59:54 +00:00
|
|
|
* @covers \MathRenderer::writeToDatabase
|
|
|
|
* @covers \MathRenderer::readFromDatabase
|
2014-10-09 13:49:26 +00:00
|
|
|
*/
|
|
|
|
public function testDBBasics() {
|
|
|
|
$this->setValues();
|
|
|
|
$this->renderer->writeToDatabase();
|
|
|
|
|
|
|
|
$renderer2 = $this->renderer = new MathLaTeXML( self::SOME_TEX );
|
|
|
|
$renderer2->readFromDatabase();
|
|
|
|
// comparing the class object does now work due to null values etc.
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$this->renderer->getTex(), $renderer2->getTex(), "test if tex is the same"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$this->renderer->getMathml(), $renderer2->getMathml(), "Check MathML encoding"
|
|
|
|
);
|
2014-10-09 13:49:26 +00:00
|
|
|
}
|
|
|
|
}
|