Skip to content

Commit 4876b4f

Browse files
committed
Merge pull request #220 from magento-south/BUGS
[South] Bug fixes
2 parents 6195cfd + f4d3e04 commit 4876b4f

File tree

16 files changed

+410
-44
lines changed

16 files changed

+410
-44
lines changed

app/code/Magento/Theme/Model/Theme/Plugin/Registration.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Psr\Log\LoggerInterface;
1313
use Magento\Framework\App\State as AppState;
14+
use Magento\Theme\Model\Theme\Collection as ThemeCollection;
15+
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeLoader;
16+
use Magento\Framework\Config\Theme;
1417

18+
/**
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
*/
1521
class Registration
1622
{
1723
/** @var ThemeRegistration */
1824
protected $themeRegistration;
1925

26+
/** @var ThemeCollection */
27+
protected $themeCollection;
28+
29+
/** @var ThemeLoader */
30+
protected $themeLoader;
31+
2032
/** @var LoggerInterface */
2133
protected $logger;
2234

@@ -25,21 +37,27 @@ class Registration
2537

2638
/**
2739
* @param ThemeRegistration $themeRegistration
40+
* @param ThemeCollection $themeCollection
41+
* @param ThemeLoader $themeLoader
2842
* @param LoggerInterface $logger
2943
* @param AppState $appState
3044
*/
3145
public function __construct(
3246
ThemeRegistration $themeRegistration,
47+
ThemeCollection $themeCollection,
48+
ThemeLoader $themeLoader,
3349
LoggerInterface $logger,
3450
AppState $appState
3551
) {
3652
$this->themeRegistration = $themeRegistration;
53+
$this->themeCollection = $themeCollection;
54+
$this->themeLoader = $themeLoader;
3755
$this->logger = $logger;
3856
$this->appState = $appState;
3957
}
4058

4159
/**
42-
* Add new theme from filesystem
60+
* Add new theme from filesystem and update existing
4361
*
4462
* @param AbstractAction $subject
4563
* @param RequestInterface $request
@@ -54,9 +72,37 @@ public function beforeDispatch(
5472
try {
5573
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
5674
$this->themeRegistration->register();
75+
$this->updateThemeData();
5776
}
5877
} catch (LocalizedException $e) {
5978
$this->logger->critical($e);
6079
}
6180
}
81+
82+
/**
83+
* Update theme data
84+
*
85+
* @return void
86+
*/
87+
protected function updateThemeData()
88+
{
89+
$themesData = $this->themeCollection->loadData();
90+
/** @var \Magento\Theme\Model\Theme $themeData */
91+
foreach ($themesData as $themeData) {
92+
if ($themeData->getParentTheme()) {
93+
$parentTheme = $this->themeLoader->getThemeByFullPath(
94+
$themeData->getParentTheme()->getFullPath()
95+
);
96+
$themeData->setParentId($parentTheme->getId());
97+
}
98+
99+
/** @var \Magento\Theme\Model\Theme $theme */
100+
$theme = $this->themeLoader->getThemeByFullPath(
101+
$themeData->getArea()
102+
. Theme::THEME_PATH_SEPARATOR
103+
. $themeData->getThemePath()
104+
);
105+
$theme->addData($themeData->toArray())->save();
106+
}
107+
}
62108
}

app/code/Magento/Theme/Test/Unit/Model/Theme/Plugin/RegistrationTest.php

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,100 @@ class RegistrationTest extends \PHPUnit_Framework_TestCase
2626
/** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
2727
protected $appState;
2828

29+
/** @var \Magento\Theme\Model\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
30+
protected $themeCollection;
31+
32+
/** @var \Magento\Theme\Model\ResourceModel\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
33+
protected $themeLoader;
34+
35+
/** @var Registration */
36+
protected $plugin;
37+
2938
public function setUp()
3039
{
3140
$this->themeRegistration = $this->getMock('Magento\Theme\Model\Theme\Registration', [], [], '', false);
3241
$this->logger = $this->getMockForAbstractClass('Psr\Log\LoggerInterface', [], '', false);
3342
$this->abstractAction = $this->getMockForAbstractClass('Magento\Backend\App\AbstractAction', [], '', false);
3443
$this->request = $this->getMockForAbstractClass('Magento\Framework\App\RequestInterface', [], '', false);
3544
$this->appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
45+
$this->themeCollection = $this->getMock('Magento\Theme\Model\Theme\Collection', [], [], '', false);
46+
$this->themeLoader = $this->getMock('Magento\Theme\Model\ResourceModel\Theme\Collection', [], [], '', false);
47+
$this->plugin = new Registration(
48+
$this->themeRegistration,
49+
$this->themeCollection,
50+
$this->themeLoader,
51+
$this->logger,
52+
$this->appState
53+
);
3654
}
3755

