Increase code coverage

This commit is contained in:
Luqgreg 2018-08-13 16:31:50 +02:00
parent 626710caf9
commit c9d1bd4f10
17 changed files with 559 additions and 52 deletions

View file

@ -93,25 +93,6 @@ class PortableInfoboxParsingHelper {
return $text;
}
/**
* @desc returns the text from inside of the first <includeonly> tag and
* without the nowiki and pre tags.
*
* @param $text string template text
*
* @return string
*/
protected function getIncludeonlyText( $text ) {
$clean = $this->removeNowikiPre( $text );
preg_match_all( "/<includeonly>(.+)<\/includeonly>/sU", $clean, $result );
if ( !isset( $result[ 1 ][ 0 ] ) ) {
return null;
}
return $result[ 1 ][ 0 ];
}
/**
* @desc From the template without <includeonly> tags, creates an array of
* strings containing only infoboxes. All template content which is not an infobox is removed.

View file

@ -52,7 +52,7 @@ class PortableInfoboxTemplateEngine {
return self::$cache[ $type ];
}
$path = self::getTemplatesDir() . DIRECTORY_SEPARATOR . self::$templates[ $type ];
$path = self::getTemplatesDir() . DIRECTORY_SEPARATOR . static::getTemplates()[ $type ];
// @see https://github.com/wikimedia/mediawiki-vendor/tree/master/zordius/lightncandy
$renderer = \LightnCandy::prepare(
@ -74,7 +74,7 @@ class PortableInfoboxTemplateEngine {
* @return bool
*/
public static function isSupportedType( $type ) {
$result = isset( static::$templates[ $type ] );
$result = isset( static::getTemplates()[ $type ] );
if ( !$result ) {
LoggerFactory::getInstance( 'PortableInfobox' )->info( self::TYPE_NOT_SUPPORTED_MESSAGE, [ 'type' => $type ] );
}

View file

@ -73,7 +73,7 @@ class InfoboxParamsValidatorTest extends MediaWikiTestCase {
}
/**
* @param $color
* @param array $color
* @dataProvider passValidateColorValueDataProvider
*/
public function testPassValidateColorValue( $color ) {
@ -148,4 +148,34 @@ class InfoboxParamsValidatorTest extends MediaWikiTestCase {
[ 'color' => '#aaaaa' ],
];
}
/**
* @param array $layout
* @dataProvider passValidateLayoutDataProvider
*/
public function testPassValidateLayout( $layout ) {
$this->assertTrue( $this->InfoboxParamsValidator->validateLayout( $layout ) );
}
public function passValidateLayoutDataProvider() {
return [
[ 'layout' => 'default' ],
[ 'layout' => 'stacked' ]
];
}
/**
* @param array $layout
* @dataProvider failValidateLayoutDataProvider
*/
public function testFailValidateLayout( $layout ) {
$this->assertFalse( $this->InfoboxParamsValidator->validateLayout( $layout ) );
}
public function failValidateLayoutDataProvider() {
return [
[ 'layout' => '' ],
[ 'layout' => 'custom' ]
];
}
}

View file

