setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php'; parent::setUp(); } /** * @param $input to check presence of some additional config fields. Possible fields: * 'isInvalidImage' - bool - if getThumbnail should return false * 'smallImageDimensions' - integer - size of small image (both width and height) * * @return PHPUnit_Framework_MockObject_MockObject */ private function mockInfoboxRenderServiceHelper( $input ) { $isValidHeroDataItem = isset( $input[ 'isValidHeroDataItem' ] ) && $input[ 'isValidHeroDataItem' ]; $createHorizontalGroupData = isset( $input[ 'createHorizontalGroupData' ] ) ? $input[ 'createHorizontalGroupData' ] : null; $extendImageData = isset( $input[ 'extendImageData' ] ) ? $input[ 'extendImageData' ] : null; $mock = $this->getMockBuilder( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper' ) ->setMethods( [ 'isValidHeroDataItem', 'validateType', 'createHorizontalGroupData', 'extendImageData' ] ) ->getMock(); $mock->expects( $this->any() ) ->method( 'isValidHeroDataItem' ) ->will( $this->returnValue( $isValidHeroDataItem ) ); $mock->expects( $this->any() ) ->method( 'validateType' ) ->will( $this->returnValue( true ) ); $mock->expects( $this->any() ) ->method( 'createHorizontalGroupData' ) ->will( $this->returnValue( $createHorizontalGroupData ) ); $mock->expects( $this->any() ) ->method( 'extendImageData' ) ->will( $this->returnValue( $extendImageData ) ); $this->mockClass( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper', $mock ); } /** * @param $html * @return string */ private function normalizeHTML( $html ) { $DOM = new DOMDocument( '1.0' ); $DOM->formatOutput = true; $DOM->preserveWhiteSpace = false; $DOM->loadXML( $html ); return $DOM->saveXML(); } public function testEuropaThemeEnabled() { $wrapper = new \Wikia\Util\GlobalStateWrapper( [ 'wgEnablePortableInfoboxEuropaTheme' => true ] ); $infoboxRenderService = new PortableInfoboxRenderService(); $output = $wrapper->wrap( function () use ( $infoboxRenderService ) { return $infoboxRenderService->renderInfobox( [ [ 'type' => 'title', 'data' => [ 'value' => 'Test' ] ] ], '', '', '', '' ); } ); $expected = $this->normalizeHTML( '' ); $result = $this->normalizeHTML( $output ); $this->assertEquals( $expected, $result ); } /** * @param $input * @param $expectedOutput * @param $description * @param $mockParams * @dataProvider testRenderInfoboxDataProvider */ public function testRenderInfobox( $input, $expectedOutput, $description, $mockParams ) { $this->mockInfoboxRenderServiceHelper( $mockParams ); $infoboxRenderService = new PortableInfoboxRenderService(); $actualOutput = $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' ], [ 'input' => [ [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ] ], 'output' => '', 'description' => 'Only title' ], [ 'input' => [ [ 'type' => 'image', 'data' => [ [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'name' => 'image', 'key' => 'image', 'caption' => 'Lorem ipsum dolor', 'isVideo' => false ] ] ] ], 'output' => '', 'description' => 'Only image', 'mockParams' => [ 'extendImageData' => [ '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 ] ] ], [ 'input' => [ [ 'type' => 'image', 'data' => [ [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'caption' => 'Lorem ipsum dolor', 'isVideo' => true, 'duration' => '1:20', 'name' => 'test', 'key' => 'test' ] ] ] ], 'output' => '', 'description' => 'Only video', 'mockParams' => [ 'extendImageData' => [ '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' => 'video', 'isVideo' => true, 'duration' => '1:20' ] ] ], [ 'input' => [ [ 'type' => 'navigation', 'data' => [ 'value' => 'navigation value', ] ] ], 'output' => '', 'description' => 'navigation only' ], [ 'input' => [ [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Only pair' ], [ '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' => 'image', 'key' => 'image', 'width' => '400', 'height' => '200', 'thumbnail' => 'http://thumbnail.jpg', 'thumbnail2x' => 'http://thumbnail2x.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' ], [ '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' ], [ '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' ] ] ] ] ] ], 'output' => '', 'description' => 'Infobox with title, group with header and two key-value pairs' ], [ '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' ] ] ], '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' ] ] ], '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' ] ]; } }