Skip to content

Commit

Permalink
Merge pull request #552 from magento-folks/bugfix
Browse files Browse the repository at this point in the history
[Folks]Bugfix
  • Loading branch information
Bomko, Alex(abomko) committed Apr 19, 2016
2 parents 951694c + c8ebe59 commit 15d3a28
Show file tree
Hide file tree
Showing 20 changed files with 419 additions and 99 deletions.
4 changes: 2 additions & 2 deletions app/code/Magento/Catalog/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories</item>
<item name="sortOrder" xsi:type="number">60</item>
</item>
<item name="schedule-design-update" xsi:type="array">
<item name="custom-options" xsi:type="array">
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions</item>
<item name="sortOrder" xsi:type="number">70</item>
</item>
<item name="custom-options" xsi:type="array">
<item name="schedule-design-update" xsi:type="array">
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\ScheduleDesignUpdate</item>
<item name="sortOrder" xsi:type="number">80</item>
</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $fo
private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName)
{
$conditions->setFormName($formName);
$conditions->setJsFormObject($formName);
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
foreach ($conditions->getConditions() as $condition) {
$this->setConditionFormName($condition, $formName);
Expand Down
82 changes: 74 additions & 8 deletions app/code/Magento/Checkout/Block/Checkout/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Checkout\Block\Checkout;

use Magento\Framework\App\ObjectManager;

class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
{
/**
Expand All @@ -22,6 +24,11 @@ class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcesso
*/
protected $merger;

/**
* @var \Magento\Customer\Model\Options
*/
private $options;

/**
* @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
* @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
Expand All @@ -38,12 +45,21 @@ public function __construct(
}

/**
* Process js Layout of block
*
* @param array $jsLayout
* @deprecated
* @return \Magento\Customer\Model\Options
*/
private function getOptions()
{
if (!is_object($this->options)) {
$this->options = ObjectManager::getInstance()->get(\Magento\Customer\Model\Options::class);
}
return $this->options;
}

/**
* @return array
*/
public function process($jsLayout)
private function getAddressAttributes()
{
/** @var \Magento\Eav\Api\Data\AttributeInterface[] $attributes */
$attributes = $this->attributeMetadataDataProvider->loadAttributesCollection(
Expand All @@ -53,16 +69,66 @@ public function process($jsLayout)

$elements = [];
foreach ($attributes as $attribute) {
$code = $attribute->getAttributeCode();
if ($attribute->getIsUserDefined()) {
continue;
}
$elements[$attribute->getAttributeCode()] = $this->attributeMapper->map($attribute);
if (isset($elements[$attribute->getAttributeCode()]['label'])) {
$label = $elements[$attribute->getAttributeCode()]['label'];
$elements[$attribute->getAttributeCode()]['label'] = __($label);
$elements[$code] = $this->attributeMapper->map($attribute);
if (isset($elements[$code]['label'])) {
$label = $elements[$code]['label'];
$elements[$code]['label'] = __($label);
}
}
return $elements;
}

/**
* Convert elements(like prefix and suffix) from inputs to selects when necessary
*
* @param array $elements address attributes
* @param array $attributesToConvert fields and their callbacks
* @return array
*/
private function convertElementsToSelect($elements, $attributesToConvert)
{
$codes = array_keys($attributesToConvert);
foreach (array_keys($elements) as $code) {
if (!in_array($code, $codes)) {
continue;
}
$options = call_user_func($attributesToConvert[$code]);
if (!is_array($options)) {
continue;
}
$elements[$code]['dataType'] = 'select';
$elements[$code]['formElement'] = 'select';

foreach ($options as $key => $value) {
$elements[$code]['options'][] = [
'value' => $key,
'label' => $value,
];
}
}

return $elements;
}

/**
* Process js Layout of block
*
* @param array $jsLayout
* @return array
*/
public function process($jsLayout)
{
$attributesToConvert = [
'prefix' => [$this->getOptions(), 'getNamePrefixOptions'],
'suffix' => [$this->getOptions(), 'getNameSuffixOptions'],
];

$elements = $this->getAddressAttributes();
$elements = $this->convertElementsToSelect($elements, $attributesToConvert);
// The following code is a workaround for custom address attributes
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']
Expand Down
64 changes: 38 additions & 26 deletions app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,47 @@
*/
namespace Magento\Checkout\Controller\Sidebar;

use Magento\Checkout\Model\Sidebar;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Response\Http;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Json\Helper\Data;
use Magento\Framework\View\Result\PageFactory;
use Psr\Log\LoggerInterface;

class RemoveItem extends Action
class RemoveItem extends \Magento\Framework\App\Action\Action
{
/**
* @var Sidebar
* @var \Magento\Checkout\Model\Sidebar
*/
protected $sidebar;

/**
* @var LoggerInterface
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* @var Data
* @var \Magento\Framework\Json\Helper\Data
*/
protected $jsonHelper;

/**
* @var PageFactory
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;

/**
* @param Context $context
* @param Sidebar $sidebar
* @param LoggerInterface $logger
* @param Data $jsonHelper
* @param PageFactory $resultPageFactory
* @codeCoverageIgnore
* @var \Magento\Framework\Data\Form\FormKey\Validator
*/
private $formKeyValidator;

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Checkout\Model\Sidebar $sidebar
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
Sidebar $sidebar,
LoggerInterface $logger,
Data $jsonHelper,
PageFactory $resultPageFactory
\Magento\Framework\App\Action\Context $context,
\Magento\Checkout\Model\Sidebar $sidebar,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->sidebar = $sidebar;
$this->logger = $logger;
Expand All @@ -63,12 +59,15 @@ public function __construct(
*/
public function execute()
{
if (!$this->getFormKeyValidator()->validate($this->getRequest())) {
return $this->resultRedirectFactory->create()->setPath('*/cart/');
}
$itemId = (int)$this->getRequest()->getParam('item_id');
try {
$this->sidebar->checkQuoteItem($itemId);
$this->sidebar->removeQuoteItem($itemId);
return $this->jsonResponse();
} catch (LocalizedException $e) {
} catch (\Magento\Framework\Exception\LocalizedException $e) {
return $this->jsonResponse($e->getMessage());
} catch (\Exception $e) {
$this->logger->critical($e);
Expand All @@ -80,7 +79,7 @@ public function execute()
* Compile JSON response
*
* @param string $error
* @return Http
* @return \Magento\Framework\App\Response\Http
*/
protected function jsonResponse($error = '')
{
Expand All @@ -90,4 +89,17 @@ protected function jsonResponse($error = '')
$this->jsonHelper->jsonEncode($response)
);
}

/**
* @return \Magento\Framework\Data\Form\FormKey\Validator
* @deprecated
*/
private function getFormKeyValidator()
{
if (!$this->formKeyValidator) {
$this->formKeyValidator = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Data\Form\FormKey\Validator::class);
}
return $this->formKeyValidator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class RemoveItemTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\Checkout\Controller\Sidebar\RemoveItem */
Expand All @@ -34,6 +37,11 @@ class RemoveItemTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $resultPageFactoryMock;

/**
* @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultRedirectFactory;

protected function setUp()
{
$this->sidebarMock = $this->getMock('Magento\Checkout\Model\Sidebar', [], [], '', false);
Expand All @@ -50,6 +58,13 @@ protected function setUp()
['representJson']
);
$this->resultPageFactoryMock = $this->getMock('Magento\Framework\View\Result\PageFactory', [], [], '', false);
$this->resultRedirectFactory = $this->getMock(
\Magento\Framework\Controller\Result\RedirectFactory::class,
['create'],
[],
'',
false
);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->removeItem = $this->objectManagerHelper->getObject(
Expand All @@ -61,12 +76,21 @@ protected function setUp()
'request' => $this->requestMock,
'response' => $this->responseMock,
'resultPageFactory' => $this->resultPageFactoryMock,
'resultRedirectFactory' => $this->resultRedirectFactory

]
);
$formKeyValidatorMock = $this->getMock('Magento\Framework\Data\Form\FormKey\Validator', [], [], '', false);
$this->setPropertyValue($this->removeItem, 'formKeyValidator', $formKeyValidatorMock);
}

public function testExecute()
{
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
->expects($this->once())
->method('validate')
->with($this->requestMock)
->willReturn(true);
$this->requestMock->expects($this->once())
->method('getParam')
->with('item_id', null)
Expand Down Expand Up @@ -118,6 +142,11 @@ public function testExecute()

public function testExecuteWithLocalizedException()
{
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
->expects($this->once())
->method('validate')
->with($this->requestMock)
->willReturn(true);
$this->requestMock->expects($this->once())
->method('getParam')
->with('item_id', null)
Expand Down Expand Up @@ -158,6 +187,11 @@ public function testExecuteWithLocalizedException()

public function testExecuteWithException()
{
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
->expects($this->once())
->method('validate')
->with($this->requestMock)
->willReturn(true);
$this->requestMock->expects($this->once())
->method('getParam')
->with('item_id', null)
Expand Down Expand Up @@ -202,4 +236,52 @@ public function testExecuteWithException()

$this->assertEquals('json represented', $this->removeItem->execute());
}

public function testExecuteWhenFormKeyValidationFailed()
{
$resultRedirect = $this->getMock(\Magento\Framework\Controller\Result\Redirect::class, [], [], '', false);
$resultRedirect->expects($this->once())->method('setPath')->with('*/cart/')->willReturnSelf();
$this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($resultRedirect);
$this->getPropertyValue($this->removeItem, 'formKeyValidator')
->expects($this->once())
->method('validate')
->with($this->requestMock)
->willReturn(false);
$this->assertEquals($resultRedirect, $this->removeItem->execute());
}

/**
* Get any object property value.
*
* @param $object
* @param $property
* @return mixed
* @deprecated
*/
protected function getPropertyValue($object, $property)
{
$reflection = new \ReflectionClass(get_class($object));
$reflectionProperty = $reflection->getProperty($property);
$reflectionProperty->setAccessible(true);

return $reflectionProperty->getValue($object);
}

/**
* Set object property value.
*
* @param $object
* @param $property
* @param $value
* @deprecated
*/
protected function setPropertyValue(&$object, $property, $value)
{
$reflection = new \ReflectionClass(get_class($object));
$reflectionProperty = $reflection->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value);

return $object;
}
}
Loading

0 comments on commit 15d3a28

Please sign in to comment.