Skip to content

Commit a6082b0

Browse files
author
Denys Rudchenko
committed
Merge branch 'MAGETWO-35744' into nord_develop
2 parents e98a487 + 5fe6f36 commit a6082b0

File tree

11 files changed

+594
-154
lines changed

11 files changed

+594
-154
lines changed

app/code/Magento/Newsletter/Block/Adminhtml/Queue/Preview.php

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@
1111
*/
1212
namespace Magento\Newsletter\Block\Adminhtml\Queue;
1313

14-
class Preview extends \Magento\Backend\Block\Widget
14+
class Preview extends \Magento\Newsletter\Block\Adminhtml\Template\Preview
1515
{
1616
/**
17-
* @var \Magento\Newsletter\Model\TemplateFactory
17+
* {@inheritdoc}
1818
*/
19-
protected $_templateFactory;
19+
protected $profilerName = "newsletter_queue_proccessing";
2020

2121
/**
2222
* @var \Magento\Newsletter\Model\QueueFactory
2323
*/
2424
protected $_queueFactory;
2525

26-
/**
27-
* @var \Magento\Newsletter\Model\SubscriberFactory
28-
*/
29-
protected $_subscriberFactory;
30-
3126
/**
3227
* @param \Magento\Backend\Block\Template\Context $context
3328
* @param \Magento\Newsletter\Model\TemplateFactory $templateFactory
@@ -42,64 +37,22 @@ public function __construct(
4237
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
4338
array $data = []
4439
) {
45-
$this->_templateFactory = $templateFactory;
4640
$this->_queueFactory = $queueFactory;
47-
$this->_subscriberFactory = $subscriberFactory;
48-
parent::__construct($context, $data);
41+
parent::__construct($context, $templateFactory, $subscriberFactory, $data);
4942
}
5043

5144
/**
52-
* Get html code
53-
*
54-
* @return string
45+
* @param \Magento\Newsletter\Model\Template $template
46+
* @param string $id
47+
* @return $this
5548
*/
56-
protected function _toHtml()
49+
protected function loadTemplate(\Magento\Newsletter\Model\Template $template, $id)
5750
{
58-
/* @var $template \Magento\Newsletter\Model\Template */
59-
$template = $this->_templateFactory->create();
60-
61-
if ($id = (int)$this->getRequest()->getParam('id')) {
62-
$queue = $this->_queueFactory->create()->load($id);
63-
$template->setTemplateType($queue->getNewsletterType());
64-
$template->setTemplateText($queue->getNewsletterText());
65-
$template->setTemplateStyles($queue->getNewsletterStyles());
66-
} else {
67-
$template->setTemplateType($this->getRequest()->getParam('type'));
68-
$template->setTemplateText($this->getRequest()->getParam('text'));
69-
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
70-
}
71-
72-
$storeId = (int)$this->getRequest()->getParam('store_id');
73-
if (!$storeId) {
74-
$defaultStore = $this->_storeManager->getDefaultStoreView();
75-
if (!$defaultStore) {
76-
$allStores = $this->_storeManager->getStores();
77-
if (isset($allStores[0])) {
78-
$defaultStore = $allStores[0];
79-
}
80-
}
81-
$storeId = $defaultStore ? $defaultStore->getId() : null;
82-
}
83-
84-
\Magento\Framework\Profiler::start("newsletter_queue_proccessing");
85-
$vars = [];
86-
87-
$vars['subscriber'] = $this->_subscriberFactory->create();
88-
89-
$template->emulateDesign($storeId);
90-
$templateProcessed = $this->_appState->emulateAreaCode(
91-
\Magento\Newsletter\Model\Template::DEFAULT_DESIGN_AREA,
92-
[$template, 'getProcessedTemplate'],
93-
[$vars, true]
94-
);
95-
$template->revertDesign();
96-
97-
if ($template->isPlain()) {
98-
$templateProcessed = "<pre>" . htmlspecialchars($templateProcessed) . "</pre>";
99-
}
100-
101-
\Magento\Framework\Profiler::stop("newsletter_queue_proccessing");
102-
103-
return $templateProcessed;
51+
/** @var \Magento\Newsletter\Model\Queue $queue */
52+
$queue = $this->_queueFactory->create()->load($id);
53+
$template->setTemplateType($queue->getNewsletterType());
54+
$template->setTemplateText($queue->getNewsletterText());
55+
$template->setTemplateStyles($queue->getNewsletterStyles());
56+
return $this;
10457
}
10558
}

