2013-04-07 00:01:56 +00:00
|
|
|
<?php
|
2014-06-27 22:20:09 +00:00
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMathML;
|
|
|
|
use MediaWiki\Extension\Math\MathRenderer;
|
|
|
|
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
2014-06-27 22:20:09 +00:00
|
|
|
* Test the database access and core functionality of MathRenderer.
|
|
|
|
*
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2014-06-27 22:20:09 +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
|
2014-06-27 22:20:09 +00:00
|
|
|
*/
|
2013-04-07 00:01:56 +00:00
|
|
|
class MathDatabaseTest extends MediaWikiTestCase {
|
2014-06-05 21:06:43 +00:00
|
|
|
/**
|
|
|
|
* @var MathRenderer
|
|
|
|
*/
|
|
|
|
private $renderer;
|
2020-05-30 01:13:31 +00:00
|
|
|
private const SOME_TEX = "a+b";
|
|
|
|
private const SOME_HTML = "a<sub>b</sub> and so on";
|
|
|
|
private const SOME_MATHML = "iℏ∂_tΨ=H^Ψ<mrow><\ci>";
|
|
|
|
private const SOME_CONSERVATIVENESS = 2;
|
|
|
|
private const SOME_OUTPUTHASH = 'C65c884f742c8591808a121a828bc09f8<';
|
2014-05-21 10:16:15 +00:00
|
|
|
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
|
|
|
* creates a new database connection and a new math renderer
|
|
|
|
* TODO: Check if there is a way to get database access without creating
|
2014-05-21 10:16:15 +00:00
|
|
|
* the connection to the database explicitly
|
2013-04-07 00:01:56 +00:00
|
|
|
* function addDBData() {
|
2014-06-27 22:20:09 +00:00
|
|
|
* $this->tablesUsed[] = 'math';
|
2013-04-07 00:01:56 +00:00
|
|
|
* }
|
2014-05-21 10:16:15 +00:00
|
|
|
* was not sufficient.
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
2020-01-28 20:18:17 +00:00
|
|
|
protected function setUp() : void {
|
2013-04-07 00:01:56 +00:00
|
|
|
parent::setUp();
|
2014-05-21 10:16:15 +00:00
|
|
|
// TODO: figure out why this is necessary
|
2013-04-07 00:01:56 +00:00
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
2018-05-19 18:43:04 +00:00
|
|
|
$this->renderer = new MathMathML( self::SOME_TEX );
|
|
|
|
$this->tablesUsed[] = 'mathoid';
|
2014-06-27 22:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
|
|
|
* Checks the tex and hash functions
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::getInputHash
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
public function testInputHash() {
|
|
|
|
$expectedhash = $this->db->encodeBlob( pack( "H32", md5( self::SOME_TEX ) ) );
|
|
|
|
$this->assertEquals( $expectedhash, $this->renderer->getInputHash() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-21 10:16:15 +00:00
|
|
|
* Helper function to set the current state of the sample renderer instance to the test values
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
public function setValues() {
|
|
|
|
// set some values
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->renderer->setTex( self::SOME_TEX );
|
|
|
|
$this->renderer->setMathml( self::SOME_MATHML );
|
2013-04-07 00:01:56 +00:00
|
|
|
}
|
2014-06-27 22:20:09 +00:00
|
|
|
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
2014-05-21 10:16:15 +00:00
|
|
|
* Checks database access. Writes an entry and reads it back.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::writeToDatabase
|
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::readFromDatabase
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
public function testDBBasics() {
|
|
|
|
$this->setValues();
|
2014-05-21 10:16:15 +00:00
|
|
|
$this->renderer->writeToDatabase( $this->db );
|
2018-05-19 18:43:04 +00:00
|
|
|
$renderer2 = new MathMathML( self::SOME_TEX );
|
2014-05-21 10:16:15 +00:00
|
|
|
$this->assertTrue( $renderer2->readFromDatabase(), 'Reading from database failed' );
|
2013-04-07 00:01:56 +00:00
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2018-05-19 18:43:04 +00:00
|
|
|
$this->renderer->getHtmlOutput(), $renderer2->getHtmlOutput(), 'test if HTML is the same'
|
2015-09-21 16:14:01 +00:00
|
|
|
);
|
2013-04-07 00:01:56 +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
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
2014-05-21 10:16:15 +00:00
|
|
|
public function testCreateTable() {
|
2018-05-19 18:43:04 +00:00
|
|
|
$this->setMwGlobals( 'wgMathValidModes', [ 'mathml' ] );
|
|
|
|
$this->db->dropTable( "mathoid", __METHOD__ );
|
2013-04-07 00:01:56 +00:00
|
|
|
$dbu = DatabaseUpdater::newForDB( $this->db );
|
2016-04-12 20:53:25 +00:00
|
|
|
$dbu->doUpdates( [ "extensions" ] );
|
2018-05-19 18:43:04 +00:00
|
|
|
$this->expectOutputRegex( '/(.*)Creating mathoid table(.*)/' );
|
2013-04-07 00:01:56 +00:00
|
|
|
$this->setValues();
|
2013-04-24 06:01:53 +00:00
|
|
|
$this->renderer->writeToDatabase();
|
2018-05-19 18:43:04 +00:00
|
|
|
$res = $this->db->select( "mathoid", "*" );
|
2013-04-07 00:01:56 +00:00
|
|
|
$row = $res->fetchRow();
|
2020-03-17 07:14:13 +00:00
|
|
|
$this->assertCount( 16, $row );
|
2013-04-07 00:01:56 +00:00
|
|
|
}
|
2014-06-05 21:06:43 +00:00
|
|
|
|
2021-01-23 00:46:48 +00:00
|
|
|
/**
|
2014-06-05 21:06:43 +00:00
|
|
|
* This test checks if no additional write operation
|
|
|
|
* is performed, if the entry already existed in the database.
|
|
|
|
*/
|
|
|
|
public function testNoWrite() {
|
|
|
|
$this->setValues();
|
|
|
|
$inputHash = $this->renderer->getInputHash();
|
|
|
|
$this->assertTrue( $this->renderer->isChanged() );
|
|
|
|
$this->assertTrue( $this->renderer->writeCache(), "Write new entry" );
|
2018-05-19 18:43:04 +00:00
|
|
|
$res = $this->db->selectField( "mathoid", "math_inputhash",
|
2016-04-12 20:53:25 +00:00
|
|
|
[ "math_inputhash" => $inputHash ] );
|
2014-06-27 22:20:09 +00:00
|
|
|
$this->assertTrue( $res !== false, "Check database entry" );
|
|
|
|
$this->assertTrue( $this->renderer->readFromDatabase(), "Read entry from database" );
|
2014-06-05 21:06:43 +00:00
|
|
|
$this->assertFalse( $this->renderer->isChanged() );
|
|
|
|
// modify the database entry manually
|
2018-05-19 18:43:04 +00:00
|
|
|
$this->db->delete( "mathoid", [ "math_inputhash" => $inputHash ] );
|
2014-06-05 21:06:43 +00:00
|
|
|
// the renderer should not be aware of the modification and should not recreate the entry
|
|
|
|
$this->assertFalse( $this->renderer->writeCache() );
|
|
|
|
// as a result no entry can be found in the database.
|
|
|
|
$this->assertFalse( $this->renderer->readFromDatabase() );
|
|
|
|
}
|
2013-04-26 12:11:41 +00:00
|
|
|
}
|