@ -236,6 +236,14 @@ class PortableInfoboxDataServiceTest extends MediaWikiTestCase {
]
];
}
public function testGetInfoboxes() {
$result = PortableInfoboxDataService::newFromTitle( $this->prepareTitle( 1 ) )
->setParsingHelper( new ParsingHelperDummy() )
->getInfoboxes();
$this->assertEquals( $result, [ "markup" ] );
}
}
class ParsingHelperDummy {
@ -252,6 +260,10 @@ class ParsingHelperDummy {
public function reparseArticle( $title ) {
return $this->infoboxesData;
}
public function getMarkup( Title $title ) {
return [ "markup" ];
}
}
class PagePropsProxyDummy {

View file

@ -1,7 +1,7 @@
<?php
/**
* @group PortableInfobox
* @covers PortableInfoboxParserTagController
* @covers PortableInfoboxRenderService
*/
class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
@ -22,7 +22,22 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
return $DOM->saveXML();
}
public function testGetImageHelper() {
$infoboxRenderService = new PortableInfoboxRenderService();
$reflection = new ReflectionClass( $infoboxRenderService );
$reflection_method = $reflection->getMethod( 'getImageHelper' );
$reflection_method->setAccessible( true );
$this->assertInstanceOf(
\PortableInfobox\Helpers\PortableInfoboxImagesHelper::class,
$reflection_method->invoke( $infoboxRenderService )
);
}
/**
* @covers PortableInfoboxRenderService::renderInfobox
* @covers PortableInfobox\Helpers\PortableInfoboxTemplateEngine
* @param $input
* @param $expectedOutput
* @param $description
@ -1588,13 +1603,204 @@ class PortableInfoboxRenderServiceTest extends MediaWikiTestCase {
'accentColor' => '',
'accentColorText' => ''
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image',
'data' => [
[
'alt' => 'image alt',
'url' => 'http://image.jpg',
'caption' => 'caption'
],
[
'alt' => 'image alt',
'url' => 'http://image.jpg',
'caption' => 'caption'
]
]
]
],
'output' => '<aside class="portable-infobox pi-background">
<h2 class="pi-item pi-item-spacing pi-title">Test Title</h2>
<div class="pi-media-collection">
<ul class="pi-media-collection-tabs">
<li class="pi-tab-link pi-item-spacing current" data-pi-tab="pi-tab-1">caption</li>
<li class="pi-tab-link pi-item-spacing" data-pi-tab="pi-tab-2">caption</li>
</ul>
<div class="pi-media-collection-tab-content current" id="pi-tab-1">
<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"/>
</a>
</figure>
</div>
<div class="pi-media-collection-tab-content" id="pi-tab-2">
<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"/>
</a>
</figure>
</div>
</div>
</aside>',
'description' => 'Simple infobox with title and image collection',
'mockParams' => [
'extendImageData' => [
'alt' => 'image alt',
'url' => 'http://image.jpg',
'caption' => 'caption',
'ref' => 1,
'width' => '400',
'height' => '200',
'thumbnail' => 'http://thumbnail.jpg',
'thumbnail2x' => 'http://thumbnail2x.jpg',
'isImage' => true
]
],
'accentColor' => '',
'accentColorText' => ''
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'group',
'data' => [
'value' => [
[
'type' => 'header',
'data' => [
'value' => 'Test Header'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'layout' => 'default',
'collapse' => 'open',
'row-items' => null
]
]
],
'output' => '<aside class="portable-infobox pi-background">
<h2 class="pi-item pi-item-spacing pi-title">Test Title</h2>
<section class="pi-item pi-group pi-border-color pi-collapse pi-collapse-open">
<h2 class="pi-item pi-header pi-secondary-font pi-item-spacing pi-secondary-background">Test Header</h2>
<div class="pi-item pi-data pi-item-spacing pi-border-color">
<h3 class="pi-data-label pi-secondary-font">test label</h3>
<div class="pi-data-value pi-font">test value</div>
</div>
<div class="pi-item pi-data pi-item-spacing pi-border-color">
<h3 class="pi-data-label pi-secondary-font">test label</h3>
<div class="pi-data-value pi-font">test value</div>
</div>
</section>
</aside>',
'description' => 'Infobox with title, collapsible group with header and two key-value pairs',
'mockParams' => [ ],
'accentColor' => '',
'accentColorText' => ''
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'group',
'data' => [
'value' => [
[
'type' => 'header',
'data' => [
'value' => 'Test Header'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'layout' => 'default',
'collapse' => 'closed',
'row-items' => null
]
]
],
'output' => '<aside class="portable-infobox pi-background">
<h2 class="pi-item pi-item-spacing pi-title">Test Title</h2>
<section class="pi-item pi-group pi-border-color pi-collapse pi-collapse-closed">
<h2 class="pi-item pi-header pi-secondary-font pi-item-spacing pi-secondary-background">Test Header</h2>
<div class="pi-item pi-data pi-item-spacing pi-border-color">
<h3 class="pi-data-label pi-secondary-font">test label</h3>
<div class="pi-data-value pi-font">test value</div>
</div>
<div class="pi-item pi-data pi-item-spacing pi-border-color">
<h3 class="pi-data-label pi-secondary-font">test label</h3>
<div class="pi-data-value pi-font">test value</div>
</div>
</section>
</aside>',
'description' => 'Infobox with title, collapsed group with header and two key-value pairs',
'mockParams' => [ ],
'accentColor' => '',
'accentColorText' => ''
]
];
}
}
class DummyPIImageHelper {
static $imageData = [];
private $ref = 1;
public function extendImageData( $imageData ) {
return self::$imageData;
$data = self::$imageData;
$data['ref'] = $this->ref++;
return $data;
}
public function extendImageCollectionData( $images ) {
$images[0]['isFirst'] = true;
return [
'images' => $images
];
}
}

View file

@ -288,6 +288,7 @@ class NodeDataTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\Node::getMetadata
* @covers PortableInfobox\Parser\Nodes\NodeInfobox::getMetadata
* @dataProvider metadataDataProvider
*
* @param $markup
@ -512,6 +513,8 @@ class NodeDataTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\Node::getRenderData
* @covers PortableInfobox\Parser\Nodes\NodeData::getLayout
* @covers PortableInfobox\Parser\Nodes\NodeData::getSpan
* @dataProvider dataRenderProvider
*
* @param $markup

View file

@ -0,0 +1,58 @@
<?php
use PortableInfobox\Parser\Nodes\NodeFactory;
/**
* @group PortableInfobox
* @covers PortableInfobox\Parser\Nodes\NodeFactory
*/
class NodeFactoryTest extends MediaWikiTestCase {
/**
* @dataProvider newFromXMLProvider
* @param $markup
* @param $expected
* @throws PortableInfobox\Parser\XmlMarkupParseErrorException
*/
public function testNewFromXML( $markup, $expected ) {
$node = NodeFactory::newFromXML( $markup, [] );
$this->assertEquals( $expected, get_class( $node ) );
}
/**
* @dataProvider newFromXMLProvider
* @param $markup
* @param $expected
* @throws PortableInfobox\Parser\XmlMarkupParseErrorException
*/
public function testNewFromSimpleXml( $markup, $expected ) {
$xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
$node = NodeFactory::newFromSimpleXml( $xmlObj, [] );
$this->assertEquals( $expected, get_class( $node ) );
}
public function newFromXMLProvider() {
return [
[
'<infobox />',
PortableInfobox\Parser\Nodes\NodeInfobox::class
],
[
'<data />',
PortableInfobox\Parser\Nodes\NodeData::class
],
[
'<MEDIA />',
PortableInfobox\Parser\Nodes\NodeMedia::class
],
[
'<image><default></default><othertag></othertag></image>',
PortableInfobox\Parser\Nodes\NodeImage::class
],
[
'<idonotexist />',
PortableInfobox\Parser\Nodes\NodeUnimplemented::class
]
];
}
}

View file

@ -0,0 +1,31 @@
<?php
/**
* @group PortableInfobox
* @covers PortableInfobox\Parser\Nodes\NodeHeader
*/
class NodeHeaderTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\NodeHeader::getData
* @covers PortableInfobox\Parser\Nodes\Node::getInnerValue
* @dataProvider dataProvider
*
* @param $markup
* @param $params
* @param $expected
*/
public function testData( $markup, $params, $expected ) {
$node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params );
$this->assertEquals( $expected, $node->getData() );
}
public function dataProvider() {
return [
[ '<header></header>', [ ], [ 'value' => '' ] ],
[ '<header>kjdflkja dafkjlsdkfj</header>', [ ], [ 'value' => 'kjdflkja dafkjlsdkfj' ] ],
[ '<header>kjdflkja<ref>dafkjlsdkfj</ref></header>', [ ], [ 'value' => 'kjdflkja<ref>dafkjlsdkfj</ref>' ] ],
];
}
}

