diff --git a/lib/Newsletter/Renderer/Blocks/Image.php b/lib/Newsletter/Renderer/Blocks/Image.php index a8927d4cf9..f76bcbb27a 100644 --- a/lib/Newsletter/Renderer/Blocks/Image.php +++ b/lib/Newsletter/Renderer/Blocks/Image.php @@ -23,18 +23,18 @@ class Image { $column_width = ColumnsHelper::columnWidth($column_count); $padded_width = StylesHelper::$padding_width * 2; // scale image to fit column width - if($element['width'] > $column_width || - ($element['width'] < $column_width && $element['fullWidth'] === true) - ) { + if($element['width'] > $column_width) { $ratio = $element['width'] / $column_width; $element['width'] = $column_width; - $element['height'] = ceil($element['height'] / $ratio); + $element['height'] = (int) ceil($element['height'] / $ratio); } - // resize image if the image is padded and wider than column width - if($element['fullWidth'] === false && $element['width'] >= $column_width) { - $ratio = $element['width'] / ($element['width'] - $padded_width); - $element['width'] = $element['width'] - $padded_width; - $element['height'] = ceil($element['height'] / $ratio); + // resize image if the image is padded and wider than padded column width + if($element['fullWidth'] === false && + $element['width'] > ($column_width - $padded_width) + ) { + $ratio = $element['width'] / ($column_width - $padded_width); + $element['width'] = $column_width - $padded_width; + $element['height'] = (int) ceil($element['height'] / $ratio); } return $element; } diff --git a/tests/unit/Newsletter/RendererCest.php b/tests/unit/Newsletter/RendererCest.php index 59205f2e5a..b5fb631f06 100644 --- a/tests/unit/Newsletter/RendererCest.php +++ b/tests/unit/Newsletter/RendererCest.php @@ -24,110 +24,110 @@ class NewsletterRendererCest { } - function itRendersCompleteNewsletter() { - $template = $this->renderer->render(); - expect(isset($template['html']))->true(); - expect(isset($template['text']))->true(); - $DOM = $this->DOM_parser->parseStr($template['html']); - // we expect to have 7 columns: - // 1x column including header - // 2x column - // 3x column - // 1x footer - expect(count($DOM('.mailpoet_cols-one')))->equals(2); - expect(count($DOM('.mailpoet_cols-two')))->equals(2); - expect(count($DOM('.mailpoet_cols-three')))->equals(3); - } + function itRendersCompleteNewsletter() { + $template = $this->renderer->render(); + expect(isset($template['html']))->true(); + expect(isset($template['text']))->true(); + $DOM = $this->DOM_parser->parseStr($template['html']); + // we expect to have 7 columns: + // 1x column including header + // 2x column + // 3x column + // 1x footer + expect(count($DOM('.mailpoet_cols-one')))->equals(2); + expect(count($DOM('.mailpoet_cols-two')))->equals(2); + expect(count($DOM('.mailpoet_cols-three')))->equals(3); + } - function itRendersOneColumn() { - $column_content = array( - 'one' - ); - $column_styles = array( - 'block' => array( - 'backgroundColor' => "#999999" - ) - ); - $DOM = $this->DOM_parser->parseStr( - $this->column_renderer->render( - $column_styles, - count($column_content), - $column_content) - ); - foreach($DOM('table.mailpoet_cols-one > tbody') as $column) { - $rendered_column_content[] = trim($column->text()); - }; - expect($rendered_column_content)->equals($column_content); - } + function itRendersOneColumn() { + $column_content = array( + 'one' + ); + $column_styles = array( + 'block' => array( + 'backgroundColor' => "#999999" + ) + ); + $DOM = $this->DOM_parser->parseStr( + $this->column_renderer->render( + $column_styles, + count($column_content), + $column_content) + ); + foreach($DOM('table.mailpoet_cols-one > tbody') as $column) { + $rendered_column_content[] = trim($column->text()); + }; + expect($rendered_column_content)->equals($column_content); + } - function itRendersTwoColumns() { - $column_content = array( - 'one', - 'two' - ); - $column_styles = array( - 'block' => array( - 'backgroundColor' => "#999999" - ) - ); - $DOM = $this->DOM_parser->parseStr( - $this->column_renderer->render( - $column_styles, - count($column_content), - $column_content) - ); - foreach($DOM('table.mailpoet_cols-two > tbody') as $column) { - $rendered_column_content[] = trim($column->text()); - }; - expect($rendered_column_content)->equals($column_content); - } + function itRendersTwoColumns() { + $column_content = array( + 'one', + 'two' + ); + $column_styles = array( + 'block' => array( + 'backgroundColor' => "#999999" + ) + ); + $DOM = $this->DOM_parser->parseStr( + $this->column_renderer->render( + $column_styles, + count($column_content), + $column_content) + ); + foreach($DOM('table.mailpoet_cols-two > tbody') as $column) { + $rendered_column_content[] = trim($column->text()); + }; + expect($rendered_column_content)->equals($column_content); + } - function itRendersThreeColumns() { - $column_content = array( - 'one', - 'two', - 'three' - ); - $column_styles = array( - 'block' => array( - 'backgroundColor' => "#999999" - ) - ); - $DOM = $this->DOM_parser->parseStr( - $this->column_renderer->render( - $column_styles, - count($column_content), - $column_content) - ); - foreach($DOM('table.mailpoet_cols-three > tbody') as $column) { - $rendered_column_content[] = trim($column->text()); - }; - expect($rendered_column_content)->equals($column_content); - } + function itRendersThreeColumns() { + $column_content = array( + 'one', + 'two', + 'three' + ); + $column_styles = array( + 'block' => array( + 'backgroundColor' => "#999999" + ) + ); + $DOM = $this->DOM_parser->parseStr( + $this->column_renderer->render( + $column_styles, + count($column_content), + $column_content) + ); + foreach($DOM('table.mailpoet_cols-three > tbody') as $column) { + $rendered_column_content[] = trim($column->text()); + }; + expect($rendered_column_content)->equals($column_content); + } - function itRendersHeader() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][0]; - $DOM = $this->DOM_parser->parseStr(Header::render($template)); - // element should be proplerly nested, and styles should be applied - expect(!empty($DOM('tr > td.mailpoet_header', 0)->html()))->true(); - expect(!empty($DOM('tr > td > a', 0)->html()))->true(); - expect($DOM('a', 0)->attr('style'))->notEmpty(); - expect($DOM('td', 0)->attr('style'))->notEmpty(); - } + function itRendersHeader() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][0]; + $DOM = $this->DOM_parser->parseStr(Header::render($template)); + // element should be proplerly nested, and styles should be applied + expect(!empty($DOM('tr > td.mailpoet_header', 0)->html()))->true(); + expect(!empty($DOM('tr > td > a', 0)->html()))->true(); + expect($DOM('a', 0)->attr('style'))->notEmpty(); + expect($DOM('td', 0)->attr('style'))->notEmpty(); + } - function itRendersImage() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][1]; - $DOM = $this->DOM_parser->parseStr(Image::render($template, $columnCount = 1)); - // element should be properly nested, it's width set and style applied - expect($DOM('tr > td > img', 0)->attr('width'))->equals(620); - expect($DOM('tr > td > img', 0)->attr('style'))->notEmpty(); - } + function itRendersImage() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][1]; + $DOM = $this->DOM_parser->parseStr(Image::render($template, $columnCount = 1)); + // element should be properly nested, it's width set and style applied + expect($DOM('tr > td > img', 0)->attr('width'))->equals(620); + expect($DOM('tr > td > img', 0)->attr('style'))->notEmpty(); + } function itAdjustsImageDimensions() { - // image gets scaled down when image width > column width & it's not padded + // image gets scaled down when image width > column width $image = array( 'width' => 800, 'height' => 600, @@ -136,164 +136,156 @@ class NewsletterRendererCest { $new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1); expect($new_image_dimensions['width'])->equals(660); expect($new_image_dimensions['height'])->equals(495); - // image gets scaled up when image width < column width & it's not padded - $image['width'] = 100; - $new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1); - expect($new_image_dimensions['width'])->equals(660); // nothing happens when image width = column width - $image['width'] = 660; + $image['width'] = 661; $new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1); expect($new_image_dimensions['width'])->equals(660); - // image is reduced by 40px when it's padded + // nothing happens when image width < column width + $image['width'] = 659; + $new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1); + expect($new_image_dimensions['width'])->equals(659); + // image is reduced by 40px when it's width > padded column width + $image['width'] = 621; $image['fullWidth'] = false; $new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1); expect($new_image_dimensions['width'])->equals(620); + // nothing happens when image with < padded column width + $image['width'] = 619; + $new_image_dimensions = Image::adjustImageDimensions($image, $columnCount = 1); + expect($new_image_dimensions['width'])->equals(619); } - function itAdjustImageSizeBasedOnColumnWidth() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][1]; - $template['width'] = '800px'; - $DOM = $this->DOM_parser->parseStr(Image::render($template, $columnCount = 2)); - // 800px resized to 330px (2-column layout) and 40px padding applied - expect($DOM('tr > td > img', 0)->attr('width'))->equals(290); - $template['width'] = '280px'; - $DOM = $this->DOM_parser->parseStr(Image::render($template, $columnCount = 2)); - // 280px image should not be resized and padding should not be applied - expect($DOM('tr > td > img', 0)->attr('width'))->equals(280); - } + function itRendersText() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][2]; + $DOM = $this->DOM_parser->parseStr(Text::render($template)); + // blockquotes and paragraphs should be converted to spans and placed inside a table + expect( + !empty($DOM('tr > td > table > tr > td.mailpoet_paragraph', 0)->html()) + )->true(); + expect( + !empty($DOM('tr > td > table > tr > td.mailpoet_blockquote', 0)->html() + ))->true(); + // ul/ol/li should have mailpoet_paragraph class added & styles applied + expect( + !empty( + $DOM('tr > td > ul.mailpoet_paragraph > li.mailpoet_paragraph', 0)->html() + ) + )->true(); + expect( + !empty( + $DOM('tr > td > ol.mailpoet_paragraph > li.mailpoet_paragraph', 0)->html() + ) + )->true(); + expect($DOM('tr > td.mailpoet_text > ul.mailpoet_paragraph', 0)->attr('style')) + ->contains('padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;'); + // headings should be styled + expect($DOM('tr > td.mailpoet_text > h1', 0)->attr('style')) + ->contains('margin:0;font-style:normal;font-weight:normal;'); - function itRendersText() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][2]; - $DOM = $this->DOM_parser->parseStr(Text::render($template)); - // blockquotes and paragraphs should be converted to spans and placed inside a table - expect( - !empty($DOM('tr > td > table > tr > td.mailpoet_paragraph', 0)->html()) - )->true(); - expect( - !empty($DOM('tr > td > table > tr > td.mailpoet_blockquote', 0)->html() - ))->true(); - // ul/ol/li should have mailpoet_paragraph class added & styles applied - expect( - !empty( - $DOM('tr > td > ul.mailpoet_paragraph > li.mailpoet_paragraph', 0)->html() - ) - )->true(); - expect( - !empty( - $DOM('tr > td > ol.mailpoet_paragraph > li.mailpoet_paragraph', 0)->html() - ) - )->true(); - expect($DOM('tr > td.mailpoet_text > ul.mailpoet_paragraph', 0)->attr('style')) - ->contains('padding-top:0;padding-bottom:0;margin-top:0;margin-bottom:0;'); - // headings should be styled - expect($DOM('tr > td.mailpoet_text > h1', 0)->attr('style')) - ->contains('margin:0;font-style:normal;font-weight:normal;'); + } - } + function itRendersDivider() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][3]; + $DOM = $this->DOM_parser->parseStr(Divider::render($template)); + // element should be properly nested and its border-top-width set + expect( + preg_match( + '/border-top-width: 3px/', + $DOM('tr > td.mailpoet_divider > table > tr > td.mailpoet_divider-cell', 0)->attr('style') + ))->equals(1); + } - function itRendersDivider() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][3]; - $DOM = $this->DOM_parser->parseStr(Divider::render($template)); - // element should be properly nested and its border-top-width set - expect( - preg_match( - '/border-top-width: 3px/', - $DOM('tr > td.mailpoet_divider > table > tr > td.mailpoet_divider-cell', 0)->attr('style') - ))->equals(1); - } + function itRendersSpacer() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][4]; + $DOM = $this->DOM_parser->parseStr(Spacer::render($template)); + // element should be properly nested and its height set + expect($DOM('tr > td.mailpoet_spacer', 0)->attr('height'))->equals(50); + } - function itRendersSpacer() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][4]; - $DOM = $this->DOM_parser->parseStr(Spacer::render($template)); - // element should be properly nested and its height set - expect($DOM('tr > td.mailpoet_spacer', 0)->attr('height'))->equals(50); - } + function itRendersButton() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][5]; + $DOM = $this->DOM_parser->parseStr(Button::render($template)); + // element should be properly nested with arcsize/styles/fillcolor set + expect( + !empty($DOM('tr > td > div > table > tr > td > a.mailpoet_button', 0)->html()) + )->true(); + expect( + preg_match( + '/line-height: 30px/', + $DOM('a.mailpoet_button', 0)->attr('style')) + )->equals(1); + expect( + preg_match( + '/arcsize="' . round(20 / 30 * 100) . '%"/', + $DOM('tr > td > div > table > tr > td', 0)->text()) + )->equals(1); + expect( + preg_match( + '/style="height:30px.*?width:100px/', + $DOM('tr > td > div > table > tr > td', 0)->text()) + )->equals(1); + expect( + preg_match( + '/style="color:#ffffff.*?font-family:Arial.*?font-size:14px/', + $DOM('tr > td > div > table > tr > td', 0)->text()) + )->equals(1); + expect( + preg_match( + '/fillcolor="#666666/', + $DOM('tr > td > div > table > tr > td', 0)->text()) + )->equals(1); + } - function itRendersButton() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][5]; - $DOM = $this->DOM_parser->parseStr(Button::render($template)); - // element should be properly nested with arcsize/styles/fillcolor set - expect( - !empty($DOM('tr > td > div > table > tr > td > a.mailpoet_button', 0)->html()) - )->true(); - expect( - preg_match( - '/line-height: 30px/', - $DOM('a.mailpoet_button', 0)->attr('style')) - )->equals(1); - expect( - preg_match( - '/arcsize="' . round(20 / 30 * 100) . '%"/', - $DOM('tr > td > div > table > tr > td', 0)->text()) - )->equals(1); - expect( - preg_match( - '/style="height:30px.*?width:100px/', - $DOM('tr > td > div > table > tr > td', 0)->text()) - )->equals(1); - expect( - preg_match( - '/style="color:#ffffff.*?font-family:Arial.*?font-size:14px/', - $DOM('tr > td > div > table > tr > td', 0)->text()) - )->equals(1); - expect( - preg_match( - '/fillcolor="#666666/', - $DOM('tr > td > div > table > tr > td', 0)->text()) - )->equals(1); - } + function itRendersSocialIcons() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][6]; + $DOM = $this->DOM_parser->parseStr(Social::render($template)); + // element should be properly nested, contain social icons and + // image source/link href/alt should be properly set + expect(!empty($DOM('tr > td', 0)->html()))->true(); + expect($DOM('a', 0)->attr('href'))->equals('http://example.com'); + expect($DOM('td > a:nth-of-type(10) > img')->attr('src'))->contains('custom.png'); + expect($DOM('td > a:nth-of-type(10) > img')->attr('alt'))->equals('custom'); + // there should be 10 icons + expect(count($DOM('a > img')))->equals(10); + } - function itRendersSocialIcons() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][0]['blocks'][0]['blocks'][6]; - $DOM = $this->DOM_parser->parseStr(Social::render($template)); - // element should be properly nested, contain social icons and - // image source/link href/alt should be properly set - expect(!empty($DOM('tr > td', 0)->html()))->true(); - expect($DOM('a', 0)->attr('href'))->equals('http://example.com'); - expect($DOM('td > a:nth-of-type(10) > img')->attr('src'))->contains('custom.png'); - expect($DOM('td > a:nth-of-type(10) > img')->attr('alt'))->equals('custom'); - // there should be 10 icons - expect(count($DOM('a > img')))->equals(10); - } + function itRendersFooter() { + $newsletter = json_decode($this->newsletter['body'], true); + $template = $newsletter['content']['blocks'][3]['blocks'][0]['blocks'][0]; + $DOM = $this->DOM_parser->parseStr(Footer::render($template)); + // element should be properly nested, and styles should be applied + expect(!empty($DOM('tr > td.mailpoet_footer', 0)->html()))->true(); + expect(!empty($DOM('tr > td > a', 0)->html()))->true(); + expect($DOM('a', 0)->attr('style'))->notEmpty(); + expect($DOM('td', 0)->attr('style'))->notEmpty(); + } - function itRendersFooter() { - $newsletter = json_decode($this->newsletter['body'], true); - $template = $newsletter['content']['blocks'][3]['blocks'][0]['blocks'][0]; - $DOM = $this->DOM_parser->parseStr(Footer::render($template)); - // element should be properly nested, and styles should be applied - expect(!empty($DOM('tr > td.mailpoet_footer', 0)->html()))->true(); - expect(!empty($DOM('tr > td > a', 0)->html()))->true(); - expect($DOM('a', 0)->attr('style'))->notEmpty(); - expect($DOM('td', 0)->attr('style'))->notEmpty(); - } + function itSetsSubject() { + $template = $this->renderer->render(); + $DOM = $this->DOM_parser->parseStr($template['html']); + $subject = trim($DOM('title')->text()); + expect($subject)->equals($this->newsletter['subject']); + } - function itSetsSubject() { - $template = $this->renderer->render(); - $DOM = $this->DOM_parser->parseStr($template['html']); - $subject = trim($DOM('title')->text()); - expect($subject)->equals($this->newsletter['subject']); - } + function itSetsPreheader() { + $template = $this->renderer->render(); + $DOM = $this->DOM_parser->parseStr($template['html']); + $preheader = trim($DOM('td.mailpoet_preheader')->text()); + expect($preheader)->equals($this->newsletter['preheader']); + } - function itSetsPreheader() { - $template = $this->renderer->render(); - $DOM = $this->DOM_parser->parseStr($template['html']); - $preheader = trim($DOM('td.mailpoet_preheader')->text()); - expect($preheader)->equals($this->newsletter['preheader']); - } - - function itPostProcessesTemplate() { - $template = $this->renderer->render(); - // !important should be stripped from everywhere except from - // with the