Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4114001
PopupTest.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
PopupTest.php
View Options
<?php
namespace
MediaWiki\Extension\Math\Tests
;
use
MediaWiki\Extension\Math\Rest\Popup
;
use
MediaWiki\Languages\LanguageFactory
;
use
MediaWiki\Languages\LanguageNameUtils
;
use
MediaWiki\Rest\Handler
;
use
MediaWiki\Rest\HttpException
;
use
MediaWiki\Rest\RequestData
;
use
MediaWiki\Tests\Rest\Handler\HandlerTestTrait
;
use
MediaWiki\Title\Title
;
use
MediaWiki\Title\TitleFactory
;
use
Wikibase\DataModel\Entity\Item
;
use
Wikibase\Lib\Store\EntityRevision
;
use
Wikimedia\ParamValidator\ParamValidator
;
/**
* @covers \MediaWiki\Extension\Math\Rest\Popup
*/
class
PopupTest
extends
MathWikibaseConnectorTestFactory
{
use
HandlerTestTrait
;
public
function
testNonExistingId
()
{
$popupHandler
=
$this
->
getPopup
();
$response
=
$this
->
executeHandler
(
$popupHandler
,
$this
->
getRequest
(
'1'
,
'en'
)
);
$this
->
assertEquals
(
400
,
$response
->
getStatusCode
()
);
$data
=
json_decode
(
$response
->
getBody
(),
true
);
$this
->
assertEquals
(
'Non-existing Wikibase ID.'
,
$data
[
'message'
]
);
}
public
function
testParameterSettingsSetup
()
{
$popupHandler
=
$this
->
getPopup
();
$this
->
assertSame
(
[
'qid'
=>
[
HANDLER
::
PARAM_SOURCE
=>
'path'
,
ParamValidator
::
PARAM_TYPE
=>
'integer'
,
ParamValidator
::
PARAM_REQUIRED
=>
true
,
]
],
$popupHandler
->
getParamSettings
()
);
}
public
function
testValidationExceptionForMalformedId
()
{
$popupHandler
=
$this
->
getPopup
();
$this
->
initHandler
(
$popupHandler
,
$this
->
getRequest
(
'Q1'
,
'en'
)
);
$this
->
expectException
(
HttpException
::
class
);
$this
->
validateHandler
(
$popupHandler
);
}
public
function
testInvalidLanguage
()
{
$languageNameUtilsMock
=
$this
->
createMock
(
LanguageNameUtils
::
class
);
$languageNameUtilsMock
->
expects
(
$this
->
once
()
)
->
method
(
'isValidCode'
)
->
with
(
'tmp'
)
->
willReturn
(
false
);
$popupHandler
=
$this
->
getPopup
(
null
,
$languageNameUtilsMock
);
$response
=
$this
->
executeHandler
(
$popupHandler
,
$this
->
getRequest
(
'1'
,
'tmp'
)
);
$this
->
assertEquals
(
400
,
$response
->
getStatusCode
()
);
$data
=
json_decode
(
$response
->
getBody
(),
true
);
$this
->
assertEquals
(
'Invalid language code.'
,
$data
[
'message'
]
);
}
/**
* @dataProvider provideItemSetups
*/
public
function
testExistingId
(
bool
$hasPart
)
{
$item
=
$this
->
setupMassEnergyEquivalenceItem
(
$hasPart
);
$popupHandler
=
$this
->
getPopup
(
null
,
null
,
$item
);
$request
=
$this
->
getRequest
(
'1'
,
'en'
);
$data
=
$this
->
executeHandlerAndGetBodyData
(
$popupHandler
,
$request
);
$this
->
assertArrayHasKey
(
'title'
,
$data
);
$this
->
assertArrayHasKey
(
'canonicalurl'
,
$data
);
$this
->
assertArrayHasKey
(
'fullurl'
,
$data
);
$this
->
assertArrayHasKey
(
'contentmodel'
,
$data
);
$this
->
assertArrayHasKey
(
'pagelanguagedir'
,
$data
);
$this
->
assertArrayHasKey
(
'pagelanguage'
,
$data
);
$this
->
assertArrayHasKey
(
'pagelanguagehtmlcode'
,
$data
);
$this
->
assertArrayHasKey
(
'extract'
,
$data
);
$this
->
assertEquals
(
'mass–energy equivalence'
,
$data
[
'title'
]
);
$this
->
assertEquals
(
'special/Q1'
,
$data
[
'canonicalurl'
]
);
$this
->
assertEquals
(
'special/Q1'
,
$data
[
'fullurl'
]
);
$this
->
assertEquals
(
'html'
,
$data
[
'contentmodel'
]
);
$html
=
$data
[
'extract'
];
// popup contains formula label and description
$this
->
assertStringContainsString
(
self
::
TEST_ITEMS
[
'Q1'
][
0
],
$html
);
$this
->
assertStringContainsString
(
self
::
TEST_ITEMS
[
'Q1'
][
1
],
$html
);
// popup contains formula and labels of each element
foreach
(
self
::
TEST_ITEMS
as
$key
=>
$part
)
{
if
(
$key
===
'Q1'
)
{
// the formula itself is not shown in the popup, only its elements
continue
;
}
$this
->
assertStringContainsString
(
$part
[
0
],
$html
);
$this
->
assertStringContainsString
(
self
::
getExpectedMathML
(
$part
[
2
]
),
$html
);
}
}
private
function
getRequest
(
$id
,
$lang
):
RequestData
{
return
new
RequestData
(
[
'pathParams'
=>
[
'qid'
=>
$id
],
'headers'
=>
[
'Accept-Language'
=>
$lang
]
]
);
}
private
function
getPopup
(
?
LanguageFactory
$languageFactoryMock
=
null
,
?
LanguageNameUtils
$languageNameUtilsMock
=
null
,
?
Item
$item
=
null
):
Popup
{
$languageFactoryMock
=
$languageFactoryMock
?:
$this
->
createMock
(
LanguageFactory
::
class
);
if
(
!
$languageNameUtilsMock
)
{
$languageNameUtilsMock
=
$this
->
createMock
(
LanguageNameUtils
::
class
);
$languageNameUtilsMock
->
method
(
'isValidCode'
)->
willReturn
(
true
);
}
$mathWikibaseConnectorMock
=
$item
?
$this
->
getWikibaseConnectorWithExistingItems
(
new
EntityRevision
(
$item
)
)
:
$this
->
getWikibaseConnector
(
$languageFactoryMock
,
$languageNameUtilsMock
);
$titleMock
=
$this
->
createMock
(
Title
::
class
);
$titleMock
->
method
(
'getLocalURL'
)->
willReturn
(
'special/Q1'
);
$titleMock
->
method
(
'getFullURL'
)->
willReturn
(
'special/Q1'
);
$titleFactoryMock
=
$this
->
createMock
(
TitleFactory
::
class
);
$titleFactoryMock
->
expects
(
$this
->
once
()
)
->
method
(
'newFromText'
)
->
willReturn
(
$titleMock
);
return
new
Popup
(
$mathWikibaseConnectorMock
,
$languageFactoryMock
,
$languageNameUtilsMock
,
$titleFactoryMock
);
}
public
static
function
provideItemSetups
():
array
{
return
[
[
true
],
[
false
],
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Tue, Aug 18, 23:48 (12 h, 16 m ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
7b/9b/655b06e3084a9a0773ba9d178249
Default Alt Text
PopupTest.php (5 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment