setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php'; parent::setUp(); } private function mockInfoboxRenderServiceHelper( $input ) { $extendImageData = isset( $input['extendImageData'] ) ? $input['extendImageData'] : null; $mock = $this->getMockBuilder( 'Wikia\PortableInfobox\Helpers\PortableInfoboxImagesHelper' ) ->setMethods( [ 'extendImageData' ] ) ->getMock(); $mock->expects( $this->any() ) ->method( 'extendImageData' ) ->will( $this->returnValue( $extendImageData ) ); $this->mockClass( 'Wikia\PortableInfobox\Helpers\PortableInfoboxImagesHelper', $mock ); } /** * @param $html * @return string */ private function normalizeHTML( $html ) { if ( empty( $hrml ) ) { return ''; } $DOM = new DOMDocument( '1.0' ); $DOM->formatOutput = true; $DOM->preserveWhiteSpace = false; $DOM->loadXML( $html ); return $DOM->saveXML(); } /** * @param $input * @param $expectedOutput * @param $description * @param $mockParams * @dataProvider testRenderInfoboxDataProvider */ public function testRenderInfobox( $input, $expectedOutput, $description, $mockParams ) { $wrapper = new \Wikia\Util\GlobalStateWrapper( [ 'wgArticleAsJson' => $mockParams['isMercury'] ?? true ] ); $this->mockInfoboxRenderServiceHelper( $mockParams ); $infoboxRenderService = new PortableInfoboxMobileRenderService(); $actualOutput = $wrapper->wrap( function () use ( $infoboxRenderService, $input ) { return $infoboxRenderService->renderInfobox( $input, '', '', '', '' ); } ); $expectedHtml = $this->normalizeHTML( $expectedOutput ); $actualHtml = $this->normalizeHTML( $actualOutput ); $this->assertEquals( $expectedHtml, $actualHtml, $description ); } public function testRenderInfoboxDataProvider() { return [ [ 'input' => [ ], 'output' => '', 'description' => 'Empty data should yield no infobox markup', 'mockParams' => [ ] ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ] ], 'output' => '', 'description' => 'Only title', 'mockParams' => [ ] ], [ 'input' => [ [ 'type' => 'navigation', 'data' => [ 'value' => 'navigation value', ] ] ], 'output' => '', 'description' => 'navigation only', 'mockParams' => [ ] ], [ 'input' => [ [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Only pair', 'mockParams' => [ ] ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ], [ 'type' => 'image', 'data' => [ [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'name' => 'image', 'key' => 'image', 'isVideo' => false ] ] ], [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Simple infobox with title, image and key-value pair', 'mockParams' => [ 'extendImageData' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 1, 'width' => '400', 'height' => '200', 'thumbnail' => 'http://image.jpg', 'thumbnail2x' => 'http://image2x.jpg', 'media-type' => 'image', 'isVideo' => false ] ] ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ], [ 'type' => 'image', 'data' => [ ] ], [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Simple infobox with title, INVALID image and key-value pair', 'mockParams' => [ ] ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ], [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Simple infobox with title, empty image and key-value pair', 'mockParams' => [ ] ], [ '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' => null, ] ] ], 'output' => '', 'description' => 'Infobox with title, group with header and two key-value pairs', 'mockParams' => [ ] ], [ 'input' => [ [ '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' => 'horizontal', 'collapse' => null, ] ] ], 'output' => '', 'description' => 'Infobox with horizontal group', 'mockParams' => [ 'createHorizontalGroupData' => [ 'header' => 'Test header', 'labels' => [ 'test label', 'test label' ], 'values' => [ 'test value', 'test value' ], 'renderLabels' => true ] ] ], [ 'input' => [ [ 'type' => 'group', 'data' => [ 'value' => [ [ 'type' => 'data', 'data' => [ 'label' => '', 'value' => 'test value' ] ], [ 'type' => 'data', 'data' => [ 'label' => '', 'value' => 'test value' ] ] ], 'layout' => 'horizontal', 'collapse' => null, ] ] ], 'output' => '', 'description' => 'Infobox with horizontal group without header and labels', 'mockParams' => [ 'createHorizontalGroupData' => [ 'labels' => [ '', '' ], 'values' => [ 'test value', 'test value' ], 'renderLabels' => false ] ] ], [ 'input' => [ [ 'type' => 'navigation', 'data' => [ 'value' => '

Links

' ] ] ], 'output' => '', 'description' => 'Infobox with navigation', 'mockParams' => [ ] ], [ 'input' => [ [ 'type' => 'image', 'data' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'ref' => 1, 'name' => 'test1', 'key' => 'test1', 'isVideo' => false ] ] ], 'output' => '', 'description' => 'WikiaMobile: Only image. Image is not small- should render hero.', 'mockParams' => [ 'isMercury' => false, 'isValidHeroDataItem' => true, 'extendImageData' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 1, 'width' => '400', 'height' => '200', 'thumbnail' => 'http://image.jpg', 'thumbnail2x' => 'http://image2x.jpg', 'media-type' => 'image', 'isVideo' => false ] ] ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ], [ 'type' => 'image', 'data' => [ 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 44, 'isVideo' => false ] ] ], 'output' => '', 'description' => 'WikiaMobile: Infobox with full hero module with title with HTML tags', 'mockParams' => [ 'isValidHeroDataItem' => true, 'isMercury' => false, 'extendImageData' => [ 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 44, 'width' => '400', 'height' => '200', 'thumbnail' => 'thumbnail.jpg', 'thumbnail2x' => 'thumbnail2x.jpg', 'isVideo' => false, 'media-type' => 'image' ] ] ], [ 'input' => [ [ 'type' => 'image', 'data' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'ref' => 1, 'name' => 'test1', 'key' => 'test1', 'isVideo' => false ] ] ], 'output' => '', 'description' => 'Mercury: Only image. Image is not small- should render hero.', 'mockParams' => [ 'isValidHeroDataItem' => true, 'extendImageData' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 1, 'width' => '400', 'height' => '200', 'thumbnail' => 'http://image.jpg', 'thumbnail2x' => 'http://image2x.jpg', 'media-type' => 'image', 'isVideo' => false, 'mercuryComponentAttrs' => json_encode( [ 'itemContext' => 'portable-infobox', 'ref' => 1 ] ) ] ] ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ], [ 'type' => 'image', 'data' => [ 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 44, 'isVideo' => false ] ] ], 'output' => '', 'description' => 'Mercury: Infobox with full hero module with title with HTML tags', 'mockParams' => [ 'isValidHeroDataItem' => true, 'extendImageData' => [ 'url' => 'http://image.jpg', 'name' => 'test1', 'key' => 'test1', 'ref' => 44, 'width' => '400', 'height' => '200', 'thumbnail' => 'thumbnail.jpg', 'thumbnail2x' => 'thumbnail2x.jpg', 'isVideo' => false, 'media-type' => 'image', 'mercuryComponentAttrs' => json_encode( [ 'itemContext' => 'portable-infobox', 'ref' => 44 ] ) ] ] ] ]; } }