3856
public function testBeforeDispatch()
3957
{
58+
$theme = $this->getMock(
59+
'Magento\Theme\Model\Theme',
60+
[
61+
'setParentId',
62+
'getArea',
63+
'getThemePath',
64+
'getParentTheme',
65+
'getId',
66+
'getFullPath',
67+
'toArray',
68+
'addData',
69+
'save',
70+
],
71+
[],
72+
'',
73+
false
74+
);
4075
$this->appState->expects($this->once())->method('getMode')->willReturn('default');
4176
$this->themeRegistration->expects($this->once())->method('register');
42-
$this->logger->expects($this->never())->method('critical');
43-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
44-
$object->beforeDispatch($this->abstractAction, $this->request);
77+
$this->themeCollection->expects($this->once())->method('loadData')->willReturn([$theme]);
78+
$theme->expects($this->once())->method('getArea')->willReturn('frontend');
79+
$theme->expects($this->once())->method('getThemePath')->willReturn('Magento/luma');
80+
$theme->expects($this->exactly(2))->method('getParentTheme')->willReturnSelf();
81+
$theme->expects($this->once())->method('getId')->willReturn(1);
82+
$theme->expects($this->once())->method('getFullPath')->willReturn('frontend/Magento/blank');
83+
$theme->expects($this->once())->method('setParentId')->with(1);
84+
$this->themeLoader->expects($this->exactly(2))
85+
->method('getThemeByFullPath')
86+
->withConsecutive(
87+
['frontend/Magento/blank'],
88+
['frontend/Magento/luma']
89+
)
90+
->will($this->onConsecutiveCalls(
91+
$theme,
92+
$theme
93+
));
94+
$theme->expects($this->once())
95+
->method('toArray')
96+
->willReturn([
97+
'title' => 'Magento Luma'
98+
]);
99+
$theme->expects($this->once())
100+
->method('addData')
101+
->with([
102+
'title' => 'Magento Luma'
103+
])
104+
->willReturnSelf();
105+
$theme->expects($this->once())
106+
->method('save');
107+
108+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
45109
}
46110

47111
public function testBeforeDispatchWithProductionMode()
48112
{
49113
$this->appState->expects($this->once())->method('getMode')->willReturn('production');
50-
$this->themeRegistration->expects($this->never())->method('register');
51-
$this->logger->expects($this->never())->method('critical');
52-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
53-
$object->beforeDispatch($this->abstractAction, $this->request);
114+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
54115
}
55116

56117
public function testBeforeDispatchWithException()
57118
{
58119
$exception = new LocalizedException(new Phrase('Phrase'));
59120
$this->themeRegistration->expects($this->once())->method('register')->willThrowException($exception);
60121
$this->logger->expects($this->once())->method('critical');
61-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
62-
$object->beforeDispatch($this->abstractAction, $this->request);
122+
123+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
63124
}
64125
}

app/code/Magento/Wishlist/Controller/Index/Add.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Api\ProductRepositoryInterface;
99
use Magento\Framework\App\Action;
10+
use Magento\Framework\Data\Form\FormKey\Validator;
1011
use Magento\Framework\Exception\NotFoundException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Framework\Controller\ResultFactory;
@@ -31,22 +32,30 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex
3132
*/
3233
protected $productRepository;
3334

35+
/**
36+
* @var Validator
37+
*/
38+
protected $formKeyValidator;
39+
3440
/**
3541
* @param Action\Context $context
3642
* @param \Magento\Customer\Model\Session $customerSession
3743
* @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
3844
* @param ProductRepositoryInterface $productRepository
45+
* @param Validator $formKeyValidator
3946
*/
4047
public function __construct(
4148
Action\Context $context,
4249
\Magento\Customer\Model\Session $customerSession,
4350
\Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
44-
ProductRepositoryInterface $productRepository
51+
ProductRepositoryInterface $productRepository,
52+
Validator $formKeyValidator
4553
) {
4654
$this->_customerSession = $customerSession;
4755
$this->wishlistProvider = $wishlistProvider;
48-
parent::__construct($context);
4956
$this->productRepository = $productRepository;
57+
$this->formKeyValidator = $formKeyValidator;
58+
parent::__construct($context);
5059
}
5160

