6
6
7
7
namespace Magento \Catalog \Controller \Product ;
8
8
9
- use Magento \Framework \ Message \ MessageInterface ;
9
+ use Magento \Catalog \ Model \ ProductRepository ;
10
10
use 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 ;
11
17
12
18
/**
13
- * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
19
+ * Test compare product.
14
20
*
21
+ * @magentoDataFixture Magento/Catalog/controllers/_files/products.php
15
22
* @magentoDbIsolation disabled
16
- *
17
23
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
24
*/
19
- class CompareTest extends \ Magento \ TestFramework \ TestCase \ AbstractController
25
+ class CompareTest extends AbstractController
20
26
{
21
- /**
22
- * @var \Magento\Catalog\Model\ProductRepository
23
- */
27
+ /** @var ProductRepository */
24
28
protected $ productRepository ;
25
29
26
- /**
27
- * @var \Magento\Framework\Data\Form\FormKey
28
- */
30
+ /** @var FormKey */
29
31
private $ formKey ;
30
32
33
+ /** @var Session */
34
+ private $ customerSession ;
35
+
36
+ /** @var Visitor */
37
+ private $ visitor ;
38
+
39
+ /** @var ParametersFactory */
40
+ private $ parametersFactory ;
41
+
31
42
/**
32
43
* @inheritDoc
33
44
*/
34
45
protected function setUp (): void
35
46
{
36
47
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 ();
39
65
}
40
66
41
67
/**
42
68
* Test adding product to compare list.
43
69
*
44
- * @throws \Magento\Framework\Exception\NoSuchEntityException
70
+ * @return void
45
71
*/
46
- public function testAddAction ()
72
+ public function testAddAction (): void
47
73
{
48
74
$ this ->_requireVisitorWithNoProducts ();
49
75
$ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -99,9 +125,9 @@ public function testAddActionForDisabledProduct(): void
99
125
/**
100
126
* Test removing a product from compare list.
101
127
*
102
- * @throws \Magento\Framework\Exception\NoSuchEntityException
128
+ * @return void
103
129
*/
104
- public function testRemoveAction ()
130
+ public function testRemoveAction (): void
105
131
{
106
132
$ this ->_requireVisitorWithTwoProducts ();
107
133
$ product = $ this ->productRepository ->get ('simple_product_2 ' );
@@ -139,9 +165,9 @@ public function testRemoveActionForDisabledProduct(): void
139
165
/**
140
166
* Test removing a product from compare list of a registered customer.
141
167
*
142
- * @throws \Magento\Framework\Exception\NoSuchEntityException
168
+ * @return void
143
169
*/
144
- public function testRemoveActionWithSession ()
170
+ public function testRemoveActionWithSession (): void
145
171
{
146
172
$ this ->_requireCustomerWithTwoProducts ();
147
173
$ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -161,8 +187,10 @@ public function testRemoveActionWithSession()
161
187
162
188
/**
163
189
* Test getting a list of compared product.
190
+ *
191
+ * @return void
164
192
*/
165
- public function testIndexActionDisplay ()
193
+ public function testIndexActionDisplay (): void
166
194
{
167
195
$ this ->_requireVisitorWithTwoProducts ();
168
196
@@ -190,8 +218,10 @@ public function testIndexActionDisplay()
190
218
191
219
/**
192
220
* Test clearing a list of compared products.
221
+ *
222
+ * @return void
193
223
*/
194
- public function testClearAction ()
224
+ public function testClearAction (): void
195
225
{
196
226
$ this ->_requireVisitorWithTwoProducts ();
197
227
@@ -212,8 +242,9 @@ public function testClearAction()
212
242
* Test escaping a session message.
213
243
*
214
244
* @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
245
+ * @return void
215
246
*/
216
- public function testRemoveActionProductNameXss ()
247
+ public function testRemoveActionProductNameXss (): void
217
248
{
218
249
$ this ->_prepareCompareListWithProductNameXss ();
219
250
$ product = $ this ->productRepository ->get ('product-with-xss ' );
@@ -228,6 +259,37 @@ public function testRemoveActionProductNameXss()
228
259
);
229
260
}
230
261
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
+
231
293
/**
232
294
* Set product status disabled.
233
295
*
@@ -246,10 +308,9 @@ private function setProductDisabled(string $sku): \Magento\Catalog\Api\Data\Prod
246
308
/**
247
309
* Preparing compare list.
248
310
*
249
- * @throws \Magento\Framework\Exception\LocalizedException
250
- * @throws \Magento\Framework\Exception\NoSuchEntityException
311
+ * @return void
251
312
*/
252
- protected function _prepareCompareListWithProductNameXss ()
313
+ protected function _prepareCompareListWithProductNameXss (): void
253
314
{
254
315
/** @var $visitor \Magento\Customer\Model\Visitor */
255
316
$ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -275,9 +336,9 @@ protected function _prepareCompareListWithProductNameXss()
275
336
/**
276
337
* Preparing compare list.
277
338
*
278
- * @throws \Magento\Framework\Exception\LocalizedException
339
+ * @return void
279
340
*/
280
- protected function _requireVisitorWithNoProducts ()
341
+ protected function _requireVisitorWithNoProducts (): void
281
342
{
282
343
/** @var $visitor \Magento\Customer\Model\Visitor */
283
344
$ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -300,10 +361,9 @@ protected function _requireVisitorWithNoProducts()
300
361
/**
301
362
* Preparing compare list.
302
363
*
303
- * @throws \Magento\Framework\Exception\LocalizedException
304
- * @throws \Magento\Framework\Exception\NoSuchEntityException
364
+ * @return void
305
365
*/
306
- protected function _requireVisitorWithTwoProducts ()
366
+ protected function _requireVisitorWithTwoProducts (): void
307
367
{
308
368
/** @var $visitor \Magento\Customer\Model\Visitor */
309
369
$ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -339,10 +399,9 @@ protected function _requireVisitorWithTwoProducts()
339
399
/**
340
400
* Preparing a compare list.
341
401
*
342
- * @throws \Magento\Framework\Exception\LocalizedException
343
- * @throws \Magento\Framework\Exception\NoSuchEntityException
402
+ * @return void
344
403
*/
345
- protected function _requireCustomerWithTwoProducts ()
404
+ protected function _requireCustomerWithTwoProducts (): void
346
405
{
347
406
$ customer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
348
407
->create (\Magento \Customer \Model \Customer::class);
@@ -405,8 +464,9 @@ protected function _requireCustomerWithTwoProducts()
405
464
* Assert that current visitor has exactly expected products in compare list
406
465
*
407
466
* @param array $expectedProductIds
467
+ * @return void
408
468
*/
409
- protected function _assertCompareListEquals (array $ expectedProductIds )
469
+ protected function _assertCompareListEquals (array $ expectedProductIds ): void
410
470
{
411
471
/** @var $compareItems \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection */
412
472
$ compareItems = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
0 commit comments