2013-04-07 00:01:56 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
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
|
|
|
*
|
2016-02-12 16:57:37 +00:00
|
|
|
* @covers MathRenderer
|
|
|
|
*
|
2013-04-07 00:01:56 +00:00
|
|
|
* @group Math
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
|
|
|
* @licence GNU GPL v2+
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
class MathRendererTest extends MediaWikiTestCase {
|
2014-05-28 19:31:41 +00:00
|
|
|
const SOME_TEX = "a+b";
|
2014-09-06 01:59:13 +00:00
|
|
|
const TEXVCCHECK_INPUT = '\forall \epsilon \exist \delta';
|
|
|
|
const TEXVCCHECK_OUTPUT = '\forall \epsilon \exists \delta '; // be aware of the s at exists
|
2015-11-29 23:14:22 +00:00
|
|
|
|
|
|
|
protected static $hasRestbase;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
$rbi = new MathRestbaseInterface();
|
|
|
|
self::$hasRestbase = $rbi->checkBackend( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the fixture, for example, opens a network connection.
|
|
|
|
* This method is called before a test is executed.
|
|
|
|
*/
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
if ( !self::$hasRestbase ) {
|
|
|
|
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-07 00:01:56 +00:00
|
|
|
/**
|
|
|
|
* Checks the tex and hash functions
|
|
|
|
* @covers MathRenderer::getTex()
|
|
|
|
* @covers MathRenderer::__construct()
|
|
|
|
*/
|
|
|
|
public function testBasics() {
|
2016-04-12 20:53:25 +00:00
|
|
|
$renderer = $this->getMockForAbstractClass( 'MathRenderer', [ self::SOME_TEX ] );
|
2013-04-07 00:01:56 +00:00
|
|
|
// check if the TeX input was corretly passed to the class
|
2014-09-06 01:59:13 +00:00
|
|
|
$this->assertEquals( self::SOME_TEX, $renderer->getTex(), "test getTex" );
|
|
|
|
$this->assertEquals( $renderer->isChanged(), false, "test if changed is initially false" );
|
2013-04-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-25 17:07:25 +00:00
|
|
|
* Test behavior of writeCache() when nothing was changed
|
2013-04-07 00:01:56 +00:00
|
|
|
* @covers MathRenderer::writeCache()
|
|
|
|
*/
|
|
|
|
public function testWriteCacheSkip() {
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'writeToDatabase',
|
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->disableOriginalConstructor()->getMock();
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'writeToDatabase' );
|
2013-04-07 00:01:56 +00:00
|
|
|
$renderer->writeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test behavior of writeCache() when values were changed.
|
|
|
|
* @covers MathRenderer::writeCache()
|
|
|
|
*/
|
|
|
|
public function testWriteCache() {
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'writeToDatabase',
|
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->disableOriginalConstructor()->getMock();
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'writeToDatabase' );
|
2013-04-07 00:01:56 +00:00
|
|
|
$renderer->writeCache();
|
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
|
2014-02-09 19:02:15 +00:00
|
|
|
public function testSetPurge() {
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->disableOriginalConstructor()->getMock();
|
2013-05-14 21:49:06 +00:00
|
|
|
$renderer->setPurge();
|
|
|
|
$this->assertEquals( $renderer->isPurge(), true, "Test purge." );
|
|
|
|
}
|
2014-09-06 01:59:13 +00:00
|
|
|
|
2015-12-03 07:22:34 +00:00
|
|
|
public function testDisableCheckingAlways() {
|
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'never' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
|
|
|
'readFromDatabase',
|
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'readFromDatabase' );
|
|
|
|
$renderer->expects( $this->once() )->method( 'setTex' )->with( self::TEXVCCHECK_OUTPUT );
|
|
|
|
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
// now setTex sould not be called again
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 07:22:34 +00:00
|
|
|
public function testDisableCheckingNever() {
|
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'always' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
|
|
|
'readFromDatabase',
|
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'readFromDatabase' );
|
|
|
|
$renderer->expects( $this->never() )->method( 'setTex' );
|
|
|
|
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckingNewUnknown() {
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
|
|
|
'readFromDatabase',
|
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->once() )->method( 'readFromDatabase' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
$renderer->expects( $this->once() )->method( 'setTex' )->with( self::TEXVCCHECK_OUTPUT );
|
|
|
|
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
// now setTex sould not be called again
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckingNewKnown() {
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->getMockBuilder( 'MathRenderer' )->setMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
|
|
|
'readFromDatabase',
|
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->exactly( 2 ) )->method( 'readFromDatabase' )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
$renderer->expects( $this->never() )->method( 'setTex' );
|
|
|
|
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
// we don't mark a object as checked even though we rely on the database cache
|
|
|
|
// so readFromDatabase will be called again
|
2015-12-14 22:58:08 +00:00
|
|
|
$this->assertEquals( $renderer->checkTeX(), true );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
2015-01-10 01:55:54 +00:00
|
|
|
}
|