PortableInfobox/tests/PortableInfoboxRenderServiceTest.php

650 lines
22 KiB
PHP
Raw Normal View History

<?php
class PortableInfoboxRenderServiceTest extends WikiaBaseTest {
private $infoboxRenderService;
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
parent::setUp();
$mock = $this->getMock( 'PortableInfoboxRenderService', [ 'getThumbnailUrl', 'isWikiaMobile' ] );
2015-05-07 13:01:48 +00:00
$mock->expects( $this->any() )->method( 'getThumbnailUrl' )->will( $this->returnValue( 'http://image.jpg' ) );
2015-05-07 12:36:30 +00:00
$this->infoboxRenderService = $mock;
}
2015-07-01 14:34:34 +00:00
private function setWikiaMobileSkin( $bool ) {
$this->infoboxRenderService->expects( $this->any() )->method( 'isWikiaMobile' )->will( $this->returnValue
2015-07-01 14:34:34 +00:00
( $bool ) );
}
/**
* @param $input
* @param $expectedOutput
* @param $description
* @dataProvider testRenderInfoboxDataProvider
*/
public function testRenderInfobox( $input, $expectedOutput, $description ) {
2015-07-01 14:34:34 +00:00
$this->setWikiaMobileSkin( false );
2015-05-07 12:36:30 +00:00
$actualOutput = $this->infoboxRenderService->renderInfobox( $input );
$actualDOM = new DOMDocument('1.0');
$expectedDOM = new DOMDocument('1.0');
$actualDOM->formatOutput = true;
$actualDOM->preserveWhiteSpace = false;
$expectedDOM->formatOutput = true;
$expectedDOM->preserveWhiteSpace = false;
$actualDOM->loadXML($actualOutput);
$expectedDOM->loadXML($expectedOutput);
2015-05-04 14:56:39 +00:00
$expectedHtml = $expectedDOM->saveXML();
$actualHtml = $actualDOM->saveXML();
2015-05-04 14:16:35 +00:00
$this->assertEquals( $expectedHtml, $actualHtml, $description );
}
/**
* @param $input
* @param $expectedOutput
* @param $description
2015-07-01 14:34:34 +00:00
* @dataProvider testRenderMobileInfoboxDataProvider
*/
public function testRenderMobileInfobox( $input, $expectedOutput, $description ) {
2015-07-01 14:34:34 +00:00
$this->setWikiaMobileSkin( true );
$actualOutput = $this->infoboxRenderService->renderInfobox( $input );
$actualDOM = new DOMDocument('1.0');
$expectedDOM = new DOMDocument('1.0');
$actualDOM->formatOutput = true;
$actualDOM->preserveWhiteSpace = false;
$expectedDOM->formatOutput = true;
$expectedDOM->preserveWhiteSpace = false;
$actualDOM->loadXML($actualOutput);
$expectedDOM->loadXML($expectedOutput);
$expectedHtml = $expectedDOM->saveXML();
$actualHtml = $actualDOM->saveXML();
$this->assertEquals( $expectedHtml, $actualHtml, $description );
}
public function testRenderMobileInfoboxDataProvider() {
2015-07-01 14:34:34 +00:00
return [
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'caption' => 'image caption',
'alt' => 'image alt',
'url' => 'http://image.jpg',
'thumbnail' => 'thumbnail.jpg',
'ref' => 1,
'name' => 'test1'
2015-07-01 14:34:34 +00:00
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'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>
<h3 class="portable-infobox-hero-caption">image caption</h3>
</hgroup>
<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-07-01 14:34:34 +00:00
</div>
<div class="portable-infobox-item item-type-key-val portable-infobox-item-margins">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
</aside>',
'description' => 'Mobile Hero infobox with title, image and key-value pair'
],
[
'input' => [
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'caption' => 'image caption',
'alt' => 'image alt',
'url' => 'http://image.jpg',
'thumbnail' => 'thumbnail.jpg',
'ref' => 1,
'name' => 'test1'
]
],
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
],
'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>
<h3 class="portable-infobox-hero-caption">image caption</h3>
</hgroup>
<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>
<div class="portable-infobox-item item-type-key-val portable-infobox-item-margins">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
</aside>',
'description' => 'Mobile Hero infobox with title, image and key-value pair - reversed (check if hero if
data is properly extracted from infobox data'
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'caption' => 'image caption',
'url' => 'http://image.jpg',
'ref' => 1,
'name' => 'test1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
],
[
'type' => 'title',
'data' => [
'value' => 'Test Title 2'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt 2',
'caption' => 'image caption 2',
'url' => 'http://image2.jpg',
'ref' => 2,
'name' => 'test2'
]
],
],
'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>
<h3 class="portable-infobox-hero-caption">image caption</h3>
</hgroup>
<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>
<div class="portable-infobox-item item-type-key-val portable-infobox-item-margins">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
<div class="portable-infobox-item item-type-title portable-infobox-item-margins">
<h2 class="portable-infobox-title">Test Title 2</h2>
</div>
<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 2" data-image-key="test2" data-image-name="test2" data-ref="2" data-params=\'[{"name":"test2", "full":"http://image2.jpg"}]\' />
</div>
</aside>',
'description' => 'Mobile Hero infobox with 2 titles, 2 images and key-value pair'
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'alt' => 'image alt',
'url' => 'http://image.jpg',
'thumbnail' => 'thumbnail.jpg',
'ref' => 1,
'name' => 'test1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'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>
<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>
<div class="portable-infobox-item item-type-key-val portable-infobox-item-margins">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
</aside>',
'description' => 'Mobile Hero infobox without image caption'
],
[
'input' => [
[
'type' => 'title',
'data' => [
'value' => 'Test Title'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'caption' => 'image caption',
'url' => 'http://image.jpg',
'ref' => 1,
'name' => 'test1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
],
[
'type' => 'title',
'data' => [
'value' => 'Test Title 2'
]
],
[
'type' => 'image',
'data' => [
'alt' => 'image alt 2',
'caption' => 'image caption 2',
'url' => 'http://image2.jpg',
'ref' => 2,
'name' => 'test2'
]
],
],
'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>
<h3 class="portable-infobox-hero-caption">image caption</h3>
</hgroup>
<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>
<div class="portable-infobox-item item-type-key-val portable-infobox-item-margins">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
<div class="portable-infobox-item item-type-title portable-infobox-item-margins">
<h2 class="portable-infobox-title">Test Title 2</h2>
</div>
<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 2" data-image-key="test2" data-image-name="test2" data-ref="2" data-params=\'[{"name":"test2", "full":"http://image2.jpg"}]\' />
</div>
</aside>',
'description' => 'Mobile Hero infobox with 2 titles, 2 images 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">
<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">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
</aside>',
'description' => 'Mobile Hero infobox without image'
],
[
'input' => [
[
'type' => 'image',
'data' => [
'alt' => 'image alt',
'caption' => 'image caption',
'alt' => 'image alt',
'url' => 'http://image.jpg',
'thumbnail' => 'thumbnail.jpg',
'ref' => 1,
'name' => 'test1'
]
],
[
'type' => 'data',
'data' => [
'label' => 'test label',
'value' => 'test value'
]
]
],
'output' => '<aside class="portable-infobox">
<div class="portable-infobox-item item-type-hero">
<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>
<div class="portable-infobox-item item-type-key-val portable-infobox-item-margins">
<h3 class="portable-infobox-item-label portable-infobox-header-font">test label</h3>
<div class="portable-infobox-item-value">test value</div>
</div>
</aside>',
'description' => 'Mobile Hero infobox without title'
2015-07-01 14:34:34 +00:00
],
];
}
public function testRenderInfoboxDataProvider() {
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">
<img src="http://image.jpg" class="portable-infobox-image" alt="image alt" 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'
],
2015-05-04 14:16:35 +00:00
[
'input' => [
[
'type' => 'navigation',
2015-05-04 14:16:35 +00:00
'data' => [
'value' => 'navigation value',
2015-05-04 14:16:35 +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-05-07 12:36:30 +00:00
</aside>',
'description' => 'navigation only'
2015-05-04 14:16:35 +00:00
],
2015-05-04 14:30:40 +00:00
[
'input' => [
[
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
]
],
'output' => '<aside class="portable-infobox">
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-05-04 14:30:40 +00:00
'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' => '<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">
2015-05-21 11:06:54 +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">
<img src="http://image.jpg" class="portable-infobox-image" alt="image alt" data-image-key="" data-image-name=""/>
</a>
</figure>
</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>',
'description' => 'Simple infobox with title, 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'
]
];
}
}