Fix unit tests

This commit is contained in:
Luqgreg 2018-08-12 17:50:16 +02:00
parent 296c523935
commit 3007c92c62
3 changed files with 128 additions and 235 deletions

View file

@ -22,170 +22,6 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
return $DOM->saveXML(); return $DOM->saveXML();
} }
/**
* @param $data
* @param $expected
*
* @dataProvider filterImagesDataProvider
*/
public function testFilterImages( $data, $expected ) {
$method = ( new ReflectionClass( 'PortableInfoboxRenderService' ) )->getMethod( 'filterImageData' );
$method->setAccessible( true );
$renderService = new PortableInfoboxRenderService();
$this->assertEquals( $expected, $method->invokeArgs( $renderService, [ $data ] ) );
}
public function filterImagesDataProvider() {
return [
[
'data' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => 'caption1',
'isVideo' => false,
],
[
'url' => 'some.url.com',
'name' => 'name2',
'key' => 'key2',
'alt' => 'alt2',
'caption' => 'caption2',
'isVideo' => false,
],
],
'expected' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => 'caption1',
'isVideo' => false,
],
[
'url' => 'some.url.com',
'name' => 'name2',
'key' => 'key2',
'alt' => 'alt2',
'caption' => 'caption2',
'isVideo' => false,
],
]
],
[
'data' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => '',
'isVideo' => false,
],
[
'url' => 'some.url.com',
'name' => 'name2',
'key' => 'key2',
'alt' => 'alt2',
'caption' => 'caption2',
'isVideo' => false,
],
[
'url' => 'some.url.com',
'name' => 'name3',
'key' => 'key3',
'alt' => 'alt3',
'caption' => 'caption3',
'isVideo' => false,
],
],
'expected' => [
1 => [
'url' => 'some.url.com',
'name' => 'name2',
'key' => 'key2',
'alt' => 'alt2',
'caption' => 'caption2',
'isVideo' => false,
],
2 => [
'url' => 'some.url.com',
'name' => 'name3',
'key' => 'key3',
'alt' => 'alt3',
'caption' => 'caption3',
'isVideo' => false,
]
]
],
[
'data' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => 'caption1',
'isVideo' => false,
],
[
'url' => 'some.url.com',
'name' => 'name2',
'key' => 'key2',
'alt' => 'alt2',
'caption' => '',
'isVideo' => false,
],
],
'expected' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => 'caption1',
'isVideo' => false,
],
],
],
[
'data' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => '',
'isVideo' => false,
],
[
'url' => 'some.url.com',
'name' => 'name2',
'key' => 'key2',
'alt' => 'alt2',
'caption' => '',
'isVideo' => false,
],
],
'expected' => [
[
'url' => 'some.url.com',
'name' => 'name1',
'key' => 'key1',
'alt' => 'alt1',
'caption' => '',
'isVideo' => false,
],
]
],
];
}
/** /**
* @param $input * @param $input
* @param $expectedOutput * @param $expectedOutput
@ -263,16 +99,13 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
[ [
'alt' => 'image alt', 'alt' => 'image alt',
'url' => 'http://image.jpg', 'url' => 'http://image.jpg',
'name' => 'image', 'caption' => 'Lorem ipsum dolor'
'key' => 'image',
'caption' => 'Lorem ipsum dolor',
'isVideo' => false
] ]
] ]
] ]
], ],
'output' => '<aside class="portable-infobox pi-background"> 'output' => '<aside class="portable-infobox pi-background">
<figure class="pi-item pi-image"> <figure class="pi-item pi-media pi-image">
<a href="http://image.jpg" class="image image-thumbnail" title="image alt"> <a href="http://image.jpg" class="image image-thumbnail" title="image alt">
<img src="http://thumbnail.jpg" srcset="http://thumbnail.jpg 1x, http://thumbnail2x.jpg 2x" class="pi-image-thumbnail" alt="image alt" <img src="http://thumbnail.jpg" srcset="http://thumbnail.jpg 1x, http://thumbnail2x.jpg 2x" class="pi-image-thumbnail" alt="image alt"
width="400" height="200"/> width="400" height="200"/>
@ -286,14 +119,11 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'alt' => 'image alt', 'alt' => 'image alt',
'url' => 'http://image.jpg', 'url' => 'http://image.jpg',
'caption' => 'Lorem ipsum dolor', 'caption' => 'Lorem ipsum dolor',
'name' => 'image',
'key' => 'image',
'width' => '400', 'width' => '400',
'height' => '200', 'height' => '200',
'thumbnail' => 'http://thumbnail.jpg', 'thumbnail' => 'http://thumbnail.jpg',
'thumbnail2x' => 'http://thumbnail2x.jpg', 'thumbnail2x' => 'http://thumbnail2x.jpg',
'media-type' => 'image', 'isImage' => true
'isVideo' => false
] ]
], ],
'accentColor' => '', 'accentColor' => '',
@ -307,19 +137,15 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
[ [
'alt' => 'image alt', 'alt' => 'image alt',
'url' => 'http://image.jpg', 'url' => 'http://image.jpg',
'caption' => 'Lorem ipsum dolor', 'caption' => 'Lorem ipsum dolor'
'isVideo' => true,
'duration' => '1:20',
'name' => 'test',
'key' => 'test'
] ]
] ]
] ]
], ],
'output' => '<aside class="portable-infobox pi-background"> 'output' => '<aside class="portable-infobox pi-background">
<figure class="pi-item pi-image pi-video"> <figure class="pi-item pi-media pi-video">
<a href="http://image.jpg" <a href="http://image.jpg"
class="image image-thumbnail video video-thumbnail" class="video"
title="image alt"> title="image alt">
<video src="http://image.jpg" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">image alt</video> <video src="http://image.jpg" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">image alt</video>
</a> </a>
@ -389,10 +215,7 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'data' => [ 'data' => [
[ [
'alt' => 'image alt', 'alt' => 'image alt',
'url' => 'http://image.jpg', 'url' => 'http://image.jpg'
'name' => 'image',
'key' => 'image',
'isVideo' => false
] ]
] ]
], ],
@ -406,7 +229,7 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
], ],
'output' => '<aside class="portable-infobox pi-background"> 'output' => '<aside class="portable-infobox pi-background">
<h2 class="pi-item pi-item-spacing pi-title">Test Title</h2> <h2 class="pi-item pi-item-spacing pi-title">Test Title</h2>
<figure class="pi-item pi-image"> <figure class="pi-item pi-media pi-image">
<a href="http://image.jpg" class="image image-thumbnail" title="image alt"> <a href="http://image.jpg" class="image image-thumbnail" title="image alt">
<img src="http://thumbnail.jpg" srcset="http://thumbnail.jpg 1x, http://thumbnail2x.jpg 2x" class="pi-image-thumbnail" alt="image alt" <img src="http://thumbnail.jpg" srcset="http://thumbnail.jpg 1x, http://thumbnail2x.jpg 2x" class="pi-image-thumbnail" alt="image alt"
width="400" height="200"/> width="400" height="200"/>
@ -422,14 +245,11 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'extendImageData' => [ 'extendImageData' => [
'alt' => 'image alt', 'alt' => 'image alt',
'url' => 'http://image.jpg', 'url' => 'http://image.jpg',
'name' => 'image',
'key' => 'image',
'width' => '400', 'width' => '400',
'height' => '200', 'height' => '200',
'thumbnail' => 'http://thumbnail.jpg', 'thumbnail' => 'http://thumbnail.jpg',
'thumbnail2x' => 'http://thumbnail2x.jpg', 'thumbnail2x' => 'http://thumbnail2x.jpg',
'media-type' => 'image', 'isImage' => true
'isVideo' => false
] ]
], ],
'accentColor' => '', 'accentColor' => '',

View file

@ -100,7 +100,10 @@ class PortableInfoboxImagesHelperTest extends MediaWikiTestCase {
'thumbnail' => null, 'thumbnail' => null,
'thumbnail2x' => null, 'thumbnail2x' => null,
'width' => $resultDimensions[ 'width' ], 'width' => $resultDimensions[ 'width' ],
'height' => $resultDimensions[ 'height' ] 'height' => $resultDimensions[ 'height' ],
'isImage' => true,
'isVideo' => false,
'isAudio' => false
]; ];
$thumb = $this->getMockBuilder( 'ThumbnailImage' ) $thumb = $this->getMockBuilder( 'ThumbnailImage' )
->disableOriginalConstructor() ->disableOriginalConstructor()

View file

@ -1,13 +1,13 @@
<?php <?php
use PortableInfobox\Helpers\PortableInfoboxDataBag; use PortableInfobox\Helpers\PortableInfoboxDataBag;
use PortableInfobox\Parser\Nodes\NodeImage; use PortableInfobox\Parser\Nodes\NodeMedia;
/** /**
* @group PortableInfobox * @group PortableInfobox
* @covers PortableInfobox\Parser\Nodes\NodeImage * @covers PortableInfobox\Parser\Nodes\NodeMedia
*/ */
class NodeImageTest extends MediaWikiTestCase { class NodeMediaTest extends MediaWikiTestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -17,13 +17,13 @@ class NodeImageTest extends MediaWikiTestCase {
} }
/** /**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getGalleryData * @covers PortableInfobox\Parser\Nodes\NodeMedia::getGalleryData
* @dataProvider galleryDataProvider * @dataProvider galleryDataProvider
* @param $marker * @param $marker
* @param $expected * @param $expected
*/ */
public function testGalleryData( $marker, $expected ) { public function testGalleryData( $marker, $expected ) {
$this->assertEquals( $expected, NodeImage::getGalleryData( $marker ) ); $this->assertEquals( $expected, NodeMedia::getGalleryData( $marker ) );
} }
public function galleryDataProvider() { public function galleryDataProvider() {
@ -83,7 +83,7 @@ class NodeImageTest extends MediaWikiTestCase {
} }
/** /**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getTabberData * @covers PortableInfobox\Parser\Nodes\NodeMedia::getTabberData
*/ */
public function testTabberData() { public function testTabberData() {
$input = '<div class="tabber"><div class="tabbertab" title="_title_"><p><a><img src="_src_"></a></p></div></div>'; $input = '<div class="tabber"><div class="tabbertab" title="_title_"><p><a><img src="_src_"></a></p></div></div>';
@ -93,18 +93,18 @@ class NodeImageTest extends MediaWikiTestCase {
'title' => '_src_', 'title' => '_src_',
] ]
]; ];
$this->assertEquals( $expected, NodeImage::getTabberData( $input ) ); $this->assertEquals( $expected, NodeMedia::getTabberData( $input ) );
} }
/** /**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getMarkers * @covers PortableInfobox\Parser\Nodes\NodeMedia::getMarkers
* @dataProvider markersProvider * @dataProvider markersProvider
* @param $ext * @param $ext
* @param $value * @param $value
* @param $expected * @param $expected
*/ */
public function testMarkers( $ext, $value, $expected ) { public function testMarkers( $ext, $value, $expected ) {
$this->assertEquals( $expected, PortableInfobox\Parser\Nodes\NodeImage::getMarkers( $value, $ext ) ); $this->assertEquals( $expected, PortableInfobox\Parser\Nodes\NodeMedia::getMarkers( $value, $ext ) );
} }
public function markersProvider() { public function markersProvider() {
@ -129,7 +129,7 @@ class NodeImageTest extends MediaWikiTestCase {
/** /**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getData * @covers PortableInfobox\Parser\Nodes\NodeMedia::getData
* @dataProvider dataProvider * @dataProvider dataProvider
* *
* @param $markup * @param $markup
@ -137,44 +137,50 @@ class NodeImageTest extends MediaWikiTestCase {
* @param $expected * @param $expected
*/ */
public function testData( $markup, $params, $expected ) { public function testData( $markup, $params, $expected ) {
$node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params ); $imageMock = empty( $params ) ? NULL : new ImageMock();
$xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
$this->assertEquals( $expected, $node->getData() ); $mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]);
$mock->expects( $this->any( ))
->method( 'getFilefromTitle' )
->willReturn( $imageMock );
$this->assertEquals( $expected, $mock->getData() );
} }
public function dataProvider() { public function dataProvider() {
// markup, params, expected // markup, params, expected
return [ return [
[ [
'<image source="img"></image>', '<media source="img"></media>',
[ ], [ ],
[ [ 'url' => '', 'name' => '', 'key' => '', 'alt' => null, 'caption' => null, 'isVideo' => false ] ] [ [ ] ]
], ],
[ [
'<image source="img"></image>', '<media source="img"></media>',
[ 'img' => 'test.jpg' ], [ 'img' => 'test.jpg' ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 'Test.jpg', 'caption' => null, 'isVideo' => false ] ] [ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 'Test.jpg', 'caption' => null ] ]
], ],
[ [
'<image source="img"><alt><default>test alt</default></alt></image>', '<media source="img"><alt><default>test alt</default></alt></media>',
[ 'img' => 'test.jpg' ], [ 'img' => 'test.jpg' ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 'test alt', 'caption' => null, 'isVideo' => false ] ] [ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 'test alt', 'caption' => null ] ]
], ],
[ [
'<image source="img"><alt source="alt source"><default>test alt</default></alt></image>', '<media source="img"><alt source="alt source"><default>test alt</default></alt></media>',
[ 'img' => 'test.jpg', 'alt source' => 2 ], [ 'img' => 'test.jpg', 'alt source' => 2 ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 2, 'caption' => null, 'isVideo' => false ] ] [ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 2, 'caption' => null ] ]
], ],
[ [
'<image source="img"><alt><default>test alt</default></alt><caption source="img"/></image>', '<media source="img"><alt><default>test alt</default></alt><caption source="img"/></media>',
[ 'img' => 'test.jpg' ], [ 'img' => 'test.jpg' ],
[ [ 'url' => '', 'name' => 'Test.jpg', 'key' => 'Test.jpg', 'alt' => 'test alt', 'caption' => 'test.jpg', 'isVideo' => false ] ] [ [ 'url' => '', 'name' => 'Test.jpg', 'alt' => 'test alt', 'caption' => 'test.jpg' ] ]
], ],
]; ];
} }
/** /**
* @covers PortableInfobox\Parser\Nodes\NodeImage::isEmpty * @covers PortableInfobox\Parser\Nodes\NodeMedia::isEmpty
* @dataProvider isEmptyProvider * @dataProvider isEmptyProvider
* *
* @param $markup * @param $markup
@ -189,12 +195,12 @@ class NodeImageTest extends MediaWikiTestCase {
public function isEmptyProvider() { public function isEmptyProvider() {
return [ return [
[ '<image></image>', [ ], true ], [ '<media></media>', [ ], true ],
]; ];
} }
/** /**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getSources * @covers PortableInfobox\Parser\Nodes\NodeMedia::getSources
* @dataProvider sourcesProvider * @dataProvider sourcesProvider
* *
* @param $markup * @param $markup
@ -209,22 +215,22 @@ class NodeImageTest extends MediaWikiTestCase {
public function sourcesProvider() { public function sourcesProvider() {
return [ return [
[ [
'<image source="img"/>', '<media source="img"/>',
[ 'img' ] [ 'img' ]
], ],
[ [
'<image source="img"><default>{{{img}}}</default><alt source="img" /></image>', '<media source="img"><default>{{{img}}}</default><alt source="img" /></media>',
[ 'img' ] [ 'img' ]
], ],
[ [
'<image source="img"><alt source="alt"/><caption source="cap"/></image>', '<media source="img"><alt source="alt"/><caption source="cap"/></media>',
[ 'img', 'alt', 'cap' ] [ 'img', 'alt', 'cap' ]
], ],
[ [
'<image source="img"><alt source="alt"><default>{{{def}}}</default></alt><caption source="cap"/></image>', '<media source="img"><alt source="alt"><default>{{{def}}}</default></alt><caption source="cap"/></media>',
[ 'img', 'alt', 'def', 'cap' ] ], [ 'img', 'alt', 'def', 'cap' ] ],
[ [
'<image/>', '<media/>',
[ ] [ ]
], ],
[ [
@ -244,8 +250,8 @@ class NodeImageTest extends MediaWikiTestCase {
public function metadataProvider() { public function metadataProvider() {
return [ return [
[ [
'<image source="img"><caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption></image>', '<media source="img"><caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption></media>',
[ 'type' => 'image', 'sources' => [ [ 'type' => 'media', 'sources' => [
'img' => [ 'label' => '', 'primary' => true ], 'img' => [ 'label' => '', 'primary' => true ],
'cap' => [ 'label' => '' ], 'cap' => [ 'label' => '' ],
'fcap' => [ 'label' => '' ] 'fcap' => [ 'label' => '' ]
@ -262,13 +268,13 @@ class NodeImageTest extends MediaWikiTestCase {
* @throws PortableInfobox\Parser\XmlMarkupParseErrorException * @throws PortableInfobox\Parser\XmlMarkupParseErrorException
*/ */
public function testVideo( $markup, $params, $expected ) { public function testVideo( $markup, $params, $expected ) {
$fileMock = new FileMock(); $videoMock = new VideoMock();
$xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup ); $xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
$mock = $this->getMock(NodeImage::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]); $mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]);
$mock->expects( $this->any( )) $mock->expects( $this->any( ))
->method( 'getFilefromTitle' ) ->method( 'getFilefromTitle' )
->willReturn( $fileMock ); ->willReturn( $videoMock );
$this->assertEquals( $expected, $mock->getData() ); $this->assertEquals( $expected, $mock->getData() );
} }
@ -276,26 +282,90 @@ class NodeImageTest extends MediaWikiTestCase {
public function videoProvider() { public function videoProvider() {
return [ return [
[ [
'<image source="img" />', '<media source="media" />',
[ 'img' => 'test.jpg' ], [ 'media' => 'test.webm' ],
[ [
[ [
'url' => 'http://test.url', 'url' => 'http://test.url',
'name' => 'Test.jpg', 'name' => 'Test.webm',
'key' => 'Test.jpg', 'alt' => 'Test.webm',
'alt' => 'Test.jpg', 'caption' => null
'caption' => null,
'isVideo' => true
] ]
] ]
],
[
'<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' ],
[
[
'url' => 'http://test.url',
'name' => 'Test.ogg',
'alt' => 'Test.ogg',
'caption' => null
]
]
],
[
'<media source="media" audio="false" />',
[ 'media' => 'test.ogg' ],
[ [ ] ]
] ]
]; ];
} }
} }
class FileMock { class ImageMock {
public function getMediaType() { public function getMediaType() {
return "VIDEO"; return MEDIATYPE_BITMAP;
}
public function getUrl() {
return '';
}
}
class VideoMock {
public function getMediaType() {
return MEDIATYPE_VIDEO;
}
public function getUrl() {
return 'http://test.url';
}
}
class AudioMock {
public function getMediaType() {
return MEDIATYPE_AUDIO;
} }
public function getUrl() { public function getUrl() {
@ -305,7 +375,7 @@ class FileMock {
class GalleryMock { class GalleryMock {
private $images; private $images;
public function __construct( Array $images = [] ) { public function __construct( array $images = [] ) {
$this->images = $images; $this->images = $images;
} }