PortableInfobox/tests/phpunit/PortableInfoboxDataServiceTest.php

297 lines
7.2 KiB
PHP
Raw Normal View History

2015-06-09 12:31:04 +00:00
<?php
use PortableInfobox\Parser\Nodes\NodeFactory;
2015-09-23 13:51:41 +00:00
2018-08-08 09:42:22 +00:00
/**
* @group PortableInfobox
* @covers PortableInfoboxDataService
*/
class PortableInfoboxDataServiceTest extends MediaWikiTestCase {
2015-06-09 12:31:04 +00:00
2015-09-23 13:51:41 +00:00
/**
* @param $id
* @param int $ns
*
* @return Title
*/
protected function prepareTitle( $id = 0, $ns = NS_MAIN ) {
$title = new Title();
$title->mArticleID = $id;
$title->mNamespace = $ns;
return $title;
}
public function testEmptyData() {
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle() )
// empty page props
2018-08-16 09:25:53 +00:00
->setPagePropsProxy( new PagePropsProxyDummy() )
2015-09-23 13:51:41 +00:00
->getData();
2018-08-16 09:25:53 +00:00
$this->assertEquals( [], $result );
2015-09-23 13:51:41 +00:00
}
public function testLoadFromProps() {
2016-12-14 12:54:53 +00:00
$data = '[{"parser_tag_version": ' .
PortableInfoboxParserTagController::PARSER_TAG_VERSION .
', "data": [], "metadata": []}]';
2015-09-23 13:51:41 +00:00
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
// purge memc so we can rerun tests
->purge()
->setPagePropsProxy( new PagePropsProxyDummy( [ '1infoboxes' => $data ] ) )
->getData();
$this->assertEquals( json_decode( $data, true ), $result );
}
public function testSave() {
$markup = '<infobox><data source="test"><default>{{{test2}}}</default></data></infobox>';
$infoboxNode = NodeFactory::newFromXML( $markup, [ 'test' => 1 ] );
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
->purge()
2018-08-16 09:25:53 +00:00
->setPagePropsProxy( new PagePropsProxyDummy() )
2015-09-23 13:51:41 +00:00
->save( $infoboxNode )
->getData();
2016-12-14 12:54:53 +00:00
$this->assertEquals(
[
[
'parser_tag_version' => PortableInfoboxParserTagController::PARSER_TAG_VERSION,
2018-10-02 07:41:19 +00:00
'data' => [
[
'type' => 'data',
'data' => [
'label' => null,
'value' => 1,
'layout' => null,
'span' => 1,
2019-02-02 22:34:48 +00:00
'source' => 'test',
'item-name' => null
2018-10-02 07:41:19 +00:00
]
]
],
2016-12-14 12:54:53 +00:00
'metadata' => [
[
'type' => 'data',
'sources' => [
'test' => [
'label' => '',
'primary' => true
],
'test2' => [
'label' => ''
]
]
]
]
]
],
$result
);
2015-09-23 13:51:41 +00:00
}
public function testTemplate() {
2016-12-14 12:54:53 +00:00
$data = [
[
'parser_tag_version' => PortableInfoboxParserTagController::PARSER_TAG_VERSION,
'data' => [],
'metadata' => []
]
];
2015-09-23 13:51:41 +00:00
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1, NS_TEMPLATE ) )
->purge()
2018-08-16 09:25:53 +00:00
->setPagePropsProxy( new PagePropsProxyDummy() )
2016-12-14 12:54:53 +00:00
->setParsingHelper( new ParsingHelperDummy( null, $data ) )
->reparseArticle();
2015-09-23 13:51:41 +00:00
$this->assertEquals( $data, $result );
}
2016-12-14 12:54:53 +00:00
public function testReparse() {
$oldData = '[{"parser_tag_version": 0, "data": [], "metadata": []}]';
$newData = [
[
'parser_tag_version' => PortableInfoboxParserTagController::PARSER_TAG_VERSION,
'data' => [],
'metadata' => []
]
];
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
// purge memc so we can rerun tests
->purge()
->setPagePropsProxy( new PagePropsProxyDummy( [ '1infoboxes' => $oldData ] ) )
->setParsingHelper( new ParsingHelperDummy( $newData ) )
->getData();
$this->assertEquals( $newData, $result );
}
2015-09-23 13:51:41 +00:00
public function testDelete() {
2016-12-14 12:54:53 +00:00
$data = '[{"parser_tag_version": ' .
PortableInfoboxParserTagController::PARSER_TAG_VERSION .
', "data": [], "metadata": []}]';
2015-09-23 13:51:41 +00:00
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
// purge memc so we can rerun tests
->purge()
->setPagePropsProxy( new PagePropsProxyDummy( [ '1infoboxes' => $data ] ) )
->delete()
->getData();
2018-08-16 09:25:53 +00:00
$this->assertEquals( [], $result );
2015-09-23 13:51:41 +00:00
}
public function testPurge() {
2016-12-14 12:54:53 +00:00
$data = '[{"parser_tag_version": ' .
PortableInfoboxParserTagController::PARSER_TAG_VERSION .
', "data": [], "metadata": []}]';
2015-09-23 13:51:41 +00:00
$service = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
// purge memc so we can rerun tests
->purge()
->setPagePropsProxy( new PagePropsProxyDummy( [ '1infoboxes' => $data ] ) );
// this should load data from props to memc
$result = $service->getData();
$service->purge()
2018-08-16 09:25:53 +00:00
->setPagePropsProxy( new PagePropsProxyDummy() );
2015-09-23 13:51:41 +00:00
$purged = $service->getData();
2018-08-16 09:25:53 +00:00
$this->assertEquals( [ json_decode( $data, true ), [] ], [ $result, $purged ] );
2015-06-09 12:31:04 +00:00
}
public function testImageListRemoveDuplicates() {
2015-09-23 13:51:41 +00:00
$images = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
->purge()
2016-12-14 12:54:53 +00:00
->setPagePropsProxy(
new PagePropsProxyDummy( [ '1infoboxes' => json_encode( $this->getInfoboxPageProps() ) ] )
)
2015-09-23 13:51:41 +00:00
->getImages();
2015-06-22 16:29:27 +00:00
2015-09-23 13:51:41 +00:00
$this->assertEquals( count( $images ), 2 );
2015-06-09 12:31:04 +00:00
}
public function testImageListFetchImages() {
2015-09-23 13:51:41 +00:00
$images = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
->purge()
2016-12-14 12:54:53 +00:00
->setPagePropsProxy(
new PagePropsProxyDummy( [ '1infoboxes' => json_encode( $this->getInfoboxPageProps() ) ] )
)
2015-09-23 13:51:41 +00:00
->getImages();
2015-06-22 16:29:27 +00:00
2015-09-23 13:51:41 +00:00
$this->assertEquals( [ 'Test.jpg', 'Test2.jpg' ], $images );
}
2015-06-09 12:31:04 +00:00
public function testTitleNullConstructor() {
2015-09-23 13:51:41 +00:00
$service = PortableInfoboxDataService::newFromTitle( null );
$result = $service->getData();
2018-08-16 09:25:53 +00:00
$this->assertEquals( [], $result );
}
public function testConstructor() {
2015-09-23 13:51:41 +00:00
$service = PortableInfoboxDataService::newFromPageID( null );
$result = $service->getData();
2018-08-16 09:25:53 +00:00
$this->assertEquals( [], $result );
2015-09-23 13:51:41 +00:00
}
2016-12-14 12:54:53 +00:00
protected function getInfoboxPageProps() {
return [
[
'parser_tag_version' => PortableInfoboxParserTagController::PARSER_TAG_VERSION,
'data' => [
[
'type' => 'data',
'data' => [
'value' => 'AAAA',
'label' => 'BBBB'
]
],
[
'type' => 'image',
'data' => [
[
'key' => 'Test.jpg',
'alt' => null,
'caption' => null,
]
]
],
[
'type' => 'image',
'data' => [
[
'key' => 'Test2.jpg',
'alt' => null,
'caption' => null
]
]
]
],
'metadata' => []
],
[
'parser_tag_version' => PortableInfoboxParserTagController::PARSER_TAG_VERSION,
'data' => [
[
'type' => 'image',
'data' => [
[
'key' => 'Test2.jpg',
'alt' => null,
'caption' => null
]
]
]
],
'metadata' => []
]
];
2015-09-23 13:51:41 +00:00
}
2018-08-13 14:31:50 +00:00
public function testGetInfoboxes() {
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
->setParsingHelper( new ParsingHelperDummy() )
->getInfoboxes();
$this->assertEquals( $result, [ "markup" ] );
}
2015-09-23 13:51:41 +00:00
}
2016-12-14 12:54:53 +00:00
class ParsingHelperDummy {
2015-09-23 13:51:41 +00:00
2016-12-14 12:54:53 +00:00
public function __construct( $infoboxesData = null, $includeonlyInfoboxesData = null ) {
$this->infoboxesData = $infoboxesData;
$this->includeonlyInfoboxesData = $includeonlyInfoboxesData;
}
2016-12-14 12:54:53 +00:00
public function parseIncludeonlyInfoboxes( $title ) {
return $this->includeonlyInfoboxesData;
}
public function reparseArticle( $title ) {
return $this->infoboxesData;
2015-09-23 13:51:41 +00:00
}
2018-08-13 14:31:50 +00:00
public function getMarkup( Title $title ) {
return [ "markup" ];
}
2015-09-23 13:51:41 +00:00
}
class PagePropsProxyDummy {
2018-08-16 09:25:53 +00:00
public function __construct( $data = [] ) {
2015-09-23 13:51:41 +00:00
$this->data = $data;
}
public function get( $id, $property ) {
2016-12-14 12:54:53 +00:00
return $this->data[ $id . $property ] ?? '';
2015-09-23 13:51:41 +00:00
}
public function set( $id, $data ) {
foreach ( $data as $property => $value ) {
$this->data[ $id . $property ] = $value;
}
}
2015-06-09 12:31:04 +00:00
}