Skip to content

Commit b03bce0

Browse files
ENGCOM-5862: Magento_ProductAlert module. Replace replace deprecated message functions #24645
- Merge Pull Request #24645 from atwixfirster/magento2:module-product-alert-replace-deprecated-message-func - Merged commits: 1. 8285e6a 2. 3f8060a
2 parents c6cf182 + 3f8060a commit b03bce0

File tree

5 files changed

+168
-27
lines changed

5 files changed

+168
-27
lines changed

app/code/Magento/ProductAlert/Controller/Add/Price.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66

77
namespace Magento\ProductAlert\Controller\Add;
88

9-
use Magento\Framework\App\Action\HttpGetActionInterface;
10-
use Magento\ProductAlert\Controller\Add as AddController;
11-
use Magento\Framework\App\Action\Context;
12-
use Magento\Customer\Model\Session as CustomerSession;
13-
use Magento\Store\Model\StoreManagerInterface;
149
use Magento\Catalog\Api\ProductRepositoryInterface;
15-
use Magento\Framework\UrlInterface;
10+
use Magento\Customer\Model\Session as CustomerSession;
1611
use Magento\Framework\App\Action\Action;
12+
use Magento\Framework\App\Action\Context;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
1714
use Magento\Framework\Controller\ResultFactory;
15+
use Magento\Framework\Controller\Result\Redirect;
1816
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\UrlInterface;
18+
use Magento\ProductAlert\Controller\Add as AddController;
19+
use Magento\Store\Model\StoreManagerInterface;
1920

2021
/**
2122
* Controller for notifying about price.
@@ -28,15 +29,17 @@ class Price extends AddController implements HttpGetActionInterface
2829
protected $storeManager;
2930

3031
/**
31-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
32+
* @var \Magento\Catalog\Api\ProductRepositoryInterface
3233
*/
3334
protected $productRepository;
3435

