Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4133624
MathoidCheckerTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
MathoidCheckerTest.php
View Options
<?php
namespace
MediaWiki\Extension\Math\InputCheck
;
use
MediaWiki\Extension\Math\Math
;
use
MediaWiki\Extension\Math\MathMathML
;
use
MediaWiki\Http\HttpRequestFactory
;
use
MediaWikiIntegrationTestCase
;
use
MockHttpTrait
;
use
RuntimeException
;
use
Wikimedia\ObjectCache\EmptyBagOStuff
;
use
Wikimedia\ObjectCache\WANObjectCache
;
/**
* @covers \MediaWiki\Extension\Math\InputCheck\MathoidChecker
*/
class
MathoidCheckerTest
extends
MediaWikiIntegrationTestCase
{
use
MockHttpTrait
;
private
const
SAMPLE_KEY
=
'global:MediaWiki
\E
xtension
\M
ath
\I
nputCheck
\M
athoidChecker:'
.
'eb27aefff6e58e58dcefa22102531a58'
;
public
static
function
provideTexExamples
()
{
return
[
[
'
\s
in x'
,
'eb27aefff6e58e58dcefa22102531a58'
],
[
'
\s
in_x'
,
'7b33c69d6eac9126d41b663b93896951'
],
];
}
/**
* @dataProvider provideTexExamples
*/
public
function
testCacheKey
(
string
$input
,
string
$expected
)
{
$checker
=
$this
->
getMathoidChecker
(
$input
);
$realKey
=
$checker
->
getCacheKey
();
$this
->
assertStringEndsWith
(
$expected
,
$realKey
);
}
public
function
testResponseFromCache
()
{
$wanCache
=
$this
->
getServiceContainer
()->
getMainWANObjectCache
();
$wanCache
->
set
(
self
::
SAMPLE_KEY
,
[
999
,
'expected'
],
WANObjectCache
::
TTL_INDEFINITE
,
[
'version'
=>
MathoidChecker
::
VERSION
]
);
$checker
=
$this
->
getMathoidChecker
();
$this
->
assertSame
(
[
999
,
'expected'
],
$checker
->
getCheckResponse
()
);
}
public
function
testResponseWithPurge
()
{
$wanCache
=
$this
->
getServiceContainer
()->
getMainWANObjectCache
();
$wanCache
->
set
(
self
::
SAMPLE_KEY
,
[
999
,
'unexpected'
],
WANObjectCache
::
TTL_INDEFINITE
,
[
'version'
=>
MathoidChecker
::
VERSION
]
);
$this
->
setFakeRequest
(
200
,
'expected'
);
$checker
=
$this
->
getMathoidChecker
(
'
\s
in x'
,
true
);
$this
->
assertSame
(
[
200
,
'expected'
],
$checker
->
getCheckResponse
()
);
}
public
function
testResponseFromResponse
()
{
$this
->
setMainCache
(
new
EmptyBagOStuff
()
);
$this
->
setFakeRequest
(
200
,
'expected'
);
$checker
=
$this
->
getMathoidChecker
();
$this
->
assertSame
(
[
200
,
'expected'
],
$checker
->
getCheckResponse
()
);
}
public
function
testFailedResponse
()
{
$this
->
setMainCache
(
new
EmptyBagOStuff
()
);
$this
->
setFakeRequest
(
401
,
false
);
$checker
=
$this
->
getMathoidChecker
();
$this
->
expectException
(
RuntimeException
::
class
);
$checker
->
getCheckResponse
();
}
/**
* @dataProvider provideMathoidSamples
* @param string $input LaTeX input to check
* @param string $mockRequestBody
* @param int $mockResponseStatus
* @param array $expeted
*/
public
function
testIsValid
(
$input
,
$mockRequestBody
,
$mockResponseStatus
,
$expeted
)
{
$request
=
$this
->
makeFakeHttpRequest
(
$mockRequestBody
,
$mockResponseStatus
);
$this
->
installMockHttp
(
$request
);
$checker
=
$this
->
getMathoidChecker
(
$input
);
$this
->
assertSame
(
$expeted
[
'valid'
],
$checker
->
isValid
()
);
}
/**
* @dataProvider provideMathoidSamples
* @param string $input LaTeX input to check
* @param string $mockRequestBody
* @param int $mockResponseStatus
* @param array $expeted
*/
public
function
testGetChecked
(
$input
,
$mockRequestBody
,
$mockResponseStatus
,
$expeted
)
{
$request
=
$this
->
makeFakeHttpRequest
(
$mockRequestBody
,
$mockResponseStatus
);
$this
->
installMockHttp
(
$request
);
$checker
=
$this
->
getMathoidChecker
(
$input
);
$this
->
assertSame
(
$expeted
[
'checked'
],
$checker
->
getValidTex
()
);
}
/**
* @dataProvider provideMathoidSamples
* @param string $input LaTeX input to check
* @param string $mockRequestBody
* @param int $mockResponseStatus
* @param array $expeted
*/
public
function
testGetError
(
$input
,
$mockRequestBody
,
$mockResponseStatus
,
$expeted
)
{
$request
=
$this
->
makeFakeHttpRequest
(
$mockRequestBody
,
$mockResponseStatus
);
$this
->
installMockHttp
(
$request
);
$checker
=
$this
->
getMathoidChecker
(
$input
);
if
(
array_key_exists
(
'error'
,
$expeted
)
)
{
$checkerError
=
$checker
->
getError
();
$this
->
assertNotNull
(
$checkerError
);
$renderedCheckerError
=
(
new
MathMathML
(
'a'
)
)
->
getError
(
$checkerError
->
getKey
(),
...
$checkerError
->
getParams
()
);
$this
->
assertStringContainsString
(
$expeted
[
'error'
],
$renderedCheckerError
);
}
else
{
$this
->
assertNull
(
$checker
->
getError
()
);
}
}
/**
* @param string $tex
* @param bool $purge
* @return MathoidChecker
*/
private
function
getMathoidChecker
(
string
$tex
=
'
\s
in x'
,
bool
$purge
=
false
):
MathoidChecker
{
return
Math
::
getCheckerFactory
()
->
newMathoidChecker
(
$tex
,
'tex'
,
$purge
);
}
private
function
setFakeRequest
(
$returnStatus
,
$content
):
void
{
$fakeHTTP
=
$this
->
createMock
(
HttpRequestFactory
::
class
);
$fakeRequest
=
$this
->
createMock
(
\MWHttpRequest
::
class
);
$fakeRequest
->
expects
(
$this
->
once
()
)->
method
(
'execute'
)->
willReturn
(
true
);
$fakeRequest
->
expects
(
$this
->
once
()
)->
method
(
'getStatus'
)->
willReturn
(
$returnStatus
);
if
(
$content
)
{
$fakeRequest
->
expects
(
$this
->
once
()
)->
method
(
'getContent'
)->
willReturn
(
$content
);
}
else
{
$fakeRequest
->
expects
(
$this
->
never
()
)->
method
(
'getContent'
);
}
$fakeHTTP
->
expects
(
$this
->
once
()
)->
method
(
'create'
)->
willReturn
(
$fakeRequest
);
$this
->
setService
(
'HttpRequestFactory'
,
$fakeHTTP
);
}
public
static
function
provideMathoidSamples
()
{
yield
'
\
sin x'
=>
[
'
\s
in x'
,
file_get_contents
(
__DIR__
.
'/data/mathoid/sinx.json'
),
200
,
[
'valid'
=>
true
,
'checked'
=>
'
\s
in x'
],
];
yield
'invalid F'
=>
[
'1+
\i
nvalid'
,
file_get_contents
(
__DIR__
.
'/data/mathoid/invalidF.json'
),
400
,
[
'valid'
=>
false
,
'checked'
=>
null
,
'error'
=>
'unknown function'
],
];
yield
'unescaped'
=>
[
'1.5%'
,
file_get_contents
(
__DIR__
.
'/data/mathoid/deprecated.json'
),
200
,
[
'valid'
=>
true
,
'checked'
=>
'1.5
\%
'
],
];
yield
'syntax error'
=>
[
'
\l
eft( x'
,
file_get_contents
(
__DIR__
.
'/data/mathoid/syntaxE.json'
),
400
,
[
'valid'
=>
false
,
'checked'
=>
null
,
'error'
=>
'Failed to parse'
],
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Aug 19, 12:14 (3 w, 2 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
68/5d/1817a9c881bb37dfb0315ede3f98
Default Alt Text
MathoidCheckerTest.php (5 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment