PortableInfobox/tests/phpunit/nodes/NodeMediaTest.php

482 lines
11 KiB
PHP
Raw Normal View History

2015-06-11 12:51:54 +00:00
<?php
use PortableInfobox\Helpers\PortableInfoboxDataBag;
2018-08-22 14:22:30 +00:00
use PortableInfobox\Helpers\PortableInfoboxImagesHelper;
use PortableInfobox\Parser\Nodes\NodeFactory;
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
/**
2018-08-12 15:50:16 +00:00
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getGalleryData
2018-08-13 14:31:50 +00:00
* @covers PortableInfobox\Helpers\PortableInfoboxDataBag
* @dataProvider galleryDataProvider
* @param $marker
* @param $expected
*/
2018-08-13 14:31:50 +00:00
public function testGalleryData( $marker, $gallery, $expected ) {
PortableInfoboxDataBag::getInstance()->setGallery( $marker, $gallery );
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`\"'"
];
2018-08-13 14:31:50 +00:00
$galleries = [
2018-08-16 09:25:53 +00:00
new GalleryMock( [
2018-08-13 14:31:50 +00:00
[
'image0_name.jpg',
'image0_caption'
],
[
'image01_name.jpg',
'image01_caption'
],
2018-08-16 09:25:53 +00:00
] ),
new GalleryMock( [
2018-08-13 14:31:50 +00:00
[
'image1_name.jpg',
'image1_caption'
]
2018-08-16 09:25:53 +00:00
] ),
2018-08-13 14:31:50 +00:00
new GalleryMock()
];
return [
2017-01-13 08:37:27 +00:00
[
'marker' => $markers[0],
2018-08-13 14:31:50 +00:00
'gallery' => $galleries[0],
'expected' => [
[
'label' => 'image0_caption',
'title' => 'image0_name.jpg',
],
[
'label' => 'image01_caption',
'title' => 'image01_name.jpg',
],
]
],
[
'marker' => $markers[1],
2018-08-13 14:31:50 +00:00
'gallery' => $galleries[1],
'expected' => [
[
'label' => 'image1_caption',
'title' => 'image1_name.jpg',
]
]
],
[
'marker' => $markers[2],
2018-08-13 14:31:50 +00:00
'gallery' => $galleries[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-10-02 07:41:19 +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-10-02 07:41:19 +00:00
$this->assertEquals( $expected, 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-10-02 07:41:19 +00:00
"\x7f'\"`UNIQ--tAbBeR-12345678-QINU`\"'\x7f" .
"<center>\x7f'\"`UNIQ--gAlLeRy-12345678-QINU`\"'\x7f</center>" .
"\x7f'\"`UNIQ--gAlLeRy-87654321-QINU`\"'\x7f",
2018-08-08 09:42:22 +00:00
[ "\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",
2018-08-16 09:25:53 +00:00
[]
]
];
}
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
2018-08-22 14:22:30 +00:00
* @param $mediaType
2015-06-11 12:51:54 +00:00
* @param $expected
*/
2018-08-22 14:22:30 +00:00
public function testData( $markup, $params, $mediaType, $expected ) {
$fileMock = is_null( $mediaType ) ? null : new FileMock( $mediaType );
$node = NodeFactory::newFromXML( $markup, $params );
2021-09-11 23:07:07 +00:00
$helper = $this->getMockBuilder( PortableInfoboxImagesHelper::class )
->setMethods( [ 'getFile', 'extendImageData' ] )
->getMock();
2018-08-22 14:22:30 +00:00
$helper->expects( $this->any() )
->method( 'getFile' )
->willReturn( $fileMock );
$helper->expects( $this->any() )
->method( 'extendImageData' )
->willReturn( [] );
2015-06-11 12:51:54 +00:00
2018-08-22 14:22:30 +00:00
$reflection = new ReflectionClass( $node );
$reflectionProperty = $reflection->getProperty( 'helper' );
$reflectionProperty->setAccessible( true );
$reflectionProperty->setValue( $node, $helper );
2018-08-12 15:50:16 +00:00
2018-08-22 14:22:30 +00:00
$this->assertEquals( $expected, $node->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-16 09:25:53 +00:00
[],
2018-08-22 14:22:30 +00:00
null,
2018-08-16 09:25:53 +00:00
[ [] ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"></media>',
[ 'img' => 'test.jpg' ],
2018-08-22 14:22:30 +00:00
MEDIATYPE_BITMAP,
[ [
'url' => 'http://test.url',
'name' => 'Test.jpg',
'alt' => 'Test.jpg',
'caption' => null,
'isImage' => true,
'isVideo' => false,
'isAudio' => false,
2019-02-02 22:34:48 +00:00
'source' => 'img',
'item-name' => null
2018-08-22 14:22:30 +00:00
] ]
],
[
2018-08-12 15:50:16 +00:00
'<media source="img"><alt><default>test alt</default></alt></media>',
[ 'img' => 'test.jpg' ],
2018-08-22 14:22:30 +00:00
MEDIATYPE_BITMAP,
[ [
'url' => 'http://test.url',
'name' => 'Test.jpg',
'alt' => 'test alt',
'caption' => null,
'isImage' => true,
'isVideo' => false,
'isAudio' => false,
2019-02-02 22:34:48 +00:00
'source' => 'img',
'item-name' => null
2018-08-22 14:22:30 +00:00
] ]
],
[
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-22 14:22:30 +00:00
MEDIATYPE_BITMAP,
[ [
'url' => 'http://test.url',
'name' => 'Test.jpg',
'alt' => 2,
'caption' => null,
'isImage' => true,
'isVideo' => false,
'isAudio' => false,
2019-02-02 22:34:48 +00:00
'source' => 'img',
'item-name' => null
2018-08-22 14:22:30 +00:00
] ]
],
[
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-22 14:22:30 +00:00
MEDIATYPE_BITMAP,
[ [
'url' => 'http://test.url',
'name' => 'Test.jpg',
'alt' => 'test alt',
'caption' => 'test.jpg',
'isImage' => true,
'isVideo' => false,
'isAudio' => false,
2019-02-02 22:34:48 +00:00
'source' => 'img',
'item-name' => null
2018-08-22 14:22:30 +00:00
] ]
],
2019-02-03 16:49:06 +00:00
[
'<media source="img" name="img" />',
[ 'img' => 'test.jpg' ],
MEDIATYPE_BITMAP,
[ [
'url' => 'http://test.url',
'name' => 'Test.jpg',
'alt' => 'Test.jpg',
'caption' => null,
'isImage' => true,
'isVideo' => false,
'isAudio' => false,
'source' => 'img',
'item-name' => 'img'
] ]
],
2018-08-22 14:22:30 +00:00
[
'<media source="media" />',
[ 'media' => 'test.webm' ],
MEDIATYPE_VIDEO,
[ [
'url' => 'http://test.url',
'name' => 'Test.webm',
'alt' => 'Test.webm',
'caption' => null,
'isImage' => false,
'isVideo' => true,
'isAudio' => false,
2019-02-02 22:34:48 +00:00
'source' => 'media',
'item-name' => null
2018-08-22 14:22:30 +00:00
] ]
],
[
'<media source="media" video="false" />',
[ 'media' => 'test.webm' ],
MEDIATYPE_VIDEO,
[ [] ]
],
2018-08-22 14:22:30 +00:00
[
'<media source="media" />',
[ 'media' => 'test.ogg' ],
MEDIATYPE_AUDIO,
[ [
'url' => 'http://test.url',
'name' => 'Test.ogg',
'alt' => 'Test.ogg',
'caption' => null,
'isImage' => false,
'isVideo' => false,
'isAudio' => true,
2019-02-02 22:34:48 +00:00
'source' => 'media',
'item-name' => null
2018-08-22 14:22:30 +00:00
] ]
],
[
'<media source="media" audio="false" />',
[ 'media' => 'test.ogg' ],
MEDIATYPE_AUDIO,
[ [] ]
]
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 ) {
2018-08-22 14:22:30 +00:00
$node = NodeFactory::newFromXML( $markup, $params );
2015-06-11 12:51:54 +00:00
$this->assertEquals( $expected, $node->isEmpty() );
}
public function isEmptyProvider() {
return [
2018-08-16 09:25:53 +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 ) {
2018-08-22 14:22:30 +00:00
$node = 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-10-02 07:41:19 +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/>',
2018-08-16 09:25:53 +00:00
[]
],
2017-01-02 13:31:45 +00:00
[
2018-10-02 07:41:19 +00:00
'<image source="img">' .
'<caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption>' .
'</image>',
2017-01-02 13:31:45 +00:00
[ 'img', 'cap', 'fcap' ]
]
];
}
/** @dataProvider metadataProvider */
public function testMetadata( $markup, $expected ) {
2018-08-22 14:22:30 +00:00
$node = NodeFactory::newFromXML( $markup, [] );
2017-01-02 13:31:45 +00:00
$this->assertEquals( $expected, $node->getMetadata() );
}
public function metadataProvider() {
return [
[
2018-10-02 07:41:19 +00:00
'<media source="img">' .
'<caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption>' .
'</media>',
[
'type' => 'media',
'sources' => [
'img' => [ 'label' => '', 'primary' => true ],
'cap' => [ 'label' => '' ],
'fcap' => [ 'label' => '' ]
]
]
2017-01-02 13:31:45 +00:00
]
2015-06-11 12:51:54 +00:00
];
}
2015-07-22 11:04:41 +00:00
2018-08-13 14:31:50 +00:00
/**
* @covers PortableInfobox\Parser\Nodes\NodeMedia::isTypeAllowed
* @covers PortableInfobox\Parser\Nodes\NodeAudio
* @covers PortableInfobox\Parser\Nodes\NodeImage
* @covers PortableInfobox\Parser\Nodes\NodeVideo
* @dataProvider isTypeAllowedProvider
* @param $markup
* @param $expected
* @throws PortableInfobox\Parser\XmlMarkupParseErrorException
*/
public function testIsTypeAllowed( $markup, $expected ) {
$types = [ MEDIATYPE_BITMAP, MEDIATYPE_DRAWING, MEDIATYPE_VIDEO, MEDIATYPE_AUDIO, 'unknown' ];
$node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [] );
$reflection = new ReflectionClass( $node );
$reflection_method = $reflection->getMethod( 'isTypeAllowed' );
$reflection_method->setAccessible( true );
foreach ( $types as $i => $type ) {
$this->assertEquals( $expected[$i], $reflection_method->invoke( $node, $type ) );
}
}
public function isTypeAllowedProvider() {
return [
[
'<media />',
[ true, true, true, true, false ]
],
[
'<media image="false" />',
[ false, false, true, true, false ]
],
[
'<media video="false" />',
[ true, true, false, true, false ]
],
[
'<media audio="false" />',
[ true, true, true, false, false ]
],
[
'<media image="false" video="false" audio="false" />',
[ false, false, false, false, false ]
],
[
'<image />',
[ true, true, true, false, false ]
],
[
'<image video="false" />',
[ true, true, false, false, false ]
],
[
'<video />',
[ false, false, true, false, false ]
],
[
'<audio />',
[ false, false, false, true, false ]
]
];
}
2015-07-22 11:04:41 +00:00
}
2018-08-22 14:22:30 +00:00
class FileMock {
protected $mediaType;
2018-08-12 15:50:16 +00:00
2018-08-22 14:22:30 +00:00
public function __construct( $mediaType ) {
$this->mediaType = $mediaType;
2018-08-12 15:50:16 +00:00
}
2015-07-22 11:04:41 +00:00
public function getMediaType() {
2018-08-22 14:22:30 +00:00
return $this->mediaType;
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;
}
}