3536
/**
36-
* @param \Magento\Framework\App\Action\Context $context
37-
* @param \Magento\Customer\Model\Session $customerSession
38-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
39-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
37+
* Price constructor.
38+
*
39+
* @param Context $context
40+
* @param CustomerSession $customerSession
41+
* @param StoreManagerInterface $storeManager
42+
* @param ProductRepositoryInterface $productRepository
4043
*/
4144
public function __construct(
4245
Context $context,
@@ -54,6 +57,7 @@ public function __construct(
5457
*
5558
* @param string $url
5659
* @return bool
60+
* @throws NoSuchEntityException
5761
*/
5862
protected function isInternal($url)
5963
{
@@ -68,13 +72,14 @@ protected function isInternal($url)
6872
/**
6973
* Method for adding info about product alert price.
7074
*
71-
* @return \Magento\Framework\Controller\Result\Redirect
75+
* @return \Magento\Framework\Controller\ResultInterface
76+
* @throws NoSuchEntityException
7277
*/
7378
public function execute()
7479
{
7580
$backUrl = $this->getRequest()->getParam(Action::PARAM_NAME_URL_ENCODED);
7681
$productId = (int)$this->getRequest()->getParam('product_id');
77-
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
82+
/** @var Redirect $resultRedirect */
7883
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
7984
if (!$backUrl || !$productId) {
8085
$resultRedirect->setPath('/');
@@ -93,17 +98,17 @@ public function execute()
9398
->setWebsiteId($store->getWebsiteId())
9499
->setStoreId($store->getId());
95100
$model->save();
96-
$this->messageManager->addSuccess(__('You saved the alert subscription.'));
101+
$this->messageManager->addSuccessMessage(__('You saved the alert subscription.'));
97102
} catch (NoSuchEntityException $noEntityException) {
98-
$this->messageManager->addError(__('There are not enough parameters.'));
103+
$this->messageManager->addErrorMessage(__('There are not enough parameters.'));
99104
if ($this->isInternal($backUrl)) {
100105
$resultRedirect->setUrl($backUrl);
101106
} else {
102107
$resultRedirect->setPath('/');
103108
}
104109
return $resultRedirect;
105110
} catch (\Exception $e) {
106-
$this->messageManager->addException(
111+
$this->messageManager->addExceptionMessage(
107112
$e,
108113
__("The alert subscription couldn't update at this time. Please try again later.")
109114
);

app/code/Magento/ProductAlert/Controller/Add/Stock.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ public function execute()
7676
->setWebsiteId($store->getWebsiteId())
7777
->setStoreId($store->getId());
7878
$model->save();
79-
$this->messageManager->addSuccess(__('Alert subscription has been saved.'));
79+
$this->messageManager->addSuccessMessage(__('Alert subscription has been saved.'));
8080
} catch (NoSuchEntityException $noEntityException) {
81-
$this->messageManager->addError(__('There are not enough parameters.'));
81+
$this->messageManager->addErrorMessage(__('There are not enough parameters.'));
8282
$resultRedirect->setUrl($backUrl);
8383
return $resultRedirect;
8484
} catch (\Exception $e) {
85-
$this->messageManager->addException(
85+
$this->messageManager->addExceptionMessage(
8686
$e,
8787
__("The alert subscription couldn't update at this time. Please try again later.")
8888
);

app/code/Magento/ProductAlert/Controller/Unsubscribe/Price.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66

77
namespace Magento\ProductAlert\Controller\Unsubscribe;
88

9+
use Magento\Framework\App\Action\HttpGetActionInterface;
910
use Magento\ProductAlert\Controller\Unsubscribe as UnsubscribeController;
1011
use Magento\Framework\App\Action\Context;
1112
use Magento\Customer\Model\Session as CustomerSession;
1213
use Magento\Catalog\Api\ProductRepositoryInterface;
1314
use Magento\Framework\Controller\ResultFactory;
1415
use Magento\Framework\Exception\NoSuchEntityException;
1516

16-
class Price extends UnsubscribeController
17+
/**
18+
* Class Price
19+
*/
20+
class Price extends UnsubscribeController implements HttpGetActionInterface
1721
{
1822
/**
1923
* @var \Magento\Catalog\Api\ProductRepositoryInterface
@@ -35,6 +39,8 @@ public function __construct(
3539
}
3640

3741
/**
42+
* Unsubscribe action
43+
*
3844
* @return \Magento\Framework\Controller\Result\Redirect
3945
*/
4046
public function execute()
@@ -51,8 +57,13 @@ public function execute()
5157
/* @var $product \Magento\Catalog\Model\Product */
5258
$product = $this->productRepository->getById($productId);
5359
if (!$product->isVisibleInCatalog()) {
54-
throw new NoSuchEntityException();
60+
$this->messageManager->addErrorMessage(
61+
__("The product wasn't found. Verify the product and try again.")
62+
);
63+
$resultRedirect->setPath('customer/account/');
64+
return $resultRedirect;
5565
}
66+
5667
/** @var \Magento\ProductAlert\Model\Price $model */
5768
$model = $this->_objectManager->create(\Magento\ProductAlert\Model\Price::class)
5869
->setCustomerId($this->customerSession->getCustomerId())
@@ -67,13 +78,13 @@ public function execute()
6778
$model->delete();
6879
}
6980

70-
$this->messageManager->addSuccess(__('You deleted the alert subscription.'));
81+
$this->messageManager->addSuccessMessage(__('You deleted the alert subscription.'));
7182
} catch (NoSuchEntityException $noEntityException) {
72-
$this->messageManager->addError(__("The product wasn't found. Verify the product and try again."));
83+
$this->messageManager->addErrorMessage(__("The product wasn't found. Verify the product and try again."));
7384
$resultRedirect->setPath('customer/account/');
7485
return $resultRedirect;
7586
} catch (\Exception $e) {
76-
$this->messageManager->addException(
87+
$this->messageManager->addExceptionMessage(
7788
$e,
7889
__("The alert subscription couldn't update at this time. Please try again later.")
7990
);

app/code/Magento/ProductAlert/Controller/Unsubscribe/Stock.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ public function execute()
9494
if ($model->getId()) {
9595
$model->delete();
9696
}
97-
$this->messageManager->addSuccess(__('You will no longer receive stock alerts for this product.'));
97+
$this->messageManager->addSuccessMessage(__('You will no longer receive stock alerts for this product.'));
9898
} catch (NoSuchEntityException $noEntityException) {
99-
$this->messageManager->addError(__('The product was not found.'));
99+
$this->messageManager->addErrorMessage(__('The product was not found.'));
100100
$resultRedirect->setPath('customer/account/');
101101
return $resultRedirect;
102102
} catch (\Exception $e) {
103-
$this->messageManager->addException(
103+
$this->messageManager->addExceptionMessage(
104104
$e,
105105
__("The alert subscription couldn't update at this time. Please try again later.")
106106
);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ProductAlert\Test\Unit\Controller\Unsubscribe;
9+
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Customer\Model\Session;
14+
use Magento\Framework\App\Request\Http;
15+
use Magento\Framework\Controller\Result\Redirect;
16+
use Magento\Framework\Controller\ResultFactory;
17+
use Magento\Framework\Message\Manager;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\ProductAlert\Controller\Unsubscribe\Price;
20+
21+
/**
22+
* Test class for \Magento\ProductAlert\Controller\Unsubscribe\Price
23+
*/
24+
class PriceTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* @var Price
28+
*/
29+
private $priceController;
30+
31+
/**
32+
* @var ObjectManager
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var Http|\PHPUnit\Framework\MockObject\MockObject
38+
*/
39+
private $requestMock;
40+
41+
/**
42+
* @var Redirect|\PHPUnit\Framework\MockObject\MockObject
43+
*/
44+
private $resultRedirectMock;
45+
46+
/**
47+
* @var ResultFactory|\PHPUnit\Framework\MockObject\MockObject
48+
*/
49+
private $resultFactoryMock;
50+
51+
/**
52+
* @var Manager|\PHPUnit\Framework\MockObject\MockObject
53+
*/
54+
private $messageManagerMock;
55+
56+
/**
57+
* @var Product|\PHPUnit\Framework\MockObject\MockObject
58+
*/
59+
private $productMock;
60+
61+
/**
62+
* @var Session|\PHPUnit\Framework\MockObject\MockObject
63+
*/
64+
private $customerSessionMock;
65+
66+
/**
67+
* @var Context|\PHPUnit\Framework\MockObject\MockObject
68+
*/
69+
private $contextMock;
70+
71+
/**
72+
* @var ProductRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
73+
*/
74+
private $productRepositoryMock;
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
protected function setUp()
80+
{
81+
$this->objectManager = new ObjectManager($this);
82+
$this->requestMock = $this->createMock(Http::class);
83+
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
84+
$this->resultRedirectMock = $this->createMock(Redirect::class);
85+
$this->messageManagerMock = $this->createMock(Manager::class);
86+
$this->productMock = $this->createMock(Product::class);
87+
$this->contextMock = $this->createMock(Context::class);
88+
$this->customerSessionMock = $this->createMock(Session::class);
89+
$this->productRepositoryMock = $this->createMock(ProductRepositoryInterface::class);
90+
$this->resultFactoryMock->expects($this->any())
91+
->method('create')
92+
->with(ResultFactory::TYPE_REDIRECT)
93+
->willReturn($this->resultRedirectMock);
94+
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
95+
$this->contextMock->expects($this->any())->method('getResultFactory')->willReturn($this->resultFactoryMock);
96+
$this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
97+
98+
$this->priceController = $this->objectManager->getObject(
99+
Price::class,
100+
[
101+
'context' => $this->contextMock,
102+
'customerSession' => $this->customerSessionMock,
103+
'productRepository' => $this->productRepositoryMock,
104+
]
105+
);
106+
}
107+
108+
public function testProductIsNotVisibleInCatalog()
109+
{
110+
$productId = 123;
111+
$this->requestMock->expects($this->any())->method('getParam')->with('product')->willReturn($productId);
112+
$this->productRepositoryMock->expects($this->any())
113+
->method('getById')
114+
->with($productId)
115+
->willReturn($this->productMock);
116+
$this->productMock->expects($this->any())->method('isVisibleInCatalog')->willReturn(false);
117+
$this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with(__("The product wasn't found. Verify the product and try again."));
118+
$this->resultRedirectMock->expects($this->once())->method('setPath')->with('customer/account/');
119+
120+
$this->assertEquals(
121+
$this->resultRedirectMock,
122+
$this->priceController->execute()
123+
);
124+
}
125+
}

0 commit comments

Comments
 (0)