Skip to content

Commit

Permalink
Merge pull request #565 from magento-nord/NORD_BUGS
Browse files Browse the repository at this point in the history
[NORD] Bug fixes
  • Loading branch information
Onischenko, Yaroslav(yonischenko) committed Apr 28, 2016
2 parents 6378276 + 8b6283e commit 79bcd91
Show file tree
Hide file tree
Showing 25 changed files with 402 additions and 105 deletions.
Expand Up @@ -122,21 +122,40 @@ public function getAddImagesButton()
*/
public function getImagesJson()
{
if (is_array($this->getElement()->getImages())) {
$value = $this->getElement()->getImages();
$value = $this->getElement()->getImages();
if (is_array($value) &&
array_key_exists('images', $value) &&
is_array($value['images']) &&
count($value['images'])
) {
$directory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
if (is_array($value['images']) && count($value['images']) > 0) {
foreach ($value['images'] as &$image) {
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
$fileHandler = $directory->stat($this->_mediaConfig->getMediaPath($image['file']));
$image['size'] = $fileHandler['size'];
}
return $this->_jsonEncoder->encode($value['images']);
$images = $this->sortImagesByPosition($value['images']);
foreach ($images as &$image) {
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
$fileHandler = $directory->stat($this->_mediaConfig->getMediaPath($image['file']));
$image['size'] = $fileHandler['size'];
}
return $this->_jsonEncoder->encode($images);
}
return '[]';
}

/**
* Sort images array by position key
*
* @param array $images
* @return array
*/
private function sortImagesByPosition($images)
{
if (is_array($images)) {
usort($images, function ($imageA, $imageB) {
return ($imageA['position'] < $imageB['position']) ? -1 : 1;
});
}
return $images;
}

/**
* @return string
*/
Expand Down
Expand Up @@ -82,37 +82,59 @@ public function testGetImagesJson()
['file_1.jpg', 'catalog/product/image_1.jpg'],
['file_2.jpg', 'catalog/product/image_2.jpg']
];
// @codingStandardsIgnoreStart
$encodedString = '[{"value_id":"1","file":"image_1.jpg","media_type":"image","url":"http:\/\/magento2.dev\/pub\/media\/catalog\/product\/image_1.jpg","size":879394},{"value_id":"2","file":"image_2.jpg","media_type":"image","url":"http:\/\/magento2.dev\/pub\/media\/catalog\/product\/image`_2.jpg","size":399659}]';
// @codingStandardsIgnoreEnd

$sizeMap = [
['catalog/product/image_1.jpg', ['size' => 399659]],
['catalog/product/image_2.jpg', ['size' => 879394]]
];

$imagesResult = [
[
'value_id' => '2',
'file' => 'file_2.jpg',
'media_type' => 'image',
'position' => '0',
'url' => 'url_to_the_image/image_2.jpg',
'size' => 879394
],
[
'value_id' => '1',
'file' => 'file_1.jpg',
'media_type' => 'image',
'position' => '1',
'url' => 'url_to_the_image/image_1.jpg',
'size' => 399659
]
];

$images = [
'images' => [
[
'value_id' => '1',
'file' => 'file_1.jpg',
'media_type' => 'image',
'position' => '1'
] ,
[
'value_id' => '2',
'file' => 'file_2.jpg',
'media_type' => 'image',
'position' => '0'
]
]
];
$firstStat = ['size' => 879394];
$secondStat = ['size' => 399659];

$this->content->setElement($this->galleryMock);
$this->galleryMock->expects($this->any())->method('getImages')->willReturn($images);
$this->galleryMock->expects($this->once())->method('getImages')->willReturn($images);
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')->willReturn($this->readMock);

$this->mediaConfigMock->expects($this->any())->method('getMediaUrl')->willReturnMap($url);
$this->mediaConfigMock->expects($this->any())->method('getMediaPath')->willReturn($mediaPath);
$this->mediaConfigMock->expects($this->any())->method('getMediaPath')->willReturnMap($mediaPath);

$this->readMock->expects($this->at(0))->method('stat')->willReturn($firstStat);
$this->readMock->expects($this->at(1))->method('stat')->willReturn($secondStat);
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($encodedString);
$this->readMock->expects($this->any())->method('stat')->willReturnMap($sizeMap);
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');

$this->assertSame($encodedString, $this->content->getImagesJson());
$this->assertSame(json_encode($imagesResult), $this->content->getImagesJson());
}

