PortableInfobox/tests/phpunit/nodes/NodeImageTest.php

316 lines
7.9 KiB
PHP
Raw Normal View History

2015-06-11 12:51:54 +00:00
<?php
use Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag;
use Wikia\PortableInfobox\Parser\Nodes\NodeImage;
2018-08-08 09:42:22 +00:00
/**
* @group PortableInfobox
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage
*/
class NodeImageTest extends MediaWikiTestCase {
2015-06-11 12:51:54 +00:00
protected function setUp() {
parent::setUp();
2018-08-08 09:42:22 +00:00
global $wgUseInstantCommons;
$wgUseInstantCommons = false;
2015-06-11 12:51:54 +00:00
}
/**
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::getGalleryData
* @dataProvider galleryDataProvider
* @param $marker
* @param $expected
*/
public function testGalleryData( $marker, $expected ) {
$this->assertEquals( $expected, NodeImage::getGalleryData( $marker ) );
}
public function galleryDataProvider() {
$markers = [
"'\"`UNIQabcd-gAlLeRy-1-QINU`\"'",
"'\"`UNIQabcd-gAlLeRy-2-QINU`\"'",
"'\"`UNIQabcd-gAlLeRy-3-QINU`\"'"
];
PortableInfoboxDataBag::getInstance()->setGallery( $markers[0],
2018-08-08 09:42:22 +00:00
new GalleryMock([
[
2018-08-08 09:42:22 +00:00
'image0_name.jpg',
'image0_caption'
],
[
2018-08-08 09:42:22 +00:00
'image01_name.jpg',
'image01_caption'
],
2018-08-08 09:42:22 +00:00
]));
PortableInfoboxDataBag::getInstance()->setGallery( $markers[1],
2018-08-08 09:42:22 +00:00
new GalleryMock([
[
2018-08-08 09:42:22 +00:00
'image1_name.jpg',
'image1_caption'
]
2018-08-08 09:42:22 +00:00
]));
PortableInfoboxDataBag::getInstance()->setGallery( $markers[2], new GalleryMock() );
return [
2017-01-13 08:37:27 +00:00
[
'marker' => $markers[0],
'expected' => [
[
'label' => 'image0_caption',
'title' => 'image0_name.jpg',
],
[
'label' => 'image01_caption',
'title' => 'image01_name.jpg',
],
]
],
[
'marker' => $markers[1],
'expected' => [
[
'label' => 'image1_caption',
'title' => 'image1_name.jpg',
]
]
],
[
'marker' => $markers[2],
'expected' => []
],
2017-01-02 13:31:45 +00:00
];
}
/**
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::getTabberData
*/
public function testTabberData() {
2018-08-08 09:42:22 +00:00
$input = '<div class="tabber"><div class="tabbertab" title="_title_"><p><a><img src="_src_"></a></p></div></div>';
2017-01-02 13:31:45 +00:00
$expected = [
[
'label' => '_title_',
2018-08-08 09:42:22 +00:00
'title' => '_src_',
2017-01-02 13:31:45 +00:00
]
];
$this->assertEquals( $expected, NodeImage::getTabberData( $input ) );
}
/**
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::getMarkers
* @dataProvider markersProvider
* @param $ext
* @param $value
* @param $expected
*/
public function testMarkers( $ext, $value, $expected ) {
$this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getMarkers( $value, $ext ) );
}
public function markersProvider() {
return [
[
'TABBER',
2018-08-08 09:42:22 +00:00
"<div>\x7f'\"`UNIQ--tAbBeR-12345678-QINU`\"'\x7f</div>",
[ "\x7f'\"`UNIQ--tAbBeR-12345678-QINU`\"'\x7f" ]
],
[
'GALLERY',
2018-08-08 09:42:22 +00:00
"\x7f'\"`UNIQ--tAbBeR-12345678-QINU`\"'\x7f<center>\x7f'\"`UNIQ--gAlLeRy-12345678-QINU`\"'\x7f</center>\x7f'\"`UNIQ--gAlLeRy-87654321-QINU`\"'\x7f",
[ "\x7f'\"`UNIQ--gAlLeRy-12345678-QINU`\"'\x7f", "\x7f'\"`UNIQ--gAlLeRy-87654321-QINU`\"'\x7f" ]
],
[
'GALLERY',
2018-08-08 09:42:22 +00:00
"\x7f'\"`UNIQ--somethingelse-12345678-QINU`\"'\x7f",
[ ]
]
];
}
2015-06-11 12:51:54 +00:00
/**
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::getData
2015-06-11 12:51:54 +00:00
* @dataProvider dataProvider
*
* @param $markup
* @param $params
* @param $expected
*/
public function testData( $markup, $params, $expected ) {
$node = \Wikia\PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params );
$this->assertEquals( $expected, $node->getData() );
}
public function dataProvider() {
// markup, params, expected
2015-06-11 12:51:54 +00:00
return [
[
'<image source="img"></image>',
[ ],
[ [ 'url' => '', 'name' => '', 'key' => '', 'alt' => null, 'caption' => null, 'isVideo' => false ] ]
],
[
'<image source="img"></image>',
[ 'img' => 'test.jpg' ],
2018-08-08 09:42:22 +00:00
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 'Test.jpg', 'caption' => null, 'isVideo' => false ] ]
],
[
'<image source="img"><alt><default>test alt</default></alt></image>',
[ 'img' => 'test.jpg' ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 'test alt', 'caption' => null, 'isVideo' => false ] ]
],
[
'<image source="img"><alt source="alt source"><default>test alt</default></alt></image>',
[ 'img' => 'test.jpg', 'alt source' => 2 ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 2, 'caption' => null, 'isVideo' => false ] ]
],
[
'<image source="img"><alt><default>test alt</default></alt><caption source="img"/></image>',
[ 'img' => 'test.jpg' ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 'test alt', 'caption' => 'test.jpg', 'isVideo' => false ] ]
],
2015-06-11 12:51:54 +00:00
];
}
/**
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::isEmpty
2015-06-11 12:51:54 +00:00
* @dataProvider isEmptyProvider
*
* @param $markup
* @param $params
* @param $expected
*/
public function testIsEmpty( $markup, $params, $expected ) {
$node = \Wikia\PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params );
$this->assertEquals( $expected, $node->isEmpty() );
}
public function isEmptyProvider() {
return [
[ '<image></image>', [ ], true ],
];
}
/**
2016-12-14 12:54:53 +00:00
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::getSources
* @dataProvider sourcesProvider
2015-06-11 12:51:54 +00:00
*
* @param $markup
* @param $expected
*/
2016-12-14 12:54:53 +00:00
public function testSources( $markup, $expected ) {
2015-06-11 12:51:54 +00:00
$node = \Wikia\PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [ ] );
2016-12-14 12:54:53 +00:00
$this->assertEquals( $expected, $node->getSources() );
2015-06-11 12:51:54 +00:00
}
2016-12-14 12:54:53 +00:00
public function sourcesProvider() {
2015-06-11 12:51:54 +00:00
return [
[
'<image source="img"/>',
[ 'img' ]
],
[
'<image source="img"><default>{{{img}}}</default><alt source="img" /></image>',
[ 'img' ]
],
[
'<image source="img"><alt source="alt"/><caption source="cap"/></image>',
[ 'img', 'alt', 'cap' ]
],
[
'<image source="img"><alt source="alt"><default>{{{def}}}</default></alt><caption source="cap"/></image>',
[ 'img', 'alt', 'def', 'cap' ] ],
[
'<image/>',
[ ]
],
2017-01-02 13:31:45 +00:00
[
'<image source="img"><caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption></image>',
[ 'img', 'cap', 'fcap' ]
]
];
}
/** @dataProvider metadataProvider */
public function testMetadata( $markup, $expected ) {
$node = \Wikia\PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [ ] );
$this->assertEquals( $expected, $node->getMetadata() );
}
public function metadataProvider() {
return [
[
'<image source="img"><caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption></image>',
[ 'type' => 'image', 'sources' => [
'img' => [ 'label' => '', 'primary' => true ],
'cap' => [ 'label' => '' ],
'fcap' => [ 'label' => '' ]
] ]
]
2015-06-11 12:51:54 +00:00
];
}
2015-07-22 11:04:41 +00:00
/**
2017-03-15 18:07:51 +00:00
* @dataProvider videoProvider
* @param $markup
* @param $params
* @param $expected
* @throws \Wikia\PortableInfobox\Parser\XmlMarkupParseErrorException
2015-07-22 11:04:41 +00:00
*/
public function testVideo( $markup, $params, $expected ) {
$fileMock = new FileMock();
$xmlObj = Wikia\PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
2018-08-08 09:42:22 +00:00
$mock = $this->getMock(NodeImage::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]);
$mock->expects( $this->any( ))
->method( 'getFilefromTitle' )
->willReturn( $fileMock );
2015-07-22 13:15:08 +00:00
2018-08-08 09:42:22 +00:00
$this->assertEquals( $expected, $mock->getData() );
2015-07-22 11:04:41 +00:00
}
2017-03-15 18:07:51 +00:00
public function videoProvider() {
2015-07-22 11:04:41 +00:00
return [
[
'<image source="img" />',
[ 'img' => 'test.jpg' ],
[
[
'url' => 'http://test.url',
'name' => 'Test.jpg',
'key' => 'Test.jpg',
2018-08-08 09:42:22 +00:00
'alt' => 'Test.jpg',
'caption' => null,
2018-08-08 09:42:22 +00:00
'isVideo' => true
]
2015-07-22 11:04:41 +00:00
]
]
];
}
}
class FileMock {
public function getMediaType() {
return "VIDEO";
}
2015-07-22 13:15:08 +00:00
public function getUrl() {
2018-08-08 09:42:22 +00:00
return 'http://test.url';
2015-07-22 11:04:41 +00:00
}
2018-08-08 09:42:22 +00:00
}
2015-09-09 11:23:34 +00:00
2018-08-08 09:42:22 +00:00
class GalleryMock {
private $images;
public function __construct( Array $images = [] ) {
$this->images = $images;
2015-09-09 11:23:34 +00:00
}
2018-08-08 09:42:22 +00:00
public function getImages() {
return $this->images;
}
}