PortableInfobox/tests/phpunit/nodes/NodeMediaTest.php

386 lines
9 KiB
PHP
Raw Normal View History

2015-06-11 12:51:54 +00:00
<?php
use PortableInfobox\Helpers\PortableInfoboxDataBag;
2018-08-12 15:50:16 +00:00
use PortableInfobox\Parser\Nodes\NodeMedia;
2018-08-08 09:42:22 +00:00
/**
* @group PortableInfobox
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia
2018-08-08 09:42:22 +00:00
*/
2018-08-12 15:50:16 +00:00
class NodeMediaTest extends MediaWikiTestCase {
2018-08-08 09:42:22 +00:00
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
}
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getGalleryData
* @dataProvider galleryDataProvider
* @param $marker
* @param $expected
*/
public function testGalleryData( $marker, $expected ) {
2018-08-12 15:50:16 +00:00
$this->assertEquals( $expected, NodeMedia::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
];
}
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::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
]
];
2018-08-12 15:50:16 +00:00
$this->assertEquals( $expected, NodeMedia::getTabberData( $input ) );
}
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getMarkers
* @dataProvider markersProvider
* @param $ext
* @param $value
* @param $expected
*/
public function testMarkers( $ext, $value, $expected ) {
2018-08-12 15:50:16 +00:00
$this->assertEquals( $expected, PortableInfobox\Parser\Nodes\NodeMedia::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
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getData
2015-06-11 12:51:54 +00:00
* @dataProvider dataProvider
*
* @param $markup
* @param $params
* @param $expected
*/
public function testData( $markup, $params, $expected ) {
2018-08-12 15:50:16 +00:00
$imageMock = empty( $params ) ? NULL : new ImageMock();
$xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
2015-06-11 12:51:54 +00:00
2018-08-12 15:50:16 +00:00
$mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]);
$mock->expects( $this->any( ))
->method( 'getFilefromTitle' )
->willReturn( $imageMock );
$this->assertEquals( $expected, $mock->getData() );
2015-06-11 12:51:54 +00:00
}
public function dataProvider() {
// markup, params, expected
2015-06-11 12:51:54 +00:00
return [
[
2018-08-12 15:50:16 +00:00
'<media source="img"></media>',
[ ],
2018-08-12 15:50:16 +00:00
[ [ ] ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"></media>',
[ 'img' => 'test.jpg' ],
2018-08-12 15:50:16 +00:00
[ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 'Test.jpg', 'caption' => null ] ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><alt><default>test alt</default></alt></media>',
[ 'img' => 'test.jpg' ],
2018-08-12 15:50:16 +00:00
[ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 'test alt', 'caption' => null ] ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><alt source="alt source"><default>test alt</default></alt></media>',
[ 'img' => 'test.jpg', 'alt source' => 2 ],
2018-08-12 15:50:16 +00:00
[ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 2, 'caption' => null ] ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><alt><default>test alt</default></alt><caption source="img"/></media>',
[ 'img' => 'test.jpg' ],
2018-08-12 15:50:16 +00:00
[ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 'test alt', 'caption' => 'test.jpg' ] ]
],
2015-06-11 12:51:54 +00:00
];
}
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::isEmpty
2015-06-11 12:51:54 +00:00
* @dataProvider isEmptyProvider
*
* @param $markup
* @param $params
* @param $expected
*/
public function testIsEmpty( $markup, $params, $expected ) {
$node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params );
2015-06-11 12:51:54 +00:00
$this->assertEquals( $expected, $node->isEmpty() );
}
public function isEmptyProvider() {
return [
2018-08-12 15:50:16 +00:00
[ '<media></media>', [ ], true ],
2015-06-11 12:51:54 +00:00
];
}
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getSources
2016-12-14 12:54:53 +00:00
* @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 ) {
$node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [ ] );
2015-06-11 12:51:54 +00:00
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 [
[
2018-08-12 15:50:16 +00:00
'<media source="img"/>',
[ 'img' ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><default>{{{img}}}</default><alt source="img" /></media>',
[ 'img' ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><alt source="alt"/><caption source="cap"/></media>',
[ 'img', 'alt', 'cap' ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><alt source="alt"><default>{{{def}}}</default></alt><caption source="cap"/></media>',
[ 'img', 'alt', 'def', 'cap' ] ],
[
2018-08-12 15:50:16 +00:00
'<media/>',
[ ]
],
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 = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [ ] );
2017-01-02 13:31:45 +00:00
$this->assertEquals( $expected, $node->getMetadata() );
}
public function metadataProvider() {
return [
[
2018-08-12 15:50:16 +00:00
'<media source="img"><caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption></media>',
[ 'type' => 'media', 'sources' => [
2017-01-02 13:31:45 +00:00
'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 PortableInfobox\Parser\XmlMarkupParseErrorException
2015-07-22 11:04:41 +00:00
*/
public function testVideo( $markup, $params, $expected ) {
2018-08-12 15:50:16 +00:00
$videoMock = new VideoMock();
$xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
2015-07-22 11:04:41 +00:00
2018-08-12 15:50:16 +00:00
$mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]);
2018-08-08 09:42:22 +00:00
$mock->expects( $this->any( ))
->method( 'getFilefromTitle' )
2018-08-12 15:50:16 +00:00
->willReturn( $videoMock );
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 [
[
2018-08-12 15:50:16 +00:00
'<media source="media" />',
[ 'media' => 'test.webm' ],
[
[
'url' => 'http://test.url',
'name' => 'Test.webm',
'alt' => 'Test.webm',
'caption' => null
]
]
],
[
'<media source="media" video="false" />',
[ 'media' => 'test.webm' ],
[ [ ] ]
]
];
}
/**
* @dataProvider audioProvider
* @param $markup
* @param $params
* @param $expected
* @throws PortableInfobox\Parser\XmlMarkupParseErrorException
*/
public function testAudio( $markup, $params, $expected ) {
$audioMock = new AudioMock();
$xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
$mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]);
$mock->expects( $this->any( ))
->method( 'getFilefromTitle' )
->willReturn( $audioMock );
$this->assertEquals( $expected, $mock->getData() );
}
public function audioProvider() {
return [
[
'<media source="media" />',
[ 'media' => 'test.ogg' ],
2015-07-22 11:04:41 +00:00
[
[
'url' => 'http://test.url',
2018-08-12 15:50:16 +00:00
'name' => 'Test.ogg',
'alt' => 'Test.ogg',
'caption' => null
]
2015-07-22 11:04:41 +00:00
]
2018-08-12 15:50:16 +00:00
],
[
'<media source="media" audio="false" />',
[ 'media' => 'test.ogg' ],
[ [ ] ]
2015-07-22 11:04:41 +00:00
]
];
}
}
2018-08-12 15:50:16 +00:00
class ImageMock {
public function getMediaType() {
return MEDIATYPE_BITMAP;
}
public function getUrl() {
return '';
}
}
class VideoMock {
public function getMediaType() {
return MEDIATYPE_VIDEO;
}
public function getUrl() {
return 'http://test.url';
}
}
class AudioMock {
2015-07-22 11:04:41 +00:00
public function getMediaType() {
2018-08-12 15:50:16 +00:00
return MEDIATYPE_AUDIO;
2015-07-22 11:04:41 +00:00
}
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;
2018-08-12 15:50:16 +00:00
public function __construct( array $images = [] ) {
2018-08-08 09:42:22 +00:00
$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;
}
}