66
77namespace Magento \Catalog \Controller \Product ;
88
9+ use Laminas \Stdlib \ParametersFactory ;
10+ use Magento \Catalog \Api \Data \ProductInterface ;
911use Magento \Catalog \Model \ProductRepository ;
12+ use Magento \Customer \Model \Session ;
13+ use Magento \Customer \Model \Visitor ;
1014use Magento \Framework \App \Request \Http as HttpRequest ;
1115use Magento \Framework \Data \Form \FormKey ;
1216use Magento \Framework \Message \MessageInterface ;
17+ use Magento \Framework \Registry ;
1318use Magento \TestFramework \TestCase \AbstractController ;
14- use Magento \Customer \Model \Session ;
15- use Magento \Customer \Model \Visitor ;
16- use Laminas \Stdlib \ParametersFactory ;
1719
1820/**
1921 * Test compare product.
@@ -39,6 +41,9 @@ class CompareTest extends AbstractController
3941 /** @var ParametersFactory */
4042 private $ parametersFactory ;
4143
44+ /** @var Registry */
45+ private $ registry ;
46+
4247 /**
4348 * @inheritDoc
4449 */
@@ -51,6 +56,7 @@ protected function setUp(): void
5156 $ this ->customerSession = $ this ->_objectManager ->get (Session::class);
5257 $ this ->visitor = $ this ->_objectManager ->get (Visitor::class);
5358 $ this ->parametersFactory = $ this ->_objectManager ->get (ParametersFactory::class);
59+ $ this ->registry = $ this ->_objectManager ->get (Registry::class);
5460 }
5561
5662 /**
@@ -85,7 +91,7 @@ public function testAddAction(): void
8591 $ this ->assertSessionMessages (
8692 $ this ->equalTo (
8793 [
88- 'You added product Simple Product 1 Name to the ' .
94+ 'You added product Simple Product 1 Name to the ' .
8995 '<a href="http://localhost/index.php/catalog/product_compare/">comparison list</a>. '
9096 ]
9197 ),
@@ -259,6 +265,31 @@ public function testRemoveActionProductNameXss(): void
259265 );
260266 }
261267
268+ /**
269+ * Test removing a product wich does not exist from compare list.
270+ *
271+ * @return void
272+ */
273+ public function testRemoveActionWithNonExistentProduct (): void
274+ {
275+ $ this ->_requireVisitorWithTwoProducts ();
276+ $ removedProduct = $ this ->productRepository ->get ('simple_product_1 ' );
277+ $ redirectUrl = 'http://localhost/index.php/catalog/product_compare/index ' ;
278+ $ this ->assertTrue ($ this ->deleteProduct ($ removedProduct ), "The product must be removed. " );
279+
280+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
281+ $ this ->getRequest ()->setParams (['product ' => $ removedProduct ->getId ()]);
282+ $ server = $ this ->getRequest ()->getServer ();
283+ $ server ['HTTP_REFERER ' ] = $ redirectUrl ;
284+ $ this ->getRequest ()->setServer ($ server );
285+ $ this ->dispatch ('catalog/product_compare/remove/ ' );
286+
287+ $ this ->assertSessionMessages ($ this ->isEmpty ());
288+ $ this ->assertRedirect ($ this ->equalTo ($ redirectUrl ));
289+ $ restProduct = $ this ->productRepository ->get ('simple_product_2 ' );
290+ $ this ->_assertCompareListEquals ([$ restProduct ->getId ()]);
291+ }
292+
262293 /**
263294 * Add not existing product to list of compared.
264295 *
@@ -486,4 +517,27 @@ protected function _assertCompareListEquals(array $expectedProductIds): void
486517 }
487518 $ this ->assertEquals ($ expectedProductIds , $ actualProductIds , "Products in current visitor's compare list. " );
488519 }
520+
521+ /**
522+ * Delete product in secure area
523+ *
524+ * @param ProductInterface $product
525+ * @return bool
526+ */
527+ private function deleteProduct (ProductInterface $ product ): bool
528+ {
529+ $ this ->registry ->unregister ('isSecureArea ' );
530+ $ this ->registry ->register ('isSecureArea ' , true );
531+
532+ try {
533+ $ result = $ this ->productRepository ->delete ($ product );
534+ } catch (\Exception $ e ) {
535+ $ result = false ;
536+ }
537+
538+ $ this ->registry ->unregister ('isSecureArea ' );
539+ $ this ->registry ->register ('isSecureArea ' , false );
540+
541+ return $ result ;
542+ }
489543}
0 commit comments