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();
}
/**
* @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 $expectedOutput
@ -263,16 +99,13 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
[
'alt' => 'image alt',
'url' => 'http://image.jpg',
'name' => 'image',
'key' => 'image',
'caption' => 'Lorem ipsum dolor',
'isVideo' => false
'caption' => 'Lorem ipsum dolor'
]
]
]
],
'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">
<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"/>
@ -286,14 +119,11 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'alt' => 'image alt',
'url' => 'http://image.jpg',
'caption' => 'Lorem ipsum dolor',
'name' => 'image',
'key' => 'image',
'width' => '400',
'height' => '200',
'thumbnail' => 'http://thumbnail.jpg',
'thumbnail2x' => 'http://thumbnail2x.jpg',
'media-type' => 'image',
'isVideo' => false
'isImage' => true
]
],
'accentColor' => '',
@ -307,19 +137,15 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
[
'alt' => 'image alt',
'url' => 'http://image.jpg',
'caption' => 'Lorem ipsum dolor',
'isVideo' => true,
'duration' => '1:20',
'name' => 'test',
'key' => 'test'
'caption' => 'Lorem ipsum dolor'
]
]
]
],
'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"
class="image image-thumbnail video video-thumbnail"
class="video"
title="image alt">
<video src="http://image.jpg" class="pi-video-player" controls="true" controlsList="nodownload" preload="metadata">image alt</video>
</a>
@ -389,10 +215,7 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'data' => [
[
'alt' => 'image alt',
'url' => 'http://image.jpg',
'name' => 'image',
'key' => 'image',
'isVideo' => false
'url' => 'http://image.jpg'
]
]
],
@ -406,7 +229,7 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
],
'output' => '<aside class="portable-infobox pi-background">
<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">
<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"/>
@ -422,14 +245,11 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'extendImageData' => [
'alt' => 'image alt',
'url' => 'http://image.jpg',
'name' => 'image',
'key' => 'image',
'width' => '400',
'height' => '200',
'thumbnail' => 'http://thumbnail.jpg',
'thumbnail2x' => 'http://thumbnail2x.jpg',
'media-type' => 'image',
'isVideo' => false
'isImage' => true
]
],
'accentColor' => '',

View file

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

View file

@ -1,13 +1,13 @@
<?php
use PortableInfobox\Helpers\PortableInfoboxDataBag;
use PortableInfobox\Parser\Nodes\NodeImage;
use PortableInfobox\Parser\Nodes\NodeMedia;
/**
* @group PortableInfobox
* @covers PortableInfobox\Parser\Nodes\NodeImage
* @covers PortableInfobox\Parser\Nodes\NodeMedia
*/
class NodeImageTest extends MediaWikiTestCase {
class NodeMediaTest extends MediaWikiTestCase {
protected function 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
* @param $marker
* @param $expected
*/
public function testGalleryData( $marker, $expected ) {
$this->assertEquals( $expected, NodeImage::getGalleryData( $marker ) );
$this->assertEquals( $expected, NodeMedia::getGalleryData( $marker ) );
}
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() {
$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_',
]
];
$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
* @param $ext
* @param $value
* @param $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() {
@ -129,7 +129,7 @@ class NodeImageTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getData
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getData
* @dataProvider dataProvider
*
* @param $markup
@ -137,44 +137,50 @@ class NodeImageTest extends MediaWikiTestCase {
* @param $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() {
// markup, params, expected
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' ],
[ [ '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' ],
[ [ '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 ],
[ [ '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' ],
[ [ '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
*
* @param $markup
@ -189,12 +195,12 @@ class NodeImageTest extends MediaWikiTestCase {
public function isEmptyProvider() {
return [
[ '<image></image>', [ ], true ],
[ '<media></media>', [ ], true ],
];
}
/**
* @covers PortableInfobox\Parser\Nodes\NodeImage::getSources
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getSources
* @dataProvider sourcesProvider
*
* @param $markup
@ -209,22 +215,22 @@ class NodeImageTest extends MediaWikiTestCase {
public function sourcesProvider() {
return [
[
'<image source="img"/>',
'<media source="img"/>',
[ 'img' ]
],
[
'<image source="img"><default>{{{img}}}</default><alt source="img" /></image>',
'<media source="img"><default>{{{img}}}</default><alt source="img" /></media>',
[ 'img' ]
],
[
'<image source="img"><alt source="alt"/><caption source="cap"/></image>',
'<media source="img"><alt source="alt"/><caption source="cap"/></media>',
[ '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' ] ],
[
'<image/>',
'<media/>',
[ ]
],
[
@ -244,8 +250,8 @@ class NodeImageTest extends MediaWikiTestCase {
public function metadataProvider() {
return [
[
'<image source="img"><caption source="cap"><format>Test {{{cap}}} and {{{fcap}}}</format></caption></image>',
[ 'type' => 'image', 'sources' => [
'<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' => '' ]
@ -262,13 +268,13 @@ class NodeImageTest extends MediaWikiTestCase {
* @throws PortableInfobox\Parser\XmlMarkupParseErrorException
*/
public function testVideo( $markup, $params, $expected ) {
$fileMock = new FileMock();
$videoMock = new VideoMock();
$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( ))
->method( 'getFilefromTitle' )
->willReturn( $fileMock );
->willReturn( $videoMock );
$this->assertEquals( $expected, $mock->getData() );
}
@ -276,26 +282,90 @@ class NodeImageTest extends MediaWikiTestCase {
public function videoProvider() {
return [
[
'<image source="img" />',
[ 'img' => 'test.jpg' ],
'<media source="media" />',
[ 'media' => 'test.webm' ],
[
[
'url' => 'http://test.url',
'name' => 'Test.jpg',
'key' => 'Test.jpg',
'alt' => 'Test.jpg',
'caption' => null,
'isVideo' => true
'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' ],
[
[
'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() {
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() {
@ -305,7 +375,7 @@ class FileMock {
class GalleryMock {
private $images;
public function __construct( Array $images = [] ) {
public function __construct( array $images = [] ) {
$this->images = $images;
}