2013-04-07 00:01:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-28 19:31:41 +00:00
|
|
|
* Test the database access and core functionality of MathRenderer.
|
2013-04-07 00:01:56 +00:00
|
|
|
*
|
|
|
|
* @group Math
|
|
|
|
* @group Database //Used by needsDB
|
|
|
|
*/
|
|
|
|
class MathDatabaseTest extends MediaWikiTestCase {
|
|
|
|
var $renderer;
|
|
|
|
const SOME_TEX = "a+b";
|
2014-05-21 10:16:15 +00:00
|
|
|
const SOME_HTML = "a<sub>b</sub> and so on";
|
|
|
|
const SOME_MATHML = "iℏ∂_tΨ=H^Ψ<mrow><\ci>";
|
|
|
|
const SOME_CONSERVATIVENESS = 2;
|
|
|
|
const SOME_OUTPUTHASH = 'C65c884f742c8591808a121a828bc09f8<';
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
* $this->tablesUsed[] = 'math';
|
|
|
|
* }
|
2014-05-21 10:16:15 +00:00
|
|
|
* was not sufficient.
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
protected function setup() {
|
|
|
|
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 );
|
|
|
|
// Create a new instance of MathSource
|
2014-05-21 10:16:15 +00:00
|
|
|
$this->renderer = new MathTexvc( self::SOME_TEX );
|
2013-04-07 00:01:56 +00:00
|
|
|
$this->tablesUsed[] = 'math';
|
|
|
|
self::setupTestDB( $this->db, "mathtest" );
|
2014-05-21 10:16:15 +00:00
|
|
|
}
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
|
|
|
* Checks the tex and hash functions
|
|
|
|
* @covers MathRenderer::getInputHash()
|
|
|
|
*/
|
|
|
|
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 );
|
2014-05-21 10:16:15 +00:00
|
|
|
$this->renderer->setHtml( self::SOME_HTML );
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->renderer->setOutputHash( self::SOME_OUTPUTHASH);
|
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.
|
|
|
|
* @covers MathRenderer::writeDatabaseEntry()
|
|
|
|
* @covers MathRenderer::readDatabaseEntry()
|
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 );
|
|
|
|
$renderer2 = new MathTexvc( self::SOME_TEX );
|
|
|
|
$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.
|
|
|
|
$this->assertEquals( $this->renderer->getTex(), $renderer2->getTex(), "test if tex is the same" );
|
2013-05-14 21:49:06 +00:00
|
|
|
$this->assertEquals( $this->renderer->getMathml(), $renderer2->getMathml(), "Check MathML encoding" );
|
2014-05-21 10:16:15 +00:00
|
|
|
$this->assertEquals( $this->renderer->getHtml(), $renderer2->getHtml(), 'test if HTML is the same' );
|
2013-04-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-21 10:16:15 +00:00
|
|
|
* Checks the creation of the math table without debugging enabled.
|
2013-04-07 00:01:56 +00:00
|
|
|
* @covers MathHooks::onLoadExtensionSchemaUpdates
|
|
|
|
*/
|
2014-05-21 10:16:15 +00:00
|
|
|
public function testCreateTable() {
|
|
|
|
$this->setMwGlobals( 'wgMathValidModes', array( MW_MATH_PNG ) );
|
2013-04-07 00:01:56 +00:00
|
|
|
$this->db->dropTable( "math", __METHOD__ );
|
|
|
|
$dbu = DatabaseUpdater::newForDB( $this->db );
|
|
|
|
$dbu->doUpdates( array( "extensions" ) );
|
|
|
|
$this->expectOutputRegex( '/(.*)Creating math table(.*)/' );
|
|
|
|
$this->setValues();
|
2013-04-24 06:01:53 +00:00
|
|
|
$this->renderer->writeToDatabase();
|
2013-04-07 00:01:56 +00:00
|
|
|
$res = $this->db->select( "math", "*" );
|
|
|
|
$row = $res->fetchRow();
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->assertEquals( 10, sizeof( $row ) );
|
2013-04-07 00:01:56 +00:00
|
|
|
}
|
2013-04-26 12:11:41 +00:00
|
|
|
}
|