2013-02-18 04:55:47 +00:00
|
|
|
<?php
|
2016-02-12 16:57:37 +00:00
|
|
|
|
2013-02-18 04:55:47 +00:00
|
|
|
/**
|
|
|
|
* PHPUnit tests for MathTexvc.
|
|
|
|
*
|
2016-02-12 16:57:37 +00:00
|
|
|
* @covers MathTexvc
|
|
|
|
*
|
2013-02-18 04:55:47 +00:00
|
|
|
* @group Extensions
|
|
|
|
* @group Math
|
2016-02-12 16:57:37 +00:00
|
|
|
*
|
|
|
|
* @licence GNU GPL v2+
|
2013-02-18 04:55:47 +00:00
|
|
|
*/
|
|
|
|
class MathTexvcTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of render() upon a cache hit.
|
|
|
|
* If the rendered object exists in the database cache, MathTexvc
|
|
|
|
* just generates HTML from it, and skips shelling out to texvc
|
|
|
|
* entirely.
|
|
|
|
* @covers MathTexvc::render
|
|
|
|
*/
|
|
|
|
function testRenderCacheHit() {
|
|
|
|
global $wgMathCheckFiles;
|
|
|
|
|
|
|
|
// Disable file checks. (This is permissable, because PHPUnit
|
|
|
|
// will backup / restore global state on test setup / teardown.)
|
|
|
|
$wgMathCheckFiles = false;
|
|
|
|
|
2013-04-24 06:01:53 +00:00
|
|
|
// Create a MathTexvc mock, replacing methods 'readFromDatabase',
|
2013-02-18 04:55:47 +00:00
|
|
|
// 'callTexvc', and 'doHTMLRender' with test doubles.
|
2014-06-05 21:06:43 +00:00
|
|
|
$texvc = $this->getMockBuilder( 'MathTexvc' )
|
|
|
|
->setMethods( array( 'readCache', 'callTexvc', 'getHtmlOutput' ) )
|
|
|
|
->disableOriginalConstructor()
|
2013-02-18 04:55:47 +00:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// When we call render() below, MathTexvc will ...
|
|
|
|
|
|
|
|
// ... first check if the item exists in the database cache:
|
2014-06-05 21:06:43 +00:00
|
|
|
$texvc->expects( $this->once() )
|
|
|
|
->method( 'readCache' )
|
|
|
|
->with()
|
2013-02-18 04:55:47 +00:00
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
|
|
|
// ... if cache lookup succeeded, it won't shell out to texvc:
|
|
|
|
$texvc->expects( $this->never() )
|
|
|
|
->method( 'callTexvc' );
|
|
|
|
|
2014-06-05 21:06:43 +00:00
|
|
|
// ... the output will not be generated if not requested
|
|
|
|
$texvc->expects( $this->never() )
|
|
|
|
->method( 'getHtmlOutput' );
|
2013-02-18 04:55:47 +00:00
|
|
|
|
|
|
|
$texvc->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test behavior of render() upon cache miss.
|
|
|
|
* If the rendered object is not in the cache, MathTexvc will shell
|
|
|
|
* out to texvc to generate it. If texvc succeeds, it'll use the
|
|
|
|
* result to generate HTML.
|
|
|
|
* @covers MathTexvc::render
|
|
|
|
*/
|
|
|
|
function testRenderCacheMiss() {
|
|
|
|
$texvc = $this->getMockBuilder( 'MathTexvc' )
|
2014-06-05 21:06:43 +00:00
|
|
|
->setMethods( array( 'readCache', 'callTexvc', 'getHtmlOutput' ) )
|
2013-02-18 04:55:47 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// When we call render() below, MathTexvc will ...
|
|
|
|
|
|
|
|
// ... first look up the item in cache:
|
|
|
|
$texvc->expects( $this->once() )
|
|
|
|
->method( 'readCache' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
|
|
|
|
// ... on cache miss, MathTexvc will shell out to texvc:
|
|
|
|
$texvc->expects( $this->once() )
|
|
|
|
->method( 'callTexvc' )
|
2013-06-07 15:37:56 +00:00
|
|
|
->will( $this->returnValue( MathTexvc::MW_TEXVC_SUCCESS ) );
|
2013-02-18 04:55:47 +00:00
|
|
|
|
2014-06-05 21:06:43 +00:00
|
|
|
// ... the output will not be generated if not requested
|
|
|
|
$texvc->expects( $this->never() )
|
|
|
|
->method( 'getHtmlOutput' );
|
2013-02-18 04:55:47 +00:00
|
|
|
|
|
|
|
$texvc->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test behavior of render() when texvc fails.
|
|
|
|
* If texvc returns a value other than MW_TEXVC_SUCCESS, render()
|
|
|
|
* returns the error object and does not attempt to generate HTML.
|
|
|
|
* @covers MathTexvc::render
|
|
|
|
*/
|
|
|
|
function testRenderTexvcFailure() {
|
|
|
|
$texvc = $this->getMockBuilder( 'MathTexvc' )
|
2014-06-05 21:06:43 +00:00
|
|
|
->setMethods( array( 'readCache', 'callTexvc', 'getHtmlOutput' ) )
|
2013-02-18 04:55:47 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// When we call render() below, MathTexvc will ...
|
|
|
|
|
|
|
|
// ... first look up the item in cache:
|
|
|
|
$texvc->expects( $this->any() )
|
|
|
|
->method( 'readCache' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
|
|
|
|
// ... on cache miss, shell out to texvc:
|
|
|
|
$texvc->expects( $this->once() )
|
2014-06-05 21:06:43 +00:00
|
|
|
->method( 'callTexvc' )
|
|
|
|
->will( $this->returnValue( false ) );
|
2013-02-18 04:55:47 +00:00
|
|
|
|
|
|
|
// ... if texvc fails, render() will not generate HTML:
|
|
|
|
$texvc->expects( $this->never() )
|
2014-06-05 21:06:43 +00:00
|
|
|
->method( 'getHtmlOutput' );
|
2013-02-18 04:55:47 +00:00
|
|
|
|
|
|
|
// ... it will return the error result instead:
|
2014-06-05 21:06:43 +00:00
|
|
|
$this->assertEquals( $texvc->render(), false );
|
2013-02-18 04:55:47 +00:00
|
|
|
}
|
2013-05-17 07:30:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests behavior of convertTexvcError
|
|
|
|
*
|
|
|
|
* @covers MathTexvc::convertTexvcError
|
|
|
|
*/
|
|
|
|
public function testConvertTexvcError() {
|
|
|
|
$texvc = $this->getMockBuilder( 'MathTexvc' )
|
2015-09-21 16:14:01 +00:00
|
|
|
->setMethods( null )
|
2013-05-17 07:30:51 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$mathFailure = wfMessage( 'math_failure' )->inContentLanguage()->escaped();
|
|
|
|
|
|
|
|
$actualLexing = $texvc->convertTexvcError( 'E' );
|
|
|
|
$expectedLexing = wfMessage( 'math_lexing_error', '' )->inContentLanguage()->escaped();
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertContains(
|
|
|
|
$mathFailure, $actualLexing, 'Lexing error contains general math failure message'
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
$expectedLexing, $actualLexing, 'Lexing error contains detailed error for lexing'
|
|
|
|
);
|
2013-05-17 07:30:51 +00:00
|
|
|
|
|
|
|
$actualSyntax = $texvc->convertTexvcError( 'S' );
|
|
|
|
$expectedSyntax = wfMessage( 'math_syntax_error', '' )->inContentLanguage()->escaped();
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertContains(
|
|
|
|
$mathFailure, $actualSyntax, 'Syntax error contains general math failure message'
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
$expectedSyntax, $actualSyntax, 'Syntax error contains detailed error for syntax'
|
|
|
|
);
|
2013-05-17 07:30:51 +00:00
|
|
|
|
|
|
|
$unknownFunction = 'figureEightIntegral';
|
|
|
|
$actualUnknownFunction = $texvc->convertTexvcError( "F$unknownFunction" );
|
2015-09-21 16:14:01 +00:00
|
|
|
$expectedUnknownFunction = wfMessage(
|
|
|
|
'math_unknown_function', $unknownFunction
|
|
|
|
)->inContentLanguage()->escaped();
|
|
|
|
$this->assertContains( $mathFailure, $actualUnknownFunction,
|
|
|
|
'Unknown function error contains general math failure message'
|
|
|
|
);
|
|
|
|
$this->assertContains( $expectedUnknownFunction, $actualUnknownFunction,
|
|
|
|
'Unknown function error contains detailed error for unknown function'
|
|
|
|
);
|
2013-05-17 07:30:51 +00:00
|
|
|
|
|
|
|
$actualUnknownError = $texvc->convertTexvcError( 'Q' );
|
|
|
|
$expectedUnknownError = wfMessage( 'math_unknown_error', '' )->inContentLanguage()->escaped();
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->assertContains(
|
|
|
|
$mathFailure, $actualUnknownError, 'Unknown error contains general math failure message'
|
|
|
|
);
|
|
|
|
$this->assertContains( $expectedUnknownError, $actualUnknownError,
|
|
|
|
'Unknown error contains detailed error for unknownError'
|
|
|
|
);
|
2013-05-17 07:30:51 +00:00
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
|
2014-05-28 19:31:41 +00:00
|
|
|
/**
|
|
|
|
* Test behavior $change when the rendered hash was changed
|
|
|
|
* @covers MathRenderer::setHash()
|
|
|
|
*/
|
|
|
|
public function testChangeHash() {
|
|
|
|
$renderer = $this->getMockBuilder( 'MathTexvc' )
|
|
|
|
->setMethods( array( 'render', 'getMathTableName' ) )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->assertEquals(
|
|
|
|
$renderer->isChanged(),
|
|
|
|
false,
|
|
|
|
"test if changed is initially false"
|
|
|
|
);
|
|
|
|
$renderer->setHash( '0000' );
|
|
|
|
$this->assertEquals(
|
|
|
|
$renderer->isChanged(),
|
|
|
|
true,
|
|
|
|
"assumes that changing a hash sets changed to true" );
|
|
|
|
}
|
2013-02-18 04:55:47 +00:00
|
|
|
}
|