7
7
// @codingStandardsIgnoreFile
8
8
9
9
namespace Magento \Catalog \Controller \Product ;
10
+ use Magento \Framework \Message \MessageInterface ;
10
11
11
12
/**
12
13
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
13
14
*/
14
15
class CompareTest extends \Magento \TestFramework \TestCase \AbstractController
15
16
{
17
+ /**
18
+ * @var \Magento\Catalog\Model\ProductRepository
19
+ */
20
+ protected $ productRepository ;
21
+
22
+ protected function setUp ()
23
+ {
24
+ parent ::setUp ();
25
+
26
+ /** @var $objectManager \Magento\TestFramework\ObjectManager */
27
+ $ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
28
+
29
+ $ this ->productRepository = $ objectManager ->create ('Magento\Catalog\Model\ProductRepository ' );
30
+ }
31
+
16
32
public function testAddAction ()
17
33
{
18
34
$ this ->_requireVisitorWithNoProducts ();
19
35
$ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
20
36
/** @var \Magento\Framework\Data\Form\FormKey $formKey */
21
37
$ formKey = $ objectManager ->get ('Magento\Framework\Data\Form\FormKey ' );
22
-
23
- $ this ->dispatch ('catalog/product_compare/add/product/1/form_key/ ' . $ formKey ->getFormKey () . '?nocookie=1 ' );
24
-
25
- /** @var $messageManager \Magento\Framework\Message\Manager */
26
- $ messageManager = $ objectManager ->get ('Magento\Framework\Message\Manager ' );
27
- $ this ->assertInstanceOf (
28
- 'Magento\Framework\Message\Success ' ,
29
- $ messageManager ->getMessages ()->getLastAddedMessage ()
30
- );
31
- $ this ->assertContains (
32
- 'Simple Product 1 Name ' ,
33
- (string )$ messageManager ->getMessages ()->getLastAddedMessage ()->getText ()
38
+ $ product = $ this ->productRepository ->get ('simple_product_1 ' );
39
+ $ this ->dispatch (
40
+ sprintf (
41
+ 'catalog/product_compare/add/product/%s/form_key/%s?nocookie=1 ' ,
42
+ $ product ->getEntityId (),
43
+ $ formKey ->getFormKey ()
44
+ )
34
45
);
35
46
47
+ $ this ->assertSessionMessages ($ this ->contains ('Simple Product 1 Name ' ), MessageInterface::TYPE_SUCCESS );
48
+
36
49
$ this ->assertRedirect ();
37
50
38
- $ this ->_assertCompareListEquals ([1 ]);
51
+ $ this ->_assertCompareListEquals ([$ product -> getEntityId () ]);
39
52
}
40
53
41
54
public function testIndexActionAddProducts ()
42
55
{
43
56
$ this ->_requireVisitorWithNoProducts ();
44
-
45
- $ this ->dispatch ('catalog/product_compare/index/items/2 ' );
57
+ $ product = $ this -> productRepository -> get ( ' simple_product_2 ' );
58
+ $ this ->dispatch ('catalog/product_compare/index/items/ ' . $ product -> getEntityId () );
46
59
47
60
$ this ->assertRedirect ($ this ->equalTo ('http://localhost/index.php/catalog/product_compare/index/ ' ));
48
61
49
- $ this ->_assertCompareListEquals ([2 ]);
62
+ $ this ->_assertCompareListEquals ([$ product -> getEntityId () ]);
50
63
}
51
64
52
65
public function testRemoveAction ()
53
66
{
54
67
$ this ->_requireVisitorWithTwoProducts ();
68
+ $ product = $ this ->productRepository ->get ('simple_product_2 ' );
69
+ $ this ->dispatch ('catalog/product_compare/remove/product/ ' . $ product ->getEntityId ());
55
70
56
- $ this ->dispatch ('catalog/product_compare/remove/product/2 ' );
57
-
58
- /** @var $messageManager \Magento\Framework\Message\Manager */
59
- $ messageManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
60
- ->get ('Magento\Framework\Message\Manager ' );
61
- $ this ->assertInstanceOf (
62
- 'Magento\Framework\Message\Success ' ,
63
- $ messageManager ->getMessages ()->getLastAddedMessage ()
64
- );
65
- $ this ->assertContains (
66
- 'Simple Product 2 Name ' ,
67
- (string )$ messageManager ->getMessages ()->getLastAddedMessage ()->getText ()
68
- );
71
+ $ this ->assertSessionMessages ($ this ->contains ('Simple Product 2 Name ' ), MessageInterface::TYPE_SUCCESS );
69
72
70
73
$ this ->assertRedirect ();
71
-
72
- $ this ->_assertCompareListEquals ([1 ]);
74
+ $ restProduct = $ this -> productRepository -> get ( ' simple_product_1 ' );
75
+ $ this ->_assertCompareListEquals ([$ restProduct -> getEntityId () ]);
73
76
}
74
77
75
78
public function testRemoveActionWithSession ()
76
79
{
77
80
$ this ->_requireCustomerWithTwoProducts ();
81
+ $ product = $ this ->productRepository ->get ('simple_product_1 ' );
82
+ $ this ->dispatch ('catalog/product_compare/remove/product/ ' . $ product ->getEntityId ());
83
+ $ secondProduct = $ this ->productRepository ->get ('simple_product_2 ' );
78
84
79
- $ this ->dispatch ('catalog/product_compare/remove/product/1 ' );
80
-
81
- /** @var $messageManager \Magento\Framework\Message\Manager */
82
- $ messageManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
83
- ->get ('Magento\Framework\Message\Manager ' );
84
- $ this ->assertInstanceOf (
85
- 'Magento\Framework\Message\Success ' ,
86
- $ messageManager ->getMessages ()->getLastAddedMessage ()
87
- );
88
- $ this ->assertContains ('Simple Product 1 Name ' ,
89
- (string )$ messageManager ->getMessages ()->getLastAddedMessage ()->getText ());
85
+ $ this ->assertSessionMessages ($ this ->contains ('Simple Product 1 Name ' ), MessageInterface::TYPE_SUCCESS );
90
86
91
87
$ this ->assertRedirect ();
92
88
93
- $ this ->_assertCompareListEquals ([2 ]);
89
+ $ this ->_assertCompareListEquals ([$ secondProduct -> getEntityId () ]);
94
90
}
95
91
96
92
public function testIndexActionDisplay ()
@@ -125,12 +121,9 @@ public function testClearAction()
125
121
126
122
$ this ->dispatch ('catalog/product_compare/clear ' );
127
123
128
- /** @var $messageManager \Magento\Framework\Message\Manager */
129
- $ messageManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
130
- ->get ('Magento\Framework\Message\Manager ' );
131
- $ this ->assertInstanceOf (
132
- 'Magento\Framework\Message\Success ' ,
133
- $ messageManager ->getMessages ()->getLastAddedMessage ()
124
+ $ this ->assertSessionMessages (
125
+ $ this ->contains ('You cleared the comparison list. ' ),
126
+ MessageInterface::TYPE_SUCCESS
134
127
);
135
128
136
129
$ this ->assertRedirect ();
@@ -144,18 +137,16 @@ public function testClearAction()
144
137
public function testRemoveActionProductNameXss ()
145
138
{
146
139
$ this ->_prepareCompareListWithProductNameXss ();
147
- $ this ->dispatch ('catalog/product_compare/remove/product/1?nocookie=1 ' );
148
- $ messages = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
149
- 'Magento\Framework\Message\Manager '
150
- )->getMessages ()->getItems ();
151
- $ isProductNamePresent = false ;
152
- foreach ($ messages as $ message ) {
153
- if (strpos ($ message ->getText (), '<script>alert("xss");</script> ' ) !== false ) {
154
- $ isProductNamePresent = true ;
155
- }
156
- $ this ->assertNotContains ('<script>alert("xss");</script> ' , (string )$ message ->getText ());
157
- }
158
- $ this ->assertTrue ($ isProductNamePresent , 'Product name was not found in session messages ' );
140
+ $ product = $ this ->productRepository ->get ('product-with-xss ' );
141
+ $ this ->dispatch ('catalog/product_compare/remove/product/ ' . $ product ->getEntityId () . '?nocookie=1 ' );
142
+
143
+ $ this ->assertSessionMessages (
144
+ $ this ->logicalNot ($ this ->contains ('<script>alert("xss");</script> ' ))
145
+ );
146
+ $ this ->assertSessionMessages (
147
+ $ this ->contains ('<script>alert("xss");</script> ' ),
148
+ MessageInterface::TYPE_SUCCESS
149
+ );
159
150
}
160
151
161
152
protected function _prepareCompareListWithProductNameXss ()
@@ -171,7 +162,8 @@ protected function _prepareCompareListWithProductNameXss()
171
162
$ item = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
172
163
'Magento\Catalog\Model\Product\Compare\Item '
173
164
);
174
- $ item ->setVisitorId ($ visitor ->getId ())->setProductId (1 )->save ();
165
+ $ firstProductEntityId = $ this ->productRepository ->get ('product-with-xss ' )->getEntityId ();
166
+ $ item ->setVisitorId ($ visitor ->getId ())->setProductId ($ firstProductEntityId )->save ();
175
167
\Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
176
168
'Magento\Customer\Model\Visitor '
177
169
)->load (
@@ -211,21 +203,23 @@ protected function _requireVisitorWithTwoProducts()
211
203
$ item = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
212
204
'Magento\Catalog\Model\Product\Compare\Item '
213
205
);
214
- $ item ->setVisitorId ($ visitor ->getId ())->setProductId (1 )->save ();
206
+ $ firstProductEntityId = $ this ->productRepository ->get ('simple_product_1 ' )->getEntityId ();
207
+ $ secondProductEntityId = $ this ->productRepository ->get ('simple_product_2 ' )->getEntityId ();
208
+ $ item ->setVisitorId ($ visitor ->getId ())->setProductId ($ firstProductEntityId )->save ();
215
209
216
210
/** @var $item \Magento\Catalog\Model\Product\Compare\Item */
217
211
$ item = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
218
212
'Magento\Catalog\Model\Product\Compare\Item '
219
213
);
220
- $ item ->setVisitorId ($ visitor ->getId ())->setProductId (2 )->save ();
214
+ $ item ->setVisitorId ($ visitor ->getId ())->setProductId ($ secondProductEntityId )->save ();
221
215
222
216
\Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
223
217
'Magento\Customer\Model\Visitor '
224
218
)->load (
225
219
$ visitor ->getId ()
226
220
);
227
221
228
- $ this ->_assertCompareListEquals ([1 , 2 ]);
222
+ $ this ->_assertCompareListEquals ([$ firstProductEntityId , $ secondProductEntityId ]);
229
223
}
230
224
231
225
protected function _requireCustomerWithTwoProducts ()
@@ -262,25 +256,28 @@ protected function _requireCustomerWithTwoProducts()
262
256
->setLastVisitAt ((new \DateTime ())->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT ))
263
257
->save ();
264
258
259
+ $ firstProductEntityId = $ this ->productRepository ->get ('simple_product_1 ' )->getEntityId ();
260
+ $ secondProductEntityId = $ this ->productRepository ->get ('simple_product_2 ' )->getEntityId ();
261
+
265
262
/** @var $item \Magento\Catalog\Model\Product\Compare\Item */
266
263
$ item = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
267
264
->create ('Magento\Catalog\Model\Product\Compare\Item ' );
268
265
$ item ->setVisitorId ($ visitor ->getId ())
269
266
->setCustomerId (1 )
270
- ->setProductId (1 )
267
+ ->setProductId ($ firstProductEntityId )
271
268
->save ();
272
269
273
270
/** @var $item \Magento\Catalog\Model\Product\Compare\Item */
274
271
$ item = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
275
272
->create ('Magento\Catalog\Model\Product\Compare\Item ' );
276
273
$ item ->setVisitorId ($ visitor ->getId ())
277
- ->setProductId (2 )
274
+ ->setProductId ($ secondProductEntityId )
278
275
->save ();
279
276
280
277
\Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get ('Magento\Customer\Model\Visitor ' )
281
278
->load ($ visitor ->getId ());
282
279
283
- $ this ->_assertCompareListEquals ([1 , 2 ]);
280
+ $ this ->_assertCompareListEquals ([$ firstProductEntityId , $ secondProductEntityId ]);
284
281
}
285
282
286
283
/**
0 commit comments