66
77namespace Magento \Catalog \Controller \Product ;
88
9- use Magento \Framework \ Message \ MessageInterface ;
9+ use Magento \Catalog \ Model \ ProductRepository ;
1010use Magento \Framework \App \Request \Http as HttpRequest ;
11+ use Magento \Framework \Data \Form \FormKey ;
12+ use Magento \Framework \Message \MessageInterface ;
13+ use Magento \TestFramework \TestCase \AbstractController ;
14+ use Magento \Customer \Model \Session ;
15+ use Magento \Customer \Model \Visitor ;
16+ use Laminas \Stdlib \ParametersFactory ;
1117
1218/**
13- * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
19+ * Test compare product.
1420 *
21+ * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
1522 * @magentoDbIsolation disabled
16- *
1723 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1824 */
19- class CompareTest extends \ Magento \ TestFramework \ TestCase \ AbstractController
25+ class CompareTest extends AbstractController
2026{
21- /**
22- * @var \Magento\Catalog\Model\ProductRepository
23- */
27+ /** @var ProductRepository */
2428 protected $ productRepository ;
2529
26- /**
27- * @var \Magento\Framework\Data\Form\FormKey
28- */
30+ /** @var FormKey */
2931 private $ formKey ;
3032
33+ /** @var Session */
34+ private $ customerSession ;
35+
36+ /** @var Visitor */
37+ private $ visitor ;
38+
39+ /** @var ParametersFactory */
40+ private $ parametersFactory ;
41+
3142 /**
3243 * @inheritDoc
3344 */
3445 protected function setUp (): void
3546 {
3647 parent ::setUp ();
37- $ this ->formKey = $ this ->_objectManager ->get (\Magento \Framework \Data \Form \FormKey::class);
38- $ this ->productRepository = $ this ->_objectManager ->create (\Magento \Catalog \Model \ProductRepository::class);
48+
49+ $ this ->formKey = $ this ->_objectManager ->get (FormKey::class);
50+ $ this ->productRepository = $ this ->_objectManager ->get (ProductRepository::class);
51+ $ this ->customerSession = $ this ->_objectManager ->get (Session::class);
52+ $ this ->visitor = $ this ->_objectManager ->get (Visitor::class);
53+ $ this ->parametersFactory = $ this ->_objectManager ->get (ParametersFactory::class);
54+ }
55+
56+ /**
57+ * @inheritdoc
58+ */
59+ protected function tearDown (): void
60+ {
61+ $ this ->customerSession ->logout ();
62+ $ this ->visitor ->setId (null );
63+
64+ parent ::tearDown ();
3965 }
4066
4167 /**
4268 * Test adding product to compare list.
4369 *
44- * @throws \Magento\Framework\Exception\NoSuchEntityException
70+ * @return void
4571 */
46- public function testAddAction ()
72+ public function testAddAction (): void
4773 {
4874 $ this ->_requireVisitorWithNoProducts ();
4975 $ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -99,9 +125,9 @@ public function testAddActionForDisabledProduct(): void
99125 /**
100126 * Test removing a product from compare list.
101127 *
102- * @throws \Magento\Framework\Exception\NoSuchEntityException
128+ * @return void
103129 */
104- public function testRemoveAction ()
130+ public function testRemoveAction (): void
105131 {
106132 $ this ->_requireVisitorWithTwoProducts ();
107133 $ product = $ this ->productRepository ->get ('simple_product_2 ' );
@@ -139,9 +165,9 @@ public function testRemoveActionForDisabledProduct(): void
139165 /**
140166 * Test removing a product from compare list of a registered customer.
141167 *
142- * @throws \Magento\Framework\Exception\NoSuchEntityException
168+ * @return void
143169 */
144- public function testRemoveActionWithSession ()
170+ public function testRemoveActionWithSession (): void
145171 {
146172 $ this ->_requireCustomerWithTwoProducts ();
147173 $ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -161,8 +187,10 @@ public function testRemoveActionWithSession()
161187
162188 /**
163189 * Test getting a list of compared product.
190+ *
191+ * @return void
164192 */
165- public function testIndexActionDisplay ()
193+ public function testIndexActionDisplay (): void
166194 {
167195 $ this ->_requireVisitorWithTwoProducts ();
168196
@@ -190,8 +218,10 @@ public function testIndexActionDisplay()
190218
191219 /**
192220 * Test clearing a list of compared products.
221+ *
222+ * @return void
193223 */
194- public function testClearAction ()
224+ public function testClearAction (): void
195225 {
196226 $ this ->_requireVisitorWithTwoProducts ();
197227
@@ -212,8 +242,9 @@ public function testClearAction()
212242 * Test escaping a session message.
213243 *
214244 * @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
245+ * @return void
215246 */
216- public function testRemoveActionProductNameXss ()
247+ public function testRemoveActionProductNameXss (): void
217248 {
218249 $ this ->_prepareCompareListWithProductNameXss ();
219250 $ product = $ this ->productRepository ->get ('product-with-xss ' );
@@ -228,6 +259,37 @@ public function testRemoveActionProductNameXss()
228259 );
229260 }
230261
262+ /**
263+ * Add not existing product to list of compared.
264+ *
265+ * @magentoAppIsolation enabled
266+ * @magentoDataFixture Magento/Customer/_files/customer.php
267+ * @return void
268+ */
269+ public function testAddNotExistingProductToCompareList (): void
270+ {
271+ $ this ->customerSession ->loginById (1 );
272+ $ this ->prepareReferer ();
273+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
274+ $ this ->getRequest ()->setParams (['product ' => 787586534 ]);
275+ $ this ->dispatch ('catalog/product_compare/add/ ' );
276+ $ this ->assertSessionMessages ($ this ->isEmpty ());
277+ $ this ->_assertCompareListEquals ([]);
278+ $ this ->assertRedirect ($ this ->stringContains ('not_existing ' ));
279+ }
280+
281+ /**
282+ * Prepare referer to test.
283+ *
284+ * @return void
285+ */
286+ private function prepareReferer (): void
287+ {
288+ $ parameters = $ this ->parametersFactory ->create ();
289+ $ parameters ->set ('HTTP_REFERER ' , 'http://localhost/not_existing ' );
290+ $ this ->getRequest ()->setServer ($ parameters );
291+ }
292+
231293 /**
232294 * Set product status disabled.
233295 *
@@ -246,10 +308,9 @@ private function setProductDisabled(string $sku): \Magento\Catalog\Api\Data\Prod
246308 /**
247309 * Preparing compare list.
248310 *
249- * @throws \Magento\Framework\Exception\LocalizedException
250- * @throws \Magento\Framework\Exception\NoSuchEntityException
311+ * @return void
251312 */
252- protected function _prepareCompareListWithProductNameXss ()
313+ protected function _prepareCompareListWithProductNameXss (): void
253314 {
254315 /** @var $visitor \Magento\Customer\Model\Visitor */
255316 $ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -275,9 +336,9 @@ protected function _prepareCompareListWithProductNameXss()
275336 /**
276337 * Preparing compare list.
277338 *
278- * @throws \Magento\Framework\Exception\LocalizedException
339+ * @return void
279340 */
280- protected function _requireVisitorWithNoProducts ()
341+ protected function _requireVisitorWithNoProducts (): void
281342 {
282343 /** @var $visitor \Magento\Customer\Model\Visitor */
283344 $ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -300,10 +361,9 @@ protected function _requireVisitorWithNoProducts()
300361 /**
301362 * Preparing compare list.
302363 *
303- * @throws \Magento\Framework\Exception\LocalizedException
304- * @throws \Magento\Framework\Exception\NoSuchEntityException
364+ * @return void
305365 */
306- protected function _requireVisitorWithTwoProducts ()
366+ protected function _requireVisitorWithTwoProducts (): void
307367 {
308368 /** @var $visitor \Magento\Customer\Model\Visitor */
309369 $ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -339,10 +399,9 @@ protected function _requireVisitorWithTwoProducts()
339399 /**
340400 * Preparing a compare list.
341401 *
342- * @throws \Magento\Framework\Exception\LocalizedException
343- * @throws \Magento\Framework\Exception\NoSuchEntityException
402+ * @return void
344403 */
345- protected function _requireCustomerWithTwoProducts ()
404+ protected function _requireCustomerWithTwoProducts (): void
346405 {
347406 $ customer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
348407 ->create (\Magento \Customer \Model \Customer::class);
@@ -405,8 +464,9 @@ protected function _requireCustomerWithTwoProducts()
405464 * Assert that current visitor has exactly expected products in compare list
406465 *
407466 * @param array $expectedProductIds
467+ * @return void
408468 */
409- protected function _assertCompareListEquals (array $ expectedProductIds )
469+ protected function _assertCompareListEquals (array $ expectedProductIds ): void
410470 {
411471 /** @var $compareItems \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection */
412472 $ compareItems = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
0 commit comments