Skip to content

Commit 97b1936

Browse files
author
vpaladiychuk
committed
MAGETWO-34995: Refactor controllers from the list (Part2)
1 parent 73e11a5 commit 97b1936

File tree

8 files changed

+162
-55
lines changed

8 files changed

+162
-55
lines changed

app/code/Magento/Tax/Controller/Adminhtml/Rate/Delete.php

100644100755
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Delete extends \Magento\Tax\Controller\Adminhtml\Rate
1313
/**
1414
* Delete Rate and Data
1515
*
16-
* @return void
16+
* @return \Magento\Framework\Controller\Result\Redirect|void
1717
*/
1818
public function execute()
1919
{
@@ -29,17 +29,25 @@ public function execute()
2929
__('Something went wrong deleting this rate because of an incorrect rate ID.')
3030
);
3131
$this->getResponse()->setRedirect($this->getUrl('tax/*/'));
32-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
33-
$this->messageManager->addError($e->getMessage());
34-
} catch (\Exception $e) {
35-
$this->messageManager->addError(__('Something went wrong deleting this rate.'));
32+
return;
3633
}
34+
return $this->getDefaultRedirect();
35+
}
36+
}
3737

38-
if ($referer = $this->getRequest()->getServer('HTTP_REFERER')) {
39-
$this->getResponse()->setRedirect($referer);
40-
} else {
41-
$this->getResponse()->setRedirect($this->getUrl("*/*/"));
42-
}
38+
/**
39+
* @inheritdoc
40+
*
41+
* @return \Magento\Framework\Controller\Result\Redirect
42+
*/
43+
public function getDefaultRedirect()
44+
{
45+
$resultRedirect = $this->resultRedirectFactory->create();
46+
if ($this->getRequest()->getServer('HTTP_REFERER')) {
47+
$resultRedirect->setRefererUrl();
48+
} else {
49+
$resultRedirect->setUrl($this->getUrl("*/*/"));
4350
}
51+
return $resultRedirect;
4452
}
45-
}
53+
}

app/code/Magento/Tax/Controller/Adminhtml/Rule/Delete.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Delete extends \Magento\Tax\Controller\Adminhtml\Rule
1111
{
1212
/**
13-
* @return void
13+
* @return \Magento\Framework\Controller\Result\Redirect|void
1414
*/
1515
public function execute()
1616
{
@@ -25,7 +25,6 @@ public function execute()
2525
$this->_redirect('tax/*/');
2626
return;
2727
}
28-
2928
return $this->getDefaultRedirect();
3029
}
3130

app/code/Magento/Theme/Controller/Adminhtml/System/Design/Theme/Delete.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,23 @@ public function execute()
3030
$theme->delete();
3131
$this->messageManager->addSuccess(__('You deleted the theme.'));
3232
}
33+
return $this->getDefaultRedirect();
34+
}
3335

34-
$redirectBack = (bool)$this->getRequest()->getParam('back', false);
36+
/**
37+
* @inheritdoc
38+
*
39+
* @return \Magento\Framework\Controller\Result\Redirect
40+
*/
41+
public function getDefaultRedirect()
42+
{
3543
/**
3644
* @todo Temporary solution. Theme module should not know about the existence of editor module.
3745
*/
38-
$redirectBack ? $this->_redirect('adminhtml/system_design_editor/index/') : $this->_redirect('adminhtml/*/');
46+
$path = (bool)$this->getRequest()->getParam('back', false)
47+
? 'adminhtml/system_design_editor/index/'
48+
: 'adminhtml/*/';
49+
$resultRedirect = $this->resultRedirectFactory->create();
50+
return $resultRedirect->setPath($path);
3951
}
4052
}

app/code/Magento/User/Controller/Adminhtml/User/Role/Delete.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ public function execute()
2727

2828
$this->_initRole()->delete();
2929
$this->messageManager->addSuccess(__('You deleted the role.'));
30-
$this->_redirect("*/*/");
30+
return $this->getDefaultRedirect();
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*
36+
* @return \Magento\Framework\Controller\Result\Redirect
37+
*/
38+
public function getDefaultRedirect()
39+
{
40+
$resultRedirect = $this->resultRedirectFactory->create();
41+
return $resultRedirect->setPath("*/*/");
3142
}
3243
}

