PortableInfobox/tests/PortableInfoboxRenderServiceHelperTest.php

613 lines
15 KiB
PHP
Raw Normal View History

<?php
use Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper;
class PortableInfoboxRenderServiceHelperTest extends WikiaBaseTest {
private $helper;
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
parent::setUp();
$this->helper = new PortableInfoboxRenderServiceHelper();
}
/**
2015-07-29 11:51:55 +00:00
* @desc mocks WikiaFileHelper methods
* @param array $input
2015-12-11 10:56:41 +00:00
* @return \PHPUnit_Framework_MockObject_MockObject
*/
2015-07-29 11:51:55 +00:00
public function createWikiaFileHelperMock( $input ) {
$fileWidth = isset( $input[ 'fileWidth' ] ) ? $input[ 'fileWidth' ] : null;
2015-09-09 12:54:08 +00:00
$fileHeight = isset( $input[ 'fileHeight' ] ) ? $input[ 'fileHeight' ] : null;
2015-12-11 17:56:49 +00:00
$fileMock = $this->getMockBuilder( 'File' )
->setConstructorArgs( [ 'TestFile' ] )
2015-09-09 12:54:08 +00:00
->setMethods( [ 'getWidth', 'getHeight' ] )
->getMock();
2015-07-28 12:15:27 +00:00
$fileMock->expects($this->any())
->method( 'getWidth' )
->will( $this->returnValue( $fileWidth ) );
2015-09-09 12:54:08 +00:00
$fileMock->expects($this->any())
->method( 'getHeight' )
->will( $this->returnValue( $fileHeight ) );
2015-07-28 12:15:27 +00:00
$this->mockStaticMethod( 'WikiaFileHelper', 'getFileFromTitle', $fileMock );
2015-09-09 12:54:08 +00:00
return $fileMock;
}
2015-12-11 17:56:49 +00:00
private function getThumbnailMock( $thumbnailSizes ) {
$thumbnailWidth = isset( $thumbnailSizes[ 'width' ] ) ? $thumbnailSizes[ 'width' ] : null;
$thumbnailHeight = isset( $thumbnailSizes[ 'height' ] ) ? $thumbnailSizes[ 'height' ] : null;
$thumbnailMock = $this->getMockBuilder( 'ThumbnailImage' )
->setMethods( [ 'getWidth', 'getHeight' ] )
->getMock();
$thumbnailMock->expects($this->any())
->method( 'getWidth' )
->will( $this->returnValue( $thumbnailWidth ) );
$thumbnailMock->expects($this->any())
->method( 'getHeight' )
->will( $this->returnValue( $thumbnailHeight ) );
return $thumbnailMock;
}
/**
* @param array $input
* @param array $expectedOutput
* @param string $description
* @dataProvider testCreateHorizontalGroupDataDataProvider
*/
public function testCreateHorizontalGroupData( $input, $expectedOutput, $description ) {
$this->assertEquals(
$expectedOutput,
$this->helper->createHorizontalGroupData( $input ),
$description
);
}
public function testCreateHorizontalGroupDataDataProvider() {
return [
[
'input' => [
[
'type' => 'header',
'data' => [
'value' => 'test header'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label 1',
'value' => 'test value 1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label 2',
'value' => 'test value 2'
]
]
],
'output' => [
'header' => 'test header',
'labels' => [ 'test label 1', 'test label 2' ],
'values' => [ 'test value 1', 'test value 2' ],
'renderLabels' => true
],
'description' => 'Horizontal group data with header and two data tags'
],
[
'input' => [
[
'type' => 'data',
'data' => [
'label' => 'test label 1',
'value' => 'test value 1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label 2',
'value' => 'test value 2'
]
]
],
'output' => [
'labels' => [ 'test label 1', 'test label 2' ],
'values' => [ 'test value 1', 'test value 2' ],
'renderLabels' => true
],
'description' => 'Horizontal group data without header'
],
[
'input' => [
[
'type' => 'header',
'data' => [
'value' => 'test header'
]
],
[
'type' => 'data',
'data' => [
'label' => '',
'value' => 'test value 1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label 2',
'value' => 'test value 2'
]
]
],
'output' => [
'header' => 'test header',
'labels' => [ '', 'test label 2' ],
'values' => [ 'test value 1', 'test value 2' ],
'renderLabels' => true
],
'description' => 'Horizontal group data with empty label'
],
[
'input' => [
[
'type' => 'data',
'data' => [
'label' => '',
'value' => 'test value 1'
]
],
[
'type' => 'data',
'data' => [
'label' => '',
'value' => 'test value 2'
]
]
],
'output' => [
'labels' => [ '', '' ],
'values' => [ 'test value 1', 'test value 2' ],
'renderLabels' => false
],
'description' => 'Horizontal group data without labels'
],
];
}
/**
* @param array $item
* @param array $heroData
* @param boolean $result
* @param string $description
* @param array $mockParams
* @dataProvider testIsValidHeroDataItemDataProvider
*/
public function testIsValidHeroDataItem( $item, $heroData, $result, $description, $mockParams ) {
2015-07-29 11:51:55 +00:00
$this->createWikiaFileHelperMock( $mockParams );
$this->assertEquals(
$result,
$this->helper->isValidHeroDataItem( $item, $heroData ),
$description
);
}
public function testIsValidHeroDataItemDataProvider() {
return [
[
'item' => [
'type' => 'title'
],
'heroData' => [],
'result' => true,
'description' => 'First title in infobox',
'mockParams' => []
],
[
'item' => [
'type' => 'title'
],
'heroData' => [
'title' => 'first infobox title'
],
'result' => false,
'description' => 'not first title in infobox',
'mockParams' => []
],
[
'item' => [
'type' => 'image',
'data' => array( null )
],
'heroData' => [],
'result' => true,
'description' => 'first image in infobox',
'mockParams' => [
'fileWidth' => 300
]
],
[
'item' => [
'type' => 'image'
],
'heroData' => [
'image' => 'first infobox image'
],
'result' => false,
'description' => 'not first image in infobox',
'mockParams' => [
'fileWidth' => 300
]
],
[
'item' => [
'type' => 'image'
],
'heroData' => [],
'result' => false,
'description' => 'too small image',
'mockParams' => [
'fileWidth' => 299
]
]
];
}
/**
* @param string $type
* @param boolean $result
* @param string $description
2015-07-29 11:51:55 +00:00
* @dataProvider testIsTypeSupportedInTemplatesDataProvider
*/
2015-07-29 11:51:55 +00:00
public function testIsTypeSupportedInTemplates( $type, $result, $description ) {
$templates = [
'testType' => 'testType.mustache'
];
$this->assertEquals(
$result,
2015-07-29 11:51:55 +00:00
$this->helper->isTypeSupportedInTemplates( $type, $templates ),
$description
);
}
2015-07-29 11:51:55 +00:00
public function testIsTypeSupportedInTemplatesDataProvider() {
return [
[
'type' => 'testType',
'result' => true,
'description' => 'valid data type'
],
[
'type' => 'invalidTestType',
'result' => false,
'description' => 'invalid data type'
]
];
}
2015-09-09 12:54:08 +00:00
/**
* @desc test getImageSizesForThumbnailer function. It should return the sizes we pass to transform function,
2015-09-09 15:04:06 +00:00
* not the sizes we want image to have. transform adjusts the correct sizes,
* that is creates thumbnail with sizes not bigger than passed, keeping the original aspect ratio.
2015-12-11 10:56:41 +00:00
*
2015-09-09 12:54:08 +00:00
* @param $mockParams
* @param $isWikiaMobile
2015-12-11 10:56:41 +00:00
* @param $wgPortableInfoboxCustomImageWidth
2015-09-09 12:54:08 +00:00
* @param $result
* @param $description
* @dataProvider testGetImageSizesForThumbnailerDataProvider
2015-09-09 12:54:08 +00:00
*/
public function testGetImageSizesForThumbnailer( $mockParams, $isWikiaMobile, $wgPortableInfoboxCustomImageWidth, $result, $description ) {
2015-12-11 10:56:41 +00:00
$this->mockGlobalVariable('wgPortableInfoboxCustomImageWidth', $wgPortableInfoboxCustomImageWidth);
2015-09-09 12:54:08 +00:00
$mock = $this->getMockBuilder( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper' )
->setMethods( [ 'isWikiaMobile' ] )
->getMock();
$mock->expects( $this->any() )->method( 'isWikiaMobile' )->will( $this->returnValue( $isWikiaMobile ) );
$file = $this->createWikiaFileHelperMock( $mockParams );
$this->assertEquals(
$result,
$mock->getImageSizesForThumbnailer( $file ),
2015-09-09 12:54:08 +00:00
$description
);
}
public function testGetImageSizesForThumbnailerDataProvider() {
2015-09-09 12:54:08 +00:00
return [
[
'mockParams' => [
'fileHeight' => 2000,
'fileWidth' => 3000
],
'isWikiaMobile' => false,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => 500,
'width' => 270
2015-09-09 12:54:08 +00:00
],
'description' => 'Big image on desktop'
],
[
'mockParams' => [
'fileHeight' => 3000,
'fileWidth' => 250
],
'isWikiaMobile' => false,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => 500,
'width' => 270
2015-09-09 12:54:08 +00:00
],
'description' => 'Tall image on desktop'
],
[
'mockParams' => [
'fileHeight' => 200,
'fileWidth' => 2000
],
'isWikiaMobile' => false,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => 200,
'width' => 270
2015-09-09 12:54:08 +00:00
],
'description' => 'Wide image on desktop'
],
[
'mockParams' => [
'fileHeight' => 50,
'fileWidth' => 45
],
'isWikiaMobile' => false,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => 50,
'width' => 270
2015-09-09 12:54:08 +00:00
],
'description' => 'Small image on desktop'
],
[
'mockParams' => [
'fileHeight' => 2000,
'fileWidth' => 3000
],
'isWikiaMobile' => true,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Big image on mobile'
],
[
'mockParams' => [
'fileHeight' => 3000,
'fileWidth' => 250
],
'isWikiaMobile' => true,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Tall image on mobile'
],
[
'mockParams' => [
'fileHeight' => 200,
'fileWidth' => 2000
],
'isWikiaMobile' => true,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Wide image on mobile'
],
[
'mockParams' => [
'fileHeight' => 50,
'fileWidth' => 45
],
'isWikiaMobile' => true,
2015-12-11 10:56:41 +00:00
'wgPortableInfoboxCustomImageWidth' => null,
2015-09-09 12:54:08 +00:00
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Small image on mobile'
2015-12-11 10:56:41 +00:00
],
[
'mockParams' => [
'fileHeight' => 2000,
'fileWidth' => 3000
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => 400,
'result' => [
'height' => 2000,
'width' => 400
],
'description' => 'Big image on desktop with custom image width'
],
[
'mockParams' => [
'fileHeight' => 3000,
'fileWidth' => 250
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => 400,
'result' => [
'height' => 3000,
'width' => 400
],
'description' => 'Tall image on desktop with custom image width'
],
[
'mockParams' => [
'fileHeight' => 200,
'fileWidth' => 2000
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => 400,
'result' => [
'height' => 200,
'width' => 400
],
'description' => 'Wide image on desktop with custom image width'
],
[
'mockParams' => [
'fileHeight' => 2000,
'fileWidth' => 3000
],
'isWikiaMobile' => true,
'wgPortableInfoboxCustomImageWidth' => 400,
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Big image on mobile with custom image width'
]
2015-09-09 12:54:08 +00:00
];
}
2015-12-11 17:56:49 +00:00
/**
* @desc test getImageSizesToDisplay function. It should return the logical size used to define HTML width and
* height in the output. This allows to differentiate between physical size and logical size, allowing
* to achieve high pereived quality on for example Retina displays.
*
* @param $thumbnailSizes
* @param $isWikiaMobile
* @param $wgPortableInfoboxCustomImageWidth
* @param $result
* @param $description
* @dataProvider testGetImageSizesToDisplayDataProvider
*/
public function testGetImageSizesToDisplay( $thumbnailSizes, $isWikiaMobile, $wgPortableInfoboxCustomImageWidth, $result, $description ) {
2015-12-11 17:56:49 +00:00
$this->mockGlobalVariable('wgPortableInfoboxCustomImageWidth', $wgPortableInfoboxCustomImageWidth);
$mock = $this->getMockBuilder( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper' )
->setMethods( [ 'isWikiaMobile' ] )
->getMock();
$mock->expects( $this->any() )->method( 'isWikiaMobile' )->will( $this->returnValue( $isWikiaMobile ) );
$thumbnailMock = $this->getThumbnailMock( $thumbnailSizes );
$this->assertEquals(
$result,
$mock->getImageSizesToDisplay( $thumbnailMock ),
2015-12-11 17:56:49 +00:00
$description
);
}
public function testGetImageSizesToDisplayDataProvider() {
return [
[
'thumbnailSizes' => [
'height' => 500,
'width' => 270
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => null,
'result' => [
'height' => 500,
'width' => 270
],
'description' => 'Regular thumbnail image on desktop with no custom width; logical size = physical size'
],
[
'thumbnailSizes' => [
'height' => 1000,
'width' => 540
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => 540,
'result' => [
'height' => 500,
'width' => 270
],
'description' => 'Regular thumbnail image on desktop with double custom width; portrait'
],
[
'thumbnailSizes' => [
'height' => 500,
'width' => 540
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => 540,
'result' => [
'height' => 250,
'width' => 270
2015-12-11 17:56:49 +00:00
],
'description' => 'Regular thumbnail image on desktop with double custom width; landscape'
],
[
'thumbnailSizes' => [
'height' => 250,
'width' => 270
],
'isWikiaMobile' => true,
'wgPortableInfoboxCustomImageWidth' => null,
'result' => [
2015-12-11 18:35:33 +00:00
'height' => 250,
2015-12-11 17:56:49 +00:00
'width' => 270
],
'description' => 'Regular thumbnail image on mobile with no custom width; logical size = physical size'
],
[
'thumbnailSizes' => [
2015-12-11 18:35:33 +00:00
'height' => 360,
2015-12-11 17:56:49 +00:00
'width' => 270
],
'isWikiaMobile' => true,
'wgPortableInfoboxCustomImageWidth' => 540,
'result' => [
2015-12-11 18:35:33 +00:00
'height' => 360,
2015-12-11 17:56:49 +00:00
'width' => 270
],
'description' => 'Regular thumbnail image on mobile with double custom width; portrait; logical size = physical size'
],
[
'thumbnailSizes' => [
2015-12-11 18:35:33 +00:00
'height' => 270,
'width' => 360
2015-12-11 17:56:49 +00:00
],
'isWikiaMobile' => true,
'wgPortableInfoboxCustomImageWidth' => 540,
'result' => [
2015-12-11 18:35:33 +00:00
'height' => 270,
'width' => 360
2015-12-11 17:56:49 +00:00
],
'description' => 'Regular thumbnail image on desktop with double custom width; landscape; logical size = physical size'
],
[
'thumbnailSizes' => [
'height' => 600,
'width' => 210
],
'isWikiaMobile' => false,
'wgPortableInfoboxCustomImageWidth' => 540,
'result' => [
'height' => 500,
'width' => 175
],
'description' => 'Regular thumbnail image on desktop with double custom width; portrait extra
thin image edge case'
]
2015-12-11 17:56:49 +00:00
];
}
}