6
6
7
7
namespace Magento \Catalog \Controller \Product ;
8
8
9
+ use Laminas \Stdlib \ParametersFactory ;
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
9
11
use Magento \Catalog \Model \ProductRepository ;
12
+ use Magento \Customer \Model \Session ;
13
+ use Magento \Customer \Model \Visitor ;
10
14
use Magento \Framework \App \Request \Http as HttpRequest ;
11
15
use Magento \Framework \Data \Form \FormKey ;
12
16
use Magento \Framework \Message \MessageInterface ;
17
+ use Magento \Framework \Registry ;
13
18
use Magento \TestFramework \TestCase \AbstractController ;
14
- use Magento \Customer \Model \Session ;
15
- use Magento \Customer \Model \Visitor ;
16
- use Laminas \Stdlib \ParametersFactory ;
17
19
18
20
/**
19
21
* Test compare product.
@@ -39,6 +41,9 @@ class CompareTest extends AbstractController
39
41
/** @var ParametersFactory */
40
42
private $ parametersFactory ;
41
43
44
+ /** @var Registry */
45
+ private $ registry ;
46
+
42
47
/**
43
48
* @inheritDoc
44
49
*/
@@ -51,6 +56,7 @@ protected function setUp(): void
51
56
$ this ->customerSession = $ this ->_objectManager ->get (Session::class);
52
57
$ this ->visitor = $ this ->_objectManager ->get (Visitor::class);
53
58
$ this ->parametersFactory = $ this ->_objectManager ->get (ParametersFactory::class);
59
+ $ this ->registry = $ this ->_objectManager ->get (Registry::class);
54
60
}
55
61
56
62
/**
@@ -85,7 +91,7 @@ public function testAddAction(): void
85
91
$ this ->assertSessionMessages (
86
92
$ this ->equalTo (
87
93
[
88
- 'You added product Simple Product 1 Name to the ' .
94
+ 'You added product Simple Product 1 Name to the ' .
89
95
'<a href="http://localhost/index.php/catalog/product_compare/">comparison list</a>. '
90
96
]
91
97
),
@@ -259,6 +265,31 @@ public function testRemoveActionProductNameXss(): void
259
265
);
260
266
}
261
267
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
+
262
293
/**
263
294
* Add not existing product to list of compared.
264
295
*
@@ -486,4 +517,27 @@ protected function _assertCompareListEquals(array $expectedProductIds): void
486
517
}
487
518
$ this ->assertEquals ($ expectedProductIds , $ actualProductIds , "Products in current visitor's compare list. " );
488
519
}
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
+ }
489
543
}
0 commit comments