public function testGetImagesJsonWithoutImages()
Expand Down
Expand Up @@ -1270,7 +1270,7 @@ protected function _saveProductCategories(array $categoriesData)
);
}
if ($categoriesIn) {
$this->_connection->insertOnDuplicate($tableName, $categoriesIn, ['position']);
$this->_connection->insertOnDuplicate($tableName, $categoriesIn, ['product_id', 'category_id']);
}
}
return $this;
Expand Down
7 changes: 6 additions & 1 deletion app/code/Magento/Newsletter/Block/Adminhtml/Queue/Edit.php
Expand Up @@ -161,7 +161,12 @@ protected function _prepareLayout()
*/
public function getPreviewUrl()
{
return $this->getUrl('*/*/preview');
if ($this->getTemplateId()) {
$params = ['template_id' => $this->getTemplateId()];
} else {
$params = ['id' => $this->getRequest()->getParam('id')];
}
return $this->getUrl('*/*/preview', $params);
}

/**
Expand Down
Expand Up @@ -35,6 +35,10 @@ protected function _prepareForm()
if ($data = $this->getFormData()) {
$mapper = ['preview_store_id' => 'store_id'];

if (empty($data['id']) && !empty($data['text'])) {
$this->_backendSession->setPreviewData($data);
}

foreach ($data as $key => $value) {
if (array_key_exists($key, $mapper)) {
$name = $mapper[$key];
Expand Down
Expand Up @@ -238,7 +238,7 @@ public function getSaveUrl()
*/
public function getPreviewUrl()
{
return $this->getUrl('*/*/preview');
return $this->getUrl('*/*/preview', ['id' => $this->getRequest()->getParam('id')]);
}

/**
Expand Down
34 changes: 31 additions & 3 deletions app/code/Magento/Newsletter/Block/Adminhtml/Template/Preview.php
Expand Up @@ -58,9 +58,11 @@ protected function _toHtml()
if ($id = (int)$this->getRequest()->getParam('id')) {
$this->loadTemplate($template, $id);
} else {
$template->setTemplateType($this->getRequest()->getParam('type'));
$template->setTemplateText($this->getRequest()->getParam('text'));
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
$previewData = $this->getPreviewData();

$template->setTemplateType($previewData['type']);
$template->setTemplateText($previewData['text']);
$template->setTemplateStyles($previewData['styles']);
}

\Magento\Framework\Profiler::start($this->profilerName);
Expand Down Expand Up @@ -88,6 +90,32 @@ protected function _toHtml()
return $templateProcessed;
}

/**
* Return template preview data
*
* @return array
*/
private function getPreviewData()
{
$previewData = [];
$previewParams = ['type', 'text', 'styles'];

$sessionData = [];
if ($this->_backendSession->hasPreviewData()) {
$sessionData = $this->_backendSession->getPreviewData();
}

foreach ($previewParams as $param) {
if (isset($sessionData[$param])) {
$previewData[$param] = $sessionData[$param];
} else {
$previewData[$param] = $this->getRequest()->getParam($param);
}
}

return $previewData;
}

/**
* Get Store Id from request or default
*
Expand Down
Expand Up @@ -35,6 +35,10 @@ protected function _prepareForm()
if ($data = $this->getFormData()) {
$mapper = ['preview_store_id' => 'store_id'];

if (empty($data['id']) && !empty($data['text'])) {
$this->_backendSession->setPreviewData($data);
}

foreach ($data as $key => $value) {
if (array_key_exists($key, $mapper)) {
$name = $mapper[$key];
Expand Down
Expand Up @@ -17,7 +17,11 @@ public function execute()
{
$this->_view->loadLayout();
$data = $this->getRequest()->getParams();
if (empty($data) || !isset($data['id'])) {

$isEmptyRequestData = empty($data) || !isset($data['id']);
$isEmptyPreviewData = !$this->_getSession()->hasPreviewData() || empty($this->_getSession()->getPreviewData());

if ($isEmptyRequestData && $isEmptyPreviewData) {
$this->_forward('noroute');
return;
}
Expand Down
Expand Up @@ -82,6 +82,7 @@ public function execute()

$this->messageManager->addSuccess(__('You saved the newsletter queue.'));
$this->_getSession()->setFormData(false);
$this->_getSession()->unsPreviewData();

$this->_redirect('*/*');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
Expand Down
Expand Up @@ -18,7 +18,10 @@ public function execute()
$this->_view->loadLayout();

