setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php'; parent::setUp(); } /** * @param $isWikiaMobile * @param $input to check presence of 'invalidImage' field * @return PHPUnit_Framework_MockObject_MockObject */ private function getInfoboxRenderServiceMock( $input ) { $isInvalidImage = isset( $input[ 'isInvalidImage' ] ) && $input[ 'isInvalidImage' ]; $isWikiaMobile = isset( $input[ 'isWikiaMobile' ] ) && $input[ 'isWikiaMobile' ]; $fileWidth = isset( $input[ 'fileWidth' ] ) ? $input[ 'fileWidth' ] : null; $mockThumbnailImage = $isInvalidImage ? false : $this->getThumbnailImageMock( $input ); $mock = $this->getMockBuilder( 'PortableInfoboxRenderService' ) ->setMethods( [ 'getThumbnail', 'isWikiaMobile', 'getFileWidth' ] ) ->getMock(); $mock->expects( $this->any() ) ->method( 'isWikiaMobile' ) ->will( $this->returnValue( $isWikiaMobile ) ); $mock->expects( $this->any() ) ->method( 'getThumbnail' ) ->will( $this->returnValue( $mockThumbnailImage ) ); $mock->expects( $this->any() ) ->method( 'getFileWidth' ) ->will( $this->returnValue( $fileWidth ) ); return $mock; } /** * @desc Returns the ThumbnailImage with hardcoded values returned by * 'getUrl', 'getWidth' and 'getHeight' functions. In case of small image return small image sizes. * @return PHPUnit_Framework_MockObject_MockObject */ private function getThumbnailImageMock() { $mockThumbnailImage = $this->getMockBuilder( 'ThumbnailImage' ) ->setMethods( [ 'getUrl', 'getWidth', 'getHeight' ] ) ->getMock(); $mockThumbnailImage->expects( $this->any() ) ->method( 'getUrl' ) ->will( $this->returnValue( 'http://image.jpg' ) ); $mockThumbnailImage->expects( $this->any() ) ->method( 'getWidth' ) ->will( $this->returnValue( 400 ) ); $mockThumbnailImage->expects( $this->any() ) ->method( 'getHeight' ) ->will( $this->returnValue( 200 ) ); return $mockThumbnailImage; } /** * @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(); } /** * @param $input * @param $expectedOutput * @param $description * @dataProvider testRenderInfoboxDataProvider */ public function testRenderInfobox( $input, $expectedOutput, $description ) { $infoboxRenderService = $this->getInfoboxRenderServiceMock( $input ); $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', 'caption' => 'Lorem ipsum dolor' ] ] ], 'output' => '', 'description' => 'Only image' ], [ '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', 'value' => 'http://image.jpg' ] ], [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Simple infobox with title, image and key-value pair' ], [ 'input' => [ 'isInvalidImage' => true, [ '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, image and group with header two key-value pairs' ], [ 'input' => [ [ 'type' => 'group', 'data' => [ 'value' => [ [ 'type' => 'header', 'data' => [ 'value' => 'Test Header' ] ], [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'layout' => 'horizontal' ] ] ], 'output' => '', 'description' => 'Infobox with title, image and horizontal group' ], [ 'input' => [ [ 'type' => 'navigation', 'data' => [ 'value' => '

Links

' ] ] ], 'output' => '', 'description' => 'Infobox with navigation' ], [ 'input' => [ 'isWikiaMobile' => true, 'fileWidth' => '450', [ 'type' => 'image', 'data' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'thumbnail' => 'thumbnail.jpg', 'ref' => 1, 'name' => 'test1' ] ] ], 'output' => '', 'description' => 'Mobile: Only image. Image is not small- should render hero.' ], [ 'input' => [ 'isWikiaMobile' => true, 'fileWidth' => '290', [ 'type' => 'image', 'data' => [ 'alt' => 'image alt', 'url' => 'http://image.jpg', 'thumbnail' => 'thumbnail.jpg', 'ref' => 1, 'name' => 'test1' ] ] ], 'output' => '', 'description' => 'Mobile: A small image. Should not render hero' ], [ 'input' => [ 'isInvalidImage' => true, 'isWikiaMobile' => true, [ 'type' => 'title', 'data' => [ 'value' => 'Test Title' ] ], [ 'type' => 'image', 'data' => [] ], [ 'type' => 'data', 'data' => [ 'label' => 'test label', 'value' => 'test value' ] ] ], 'output' => '', 'description' => 'Mobile: Simple infobox with title, INVALID image and key-value pair' ] ]; } /** * @covers PortableInfoboxRenderService::sanitizeInfoboxTitle * @dataProvider sanitizeInfoboxTitleSourceProvider * * @param $source string * @param $expected string */ public function testSanitizeInfoboxTitle( $source, $expected ) { $renderService = new PortableInfoboxRenderService(); $this->assertEquals( $expected, $renderService->sanitizeInfoboxTitle( $source ) ); } public function sanitizeInfoboxTitleSourceProvider() { return [ [ 'Test Title' , 'Test Title'], [ ' Test Title ' , 'Test Title'], [ 'Test Title ' , 'Test Title'], [ 'Test Title with link' , 'Test Title with link'], [ 'Real world DBGT Logotitle example' , 'Real world title example'] ]; } }