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 \Parameters ;
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+
3139 /**
3240 * @inheritDoc
3341 */
3442 protected function setUp (): void
3543 {
3644 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);
45+
46+ $ this ->formKey = $ this ->_objectManager ->get (FormKey::class);
47+ $ this ->productRepository = $ this ->_objectManager ->get (ProductRepository::class);
48+ $ this ->customerSession = $ this ->_objectManager ->get (Session::class);
49+ $ this ->visitor = $ this ->_objectManager ->get (Visitor::class);
50+ }
51+
52+ /**
53+ * @inheritdoc
54+ */
55+ protected function tearDown (): void
56+ {
57+ $ this ->customerSession ->logout ();
58+ $ this ->visitor ->setId (null );
59+
60+ parent ::tearDown ();
3961 }
4062
4163 /**
4264 * Test adding product to compare list.
4365 *
44- * @throws \Magento\Framework\Exception\NoSuchEntityException
66+ * @return void
4567 */
46- public function testAddAction ()
68+ public function testAddAction (): void
4769 {
4870 $ this ->_requireVisitorWithNoProducts ();
4971 $ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -99,9 +121,9 @@ public function testAddActionForDisabledProduct(): void
99121 /**
100122 * Test removing a product from compare list.
101123 *
102- * @throws \Magento\Framework\Exception\NoSuchEntityException
124+ * @return void
103125 */
104- public function testRemoveAction ()
126+ public function testRemoveAction (): void
105127 {
106128 $ this ->_requireVisitorWithTwoProducts ();
107129 $ product = $ this ->productRepository ->get ('simple_product_2 ' );
@@ -139,9 +161,9 @@ public function testRemoveActionForDisabledProduct(): void
139161 /**
140162 * Test removing a product from compare list of a registered customer.
141163 *
142- * @throws \Magento\Framework\Exception\NoSuchEntityException
164+ * @return void
143165 */
144- public function testRemoveActionWithSession ()
166+ public function testRemoveActionWithSession (): void
145167 {
146168 $ this ->_requireCustomerWithTwoProducts ();
147169 $ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -161,8 +183,10 @@ public function testRemoveActionWithSession()
161183
162184 /**
163185 * Test getting a list of compared product.
186+ *
187+ * @return void
164188 */
165- public function testIndexActionDisplay ()
189+ public function testIndexActionDisplay (): void
166190 {
167191 $ this ->_requireVisitorWithTwoProducts ();
168192
@@ -190,8 +214,10 @@ public function testIndexActionDisplay()
190214
191215 /**
192216 * Test clearing a list of compared products.
217+ *
218+ * @return void
193219 */
194- public function testClearAction ()
220+ public function testClearAction (): void
195221 {
196222 $ this ->_requireVisitorWithTwoProducts ();
197223
@@ -212,8 +238,9 @@ public function testClearAction()
212238 * Test escaping a session message.
213239 *
214240 * @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
241+ * @return void
215242 */
216- public function testRemoveActionProductNameXss ()
243+ public function testRemoveActionProductNameXss (): void
217244 {
218245 $ this ->_prepareCompareListWithProductNameXss ();
219246 $ product = $ this ->productRepository ->get ('product-with-xss ' );
@@ -228,6 +255,36 @@ public function testRemoveActionProductNameXss()
228255 );
229256 }
230257
258+ /**
259+ * Add not existing product to list of compared.
260+ *
261+ * @magentoAppIsolation enabled
262+ * @magentoDataFixture Magento/Customer/_files/customer.php
263+ * @return void
264+ */
265+ public function testAddNotExistingProductToCompactionList () : void
266+ {
267+ $ this ->customerSession ->loginById (1 );
268+ $ this ->prepareReferer ();
269+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
270+ $ this ->getRequest ()->setParams (['product ' => 787586534 ]);
271+ $ this ->dispatch ('catalog/product_compare/add/ ' );
272+ $ this ->_assertCompareListEquals ([]);
273+ $ this ->assertRedirect ($ this ->stringContains ('not_existing ' ));
274+ }
275+
276+ /**
277+ * Prepare referer to test.
278+ *
279+ * @return void
280+ */
281+ private function prepareReferer (): void
282+ {
283+ $ parameters = $ this ->_objectManager ->create (Parameters::class);
284+ $ parameters ->set ('HTTP_REFERER ' , 'http://localhost/not_existing ' );
285+ $ this ->getRequest ()->setServer ($ parameters );
286+ }
287+
231288 /**
232289 * Set product status disabled.
233290 *
@@ -246,10 +303,9 @@ private function setProductDisabled(string $sku): \Magento\Catalog\Api\Data\Prod
246303 /**
247304 * Preparing compare list.
248305 *
249- * @throws \Magento\Framework\Exception\LocalizedException
250- * @throws \Magento\Framework\Exception\NoSuchEntityException
306+ * @return void
251307 */
252- protected function _prepareCompareListWithProductNameXss ()
308+ protected function _prepareCompareListWithProductNameXss (): void
253309 {
254310 /** @var $visitor \Magento\Customer\Model\Visitor */
255311 $ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -275,9 +331,9 @@ protected function _prepareCompareListWithProductNameXss()
275331 /**
276332 * Preparing compare list.
277333 *
278- * @throws \Magento\Framework\Exception\LocalizedException
334+ * @return void
279335 */
280- protected function _requireVisitorWithNoProducts ()
336+ protected function _requireVisitorWithNoProducts (): void
281337 {
282338 /** @var $visitor \Magento\Customer\Model\Visitor */
283339 $ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -300,10 +356,9 @@ protected function _requireVisitorWithNoProducts()
300356 /**
301357 * Preparing compare list.
302358 *
303- * @throws \Magento\Framework\Exception\LocalizedException
304- * @throws \Magento\Framework\Exception\NoSuchEntityException
359+ * @return void
305360 */
306- protected function _requireVisitorWithTwoProducts ()
361+ protected function _requireVisitorWithTwoProducts (): void
307362 {
308363 /** @var $visitor \Magento\Customer\Model\Visitor */
309364 $ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -339,10 +394,9 @@ protected function _requireVisitorWithTwoProducts()
339394 /**
340395 * Preparing a compare list.
341396 *
342- * @throws \Magento\Framework\Exception\LocalizedException
343- * @throws \Magento\Framework\Exception\NoSuchEntityException
397+ * @return void
344398 */
345- protected function _requireCustomerWithTwoProducts ()
399+ protected function _requireCustomerWithTwoProducts (): void
346400 {
347401 $ customer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
348402 ->create (\Magento \Customer \Model \Customer::class);
@@ -405,8 +459,9 @@ protected function _requireCustomerWithTwoProducts()
405459 * Assert that current visitor has exactly expected products in compare list
406460 *
407461 * @param array $expectedProductIds
462+ * @return void
408463 */
409- protected function _assertCompareListEquals (array $ expectedProductIds )
464+ protected function _assertCompareListEquals (array $ expectedProductIds ): void
410465 {
411466 /** @var $compareItems \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection */
412467 $ compareItems = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
0 commit comments