mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-11-28 01:10:09 +00:00
9236575a1a
* Uses BagOfStuff caching instead of custom-made DB cache * By configuring the BagOfStuff cache in a way that it writes to the database no performance implications are expected * For WMF-use this should have no effect since restbase is used to cache stuff * Replaces Ib2c216f54e6817ee2c3be0355ba72bd4769ba6ea Bug: T349442 Change-Id: I1ce8ad9cf4c1a9ae71f447e4e067b39ee2601640
164 lines
5.1 KiB
PHP
164 lines
5.1 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Extension\Math\MathRenderer;
|
|
use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
|
|
|
|
/**
|
|
* Test the database access and core functionality of MathRenderer.
|
|
*
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer
|
|
*
|
|
* @group Math
|
|
*
|
|
* @license GPL-2.0-or-later
|
|
*/
|
|
class MathRendererTest extends MediaWikiIntegrationTestCase {
|
|
use MathMockHttpTrait;
|
|
|
|
private const SOME_TEX = "a+b";
|
|
private const TEXVCCHECK_INPUT = '\sin x^2';
|
|
private const TEXVCCHECK_OUTPUT = '\sin x^{2}';
|
|
|
|
/**
|
|
* Checks the tex and hash functions
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::getTex
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::__construct
|
|
*/
|
|
public function testBasics() {
|
|
$renderer = $this->getMockForAbstractClass( MathRenderer::class, [ self::SOME_TEX ] );
|
|
/** @var MathRenderer $renderer */
|
|
// check if the TeX input was corretly passed to the class
|
|
$this->assertEquals( self::SOME_TEX, $renderer->getTex(), "test getTex" );
|
|
$this->assertFalse( $renderer->isChanged(), "test if changed is initially false" );
|
|
}
|
|
|
|
/**
|
|
* Test behavior of writeCache() when nothing was changed
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::writeCache
|
|
*/
|
|
public function testWriteCacheSkip() {
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'writeToCache',
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput'
|
|
] )->getMock();
|
|
$renderer->expects( $this->never() )->method( 'writeToCache' );
|
|
/** @var MathRenderer $renderer */
|
|
$renderer->writeCache();
|
|
}
|
|
|
|
/**
|
|
* Test behavior of writeCache() when values were changed.
|
|
* @covers \MediaWiki\Extension\Math\MathRenderer::writeCache
|
|
*/
|
|
public function testWriteCache() {
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'writeToCache',
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput'
|
|
] )->getMock();
|
|
$renderer->expects( $this->never() )->method( 'writeToCache' );
|
|
/** @var MathRenderer $renderer */
|
|
$renderer->writeCache();
|
|
}
|
|
|
|
public function testSetPurge() {
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput'
|
|
] )->getMock();
|
|
/** @var MathRenderer $renderer */
|
|
$renderer->setPurge();
|
|
$this->assertTrue( $renderer->isPurge(), "Test purge." );
|
|
}
|
|
|
|
public function testDisableCheckingAlways() {
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'never' );
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput',
|
|
'readFromCache',
|
|
'setTex'
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
|
$renderer->expects( $this->never() )->method( 'readFromCache' );
|
|
$renderer->expects( $this->once() )->method( 'setTex' )->with( self::TEXVCCHECK_OUTPUT );
|
|
|
|
/** @var MathRenderer $renderer */
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
// now setTex should not be called again
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
}
|
|
|
|
public function testDisableCheckingNever() {
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'always' );
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput',
|
|
'readFromCache',
|
|
'setTex'
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
|
$renderer->expects( $this->never() )->method( 'readFromCache' );
|
|
$renderer->expects( $this->never() )->method( 'setTex' );
|
|
|
|
/** @var MathRenderer $renderer */
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
}
|
|
|
|
public function testCheckingNewUnknown() {
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput',
|
|
'readFromCache',
|
|
'setTex'
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
|
$renderer->expects( $this->once() )->method( 'readFromCache' )
|
|
->willReturn( false );
|
|
$renderer->expects( $this->once() )->method( 'setTex' )->with( self::TEXVCCHECK_OUTPUT );
|
|
|
|
/** @var MathRenderer $renderer */
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
// now setTex should not be called again
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
}
|
|
|
|
public function testCheckingNewKnown() {
|
|
$this->setupGoodMathRestBaseMockHttp();
|
|
|
|
$this->setMwGlobals( "wgMathDisableTexFilter", 'new' );
|
|
$renderer =
|
|
$this->getMockBuilder( MathRenderer::class )->onlyMethods( [
|
|
'render',
|
|
'getMathTableName',
|
|
'getHtmlOutput',
|
|
'readFromCache',
|
|
'setTex'
|
|
] )->setConstructorArgs( [ self::TEXVCCHECK_INPUT ] )->getMock();
|
|
$renderer->expects( $this->once() )->method( 'readFromCache' )
|
|
->willReturn( true );
|
|
$renderer->expects( $this->never() )->method( 'setTex' );
|
|
|
|
/** @var MathRenderer $renderer */
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
// we don't mark a object as checked even though we rely on the database cache
|
|
// so readFromDatabase will be called again
|
|
$this->assertTrue( $renderer->checkTeX() );
|
|
}
|
|
}
|