app/code/Magento/User/Controller/Adminhtml/User/Role/SaveRole.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function _deleteUserFromRole($userId, $roleId)
5252
/**
5353
* Role form submit action to save or create new role
5454
*
55-
* @return void
55+
* @return \Magento\Framework\Controller\Result\Redirect|void
5656
*/
5757
public function execute()
5858
{
@@ -74,8 +74,7 @@ public function execute()
7474
$role = $this->_initRole('role_id');
7575
if (!$role->getId() && $rid) {
7676
$this->messageManager->addError(__('This role no longer exists.'));
77-
$this->_redirect('adminhtml/*/');
78-
return;
77+
return $this->getDefaultRedirect();
7978
}
8079

8180
$roleName = $this->_filterManager->removeTags($this->getRequest()->getParam('rolename', false));
@@ -99,7 +98,17 @@ public function execute()
9998
$this->_addUserToRole($nRuid, $role->getId());
10099
}
101100
$this->messageManager->addSuccess(__('You saved the role.'));
102-
$this->_redirect('adminhtml/*/');
103-
return;
101+
return $this->getDefaultRedirect();
102+
}
103+
104+
/**
105+
* @inheritdoc
106+
*
107+
* @return \Magento\Framework\Controller\Result\Redirect
108+
*/
109+
public function getDefaultRedirect()
110+
{
111+
$resultRedirect = $this->resultRedirectFactory->create();
112+
return $resultRedirect->setPath('adminhtml/*/');
104113
}
105114
}

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,45 @@ public function execute()
9191
return;
9292
}
9393

