2014-10-09 13:49:26 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathLaTeXML;
|
|
|
|
|
2014-10-09 13:49:26 +00:00
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\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;
|
2020-05-30 01:13:31 +00:00
|
|
|
private const SOME_TEX = "a+b";
|
|
|
|
private const SOME_HTML = "a<sub>b</sub>";
|
|
|
|
private const SOME_MATHML = "iℏ∂_tΨ=H^Ψ<mrow><\ci>";
|
|
|
|
private const SOME_LOG = "Sample Log Text.";
|
|
|
|
private const SOME_TIMESTAMP = 1272509157;
|
|
|
|
private const SOME_SVG = "<?xml </svg >>%%LIKE;'\" DROP TABLE math;";
|
2014-10-09 13:49:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-07-22 11:56:28 +00:00
|
|
|
protected function setUp(): void {
|
2014-10-09 13:49:26 +00:00
|
|
|
parent::setUp();
|
|
|
|
// TODO: figure out why this is necessary
|
2021-07-27 14:26:50 +00:00
|
|
|
$this->db = wfGetDB( DB_PRIMARY );
|
2014-10-09 13:49:26 +00:00
|
|
|
$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
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\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, [] );
|
2021-01-29 17:01:16 +00:00
|
|
|
$this->assertEquals( "mathlatexml", $tableName, "Wrong latexml table name" );
|
2014-10-09 13:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-12 19:40:34 +00:00
|
|
|
* Checks the creation of the math table.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\Hooks::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();
|
2020-03-17 07:14:13 +00:00
|
|
|
$this->assertCount( 12, $row );
|
2014-10-09 13:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks database access. Writes an entry and reads it back.
|
|
|
|
* @depends testCreateTable
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::writeToDatabase
|
|
|
|
* @covers \MediaWiki\Extension\Math\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
|
|
|
}
|
|
|
|
}
|