app/code/Magento/Newsletter/Block/Adminhtml/Template/Preview.php

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
/**
88
* Newsletter template preview block
9-
*
10-
* @author Magento Core Team <core@magentocommerce.com>
119
*/
1210
namespace Magento\Newsletter\Block\Adminhtml\Template;
1311

1412
class Preview extends \Magento\Backend\Block\Widget
1513
{
14+
/**
15+
* Name for profiler
16+
*
17+
* @var string
18+
*/
19+
protected $profilerName = "newsletter_template_proccessing";
20+
1621
/**
1722
* @var \Magento\Newsletter\Model\TemplateFactory
1823
*/
@@ -51,34 +56,22 @@ protected function _toHtml()
5156
$template = $this->_templateFactory->create();
5257

5358
if ($id = (int)$this->getRequest()->getParam('id')) {
54-
$template->load($id);
59+
$this->loadTemplate($template, $id);
5560
} else {
5661
$template->setTemplateType($this->getRequest()->getParam('type'));
5762
$template->setTemplateText($this->getRequest()->getParam('text'));
5863
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
5964
}
6065

61-
$storeId = (int)$this->getRequest()->getParam('store_id');
62-
if (!$storeId) {
63-
$defaultStore = $this->_storeManager->getDefaultStoreView();
64-
if (!$defaultStore) {
65-
$allStores = $this->_storeManager->getStores();
66-
if (isset($allStores[0])) {
67-
$defaultStore = $allStores[0];
68-
}
69-
}
70-
$storeId = $defaultStore ? $defaultStore->getId() : null;
71-
}
72-
73-
\Magento\Framework\Profiler::start("newsletter_template_proccessing");
66+
\Magento\Framework\Profiler::start($this->profilerName);
7467
$vars = [];
7568

7669
$vars['subscriber'] = $this->_subscriberFactory->create();
7770
if ($this->getRequest()->getParam('subscriber')) {
7871
$vars['subscriber']->load($this->getRequest()->getParam('subscriber'));
7972
}
8073

81-
$template->emulateDesign($storeId);
74+
$template->emulateDesign($this->getStoreId());
8275
$templateProcessed = $this->_appState->emulateAreaCode(
8376
\Magento\Newsletter\Model\Template::DEFAULT_DESIGN_AREA,
8477
[$template, 'getProcessedTemplate'],
@@ -90,8 +83,42 @@ protected function _toHtml()
9083
$templateProcessed = "<pre>" . htmlspecialchars($templateProcessed) . "</pre>";
9184
}
9285

93-
\Magento\Framework\Profiler::stop("newsletter_template_proccessing");
86+
\Magento\Framework\Profiler::stop($this->profilerName);
9487

9588
return $templateProcessed;
9689
}
90+
91+
/**
92+
* Get Store Id from request or default
93+
*
94+
* @return int|null
95+
*/
96+
protected function getStoreId()
97+
{
98+
$storeId = (int)$this->getRequest()->getParam('store_id');
99+
if ($storeId) {
100+
return $storeId;
101+
}
102+
103+
$defaultStore = $this->_storeManager->getDefaultStoreView();
104+
if (!$defaultStore) {
105+
$allStores = $this->_storeManager->getStores();
106+
if (isset($allStores[0])) {
107+
$defaultStore = $allStores[0];
108+
}
109+
}
110+
111+
return $defaultStore ? $defaultStore->getId() : null;
112+
}
113+
114+
/**
115+
* @param \Magento\Newsletter\Model\Template $template
116+
* @param string $id
117+
* @return $this
118+
*/
119+
protected function loadTemplate(\Magento\Newsletter\Model\Template $template, $id)
120+
{
121+
$template->load($id);
122+
return $this;
123+
}
97124
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Newsletter\Test\Unit\Block\Adminhtml\Queue;
7+
8+
class PreviewTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
12+
*/
13+
protected $objectManager;
14+
15+
/**
16+
* @var \Magento\Newsletter\Model\Template
17+
*/
18+
protected $template;
19+
20+
/**
21+
* @var \Magento\Framework\App\RequestInterface
22+
*/
23+
protected $request;
24+
25+
/**
26+
* @var \Magento\Newsletter\Model\Queue
27+
*/
28+
protected $queue;
29+
30+
/**
31+
* @var \Magento\Store\Model\StoreManagerInterface
32+
*/
33+
protected $storeManager;
34+
35+
/**
36+
* @var \Magento\Newsletter\Block\Adminhtml\Queue\Preview
37+
*/
38+
protected $preview;
39+
40+
public function setUp()
41+
{
42+
$templateFactory = $this->getMock('Magento\Newsletter\Model\TemplateFactory', ['create'], [], '', false);
43+
$this->template = $this->getMock('Magento\Newsletter\Model\Template', [], [], '', false);
44+
$templateFactory->expects($this->once())->method('create')->will($this->returnValue($this->template));
45+
$queueFactory = $this->getMock('Magento\Newsletter\Model\QueueFactory', ['create'], [], '', false);
46+
$this->queue = $this->getMock('Magento\Newsletter\Model\Queue', ['load'], [], '', false);
47+
$queueFactory->expects($this->any())->method('create')->will($this->returnValue($this->queue));
48+
49+
$this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
50+
$this->storeManager = $this->getMock(
51+
'Magento\Store\Model\StoreManager',
52+
['getStores', 'getDefaultStoreView'],
53+
[],
54+
'',
55+
false
56+
);
57+
58+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59+
$this->preview = $this->objectManager->getObject(
60+
'Magento\Newsletter\Block\Adminhtml\Queue\Preview',
61+
[
62+
'templateFactory' => $templateFactory,
63+
'queueFactory' => $queueFactory,
64+
'request' => $this->request,
65+
'storeManager' => $this->storeManager
66+
]
67+
);
68+
}
69+
70+
public function testToHtmlEmpty()
71+
{
72+
/** @var \Magento\Store\Model\Store $store */
73+
$store = $this->getMock('Magento\Store\Model\Store', ['getId'], [], '', false);
74+
$this->storeManager->expects($this->once())->method('getDefaultStoreView')->will($this->returnValue($store));
75+
$result = $this->preview->toHtml();
76+
$this->assertEquals('', $result);
77+
}
78+
79+
public function testToHtmlWithId()
80+
{
81+
$this->request->expects($this->any())->method('getParam')->will($this->returnValueMap(
82+
[
83+
['id', null, 1],
84+
['store_id', null, 0]
85+
]
86+
));
87+
$this->queue->expects($this->once())->method('load')->will($this->returnSelf());
88+
$this->template->expects($this->any())->method('isPlain')->will($this->returnValue(true));
89+
/** @var \Magento\Store\Model\Store $store */
90+
$this->storeManager->expects($this->once())->method('getDefaultStoreView')->will($this->returnValue(null));
91+
$store = $this->getMock('Magento\Store\Model\Store', ['getId'], [], '', false);
92+
$this->storeManager->expects($this->once())->method('getStores')->will($this->returnValue([0 => $store]));
93+
$result = $this->preview->toHtml();
94+
$this->assertEquals('<pre></pre>', $result);
95+
}
96+
}

0 commit comments

Comments
 (0)