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 \Parameters ;
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
+
31
39
/**
32
40
* @inheritDoc
33
41
*/
34
42
protected function setUp (): void
35
43
{
36
44
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 ();
39
61
}
40
62
41
63
/**
42
64
* Test adding product to compare list.
43
65
*
44
- * @throws \Magento\Framework\Exception\NoSuchEntityException
66
+ * @return void
45
67
*/
46
- public function testAddAction ()
68
+ public function testAddAction (): void
47
69
{
48
70
$ this ->_requireVisitorWithNoProducts ();
49
71
$ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -99,9 +121,9 @@ public function testAddActionForDisabledProduct(): void
99
121
/**
100
122
* Test removing a product from compare list.
101
123
*
102
- * @throws \Magento\Framework\Exception\NoSuchEntityException
124
+ * @return void
103
125
*/
104
- public function testRemoveAction ()
126
+ public function testRemoveAction (): void
105
127
{
106
128
$ this ->_requireVisitorWithTwoProducts ();
107
129
$ product = $ this ->productRepository ->get ('simple_product_2 ' );
@@ -139,9 +161,9 @@ public function testRemoveActionForDisabledProduct(): void
139
161
/**
140
162
* Test removing a product from compare list of a registered customer.
141
163
*
142
- * @throws \Magento\Framework\Exception\NoSuchEntityException
164
+ * @return void
143
165
*/
144
- public function testRemoveActionWithSession ()
166
+ public function testRemoveActionWithSession (): void
145
167
{
146
168
$ this ->_requireCustomerWithTwoProducts ();
147
169
$ product = $ this ->productRepository ->get ('simple_product_1 ' );
@@ -161,8 +183,10 @@ public function testRemoveActionWithSession()
161
183
162
184
/**
163
185
* Test getting a list of compared product.
186
+ *
187
+ * @return void
164
188
*/
165
- public function testIndexActionDisplay ()
189
+ public function testIndexActionDisplay (): void
166
190
{
167
191
$ this ->_requireVisitorWithTwoProducts ();
168
192
@@ -190,8 +214,10 @@ public function testIndexActionDisplay()
190
214
191
215
/**
192
216
* Test clearing a list of compared products.
217
+ *
218
+ * @return void
193
219
*/
194
- public function testClearAction ()
220
+ public function testClearAction (): void
195
221
{
196
222
$ this ->_requireVisitorWithTwoProducts ();
197
223
@@ -212,8 +238,9 @@ public function testClearAction()
212
238
* Test escaping a session message.
213
239
*
214
240
* @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
241
+ * @return void
215
242
*/
216
- public function testRemoveActionProductNameXss ()
243
+ public function testRemoveActionProductNameXss (): void
217
244
{
218
245
$ this ->_prepareCompareListWithProductNameXss ();
219
246
$ product = $ this ->productRepository ->get ('product-with-xss ' );
@@ -228,6 +255,36 @@ public function testRemoveActionProductNameXss()
228
255
);
229
256
}
230
257
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
+
231
288
/**
232
289
* Set product status disabled.
233
290
*
@@ -246,10 +303,9 @@ private function setProductDisabled(string $sku): \Magento\Catalog\Api\Data\Prod
246
303
/**
247
304
* Preparing compare list.
248
305
*
249
- * @throws \Magento\Framework\Exception\LocalizedException
250
- * @throws \Magento\Framework\Exception\NoSuchEntityException
306
+ * @return void
251
307
*/
252
- protected function _prepareCompareListWithProductNameXss ()
308
+ protected function _prepareCompareListWithProductNameXss (): void
253
309
{
254
310
/** @var $visitor \Magento\Customer\Model\Visitor */
255
311
$ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -275,9 +331,9 @@ protected function _prepareCompareListWithProductNameXss()
275
331
/**
276
332
* Preparing compare list.
277
333
*
278
- * @throws \Magento\Framework\Exception\LocalizedException
334
+ * @return void
279
335
*/
280
- protected function _requireVisitorWithNoProducts ()
336
+ protected function _requireVisitorWithNoProducts (): void
281
337
{
282
338
/** @var $visitor \Magento\Customer\Model\Visitor */
283
339
$ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -300,10 +356,9 @@ protected function _requireVisitorWithNoProducts()
300
356
/**
301
357
* Preparing compare list.
302
358
*
303
- * @throws \Magento\Framework\Exception\LocalizedException
304
- * @throws \Magento\Framework\Exception\NoSuchEntityException
359
+ * @return void
305
360
*/
306
- protected function _requireVisitorWithTwoProducts ()
361
+ protected function _requireVisitorWithTwoProducts (): void
307
362
{
308
363
/** @var $visitor \Magento\Customer\Model\Visitor */
309
364
$ visitor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
@@ -339,10 +394,9 @@ protected function _requireVisitorWithTwoProducts()
339
394
/**
340
395
* Preparing a compare list.
341
396
*
342
- * @throws \Magento\Framework\Exception\LocalizedException
343
- * @throws \Magento\Framework\Exception\NoSuchEntityException
397
+ * @return void
344
398
*/
345
- protected function _requireCustomerWithTwoProducts ()
399
+ protected function _requireCustomerWithTwoProducts (): void
346
400
{
347
401
$ customer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
348
402
->create (\Magento \Customer \Model \Customer::class);
@@ -405,8 +459,9 @@ protected function _requireCustomerWithTwoProducts()
405
459
* Assert that current visitor has exactly expected products in compare list
406
460
*
407
461
* @param array $expectedProductIds
462
+ * @return void
408
463
*/
409
- protected function _assertCompareListEquals (array $ expectedProductIds )
464
+ protected function _assertCompareListEquals (array $ expectedProductIds ): void
410
465
{
411
466
/** @var $compareItems \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection */
412
467
$ compareItems = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
0 commit comments