From c9d1bd4f102313d2c27061b0fdc0df98f3435be7 Mon Sep 17 00:00:00 2001 From: Luqgreg Date: Mon, 13 Aug 2018 16:31:50 +0200 Subject: [PATCH] Increase code coverage --- .../Helpers/PortableInfoboxParsingHelper.php | 19 -- .../Helpers/PortableInfoboxTemplateEngine.php | 6 +- tests/phpunit/InfoboxParamsValidatorTest.php | 34 ++- .../PortableInfoboxDataServiceTest.php | 12 + .../PortableInfoboxRenderServiceTest.php | 210 +++++++++++++++++- tests/phpunit/nodes/NodeDataTest.php | 3 + tests/phpunit/nodes/NodeFactoryTest.php | 58 +++++ tests/phpunit/nodes/NodeHeaderTest.php | 31 +++ tests/phpunit/nodes/NodeMediaTest.php | 123 +++++++--- tests/phpunit/nodes/NodeTitleTest.php | 46 ++++ tests/phpunit/nodes/NodeUnimplementedTest.php | 20 ++ .../sanitizers/NodeDataSanitizerTest.php | 1 + .../NodeHorizontalGroupSanitizerTest.php | 1 + .../sanitizers/NodeImageSanitizerTest.php | 1 + .../sanitizers/NodeTitleSanitizerTest.php | 1 + .../sanitizers/PassThroughSanitizerTest.php | 1 + .../sanitizers/SanitizerBuilderTest.php | 44 ++++ 17 files changed, 559 insertions(+), 52 deletions(-) create mode 100644 tests/phpunit/nodes/NodeFactoryTest.php create mode 100644 tests/phpunit/nodes/NodeHeaderTest.php create mode 100644 tests/phpunit/nodes/NodeTitleTest.php create mode 100644 tests/phpunit/nodes/NodeUnimplementedTest.php create mode 100644 tests/phpunit/sanitizers/SanitizerBuilderTest.php diff --git a/includes/services/Helpers/PortableInfoboxParsingHelper.php b/includes/services/Helpers/PortableInfoboxParsingHelper.php index aa7e48a..1e6b0f6 100644 --- a/includes/services/Helpers/PortableInfoboxParsingHelper.php +++ b/includes/services/Helpers/PortableInfoboxParsingHelper.php @@ -93,25 +93,6 @@ class PortableInfoboxParsingHelper { return $text; } - /** - * @desc returns the text from inside of the first 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>/sU", $clean, $result ); - if ( !isset( $result[ 1 ][ 0 ] ) ) { - return null; - } - - return $result[ 1 ][ 0 ]; - } - /** * @desc From the template without tags, creates an array of * strings containing only infoboxes. All template content which is not an infobox is removed. diff --git a/includes/services/Helpers/PortableInfoboxTemplateEngine.php b/includes/services/Helpers/PortableInfoboxTemplateEngine.php index d88caf5..27b9f39 100644 --- a/includes/services/Helpers/PortableInfoboxTemplateEngine.php +++ b/includes/services/Helpers/PortableInfoboxTemplateEngine.php @@ -25,7 +25,7 @@ class PortableInfoboxTemplateEngine { 'media-collection' => 'PortableInfoboxItemMediaCollection.hbs', 'xml-parse-error' => 'PortableInfoboxMarkupDebug.hbs' ]; - + public function __construct() {} public static function getTemplatesDir() { @@ -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 ] ); } diff --git a/tests/phpunit/InfoboxParamsValidatorTest.php b/tests/phpunit/InfoboxParamsValidatorTest.php index 04b2525..188afac 100644 --- a/tests/phpunit/InfoboxParamsValidatorTest.php +++ b/tests/phpunit/InfoboxParamsValidatorTest.php @@ -11,7 +11,7 @@ class InfoboxParamsValidatorTest extends MediaWikiTestCase { parent::setUp(); $this->InfoboxParamsValidator = new PortableInfobox\Helpers\InfoboxParamsValidator(); } - + protected function tearDown() { unset($this->InfoboxParamsValidator); parent::tearDown(); @@ -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' ] + ]; + } } diff --git a/tests/phpunit/PortableInfoboxDataServiceTest.php b/tests/phpunit/PortableInfoboxDataServiceTest.php index f83e0e5..e81d95c 100644 --- a/tests/phpunit/PortableInfoboxDataServiceTest.php +++ b/tests/phpunit/PortableInfoboxDataServiceTest.php @@ -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 { diff --git a/tests/phpunit/PortableInfoboxRenderServiceTest.php b/tests/phpunit/PortableInfoboxRenderServiceTest.php index a4d437c..2de438f 100644 --- a/tests/phpunit/PortableInfoboxRenderServiceTest.php +++ b/tests/phpunit/PortableInfoboxRenderServiceTest.php @@ -1,7 +1,7 @@ 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' => '', + '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' => '', + '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' => '', + '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 + ]; } } \ No newline at end of file diff --git a/tests/phpunit/nodes/NodeDataTest.php b/tests/phpunit/nodes/NodeDataTest.php index 46559fb..5ae4634 100644 --- a/tests/phpunit/nodes/NodeDataTest.php +++ b/tests/phpunit/nodes/NodeDataTest.php @@ -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 diff --git a/tests/phpunit/nodes/NodeFactoryTest.php b/tests/phpunit/nodes/NodeFactoryTest.php new file mode 100644 index 0000000..b68b3cb --- /dev/null +++ b/tests/phpunit/nodes/NodeFactoryTest.php @@ -0,0 +1,58 @@ +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 [ + [ + '', + PortableInfobox\Parser\Nodes\NodeInfobox::class + ], + [ + '', + PortableInfobox\Parser\Nodes\NodeData::class + ], + [ + '', + PortableInfobox\Parser\Nodes\NodeMedia::class + ], + [ + '', + PortableInfobox\Parser\Nodes\NodeImage::class + ], + [ + '', + PortableInfobox\Parser\Nodes\NodeUnimplemented::class + ] + ]; + } +} \ No newline at end of file diff --git a/tests/phpunit/nodes/NodeHeaderTest.php b/tests/phpunit/nodes/NodeHeaderTest.php new file mode 100644 index 0000000..546916e --- /dev/null +++ b/tests/phpunit/nodes/NodeHeaderTest.php @@ -0,0 +1,31 @@ +assertEquals( $expected, $node->getData() ); + } + + public function dataProvider() { + return [ + [ '
', [ ], [ 'value' => '' ] ], + [ '
kjdflkja dafkjlsdkfj
', [ ], [ 'value' => 'kjdflkja dafkjlsdkfj' ] ], + [ '
kjdflkjadafkjlsdkfj
', [ ], [ 'value' => 'kjdflkjadafkjlsdkfj' ] ], + ]; + } + +} diff --git a/tests/phpunit/nodes/NodeMediaTest.php b/tests/phpunit/nodes/NodeMediaTest.php index c30b672..5bc6a1d 100644 --- a/tests/phpunit/nodes/NodeMediaTest.php +++ b/tests/phpunit/nodes/NodeMediaTest.php @@ -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,29 +34,30 @@ class NodeMediaTest extends MediaWikiTestCase { "'\"`UNIQabcd-gAlLeRy-2-QINU`\"'", "'\"`UNIQabcd-gAlLeRy-3-QINU`\"'" ]; - PortableInfoboxDataBag::getInstance()->setGallery( $markers[0], - new GalleryMock([ - [ - 'image0_name.jpg', - 'image0_caption' - ], - [ - 'image01_name.jpg', - 'image01_caption' - ], - ])); - PortableInfoboxDataBag::getInstance()->setGallery( $markers[1], - new GalleryMock([ - [ - 'image1_name.jpg', - 'image1_caption' - ] - ])); - PortableInfoboxDataBag::getInstance()->setGallery( $markers[2], new GalleryMock() ); + $galleries = [ + new GalleryMock([ + [ + 'image0_name.jpg', + 'image0_caption' + ], + [ + 'image01_name.jpg', + 'image01_caption' + ], + ]), + new GalleryMock([ + [ + 'image1_name.jpg', + 'image1_caption' + ] + ]), + 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 = '

'; @@ -207,7 +213,7 @@ class NodeMediaTest extends MediaWikiTestCase { * @param $expected */ public function testSources( $markup, $expected ) { - $node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [ ] ); + $node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [] ); $this->assertEquals( $expected, $node->getSources() ); } @@ -242,7 +248,7 @@ class NodeMediaTest extends MediaWikiTestCase { /** @dataProvider metadataProvider */ public function testMetadata( $markup, $expected ) { - $node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [ ] ); + $node = PortableInfobox\Parser\Nodes\NodeFactory::newFromXML( $markup, [] ); $this->assertEquals( $expected, $node->getMetadata() ); } @@ -271,8 +277,8 @@ class NodeMediaTest extends MediaWikiTestCase { $videoMock = new VideoMock(); $xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup ); - $mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]); - $mock->expects( $this->any( )) + $mock = $this->getMock( NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ] ); + $mock->expects( $this->any() ) ->method( 'getFilefromTitle' ) ->willReturn( $videoMock ); @@ -312,8 +318,8 @@ class NodeMediaTest extends MediaWikiTestCase { $audioMock = new AudioMock(); $xmlObj = PortableInfobox\Parser\XmlParser::parseXmlString( $markup ); - $mock = $this->getMock(NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ]); - $mock->expects( $this->any( )) + $mock = $this->getMock( NodeMedia::class, [ 'getFilefromTitle' ], [ $xmlObj, $params ] ); + $mock->expects( $this->any() ) ->method( 'getFilefromTitle' ) ->willReturn( $audioMock ); @@ -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 [ + [ + '', + [ true, true, true, true, false ] + ], + [ + '', + [ false, false, true, true, false ] + ], + [ + '', + [ true, true, false, true, false ] + ], + [ + '', + [ true, true, true, false, false ] + ], + [ + '', + [ false, false, false, false, false ] + ], + [ + '', + [ true, true, true, false, false ] + ], + [ + '', + [ true, true, false, false, false ] + ], + [ + '