94-
$buyRequest = new \Magento\Framework\Object($requestParams);
95-
96-
$result = $wishlist->addNewItem($product, $buyRequest);
97-
if (is_string($result)) {
98-
throw new \Magento\Framework\Exception\LocalizedException(__($result));
99-
}
100-
$wishlist->save();
101-
102-
$this->_eventManager->dispatch(
103-
'wishlist_add_product',
104-
['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
105-
);
106-
107-
$referer = $session->getBeforeWishlistUrl();
108-
if ($referer) {
109-
$session->setBeforeWishlistUrl(null);
110-
} else {
111-
$referer = $this->_redirect->getRefererUrl();
94+
try {
95+
$buyRequest = new \Magento\Framework\Object($requestParams);
96+
97+
$result = $wishlist->addNewItem($product, $buyRequest);
98+
if (is_string($result)) {
99+
throw new \Magento\Framework\Exception\LocalizedException(__($result));
100+
}
101+
$wishlist->save();
102+
103+
$this->_eventManager->dispatch(
104+
'wishlist_add_product',
105+
['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
106+
);
107+
108+
$referer = $session->getBeforeWishlistUrl();
109+
if ($referer) {
110+
$session->setBeforeWishlistUrl(null);
111+
} else {
112+
$referer = $this->_redirect->getRefererUrl();
113+
}
114+
115+
116+
/** @var $helper \Magento\Wishlist\Helper\Data */
117+
$helper = $this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
118+
$message = __(
119+
'%1 has been added to your wishlist. Click <a href="%2">here</a> to continue shopping.',
120+
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($product->getName()),
121+
$this->_objectManager->get('Magento\Framework\Escaper')->escapeUrl($referer)
122+
);
123+
$this->messageManager->addSuccess($message);
124+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
125+
$this->messageManager->addError(
126+
__('An error occurred while adding item to wish list: %1', $e->getMessage())
127+
);
128+
} catch (\Exception $e) {
129+
$this->messageManager->addError(__('An error occurred while adding item to wish list.'));
130+
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
112131
}
113132

114-
$this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
115-
$message = __(
116-
'%1 has been added to your wishlist. Click <a href="%2">here</a> to continue shopping.',
117-
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($product->getName()),
118-
$this->_objectManager->get('Magento\Framework\Escaper')->escapeUrl($referer)
119-
);
120-
$this->messageManager->addSuccess($message);
121133
$this->_redirect('*', ['wishlist_id' => $wishlist->getId()]);
122134
}
123135
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ public function __construct(
3030
}
3131

3232
/**
33-
* Add cart item to wishlist and remove from cart
34-
*
35-
* @return \Magento\Framework\App\Response\Http
33+
* @return \Magento\Framework\Controller\Result\Redirect
3634
* @throws NotFoundException
37-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
35+
* @throws \Magento\Framework\Exception\LocalizedException
3836
*/
3937
public function execute()
4038
{
@@ -71,8 +69,17 @@ public function execute()
7169
$this->messageManager->addSuccess(__("%1 has been moved to wish list %2", $productName, $wishlistName));
7270
$wishlist->save();
7371

74-
return $this->getResponse()->setRedirect(
75-
$this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl()
76-
);
72+
return $this->getDefaultRedirect();
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*
78+
* @return \Magento\Framework\Controller\Result\Redirect
79+
*/
80+
public function getDefaultRedirect()
81+
{
82+
$resultRedirect = $this->resultRedirectFactory->create();
83+
return $resultRedirect->setUrl($this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl());
7784
}
7885
}

app/code/Magento/Wishlist/Test/Unit/Controller/Index/AddTest.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ public function testExecuteWithProductIdAndWithoutProduct()
434434

435435
/**
436436
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
437-
* @expectedException \Magento\Framework\Exception\LocalizedException
438437
*/
439438
public function testExecuteWithProductAndCantAddProductToWishlist()
440439
{
@@ -444,11 +443,17 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
444443
->method('addNewItem')
445444
->will($this->returnValue('Can\'t add product to wishlist'));
446445

446+
$wishlist
447+
->expects($this->once())
448+
->method('getId')
449+
->will($this->returnValue(2));
450+
447451
$this->wishlistProvider
448452
->expects($this->once())
449453
->method('getWishlist')
450454
->will($this->returnValue($wishlist));
451455

456+
452457
$request = $this->getMock('Magento\Framework\App\Request\Http', ['getParams'], [], '', false);
453458
$request
454459
->expects($this->once())
@@ -460,8 +465,20 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
460465
$eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false);
461466
$url = $this->getMock('Magento\Framework\Url', null, [], '', false);
462467
$actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', null, [], '', false);
468+
$redirect = $this->getMock('\Magento\Store\App\Response\Redirect', ['redirect'], [], '', false);
469+
$redirect
470+
->expects($this->once())
471+
->method('redirect')
472+
->with($response, '*', ['wishlist_id' => 2])
473+
->will($this->returnValue(null));
463474

464475
$view = $this->getMock('Magento\Framework\App\View', null, [], '', false);
476+
$messageManager = $this->getMock('Magento\Framework\Message\Manager', ['addError'], [], '', false);
477+
$messageManager
478+
->expects($this->once())
479+
->method('addError')
480+
->with('An error occurred while adding item to wish list: Can\'t add product to wishlist')
481+
->will($this->returnValue(null));
465482

466483
$this->context
467484
->expects($this->any())
@@ -487,10 +504,18 @@ public function testExecuteWithProductAndCantAddProductToWishlist()
487504
->expects($this->any())
488505
->method('getActionFlag')
489506
->will($this->returnValue($actionFlag));
507+
$this->context
508+
->expects($this->any())
509+
->method('getRedirect')
510+
->will($this->returnValue($redirect));
490511
$this->context
491512
->expects($this->any())
492513
->method('getView')
493514
->will($this->returnValue($view));
515+
$this->context
516+
->expects($this->any())
517+
->method('getMessageManager')
518+
->will($this->returnValue($messageManager));
494519

495520
$this->customerSession
496521
->expects($this->exactly(1))
@@ -612,6 +637,19 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
612637
->with('http://test-url.com')
613638
->will($this->returnValue('http://test-url.com'));
614639

640+
$logger = $this->getMock(
641+
'Magento\Framework\Logger\Monolog',
642+
['critical'],
643+
[],
644+
'',
645+
false
646+
);
647+
$logger
648+
->expects($this->once())
649+
->method('critical')
650+
->with($exception)
651+
->will($this->returnValue(true));
652+
615653
$om = $this->getMock('Magento\Framework\App\ObjectManager', ['get'], [], '', false);
616654
$om
617655
->expects($this->at(0))
@@ -628,6 +666,11 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
628666
->method('get')
629667
->with('Magento\Framework\Escaper')
630668
->will($this->returnValue($escaper));
669+
$om
670+
->expects($this->at(3))
671+
->method('get')
672+
->with('Psr\Log\LoggerInterface')
673+
->will($this->returnValue($logger));
631674

632675
$response = $this->getMock('Magento\Framework\App\Response\Http', null, [], '', false);
633676
$eventManager = $this->getMock('Magento\Framework\Event\Manager', ['dispatch'], [], '', false);
@@ -657,7 +700,13 @@ public function testExecuteProductAddedToWishlistAfterObjectManagerThrowExceptio
657700
);
658701
$messageManager
659702
->expects($this->once())
660-
->method('addSuccess');
703+
->method('addError')
704+
->with('An error occurred while adding item to wish list.')
705+
->will($this->returnValue(null));
706+
$messageManager
707+
->expects($this->once())
708+
->method('addSuccess')
709+
->will($this->throwException($exception));
661710

662711
$this->context
663712
->expects($this->any())

0 commit comments

Comments
 (0)