Add test for image renderer

[MAILPOET-2086]
This commit is contained in:
Rostislav Wolny
2019-05-16 16:14:15 +02:00
committed by M. Shull
parent 48a6a842da
commit 9a2101f13f
2 changed files with 46 additions and 4 deletions

View File

@@ -31,11 +31,10 @@ class Image {
}
$image_template = '
<img src="' . EHelper::escapeHtmlLinkAttr($element['src']) . '"
width="' . EHelper::escapeHtmlAttr($element['width']) . '" alt="' . EHelper::escapeHtmlAttr($element['alt']) . '"' . $style . '/>
<img src="' . EHelper::escapeHtmlLinkAttr($element['src']) . '" width="' . EHelper::escapeHtmlAttr($element['width']) . '" alt="' . EHelper::escapeHtmlAttr($element['alt']) . '"' . $style . '/>
';
if (!empty($element['link'])) {
$image_template = '<a href="' . EHelper::escapeHtmlLinkAttr($element['link']) . '">' . $image_template . '</a>';
$image_template = '<a href="' . EHelper::escapeHtmlLinkAttr($element['link']) . '">' . trim($image_template) . '</a>';
}
$align = 'center';
if (!empty($element['styles']['block']['textAlign']) && in_array($element['styles']['block']['textAlign'], ['left', 'right'])) {
@@ -45,7 +44,7 @@ class Image {
$template = '
<tr>
<td class="mailpoet_image ' . (($element['fullWidth'] === false) ? 'mailpoet_padded_vertical mailpoet_padded_side' : '') . '" align="' . EHelper::escapeHtmlAttr($align) . '" valign="top">
' . $image_template . '
' . trim($image_template) . '
</td>
</tr>';
return $template;

View File

@@ -0,0 +1,43 @@
<?php
namespace MailPoet\Newsletter\Renderer\Blocks;
class ImageTest extends \MailPoetUnitTest {
private $block = [
'type' => 'image',
'link' => 'http://example.com',
'src' => 'http://mailpoet.localhost/image.jpg',
'alt' => 'Alt text',
'fullWidth' => false,
'width' => '310.015625px',
'height' => '390px',
'styles' => [
'block' => [
'textAlign' => 'center',
],
],
];
function testItRendersCorrectly() {
$output = Image::render($this->block, 200);
$expected_result = '
<tr>
<td class="mailpoet_image mailpoet_padded_vertical mailpoet_padded_side" align="center" valign="top">
<a href="http://example.com"><img src="http://mailpoet.localhost/image.jpg" width="160" alt="Alt text"/></a>
</td>
</tr>';
expect($output)->equals($expected_result);
}
function testItRendersWithoutLink() {
$this->block['link'] = null;
$output = Image::render($this->block, 200);
$expected_result = '
<tr>
<td class="mailpoet_image mailpoet_padded_vertical mailpoet_padded_side" align="center" valign="top">
<img src="http://mailpoet.localhost/image.jpg" width="160" alt="Alt text"/>
</td>
</tr>';
expect($output)->equals($expected_result);
}
}