$data = $this->getRequest()->getParams();
if (empty($data) || !isset($data['id'])) {
$isEmptyRequestData = empty($data) || !isset($data['id']);
$isEmptyPreviewData = !$this->_getSession()->hasPreviewData() || empty($this->_getSession()->getPreviewData());

if ($isEmptyRequestData && $isEmptyPreviewData) {
$this->_forward('noroute');
return $this;
}
Expand Down
Expand Up @@ -63,7 +63,7 @@ public function execute()

$this->messageManager->addSuccess(__('The newsletter template has been saved.'));
$this->_getSession()->setFormData(false);

$this->_getSession()->unsPreviewData();
$this->_redirect('*/template');
return;
} catch (LocalizedException $e) {
Expand Down
Expand Up @@ -62,6 +62,12 @@ protected function setUp()
$appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$context->expects($this->once())->method('getAppState')->will($this->returnValue($appState));

$backendSession = $this->getMockBuilder('Magento\Backend\Model\Session')
->disableOriginalConstructor()
->getMock();

$context->expects($this->once())->method('getBackendSession')->willReturn($backendSession);

$templateFactory = $this->getMock('Magento\Newsletter\Model\TemplateFactory', ['create'], [], '', false);
$this->template = $this->getMock('Magento\Newsletter\Model\Template', [], [], '', false);
$templateFactory->expects($this->once())->method('create')->will($this->returnValue($this->template));
Expand Down
Expand Up @@ -10,6 +10,7 @@
<attribute name="id" value="html-body"/>
<attribute name="class" value="preview-window"/>
<referenceContainer name="backend.page" remove="true"/>
<referenceContainer name="menu.wrapper" remove="true"/>
<referenceContainer name="root">
<block name="preview.page.content" class="Magento\Backend\Block\Page" template="Magento_Newsletter::preview/iframeswitcher.phtml">
<block class="Magento\Newsletter\Block\Adminhtml\Queue\Preview\Form" name="preview_form"/>
Expand Down
Expand Up @@ -10,6 +10,7 @@
<attribute name="id" value="html-body"/>
<attribute name="class" value="preview-window"/>
<referenceContainer name="backend.page" remove="true"/>
<referenceContainer name="menu.wrapper" remove="true"/>
<referenceContainer name="root">
<block name="preview.page.content" class="Magento\Backend\Block\Page" template="Magento_Newsletter::preview/iframeswitcher.phtml">
<block class="Magento\Newsletter\Block\Adminhtml\Template\Preview\Form" name="preview_form"/>
Expand Down
Expand Up @@ -42,8 +42,8 @@ function unBlockPreview() {
jQuery('body').trigger('hideLoadingPopup');
}

Event.observe(window, 'load', preview);
Event.observe(previewIframe, 'load', unBlockPreview);
jQuery(document).ready(preview);
jQuery(previewIframe).ready(unBlockPreview);

jQuery("#preview_iframe").load(function() {
jQuery(this).height(jQuery(this).contents().find("html").height() );
Expand Down
Expand Up @@ -249,11 +249,7 @@ define([
tmpVideoData.videoUrl = tmpInputData.videoUrl;
}

if (tmpVideoData.isBase) {
videoData.unshift(tmpVideoData);
} else {
videoData.push(tmpVideoData);
}
videoData.push(tmpVideoData);
}

return videoData;
Expand Down

0 comments on commit 79bcd91

Please sign in to comment.