2013-04-07 00:01:56 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
use MediaWiki\Extension\Math\MathRenderer;
|
2021-08-11 13:38:51 +00:00
|
|
|
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
2021-04-07 22:22:05 +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
|
|
|
*
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2013-04-07 00:01:56 +00:00
|
|
|
* @group Math
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
2018-04-13 14:06:39 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
2021-10-11 22:51:10 +00:00
|
|
|
class MathRendererTest extends MediaWikiIntegrationTestCase {
|
2021-08-11 13:38:51 +00:00
|
|
|
use MathMockHttpTrait;
|
2015-11-29 23:14:22 +00:00
|
|
|
|
2021-08-11 13:38:51 +00:00
|
|
|
private const SOME_TEX = "a+b";
|
|
|
|
private const TEXVCCHECK_INPUT = '\sin x^2';
|
|
|
|
private const TEXVCCHECK_OUTPUT = '\sin x^{2}';
|
2015-11-29 23:14:22 +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::getTex
|
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::__construct
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
public function testBasics() {
|
2018-06-06 10:13:04 +00:00
|
|
|
$renderer = $this->getMockForAbstractClass( MathRenderer::class, [ self::SOME_TEX ] );
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
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" );
|
2019-11-26 11:01:30 +00:00
|
|
|
$this->assertFalse( $renderer->isChanged(), "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
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::writeCache
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
public function testWriteCacheSkip() {
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2023-11-19 19:23:09 +00:00
|
|
|
'writeToCache',
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput'
|
2019-11-26 11:01:30 +00:00
|
|
|
] )->getMock();
|
2023-11-19 19:23:09 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'writeToCache' );
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
2013-04-07 00:01:56 +00:00
|
|
|
$renderer->writeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test behavior of writeCache() when values were changed.
|
2021-04-07 22:22:05 +00:00
|
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::writeCache
|
2013-04-07 00:01:56 +00:00
|
|
|
*/
|
|
|
|
public function testWriteCache() {
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2023-11-19 19:23:09 +00:00
|
|
|
'writeToCache',
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput'
|
2019-11-26 11:01:30 +00:00
|
|
|
] )->getMock();
|
2023-11-19 19:23:09 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'writeToCache' );
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
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 =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput'
|
2019-11-26 11:01:30 +00:00
|
|
|
] )->getMock();
|
|
|
|
/** @var MathRenderer $renderer */
|
2013-05-14 21:49:06 +00:00
|
|
|
$renderer->setPurge();
|
2019-11-26 11:01:30 +00:00
|
|
|
$this->assertTrue( $renderer->isPurge(), "Test purge." );
|
2013-05-14 21:49:06 +00:00
|
|
|
}
|
2014-09-06 01:59:13 +00:00
|
|
|
|
2015-12-03 07:22:34 +00:00
|
|
|
public function testDisableCheckingAlways() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
|
2015-12-03 07:22:34 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'never' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
2023-11-19 19:23:09 +00:00
|
|
|
'readFromCache',
|
2014-09-06 01:59:13 +00:00
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2023-11-19 19:23:09 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'readFromCache' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->once() )->method( 'setTex' )->with( self::TEXVCCHECK_OUTPUT );
|
|
|
|
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
2021-08-11 13:38:51 +00:00
|
|
|
// now setTex should not be called again
|
2019-11-26 11:01:30 +00:00
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
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 =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
2023-11-19 19:23:09 +00:00
|
|
|
'readFromCache',
|
2014-09-06 01:59:13 +00:00
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2023-11-19 19:23:09 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'readFromCache' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'setTex' );
|
|
|
|
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckingNewUnknown() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
2023-11-19 19:23:09 +00:00
|
|
|
'readFromCache',
|
2014-09-06 01:59:13 +00:00
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2023-11-19 19:23:09 +00:00
|
|
|
$renderer->expects( $this->once() )->method( 'readFromCache' )
|
2022-09-29 13:54:47 +00:00
|
|
|
->willReturn( false );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->once() )->method( 'setTex' )->with( self::TEXVCCHECK_OUTPUT );
|
|
|
|
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
2021-08-11 13:38:51 +00:00
|
|
|
// now setTex should not be called again
|
2019-11-26 11:01:30 +00:00
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckingNewKnown() {
|
2021-08-11 13:38:51 +00:00
|
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer =
|
2021-05-04 08:06:42 +00:00
|
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
2014-09-06 01:59:13 +00:00
|
|
|
'render',
|
|
|
|
'getMathTableName',
|
|
|
|
'getHtmlOutput',
|
2023-11-19 19:23:09 +00:00
|
|
|
'readFromCache',
|
2014-09-06 01:59:13 +00:00
|
|
|
'setTex'
|
2016-04-12 20:53:25 +00:00
|
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
2023-11-19 19:23:09 +00:00
|
|
|
$renderer->expects( $this->once() )->method( 'readFromCache' )
|
2022-09-29 13:54:47 +00:00
|
|
|
->willReturn( true );
|
2014-09-06 01:59:13 +00:00
|
|
|
$renderer->expects( $this->never() )->method( 'setTex' );
|
|
|
|
|
2019-11-26 11:01:30 +00:00
|
|
|
/** @var MathRenderer $renderer */
|
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
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
|
2019-11-26 11:01:30 +00:00
|
|
|
$this->assertTrue( $renderer->checkTeX() );
|
2014-09-06 01:59:13 +00:00
|
|
|
}
|
2015-01-10 01:55:54 +00:00
|
|
|
}
|