PortableInfobox/tests/PortableInfoboxRenderServiceTest.php

558 lines
17 KiB
PHP
Raw Normal View History

<?php
class PortableInfoboxRenderServiceTest extends WikiaBaseTest {
2015-07-29 11:51:55 +00:00
//todo: https://wikia-inc.atlassian.net/browse/DAT-3076
//todo: we are testing a lot of functionality and have issues with mocking
//todo: we should move all render service test to API tests
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
/**
2015-07-21 12:30:13 +00:00
* @param $input to check presence of some additional config fields. Possible fields:
* 'isInvalidImage' - bool - if getThumbnail should return false
* 'isWikiaMobile' - bool - if we want to test mobile env
* 'smallImageDimensions' - integer - size of small image (both width and height)
2015-07-22 13:28:39 +00:00
*
2015-07-10 12:31:43 +00:00
* @return PHPUnit_Framework_MockObject_MockObject
2015-07-10 14:57:57 +00:00
*/
2015-07-29 08:58:39 +00:00
private function mockInfoboxRenderServiceHelper( $input ) {
$isValidHeroDataItem = isset( $input[ 'isValidHeroDataItem' ] ) && $input[ 'isValidHeroDataItem' ];
2015-07-10 14:57:57 +00:00
$isWikiaMobile = isset( $input[ 'isWikiaMobile' ] ) && $input[ 'isWikiaMobile' ];
2015-07-29 08:58:39 +00:00
$createHorizontalGroupData = isset( $input[ 'createHorizontalGroupData' ] ) ?
$input[ 'createHorizontalGroupData' ] : null;
$extendImageData = isset( $input[ 'extendImageData' ] ) ? $input[ 'extendImageData' ] : null;
2015-07-22 13:28:39 +00:00
2015-07-29 08:58:39 +00:00
$mock = $this->getMockBuilder( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper' )
2015-07-29 11:51:55 +00:00
->setMethods( [ 'isValidHeroDataItem', 'validateType', 'isWikiaMobile',
2015-07-29 08:58:39 +00:00
'createHorizontalGroupData', 'extendImageData' ] )
2015-07-10 12:10:03 +00:00
->getMock();
2015-07-29 08:58:39 +00:00
$mock->expects( $this->any() )
->method( 'isValidHeroDataItem' )
->will( $this->returnValue( $isValidHeroDataItem ) );
$mock->expects( $this->any() )
->method( 'validateType' )
->will( $this->returnValue( true ) );
2015-07-10 12:10:03 +00:00
$mock->expects( $this->any() )
->method( 'isWikiaMobile' )
->will( $this->returnValue( $isWikiaMobile ) );
$mock->expects( $this->any() )
2015-07-29 08:58:39 +00:00
->method( 'createHorizontalGroupData' )
->will( $this->returnValue( $createHorizontalGroupData ) );
2015-07-20 09:52:32 +00:00
$mock->expects( $this->any() )
2015-07-29 08:58:39 +00:00
->method( 'extendImageData' )
->will( $this->returnValue( $extendImageData ) );
2015-07-29 08:58:39 +00:00
$this->mockClass( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper', $mock );
}
2015-07-13 08:56:19 +00:00
/**
* @param $html
* @return string
2015-07-22 13:28:39 +00:00
*/
2015-07-13 08:56:19 +00:00
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-29 08:58:39 +00:00
public function testRenderInfobox( $input, $expectedOutput, $description, $mockParams ) {
$this->mockInfoboxRenderServiceHelper( $mockParams );
$infoboxRenderService = new PortableInfoboxRenderService();
2015-07-10 14:57:57 +00:00
$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',
'name' => 'image',
'key' => 'image',
'caption' => 'Lorem ipsum dolor',
'isVideo' => false
]
]
],
'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-29 08:58:39 +00:00
<img src="http://thumbnail.jpg" class="portable-infobox-image" alt="image alt" width="400" height="200" data-image-key="image" data-image-name="image"/>
</a>
<figcaption class="portable-infobox-item-margins portable-infobox-image-caption">Lorem ipsum dolor</figcaption>
</figure>
</div>
</aside>',
2015-07-29 08:58:39 +00:00
'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',
'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' => '<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 video video-thumbnail small"
title="image alt">
<img src="http://thumbnail.jpg" class="portable-infobox-image"
alt="image alt" width="400" height="200" data-video-key="image"
data-video-name="image"/>
<span class="duration" itemprop="duration">1:20</span>
<span class="play-circle"></span>
</a>
<figcaption class="portable-infobox-item-margins portable-infobox-image-caption">Lorem ipsum dolor</figcaption>
</figure>
</div>
</aside>',
'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',
'media-type' => 'video',
'isVideo' => true,
'duration' => '1:20'
2015-07-29 08:58:39 +00:00
]
]
],
[
'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-29 08:58:39 +00:00
'url' => 'http://image.jpg',
'name' => 'image',
'key' => 'image',
'isVideo' => false
]
],
[
'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-07-29 08:58:39 +00:00
<a href="http://image.jpg" class="image image-thumbnail" title="image alt">
<img src="http://thumbnail.jpg" class="portable-infobox-image" alt="image alt" width="400" height="200" data-image-key="image" data-image-name="image"/>
</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-07-29 08:58:39 +00:00
'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',
'media-type' => 'image',
'isVideo' => false
2015-07-21 11:33:24 +00:00
]
2015-07-29 08:58:39 +00:00
]
2015-07-21 11:33:24 +00:00
],
[
'input' => [
[
'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>',
2015-07-21 11:33:24 +00:00
'description' => 'Infobox with title, group with header and two key-value pairs'
2015-05-04 14:30:40 +00:00
],
2015-06-11 17:28:49 +00:00
[
'input' => [
[
'type' => 'group',
'data' => [
'value' => [
[
'type' => 'header',
'data' => [
2015-07-29 08:58:39 +00:00
'value' => 'Test header'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
2015-06-11 17:28:49 +00:00
]
],
[
'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-29 08:58:39 +00:00
<table class="portable-infobox-horizontal-group-content">
<caption
class="portable-infobox-header portable-infobox-secondary-font portable-infobox-secondary-background">Test header</caption>
<thead>
<tr>
<th
class="portable-infobox-horizontal-group-item portable-infobox-item-label portable-infobox-secondary-font">test label</th>
<th
class="portable-infobox-horizontal-group-item portable-infobox-item-label portable-infobox-secondary-font">test label</th>
</tr>
</thead>
<tbody>
<tr>
<td class="portable-infobox-horizontal-group-item portable-infobox-item-value">test value</td>
<td class="portable-infobox-horizontal-group-item portable-infobox-item-value">test value</td>
</tr>
</tbody>
</table>
2015-06-11 17:28:49 +00:00
</section>
</aside>',
2015-07-29 08:58:39 +00:00
'description' => 'Infobox with title and horizontal group',
'mockParams' => [
'createHorizontalGroupData' => [
'header' => 'Test header',
'labels' => ['test label', 'test label'],
'values' => ['test value', 'test value'],
]
]
2015-06-11 17:28:49 +00:00
],
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' => [
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'url' => 'http://image.jpg',
'ref' => 1,
2015-07-29 08:58:39 +00:00
'name' => 'test1',
'key' => 'test1',
'isVideo' => false
]
]
],
'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>',
2015-07-29 08:58:39 +00:00
'description' => 'Mobile: Only image. Image is not small- should render hero.',
'mockParams' => [
2015-07-29 11:51:55 +00:00
'isWikiaMobile' => true,
2015-07-29 08:58:39 +00:00
'isValidHeroDataItem' => true,
'extendImageData' => [
'alt' => 'image alt',
'url' => 'http://image.jpg',
'name' => 'test1',
'key' => 'test1',
'ref' => 1,
'width' => '400',
'height' => '200',
'thumbnail' => 'http://image.jpg',
'media-type' => 'image',
'isVideo' => false
]
]
],
2015-07-10 14:57:57 +00:00
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test <img /><a href="example.com">Title</a>'
2015-07-10 14:57:57 +00:00
]
],
[
'type' => 'image',
'data' => [
'url' => 'http://image.jpg',
2015-07-29 08:58:39 +00:00
'name' => 'test1',
'key' => 'test1',
'ref' => 44,
'isVideo' => false
]
2015-07-10 14:57:57 +00:00
]
],
'output' => '<aside class="portable-infobox">
<div class="portable-infobox-item item-type-hero">
<hgroup class="portable-infobox-hero-title-wrapper portable-infobox-item-margins">
<h2 class="portable-infobox-hero-title">Test Title</h2>
</hgroup>
2015-07-29 08:58:39 +00:00
<img src="data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D" data-src="thumbnail.jpg" class="portable-infobox-image lazy media article-media" alt="" data-image-key="test1" data-image-name="test1" data-ref="44" data-params=\'[{"name":"test1", "full":"http://image.jpg"}]\'/>
</div>
</aside>',
2015-07-29 08:58:39 +00:00
'description' => 'Mobile: Infobox with full hero module with title with HTML tags',
'mockParams' => [
'isValidHeroDataItem' => true,
'isWikiaMobile' => true,
'extendImageData' => [
'url' => 'http://image.jpg',
'name' => 'test1',
'key' => 'test1',
'ref' => 44,
'width' => '400',
'height' => '200',
'thumbnail' => 'thumbnail.jpg',
'isVideo' => false,
'media-type' => 'image'
2015-07-29 08:58:39 +00:00
]
]
]
];
}
}