5261
/**
@@ -60,6 +69,12 @@ public function __construct(
6069
*/
6170
public function execute()
6271
{
72+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
73+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
74+
if (!$this->formKeyValidator->validate($this->getRequest())) {
75+
return $resultRedirect->setPath('*/');
76+
}
77+
6378
$wishlist = $this->wishlistProvider->getWishlist();
6479
if (!$wishlist) {
6580
throw new NotFoundException(__('Page not found.'));
@@ -75,8 +90,6 @@ public function execute()
7590
}
7691

7792
$productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
78-
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
79-
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
8093
if (!$productId) {
8194
$resultRedirect->setPath('*/');
8295
return $resultRedirect;

app/code/Magento/Wishlist/Controller/Index/Cart.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,23 @@ class Cart extends \Magento\Wishlist\Controller\AbstractIndex
5959
*/
6060
protected $helper;
6161

62+
/**
63+
* @var \Magento\Framework\Data\Form\FormKey\Validator
64+
*/
65+
protected $formKeyValidator;
66+
6267
/**
6368
* @param Action\Context $context
6469
* @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
6570
* @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
6671
* @param \Magento\Wishlist\Model\ItemFactory $itemFactory
6772
* @param \Magento\Checkout\Model\Cart $cart
68-
* @param \Magento\Wishlist\Model\Item\OptionFactory $
73+
* @param \Magento\Wishlist\Model\Item\OptionFactory $optionFactory
6974
* @param \Magento\Catalog\Helper\Product $productHelper
7075
* @param \Magento\Framework\Escaper $escaper
7176
* @param \Magento\Wishlist\Helper\Data $helper
7277
* @param \Magento\Checkout\Helper\Cart $cartHelper
78+
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
7379
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7480
*/
7581
public function __construct(
@@ -82,7 +88,8 @@ public function __construct(
8288
\Magento\Catalog\Helper\Product $productHelper,
8389
\Magento\Framework\Escaper $escaper,
8490
\Magento\Wishlist\Helper\Data $helper,
85-
\Magento\Checkout\Helper\Cart $cartHelper
91+
\Magento\Checkout\Helper\Cart $cartHelper,
92+
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
8693
) {
8794
$this->wishlistProvider = $wishlistProvider;
8895
$this->quantityProcessor = $quantityProcessor;
@@ -93,6 +100,7 @@ public function __construct(
93100
$this->escaper = $escaper;
94101
$this->helper = $helper;
95102
$this->cartHelper = $cartHelper;
103+
$this->formKeyValidator = $formKeyValidator;
96104
parent::__construct($context);
97105
}
98106

@@ -108,9 +116,13 @@ public function __construct(
108116
*/
109117
public function execute()
110118
{
111-
$itemId = (int)$this->getRequest()->getParam('item');
112119
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
113120
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
121+
if (!$this->formKeyValidator->validate($this->getRequest())) {
122+
return $resultRedirect->setPath('*/*/');
123+
}
124+
125+
$itemId = (int)$this->getRequest()->getParam('item');
114126
/* @var $item \Magento\Wishlist\Model\Item */
115127
$item = $this->itemFactory->create()->load($itemId);
116128
if (!$item->getId()) {

app/code/Magento/Wishlist/Controller/Index/Fromcart.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Checkout\Model\Cart as CheckoutCart;
1010
use Magento\Customer\Model\Session;
1111
use Magento\Framework\App\Action;
12+
use Magento\Framework\Data\Form\FormKey\Validator;
1213
use Magento\Framework\Escaper;
1314
use Magento\Framework\Exception\NotFoundException;
1415
use Magento\Framework\Exception\LocalizedException;
@@ -46,27 +47,35 @@ class Fromcart extends \Magento\Wishlist\Controller\AbstractIndex
4647
*/
4748
protected $escaper;
4849

50+
/**
51+
* @var Validator
52+
*/
53+
protected $formKeyValidator;
54+
4955
/**
5056
* @param Action\Context $context
5157
* @param WishlistProviderInterface $wishlistProvider
5258
* @param WishlistHelper $wishlistHelper
5359
* @param CheckoutCart $cart
5460
* @param CartHelper $cartHelper
5561
* @param Escaper $escaper
62+
* @param Validator $formKeyValidator
5663
*/
5764
public function __construct(
5865
Action\Context $context,
5966
WishlistProviderInterface $wishlistProvider,
6067
WishlistHelper $wishlistHelper,
6168
CheckoutCart $cart,
6269
CartHelper $cartHelper,
63-
Escaper $escaper
70+
Escaper $escaper,
71+
Validator $formKeyValidator
6472
) {
6573
$this->wishlistProvider = $wishlistProvider;
6674
$this->wishlistHelper = $wishlistHelper;
6775
$this->cart = $cart;
6876
$this->cartHelper = $cartHelper;
6977
$this->escaper = $escaper;
78+
$this->formKeyValidator = $formKeyValidator;
7079
parent::__construct($context);
7180
}
7281

@@ -79,6 +88,12 @@ public function __construct(
7988
*/
8089
public function execute()
8190
{
91+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
92+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
93+
if (!$this->formKeyValidator->validate($this->getRequest())) {
94+
return $resultRedirect->setPath('*/*/');
95+
}
96+
8297
$wishlist = $this->wishlistProvider->getWishlist();
8398
if (!$wishlist) {
8499
throw new NotFoundException(__('Page not found.'));
@@ -112,9 +127,6 @@ public function execute()
112127
} catch (\Exception $e) {
113128
$this->messageManager->addExceptionMessage($e, __('We can\'t move the item to the wish list.'));
114129
}
115-
116-
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
117-
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
118130
return $resultRedirect->setUrl($this->cartHelper->getCartUrl());
119131
}
120132
}

0 commit comments

Comments
 (0)