Skip to content

Commit

Permalink
Merge pull request #220 from magento-south/BUGS
Browse files Browse the repository at this point in the history
[South] Bug fixes
  • Loading branch information
slavvka committed Dec 9, 2015
2 parents 6195cfd + f4d3e04 commit 4876b4f
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 44 deletions.
48 changes: 47 additions & 1 deletion app/code/Magento/Theme/Model/Theme/Plugin/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\State as AppState;
use Magento\Theme\Model\Theme\Collection as ThemeCollection;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeLoader;
use Magento\Framework\Config\Theme;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Registration
{
/** @var ThemeRegistration */
protected $themeRegistration;

/** @var ThemeCollection */
protected $themeCollection;

/** @var ThemeLoader */
protected $themeLoader;

/** @var LoggerInterface */
protected $logger;

Expand All @@ -25,21 +37,27 @@ class Registration

/**
* @param ThemeRegistration $themeRegistration
* @param ThemeCollection $themeCollection
* @param ThemeLoader $themeLoader
* @param LoggerInterface $logger
* @param AppState $appState
*/
public function __construct(
ThemeRegistration $themeRegistration,
ThemeCollection $themeCollection,
ThemeLoader $themeLoader,
LoggerInterface $logger,
AppState $appState
) {
$this->themeRegistration = $themeRegistration;
$this->themeCollection = $themeCollection;
$this->themeLoader = $themeLoader;
$this->logger = $logger;
$this->appState = $appState;
}

/**
* Add new theme from filesystem
* Add new theme from filesystem and update existing
*
* @param AbstractAction $subject
* @param RequestInterface $request
Expand All @@ -54,9 +72,37 @@ public function beforeDispatch(
try {
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
$this->themeRegistration->register();
$this->updateThemeData();
}
} catch (LocalizedException $e) {
$this->logger->critical($e);
}
}

/**
* Update theme data
*
* @return void
*/
protected function updateThemeData()
{
$themesData = $this->themeCollection->loadData();
/** @var \Magento\Theme\Model\Theme $themeData */
foreach ($themesData as $themeData) {
if ($themeData->getParentTheme()) {
$parentTheme = $this->themeLoader->getThemeByFullPath(
$themeData->getParentTheme()->getFullPath()
);
$themeData->setParentId($parentTheme->getId());
}

/** @var \Magento\Theme\Model\Theme $theme */
$theme = $this->themeLoader->getThemeByFullPath(
$themeData->getArea()
. Theme::THEME_PATH_SEPARATOR
. $themeData->getThemePath()
);
$theme->addData($themeData->toArray())->save();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,100 @@ class RegistrationTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
protected $appState;

/** @var \Magento\Theme\Model\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
protected $themeCollection;

/** @var \Magento\Theme\Model\ResourceModel\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
protected $themeLoader;

/** @var Registration */
protected $plugin;

public function setUp()
{
$this->themeRegistration = $this->getMock('Magento\Theme\Model\Theme\Registration', [], [], '', false);
$this->logger = $this->getMockForAbstractClass('Psr\Log\LoggerInterface', [], '', false);
$this->abstractAction = $this->getMockForAbstractClass('Magento\Backend\App\AbstractAction', [], '', false);
$this->request = $this->getMockForAbstractClass('Magento\Framework\App\RequestInterface', [], '', false);
$this->appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$this->themeCollection = $this->getMock('Magento\Theme\Model\Theme\Collection', [], [], '', false);
$this->themeLoader = $this->getMock('Magento\Theme\Model\ResourceModel\Theme\Collection', [], [], '', false);
$this->plugin = new Registration(
$this->themeRegistration,
$this->themeCollection,
$this->themeLoader,
$this->logger,
$this->appState
);
}

public function testBeforeDispatch()
{
$theme = $this->getMock(
'Magento\Theme\Model\Theme',
[
'setParentId',
'getArea',
'getThemePath',
'getParentTheme',
'getId',
'getFullPath',
'toArray',
'addData',
'save',
],
[],
'',
false
);
$this->appState->expects($this->once())->method('getMode')->willReturn('default');
$this->themeRegistration->expects($this->once())->method('register');
$this->logger->expects($this->never())->method('critical');
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
$object->beforeDispatch($this->abstractAction, $this->request);
$this->themeCollection->expects($this->once())->method('loadData')->willReturn([$theme]);
$theme->expects($this->once())->method('getArea')->willReturn('frontend');
$theme->expects($this->once())->method('getThemePath')->willReturn('Magento/luma');
$theme->expects($this->exactly(2))->method('getParentTheme')->willReturnSelf();
$theme->expects($this->once())->method('getId')->willReturn(1);
$theme->expects($this->once())->method('getFullPath')->willReturn('frontend/Magento/blank');
$theme->expects($this->once())->method('setParentId')->with(1);
$this->themeLoader->expects($this->exactly(2))
->method('getThemeByFullPath')
->withConsecutive(
['frontend/Magento/blank'],
['frontend/Magento/luma']
)
->will($this->onConsecutiveCalls(
$theme,
$theme
));
$theme->expects($this->once())
->method('toArray')
->willReturn([
'title' => 'Magento Luma'
]);
$theme->expects($this->once())
->method('addData')
->with([
'title' => 'Magento Luma'
])
->willReturnSelf();
$theme->expects($this->once())
->method('save');

$this->plugin->beforeDispatch($this->abstractAction, $this->request);
}

public function testBeforeDispatchWithProductionMode()
{
$this->appState->expects($this->once())->method('getMode')->willReturn('production');
$this->themeRegistration->expects($this->never())->method('register');
$this->logger->expects($this->never())->method('critical');
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
$object->beforeDispatch($this->abstractAction, $this->request);
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
}

public function testBeforeDispatchWithException()
{
$exception = new LocalizedException(new Phrase('Phrase'));
$this->themeRegistration->expects($this->once())->method('register')->willThrowException($exception);
$this->logger->expects($this->once())->method('critical');
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
$object->beforeDispatch($this->abstractAction, $this->request);

$this->plugin->beforeDispatch($this->abstractAction, $this->request);
}
}
21 changes: 17 additions & 4 deletions app/code/Magento/Wishlist/Controller/Index/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;
Expand All @@ -31,22 +32,30 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex
*/
protected $productRepository;

/**
* @var Validator
*/
protected $formKeyValidator;

/**
* @param Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
* @param ProductRepositoryInterface $productRepository
* @param Validator $formKeyValidator
*/
public function __construct(
Action\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
ProductRepositoryInterface $productRepository
ProductRepositoryInterface $productRepository,
Validator $formKeyValidator
) {
$this->_customerSession = $customerSession;
$this->wishlistProvider = $wishlistProvider;
parent::__construct($context);
$this->productRepository = $productRepository;
$this->formKeyValidator = $formKeyValidator;
parent::__construct($context);
}

/**
Expand All @@ -60,6 +69,12 @@ public function __construct(
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
return $resultRedirect->setPath('*/');
}

$wishlist = $this->wishlistProvider->getWishlist();
if (!$wishlist) {
throw new NotFoundException(__('Page not found.'));
Expand All @@ -75,8 +90,6 @@ public function execute()
}

$productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$productId) {
$resultRedirect->setPath('*/');
return $resultRedirect;
Expand Down
18 changes: 15 additions & 3 deletions app/code/Magento/Wishlist/Controller/Index/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@ class Cart extends \Magento\Wishlist\Controller\AbstractIndex
*/
protected $helper;

/**
* @var \Magento\Framework\Data\Form\FormKey\Validator
*/
protected $formKeyValidator;

/**
* @param Action\Context $context
* @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
* @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
* @param \Magento\Wishlist\Model\ItemFactory $itemFactory
* @param \Magento\Checkout\Model\Cart $cart
* @param \Magento\Wishlist\Model\Item\OptionFactory $
* @param \Magento\Wishlist\Model\Item\OptionFactory $optionFactory
* @param \Magento\Catalog\Helper\Product $productHelper
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Wishlist\Helper\Data $helper
* @param \Magento\Checkout\Helper\Cart $cartHelper
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -82,7 +88,8 @@ public function __construct(
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Framework\Escaper $escaper,
\Magento\Wishlist\Helper\Data $helper,
\Magento\Checkout\Helper\Cart $cartHelper
\Magento\Checkout\Helper\Cart $cartHelper,
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
) {
$this->wishlistProvider = $wishlistProvider;
$this->quantityProcessor = $quantityProcessor;
Expand All @@ -93,6 +100,7 @@ public function __construct(
$this->escaper = $escaper;
$this->helper = $helper;
$this->cartHelper = $cartHelper;
$this->formKeyValidator = $formKeyValidator;
parent::__construct($context);
}

Expand All @@ -108,9 +116,13 @@ public function __construct(
*/
public function execute()
{
$itemId = (int)$this->getRequest()->getParam('item');
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
return $resultRedirect->setPath('*/*/');
}

$itemId = (int)$this->getRequest()->getParam('item');
/* @var $item \Magento\Wishlist\Model\Item */
$item = $this->itemFactory->create()->load($itemId);
if (!$item->getId()) {
Expand Down
20 changes: 16 additions & 4 deletions app/code/Magento/Wishlist/Controller/Index/Fromcart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Checkout\Model\Cart as CheckoutCart;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -46,27 +47,35 @@ class Fromcart extends \Magento\Wishlist\Controller\AbstractIndex
*/
protected $escaper;

/**
* @var Validator
*/
protected $formKeyValidator;

/**
* @param Action\Context $context
* @param WishlistProviderInterface $wishlistProvider
* @param WishlistHelper $wishlistHelper
* @param CheckoutCart $cart
* @param CartHelper $cartHelper
* @param Escaper $escaper
* @param Validator $formKeyValidator
*/
public function __construct(
Action\Context $context,
WishlistProviderInterface $wishlistProvider,
WishlistHelper $wishlistHelper,
CheckoutCart $cart,
CartHelper $cartHelper,
Escaper $escaper
Escaper $escaper,
Validator $formKeyValidator
) {
$this->wishlistProvider = $wishlistProvider;
$this->wishlistHelper = $wishlistHelper;
$this->cart = $cart;
$this->cartHelper = $cartHelper;
$this->escaper = $escaper;
$this->formKeyValidator = $formKeyValidator;
parent::__construct($context);
}

Expand All @@ -79,6 +88,12 @@ public function __construct(
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
return $resultRedirect->setPath('*/*/');
}

$wishlist = $this->wishlistProvider->getWishlist();
if (!$wishlist) {
throw new NotFoundException(__('Page not found.'));
Expand Down Expand Up @@ -112,9 +127,6 @@ public function execute()
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('We can\'t move the item to the wish list.'));
}

/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setUrl($this->cartHelper->getCartUrl());
}
}
Loading

0 comments on commit 4876b4f

Please sign in to comment.