Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/develop' into PR_Branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mazhalai committed Apr 2, 2016
2 parents 4733c43 + d447fa6 commit b2c8564
Show file tree
Hide file tree
Showing 109 changed files with 2,466 additions and 1,352 deletions.
27 changes: 27 additions & 0 deletions app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Request\DataPersistorInterface;

/**
* Class Save
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Save extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
Expand Down Expand Up @@ -37,6 +42,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product
*/
protected $productRepository;

/**
* @var DataPersistorInterface
*/
protected $dataPersistor;

/**
* @var StoreManagerInterface
*/
Expand Down Expand Up @@ -110,6 +120,7 @@ public function execute()
$this->copyToStores($data, $productId);

$this->messageManager->addSuccess(__('You saved the product.'));
$this->getDataPersistor()->clear('catalog_product');
if ($product->getSku() != $originalSku) {
$this->messageManager->addNotice(
__(
Expand All @@ -131,11 +142,13 @@ public function execute()
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
$this->_session->setProductData($data);
$this->getDataPersistor()->set('catalog_product', $data);
$redirectBack = $productId ? true : 'new';
} catch (\Exception $e) {
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
$this->messageManager->addError($e->getMessage());
$this->_session->setProductData($data);
$this->getDataPersistor()->set('catalog_product', $data);
$redirectBack = $productId ? true : 'new';
}
} else {
Expand Down Expand Up @@ -246,4 +259,18 @@ private function getStoreManager()
}
return $this->storeManager;
}

/**
* Retrieve data persistor
*
* @return DataPersistorInterface|mixed
*/
protected function getDataPersistor()
{
if (null === $this->dataPersistor) {
$this->dataPersistor = $this->_objectManager->get(DataPersistorInterface::class);
}

return $this->dataPersistor;
}
}
22 changes: 21 additions & 1 deletion app/code/Magento/Catalog/Model/ImageExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,32 @@ public function process(\DOMElement $mediaNode, $mediaParentTag)
if ($attribute->nodeType != XML_ELEMENT_NODE) {
continue;
}
$nodeValue = $attribute->nodeValue;
if ($attribute->tagName == 'background') {
$nodeValue = $this->processImageBackground($attribute->nodeValue);
} else {
$nodeValue = $attribute->nodeValue;
}
$result[$mediaParentTag][$moduleNameImage][Image::MEDIA_TYPE_CONFIG_NODE][$imageId][$attribute->tagName]
= $nodeValue;
}
}

return $result;
}

/**
* Convert rgb background string into array
*
* @param string $backgroundString
* @return int[]
*/
private function processImageBackground($backgroundString)
{
$pattern = '#\[(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\]#';
$backgroundArray = [];
if (preg_match($pattern, $backgroundString, $backgroundArray)) {
array_shift($backgroundArray);
}
return $backgroundArray;
}
}
41 changes: 35 additions & 6 deletions app/code/Magento/Catalog/Model/Locator/RegistryLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
namespace Magento\Catalog\Model\Locator;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Registry;
use Magento\Store\Api\Data\StoreInterface;

/**
* Class RegistryLocator
Expand All @@ -15,7 +18,17 @@ class RegistryLocator implements LocatorInterface
/**
* @var Registry
*/
protected $registry;
private $registry;

/**
* @var ProductInterface
*/
private $product;

/**
* @var StoreInterface
*/
private $store;

/**
* @param Registry $registry
Expand All @@ -27,30 +40,46 @@ public function __construct(Registry $registry)

/**
* {@inheritdoc}
* @throws NotFoundException
*/
public function getProduct()
{
return $this->registry->registry('current_product');
if (null !== $this->product) {
return $this->product;
}

if ($product = $this->registry->registry('current_product')) {
return $this->product = $product;
}

throw new NotFoundException(__('Product was not registered'));
}

/**
* {@inheritdoc}
* @throws NotFoundException
*/
public function getStore()
{
return $this->registry->registry('current_store');
}
if (null !== $this->store) {
return $this->store;
}

if ($store = $this->registry->registry('current_store')) {
return $this->store = $store;
}

throw new NotFoundException(__('Store was not registered'));
}

/**
* {@inheritdoc}
*/
public function getWebsiteIds()
{
return $this->registry->registry('current_product')->getWebsiteIds();
return $this->getProduct()->getWebsiteIds();
}


