2015-04-27 14:05:31 +00:00
< ? php
2015-07-01 16:03:57 +00:00
class PortableInfoboxRenderServiceTest extends WikiaBaseTest {
2015-05-04 14:43:53 +00:00
protected function setUp () {
$this -> setupFile = dirname ( __FILE__ ) . '/../PortableInfobox.setup.php' ;
2015-04-27 14:05:31 +00:00
parent :: setUp ();
2015-07-10 11:19:08 +00:00
}
2015-04-27 14:05:31 +00:00
2015-07-10 12:31:43 +00:00
/**
* @ param $isWikiaMobile
* @ param $input to check presence of 'invalidImage' field
* @ return PHPUnit_Framework_MockObject_MockObject
2015-07-10 14:57:57 +00:00
*/
private function getInfoboxRenderServiceMock ( $input )
2015-07-10 11:19:08 +00:00
{
2015-07-13 08:56:19 +00:00
$isInvalidImage = isset ( $input [ 'isInvalidImage' ] ) && $input [ 'isInvalidImage' ];
2015-07-10 14:57:57 +00:00
$isWikiaMobile = isset ( $input [ 'isWikiaMobile' ] ) && $input [ 'isWikiaMobile' ];
2015-07-17 13:43:08 +00:00
$mockThumbnailImage = $isInvalidImage ? false : $this -> getThumbnailImageMock ( $input );
2015-05-07 12:36:30 +00:00
2015-07-10 12:10:03 +00:00
$mock = $this -> getMockBuilder ( 'PortableInfoboxRenderService' )
-> setMethods ( [ 'getThumbnail' , 'isWikiaMobile' ] )
-> getMock ();
$mock -> expects ( $this -> any () )
-> method ( 'isWikiaMobile' )
-> will ( $this -> returnValue ( $isWikiaMobile ) );
$mock -> expects ( $this -> any () )
-> method ( 'getThumbnail' )
-> will ( $this -> returnValue ( $mockThumbnailImage ) );
2015-04-27 14:05:31 +00:00
2015-07-10 11:19:08 +00:00
return $mock ;
2015-07-01 14:34:34 +00:00
}
2015-04-27 14:05:31 +00:00
/**
2015-07-10 12:31:43 +00:00
* @ desc Returns the ThumbnailImage with hardcoded values returned by
2015-07-17 13:43:08 +00:00
* 'getUrl' , 'getWidth' and 'getHeight' functions . In case of small image return small image sizes .
2015-07-10 12:31:43 +00:00
* @ return PHPUnit_Framework_MockObject_MockObject
2015-04-27 14:05:31 +00:00
*/
2015-07-17 13:43:08 +00:00
private function getThumbnailImageMock ( $input ) {
$isSmallImage = isset ( $input [ 'isSmallImage' ] ) && $input [ 'isSmallImage' ];
2015-07-10 11:19:08 +00:00
$mockThumbnailImage = $this -> getMockBuilder ( 'ThumbnailImage' )
-> setMethods ( [ 'getUrl' , 'getWidth' , 'getHeight' ] )
-> getMock ();
2015-07-09 13:50:12 +00:00
$mockThumbnailImage -> expects ( $this -> any () )
-> method ( 'getUrl' )
2015-07-09 16:06:22 +00:00
-> will ( $this -> returnValue ( 'http://image.jpg' ) );
2015-07-17 13:43:08 +00:00
if ( $isSmallImage ) {
$mockThumbnailImage -> expects ( $this -> any () )
-> method ( 'getWidth' )
-> will ( $this -> returnValue ( 100 ) );
$mockThumbnailImage -> expects ( $this -> any () )
-> method ( 'getHeight' )
-> will ( $this -> returnValue ( 100 ) );
} else {
$mockThumbnailImage -> expects ( $this -> any () )
-> method ( 'getWidth' )
-> will ( $this -> returnValue ( 400 ) );
$mockThumbnailImage -> expects ( $this -> any () )
-> method ( 'getHeight' )
-> will ( $this -> returnValue ( 200 ) );
}
2015-04-27 14:05:31 +00:00
2015-07-10 11:19:08 +00:00
return $mockThumbnailImage ;
2015-05-20 11:56:56 +00:00
}
2015-04-27 14:05:31 +00:00
2015-07-13 08:56:19 +00:00
/**
* @ param $html
* @ return string
*/
private function normalizeHTML ( $html ) {
2015-07-10 14:57:57 +00:00
$DOM = new DOMDocument ( '1.0' );
$DOM -> formatOutput = true ;
$DOM -> preserveWhiteSpace = false ;
2015-07-13 08:56:19 +00:00
$DOM -> loadXML ( $html );
2015-05-04 14:16:35 +00:00
2015-07-10 14:57:57 +00:00
return $DOM -> saveXML ();
2015-04-27 14:05:31 +00:00
}
2015-05-20 11:56:56 +00:00
/**
* @ param $input
* @ param $expectedOutput
* @ param $description
2015-07-10 14:57:57 +00:00
* @ dataProvider testRenderInfoboxDataProvider
2015-05-20 11:56:56 +00:00
*/
2015-07-10 14:57:57 +00:00
public function testRenderInfobox ( $input , $expectedOutput , $description ) {
$infoboxRenderService = $this -> getInfoboxRenderServiceMock ( $input );
$actualOutput = $infoboxRenderService -> renderInfobox ( $input );
2015-05-20 11:56:56 +00:00
2015-07-13 08:56:19 +00:00
$expectedHtml = $this -> normalizeHTML ( $expectedOutput ) ;
$actualHtml = $this -> normalizeHTML ( $actualOutput );
2015-05-20 11:56:56 +00:00
$this -> assertEquals ( $expectedHtml , $actualHtml , $description );
}
2015-04-27 14:05:31 +00:00
public function testRenderInfoboxDataProvider () {
2015-07-01 14:34:34 +00:00
return [
[
2015-04-27 14:05:31 +00:00
'input' => [],
'output' => '' ,
'description' => 'Empty data should yield no infobox markup'
2015-07-01 16:03:57 +00:00
],
[
'input' => [
[
'type' => 'title' ,
'data' => [
'value' => 'Test Title'
]
2015-04-27 14:05:31 +00:00
]
2015-07-01 16:03:57 +00:00
],
'output' => ' < aside class = " portable-infobox " >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item item-type-title portable-infobox-item-margins " >
< h2 class = " portable-infobox-title " > Test Title </ h2 >
2015-07-01 16:03:57 +00:00
</ div >
</ aside > ' ,
2015-04-27 14:05:31 +00:00
'description' => 'Only title'
2015-07-01 16:03:57 +00:00
],
[
'input' => [
[
'type' => 'image' ,
'data' => [
'alt' => 'image alt' ,
'url' => 'http://image.jpg' ,
2015-05-20 11:56:56 +00:00
'caption' => 'Lorem ipsum dolor'
2015-07-01 16:03:57 +00:00
]
2015-05-20 11:56:56 +00:00
]
2015-07-01 16:03:57 +00:00
],
'output' => ' < aside class = " portable-infobox " >
< div class = " portable-infobox-item item-type-image no-margins " >
2015-05-25 13:57:16 +00:00
< figure class = " portable-infobox-image-wrapper " >
2015-05-20 11:56:56 +00:00
< a href = " http://image.jpg " class = " image image-thumbnail " title = " image alt " >
2015-07-09 13:50:12 +00:00
< img src = " http://image.jpg " class = " portable-infobox-image " alt = " image alt " width = " 400 " height = " 200 " data - image - key = " " data - image - name = " " />
2015-05-20 11:56:56 +00:00
</ a >
< figcaption class = " portable-infobox-item-margins portable-infobox-image-caption " > Lorem ipsum dolor </ figcaption >
</ figure >
2015-07-01 16:03:57 +00:00
</ div >
</ aside > ' ,
2015-05-25 13:57:16 +00:00
'description' => 'Only image'
2015-07-01 16:03:57 +00:00
],
[
'input' => [
[
2015-07-01 14:19:31 +00:00
'type' => 'navigation' ,
2015-07-01 16:03:57 +00:00
'data' => [
2015-07-01 14:19:31 +00:00
'value' => 'navigation value' ,
2015-07-01 16:03:57 +00:00
]
]
],
'output' => ' < aside class = " portable-infobox " >
2015-07-02 08:22:37 +00:00
< nav class = " portable-infobox-navigation portable-infobox-item-margins portable-infobox-secondary-background portable-infobox-secondary-font " > navigation value </ nav >
2015-07-01 16:03:57 +00:00
</ aside > ' ,
2015-07-01 14:19:31 +00:00
'description' => 'navigation only'
2015-07-01 16:03:57 +00:00
],
[
'input' => [
[
'type' => 'data' ,
'data' => [
'label' => 'test label' ,
'value' => 'test value'
]
2015-05-04 14:30:40 +00:00
]
2015-07-01 16:03:57 +00:00
],
'output' => ' < aside class = " portable-infobox " >
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-07 10:48:19 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-07-01 16:03:57 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
</ aside > ' ,
2015-05-04 14:30:40 +00:00
'description' => 'Only pair'
2015-07-01 16:03:57 +00:00
],
[
'input' => [
[
'type' => 'title' ,
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image' ,
'data' => [
'alt' => 'image alt' ,
2015-07-09 16:06:22 +00:00
'value' => 'http://image.jpg'
2015-07-01 16:03:57 +00:00
]
],
[
'type' => 'data' ,
'data' => [
'label' => 'test label' ,
'value' => 'test value'
]
]
],
2015-05-20 11:56:24 +00:00
'output' => ' < aside class = " portable-infobox " >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item item-type-title portable-infobox-item-margins " >
< h2 class = " portable-infobox-title " > Test Title </ h2 >
</ div >
2015-05-20 11:56:56 +00:00
< div class = " portable-infobox-item item-type-image no-margins " >
2015-05-25 13:57:16 +00:00
< figure class = " portable-infobox-image-wrapper " >
2015-05-07 12:36:30 +00:00
< a href = " " class = " image image-thumbnail " title = " image alt " >
2015-07-09 13:50:12 +00:00
< img src = " http://image.jpg " class = " portable-infobox-image " alt = " image alt " width = " 400 " height = " 200 " data - image - key = " " data - image - name = " " />
2015-05-20 11:56:56 +00:00
</ a >
</ figure >
</ div >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-02 08:22:37 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
</ aside > ' ,
2015-04-27 14:05:31 +00:00
'description' => 'Simple infobox with title, image and key-value pair'
2015-05-04 14:30:40 +00:00
],
2015-04-27 14:05:31 +00:00
[
'input' => [
2015-07-13 08:56:19 +00:00
'isInvalidImage' => true ,
2015-04-27 14:05:31 +00:00
[
'type' => 'title' ,
'data' => [
'value' => 'Test Title'
2015-05-07 13:25:26 +00:00
]
2015-04-27 14:05:31 +00:00
],
[
'type' => 'image' ,
2015-07-10 12:10:03 +00:00
'data' => []
2015-04-27 14:05:31 +00:00
],
[
2015-05-04 10:48:57 +00:00
'type' => 'data' ,
2015-04-27 14:05:31 +00:00
'data' => [
'label' => 'test label' ,
'value' => 'test value'
2015-05-07 13:25:26 +00:00
]
2015-04-27 14:05:31 +00:00
]
],
2015-05-20 11:56:24 +00:00
'output' => ' < aside class = " portable-infobox " >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item item-type-title portable-infobox-item-margins " >
< h2 class = " portable-infobox-title " > Test Title </ h2 >
</ div >
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-02 08:22:37 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
</ aside > ' ,
2015-07-10 12:10:03 +00:00
'description' => 'Simple infobox with title, INVALID image and key-value pair'
2015-04-27 14:05:31 +00:00
],
[
'input' => [
[
'type' => 'title' ,
'data' => [
'value' => 'Test Title'
2015-05-07 13:25:26 +00:00
]
2015-04-27 14:05:31 +00:00
],
[
2015-05-04 10:48:57 +00:00
'type' => 'data' ,
2015-04-27 14:05:31 +00:00
'data' => [
'label' => 'test label' ,
'value' => 'test value'
2015-05-07 13:25:26 +00:00
]
2015-04-27 14:05:31 +00:00
]
],
2015-05-20 11:56:24 +00:00
'output' => ' < aside class = " portable-infobox " >
2015-05-04 14:30:40 +00:00
< div class = " portable-infobox-item item-type-title portable-infobox-item-margins " >
< h2 class = " portable-infobox-title " > Test Title </ h2 >
</ div >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-02 08:22:37 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-05-07 12:36:30 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
2015-05-04 14:30:40 +00:00
</ div >
</ aside > ' ,
2015-05-07 12:36:30 +00:00
'description' => 'Simple infobox with title, empty image and key-value pair'
2015-05-04 14:30:40 +00:00
],
[
'input' => [
[
'type' => 'title' ,
'data' => [
'value' => 'Test Title'
2015-05-07 13:25:26 +00:00
]
2015-05-04 14:30:40 +00:00
],
[
'type' => 'group' ,
'data' => [
'value' => [
[
'type' => 'header' ,
'data' => [
'value' => 'Test Header'
2015-05-07 13:25:26 +00:00
]
2015-05-04 14:30:40 +00:00
],
[
2015-05-07 12:36:30 +00:00
'type' => 'data' ,
2015-05-04 14:30:40 +00:00
'data' => [
'label' => 'test label' ,
'value' => 'test value'
2015-05-07 13:25:26 +00:00
]
2015-05-04 14:30:40 +00:00
],
[
2015-05-07 12:36:30 +00:00
'type' => 'data' ,
2015-05-04 14:30:40 +00:00
'data' => [
'label' => 'test label' ,
'value' => 'test value'
2015-05-07 13:25:26 +00:00
]
2015-05-04 14:30:40 +00:00
]
]
2015-05-07 13:25:26 +00:00
]
2015-05-04 14:30:40 +00:00
]
],
2015-05-20 11:56:24 +00:00
'output' => ' < aside class = " portable-infobox " >
2015-05-04 14:30:40 +00:00
< div class = " portable-infobox-item item-type-title portable-infobox-item-margins " >
< h2 class = " portable-infobox-title " > Test Title </ h2 >
</ div >
< section class = " portable-infobox-item item-type-group " >
2015-07-02 08:22:37 +00:00
< div class = " portable-infobox-item item-type-header portable-infobox-item-margins portable-infobox-secondary-background " >
< h2 class = " portable-infobox-header portable-infobox-secondary-font " > Test Header </ h2 >
2015-05-04 14:30:40 +00:00
</ div >
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-02 08:22:37 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-05-04 14:30:40 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-02 08:22:37 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-05-04 14:30:40 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
</ section >
</ aside > ' ,
'description' => 'Infobox with title, image and group with header two key-value pairs'
],
2015-06-11 17:28:49 +00:00
[
'input' => [
[
'type' => 'group' ,
'data' => [
'value' => [
[
'type' => 'header' ,
'data' => [
'value' => 'Test Header'
]
],
[
'type' => 'data' ,
'data' => [
'label' => 'test label' ,
'value' => 'test value'
]
]
],
'layout' => 'horizontal'
]
]
],
'output' => ' < aside class = " portable-infobox " >
< section class = " portable-infobox-item item-type-group group-layout-horizontal " >
2015-07-02 08:22:37 +00:00
< div class = " portable-infobox-item item-type-header portable-infobox-item-margins portable-infobox-secondary-background " >
< h2 class = " portable-infobox-header portable-infobox-secondary-font " > Test Header </ h2 >
2015-06-11 17:28:49 +00:00
</ div >
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
2015-07-02 08:22:37 +00:00
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
2015-06-11 17:28:49 +00:00
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
</ section >
</ aside > ' ,
'description' => 'Infobox with title, image and horizontal group'
],
2015-05-04 14:30:40 +00:00
[
'input' => [
[
2015-07-01 14:19:31 +00:00
'type' => 'navigation' ,
2015-05-04 14:30:40 +00:00
'data' => [
2015-05-07 12:36:30 +00:00
'value' => '<p>Links</p>'
2015-05-07 13:25:26 +00:00
]
2015-05-04 14:30:40 +00:00
]
],
2015-05-20 11:56:24 +00:00
'output' => ' < aside class = " portable-infobox " >
2015-07-02 08:22:37 +00:00
< nav class = " portable-infobox-navigation portable-infobox-item-margins portable-infobox-secondary-background portable-infobox-secondary-font " >
2015-05-04 14:30:40 +00:00
< p > Links </ p >
2015-07-01 14:19:31 +00:00
</ nav >
2015-05-04 14:30:40 +00:00
</ aside > ' ,
2015-07-01 14:19:31 +00:00
'description' => 'Infobox with navigation'
2015-07-10 14:57:57 +00:00
],
2015-05-20 11:56:56 +00:00
[
'input' => [
2015-07-10 14:57:57 +00:00
'isWikiaMobile' => true ,
2015-05-20 11:56:56 +00:00
[
'type' => 'image' ,
'data' => [
'alt' => 'image alt' ,
'url' => 'http://image.jpg' ,
'thumbnail' => 'thumbnail.jpg' ,
'ref' => 1 ,
2015-07-17 13:43:08 +00:00
'name' => 'test1' ,
'width' => 400 ,
'height' => 200
2015-05-20 11:56:56 +00:00
]
]
],
'output' => ' < aside class = " portable-infobox " >
2015-07-16 11:21:20 +00:00
< div class = " portable-infobox-item item-type-hero " >
2015-07-10 11:19:08 +00:00
< img src = " data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D " data - src = " http://image.jpg " class = " portable-infobox-image lazy media article-media " alt = " image alt " data - image - key = " test1 " data - image - name = " test1 " data - ref = " 1 " data - params = \ ' [{ " name " : " test1 " , " full " : " http://image.jpg " }] \ ' />
2015-05-20 11:56:56 +00:00
</ div >
</ aside > ' ,
2015-07-17 13:43:08 +00:00
'description' => 'Mobile: Only image. Image is not small- should render hero.'
],
[
'input' => [
'isWikiaMobile' => true ,
'isSmallImage' => true ,
[
'type' => 'image' ,
'data' => [
'alt' => 'image alt' ,
'url' => 'http://image.jpg' ,
'thumbnail' => 'thumbnail.jpg' ,
'ref' => 1 ,
'name' => 'test1' ,
'width' => 100 ,
'height' => 100
]
]
],
'output' => ' < aside class = " portable-infobox " >
< div class = " portable-infobox-item item-type-image no-margins " >
< img src = " data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D " data - src = " http://image.jpg " class = " portable-infobox-image lazy media article-media " alt = " image alt " data - image - key = " test1 " data - image - name = " test1 " data - ref = " 1 " data - params = \ ' [{ " name " : " test1 " , " full " : " http://image.jpg " }] \ ' />
</ div >
</ aside > ' ,
'description' => 'Mobile: Only a small image. Should not render hero'
2015-07-10 14:57:57 +00:00
],
[
'input' => [
2015-07-13 08:56:19 +00:00
'isInvalidImage' => true ,
2015-07-10 14:57:57 +00:00
'isWikiaMobile' => true ,
[
'type' => 'title' ,
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image' ,
'data' => []
],
[
'type' => 'data' ,
'data' => [
'label' => 'test label' ,
'value' => 'test value'
]
]
],
'output' => ' < aside class = " portable-infobox " >
2015-07-17 13:43:08 +00:00
< div class = " portable-infobox-item item-type-title portable-infobox-item-margins " >
< h2 class = " portable-infobox-title " > Test Title </ h2 >
2015-07-10 14:57:57 +00:00
</ div >
< div class = " portable-infobox-item item-type-key-val portable-infobox-item-margins " >
< h3 class = " portable-infobox-item-label portable-infobox-secondary-font " > test label </ h3 >
< div class = " portable-infobox-item-value " > test value </ div >
</ div >
</ aside > ' ,
'description' => 'Mobile: Simple infobox with title, INVALID image and key-value pair'
2015-04-27 14:05:31 +00:00
]
];
}
}