PortableInfobox/tests/PortableInfoboxRenderServiceTest.php

467 lines
14 KiB
PHP
Raw Normal View History

<?php
class PortableInfoboxRenderServiceTest extends WikiaBaseTest {
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
parent::setUp();
2015-07-10 11:19:08 +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' ];
$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-07-10 11:19:08 +00:00
return $mock;
2015-07-01 14:34:34 +00:00
}
/**
2015-07-10 12:31:43 +00:00
* @desc Returns the ThumbnailImage with hardcoded values returned by
* '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
*/
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' ) );
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-07-10 11:19:08 +00:00
return $mockThumbnailImage;
}
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();
}
/**
* @param $input
* @param $expectedOutput
* @param $description
2015-07-10 14:57:57 +00:00
* @dataProvider testRenderInfoboxDataProvider
*/
2015-07-10 14:57:57 +00:00
public function testRenderInfobox( $input, $expectedOutput, $description ) {
$infoboxRenderService = $this->getInfoboxRenderServiceMock( $input );
$actualOutput = $infoboxRenderService->renderInfobox( $input );
2015-07-13 08:56:19 +00:00
$expectedHtml = $this->normalizeHTML( $expectedOutput) ;
$actualHtml = $this->normalizeHTML( $actualOutput );
$this->assertEquals( $expectedHtml, $actualHtml, $description );
}
public function testRenderInfoboxDataProvider() {
2015-07-01 14:34:34 +00:00
return [
[
'input' => [],
'output' => '',
'description' => 'Empty data should yield no infobox markup'
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
]
],
'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>
</aside>',
'description' => 'Only title'
],
[
'input' => [
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'url' => 'http://image.jpg',
'caption' => 'Lorem ipsum dolor'
]
]
],
'output' => '<aside class="portable-infobox">
<div class="portable-infobox-item item-type-image no-margins">
<figure class="portable-infobox-image-wrapper">
<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=""/>
</a>
<figcaption class="portable-infobox-item-margins portable-infobox-image-caption">Lorem ipsum dolor</figcaption>
</figure>
</div>
</aside>',
'description' => 'Only image'
],
[
'input' => [
[
'type' => 'navigation',
'data' => [
'value' => 'navigation value',
]
]
],
'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>
</aside>',
'description' => 'navigation only'
],
[
'input' => [
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
2015-05-04 14:30:40 +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>
<div class="portable-infobox-item-value">test value</div>
</div>
</aside>',
2015-05-04 14:30:40 +00:00
'description' => 'Only pair'
],
[
'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'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'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-image no-margins">
<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=""/>
</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>',
'description' => 'Simple infobox with title, image and key-value pair'
2015-05-04 14:30:40 +00:00
],
[
'input' => [
2015-07-13 08:56:19 +00:00
'isInvalidImage' => true,
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image',
2015-07-10 12:10:03 +00:00
'data' => []
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'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'
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'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-04 14:30:40 +00:00
],
[
'type' => 'group',
'data' => [
'value' => [
[
'type' => 'header',
'data' => [
'value' => 'Test Header'
]
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-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-04 14:30:40 +00:00
]
]
]
2015-05-04 14:30:40 +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' => [
[
'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-04 14:30:40 +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>
</nav>
2015-05-04 14:30:40 +00:00
</aside>',
'description' => 'Infobox with navigation'
2015-07-10 14:57:57 +00:00
],
[
'input' => [
2015-07-10 14:57:57 +00:00
'isWikiaMobile' => true,
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'url' => 'http://image.jpg',
'thumbnail' => 'thumbnail.jpg',
'ref' => 1,
'name' => 'test1',
'width' => 400,
'height' => 200
]
]
],
'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"}]\' />
</div>
</aside>',
'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">
<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'
]
];
}
}