/**
* {@inheritdoc}
*/
Expand Down
40 changes: 40 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/ImageExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Test\Unit\Model;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class ImageExtractorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Catalog\Model\ImageExtractor
*/
private $model;

protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->model = $objectManager->getObject(\Magento\Catalog\Model\ImageExtractor::class);
}

public function testProcess()
{
$expectedArray = include(__DIR__ . '/_files/converted_view.php');
$this->assertEquals($expectedArray, $this->model->process($this->getDomElement(), 'media'));
}

/**
* Get mocked dom element
*
* @return \DOMElement
*/
private function getDomElement()
{
$doc = new \DOMDocument();
$doc->load(__DIR__ . '/_files/valid_view.xml');
return $doc->getElementsByTagName('images')->item(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Model\Locator\RegistryLocator;
use Magento\Framework\Registry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Api\Data\StoreInterface;

/**
* Class RegistryLocatorTest
Expand All @@ -26,36 +27,75 @@ class RegistryLocatorTest extends \PHPUnit_Framework_TestCase
protected $model;

/**
* @var Registry
* @var Registry|\PHPUnit_Framework_MockObject_MockObject
*/
protected $registryMock;

/**
* @var ProductInterface
* @var ProductInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productMock;

/**
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeMock;

protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->registryMock = $this->getMockBuilder('Magento\Framework\Registry')
$this->registryMock = $this->getMockBuilder(Registry::class)
->setMethods(['registry'])
->getMock();
$this->productMock = $this->getMockBuilder('Magento\Catalog\Api\Data\ProductInterface')
$this->productMock = $this->getMockBuilder(ProductInterface::class)
->getMockForAbstractClass();
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->getMockForAbstractClass();

$this->model = $this->objectManager->getObject(RegistryLocator::class, [
'registry' => $this->registryMock,
]);
}

public function testGetProduct()
{
$this->registryMock->expects($this->once())
->method('registry')
->with('current_product')
->willReturn($this->productMock);

$this->model = $this->objectManager->getObject('Magento\Catalog\Model\Locator\RegistryLocator', [
'registry' => $this->registryMock,
]);
$this->assertInstanceOf(ProductInterface::class, $this->model->getProduct());
// Lazy loading
$this->assertInstanceOf(ProductInterface::class, $this->model->getProduct());
}

public function testGetProduct()
public function testGetStore()
{
$this->registryMock->expects($this->once())
->method('registry')
->with('current_store')
->willReturn($this->storeMock);

$this->assertInstanceOf(StoreInterface::class, $this->model->getStore());
// Lazy loading
$this->assertInstanceOf(StoreInterface::class, $this->model->getStore());
}

/**
* @expectedException \Magento\Framework\Exception\NotFoundException
* @expectedExceptionMessage Product was not registered
*/
public function testGetProductWithException()
{
$this->assertInstanceOf(ProductInterface::class, $this->model->getProduct());
}

/**
* @expectedException \Magento\Framework\Exception\NotFoundException
* @expectedExceptionMessage Store was not registered
*/
public function testGetStoreWithException()
{
$this->assertInstanceOf('Magento\Catalog\Api\Data\ProductInterface', $this->model->getProduct());
$this->assertInstanceOf(StoreInterface::class, $this->model->getStore());
}
}
19 changes: 19 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/_files/converted_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
return [
"media" => [
"Magento_Catalog" => [
"images" => [
"swatch_thumb_base" => [
"type" => "swatch_thumb",
"width" => 75,
"height" => 75,
"background" => [255, 25, 2]
]
]
]
]
];
18 changes: 18 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/_files/valid_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/view.xsd">
<media>
<images module="Magento_Catalog">
<image id="swatch_thumb_base" type="swatch_thumb">
<width>75</width>
<height>75</height>
<background>[255, 25, 2]</background>
</image>
</images>
</media>
</view>
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function setUp()
'getExistsStoreValueFlag'
])->getMockForAbstractClass();
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(['load', 'getId'])
->setMethods(['load', 'getId', 'getConfig'])
->getMockForAbstractClass();
$this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
->disableOriginalConstructor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function prepareDataSource(array $dataSource)
foreach ($dataSource['data']['items'] as & $item) {
$websites = [];
foreach ($item[$fieldName] as $websiteId) {
if (!isset($websiteNames[$websiteId])) {
continue;
}
$websites[] = $websiteNames[$websiteId];
}
$item[$fieldName] = implode(', ', $websites);
Expand Down
Loading

0 comments on commit b2c8564

Please sign in to comment.