View file

@ -18,11 +18,13 @@ class NodeMediaTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getGalleryData
* @covers PortableInfobox\Helpers\PortableInfoboxDataBag
* @dataProvider galleryDataProvider
* @param $marker
* @param $expected
*/
public function testGalleryData( $marker, $expected ) {
public function testGalleryData( $marker, $gallery, $expected ) {
PortableInfoboxDataBag::getInstance()->setGallery( $marker, $gallery );
$this->assertEquals( $expected, NodeMedia::getGalleryData( $marker ) );
}
@ -32,7 +34,7 @@ class NodeMediaTest extends MediaWikiTestCase {
"'\"`UNIQabcd-gAlLeRy-2-QINU`\"'",
"'\"`UNIQabcd-gAlLeRy-3-QINU`\"'"
];
PortableInfoboxDataBag::getInstance()->setGallery( $markers[0],
$galleries = [
new GalleryMock([
[
'image0_name.jpg',
@ -42,19 +44,20 @@ class NodeMediaTest extends MediaWikiTestCase {
'image01_name.jpg',
'image01_caption'
],
]));
PortableInfoboxDataBag::getInstance()->setGallery( $markers[1],
]),
new GalleryMock([
[
'image1_name.jpg',
'image1_caption'
]
]));
PortableInfoboxDataBag::getInstance()->setGallery( $markers[2], new GalleryMock() );
]),
new GalleryMock()
];
return [
[
'marker' => $markers[0],
'gallery' => $galleries[0],
'expected' => [
[
'label' => 'image0_caption',
@ -68,6 +71,7 @@ class NodeMediaTest extends MediaWikiTestCase {
],
[
'marker' => $markers[1],
'gallery' => $galleries[1],
'expected' => [
[
'label' => 'image1_caption',
@ -77,6 +81,7 @@ class NodeMediaTest extends MediaWikiTestCase {
],
[
'marker' => $markers[2],
'gallery' => $galleries[2],
'expected' => []
],
];
@ -84,6 +89,7 @@ class NodeMediaTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\NodeMedia::getTabberData
* @covers PortableInfobox\Helpers\HtmlHelper
*/
public function testTabberData() {
$input = '<div class="tabber"><div class="tabbertab" title="_title_"><p><a><img src="_src_"></a></p></div></div>';
@ -341,6 +347,71 @@ class NodeMediaTest extends MediaWikiTestCase {
]
];
}
/**
* @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 ]
]
];
}
}
class ImageMock {

View file

@ -0,0 +1,46 @@
<?php
/**
* @group PortableInfobox
* @covers PortableInfobox\Parser\Nodes\NodeTitle
*/
class NodeTitleTest extends MediaWikiTestCase {
/**
* @covers PortableInfobox\Parser\Nodes\NodeTitle::getData
* @dataProvider dataProvider
*
* @param $markup
* @param $params
* @param $expected
*/
public function testData( $markup, $params, $expected ) {
$node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, $params );
$this->assertEquals( $expected, $node->getData() );
}
public function dataProvider() {
return [
[ '<title source="test"/>', [ 'test' => 'test' ], [ 'value' => 'test' ] ],
[ '<title source="test"><default>def</default></title>', [ ], [ 'value' => 'def' ] ],
[ '<title source="test"><default>def</default></title>', [ ],
[ 'value' => 'def' ] ],
[ '<title source="test"><default>def</default></title>', [ 'l' => 1 ],
[ 'value' => 'def' ] ],
[ '<title source="test"><default>def</default></title>', [ 'l' => 1 ],
[ 'value' => 'def' ] ],
[ '<title source="test"><default>def</default></title>', [ 'test' => 1 ],
[ 'value' => 1 ] ],
[ '<title></title>', [ ], [ 'value' => null ] ],
[ '<title source="test"><format>{{{test}}}%</format><default>def</default></title>', [ 'test' => 1 ],
[ 'value' => '{{{test}}}%' ] ],
[ '<title source="test"><format>{{{not_defined_var}}}%</format><default>def</default></title>', [ 'test' => 1 ],
[ 'value' => '{{{not_defined_var}}}%' ] ],
[ '<title source="test"><format>{{{test}}}%</format><default>def</default></title>', [ ],
[ 'value' => 'def' ] ],
[ '<title source="test"><format>{{{test}}}%</format></title>', [ 'test' => 0 ],
[ 'value' => '{{{test}}}%' ] ],
];
}
}

View file

@ -0,0 +1,20 @@
<?php
use PortableInfobox\Parser\Nodes\NodeUnimplemented;
/**
* @group PortableInfobox
* @covers PortableInfobox\Parser\Nodes\NodeUnimplemented
*/
class NodeUnimplementedTest extends MediaWikiTestCase {
/**
* @expectedException PortableInfobox\Parser\Nodes\UnimplementedNodeException
*/
public function testNewFromXML() {
( new NodeUnimplemented(
PortableInfobox\Parser\XmlParser::parseXmlString( "<foo/>" ),
[]
) )->getData();
}
}

View file

@ -5,6 +5,7 @@ use PortableInfobox\Sanitizers\NodeDataSanitizer;
/**
* @group PortableInfobox
* @covers PortableInfobox\Sanitizers\NodeSanitizer
* @covers PortableInfobox\Sanitizers\NodeDataSanitizer
*/
class NodeDataSanitizerTest extends MediaWikiTestCase {

View file

@ -5,6 +5,7 @@ use PortableInfobox\Sanitizers\NodeHorizontalGroupSanitizer;
/**
* @group PortableInfobox
* @covers PortableInfobox\Sanitizers\NodeSanitizer
* @covers PortableInfobox\Sanitizers\NodeHorizontalGroupSanitizer
*/
class NodeHorizontalGroupSanitizerTest extends MediaWikiTestCase {

View file

@ -5,6 +5,7 @@ use PortableInfobox\Sanitizers\NodeImageSanitizer;
/**
* @group PortableInfobox
* @covers PortableInfobox\Sanitizers\NodeSanitizer
* @covers PortableInfobox\Sanitizers\NodeImageSanitizer
*/
class NodeImageSanitizerTest extends MediaWikiTestCase {

View file

@ -5,6 +5,7 @@ use PortableInfobox\Sanitizers\NodeTitleSanitizer;
/**
* @group PortableInfobox
* @covers PortableInfobox\Sanitizers\NodeSanitizer
* @covers PortableInfobox\Sanitizers\NodeTitleSanitizer
*/
class NodeTitleSanitizerTest extends MediaWikiTestCase {

View file

@ -5,6 +5,7 @@ use PortableInfobox\Sanitizers\PassThroughSanitizer;
/**
* @group PortableInfobox
* @covers PortableInfobox\Sanitizers\NodeSanitizer
* @covers PortableInfobox\Sanitizers\PassThroughSanitizer
*/
class PassThroughSanitizerTest extends MediaWikiTestCase {

View file

@ -0,0 +1,44 @@
<?php
use PortableInfobox\Sanitizers\SanitizerBuilder;
/**
* @group PortableInfobox
* @covers PortableInfobox\Sanitizers\SanitizerBuilder
*/
class SanitizerBuilderTest extends MediaWikiTestCase {
/**
* @param $type
* @param $expected
* @dataProvider createFromTypeProvide
*/
public function testCreateFromType( $type, $expected ) {
$this->assertType( $expected, SanitizerBuilder::createFromType( $type ) );
}
public function createFromTypeProvide() {
return [
[
'data',
PortableInfobox\Sanitizers\NodeDataSanitizer::class
],
[
'horizontal-group-content',
PortableInfobox\Sanitizers\NodeHorizontalGroupSanitizer::class
],
[
'title',
PortableInfobox\Sanitizers\NodeTitleSanitizer::class
],
[
'image',
PortableInfobox\Sanitizers\NodeImageSanitizer::class
],
[
'unknown',
PortableInfobox\Sanitizers\PassThroughSanitizer::class
],
];
}
}