|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopJsonResponse\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverShop\Cart\ShoppingCartController; |
|
6
|
|
|
use SilverShop\Tests\ShopTest; |
|
7
|
|
|
use SilverShop\Model\Order; |
|
8
|
|
|
use SilverShop\Model\Modifiers\Tax\FlatTax; |
|
9
|
|
|
use SilverShop\Page\Product; |
|
10
|
|
|
use SilverShop\Page\ProductCategory; |
|
11
|
|
|
use SilverShop\Cart\ShoppingCart; |
|
12
|
|
|
use SilverStripe\Dev\FunctionalTest; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Functional tests of json responses for shopping cart of Silverstripe Shop |
|
16
|
|
|
* |
|
17
|
|
|
* @package shop |
|
18
|
|
|
* @subpackage tests |
|
19
|
|
|
*/ |
|
20
|
|
|
class SilverShopJsonResponseTest extends FunctionalTest |
|
21
|
|
|
{ |
|
22
|
|
|
protected static $fixture_file = [ |
|
23
|
|
|
'vendor/silvershop/core/tests/php/Fixtures/shop.yml' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
public $mp3player; |
|
27
|
|
|
public $socks; |
|
28
|
|
|
public $cart; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
public function setUpOnce(): void |
|
32
|
|
|
{ |
|
33
|
|
|
if (!ShoppingCartController::has_extension('SilvershopJsonResponse')) { |
|
34
|
|
|
ShoppingCartController::add_extension('SilvershopJsonResponse'); |
|
35
|
|
|
} |
|
36
|
|
|
parent::setUpOnce(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setUp(): void |
|
40
|
|
|
{ |
|
41
|
|
|
parent::setUp(); |
|
42
|
|
|
ShopTest::setConfiguration(); //reset config |
|
43
|
|
|
Order::config()->modifiers = [ FlatTax::class ]; |
|
44
|
|
|
|
|
45
|
|
|
// Needed, so that products can be published |
|
46
|
|
|
$this->logInWithPermission('ADMIN'); |
|
47
|
|
|
|
|
48
|
|
|
$this->mp3player = $this->objFromFixture(Product::class, 'mp3player'); |
|
49
|
|
|
$this->socks = $this->objFromFixture(Product::class, 'socks'); |
|
50
|
|
|
|
|
51
|
|
|
//publish some product categories and products |
|
52
|
|
|
$this->objFromFixture(ProductCategory::class, 'products')->copyVersionToStage('Stage', 'Live'); |
|
53
|
|
|
$this->objFromFixture(ProductCategory::class, 'clothing')->copyVersionToStage('Stage', 'Live'); |
|
54
|
|
|
$this->objFromFixture(ProductCategory::class, 'clearance')->copyVersionToStage('Stage', 'Live'); |
|
55
|
|
|
|
|
56
|
|
|
$this->mp3player->copyVersionToStage('Stage', 'Live'); |
|
57
|
|
|
$this->socks->copyVersionToStage('Stage', 'Live'); |
|
58
|
|
|
|
|
59
|
|
|
$this->cart = ShoppingCart::singleton(); |
|
60
|
|
|
$this->cart->clear(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testGet(): void |
|
64
|
|
|
{ |
|
65
|
|
|
// add items via url to setup cart |
|
66
|
|
|
$this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, ['quantity' => 5])); |
|
|
|
|
|
|
67
|
|
|
$this->get(ShoppingCartController::add_item_link($this->socks)); |
|
68
|
|
|
$shoppingcart = ShoppingCart::curr(); |
|
69
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
|
70
|
|
|
|
|
71
|
|
|
$response = $this->get('shoppingcart/get?ajax=1'); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertStringContainsString( |
|
74
|
|
|
'tableTitle', |
|
75
|
|
|
$response->getBody(), |
|
76
|
|
|
"Json contains tableTitle" |
|
77
|
|
|
); |
|
78
|
|
|
$this->assertStringContainsString( |
|
79
|
|
|
'tableValue', |
|
80
|
|
|
$response->getBody(), |
|
81
|
|
|
"Json contains tableValue" |
|
82
|
|
|
); |
|
83
|
|
|
$this->assertStringContainsString( |
|
84
|
|
|
'Tax @ 15.0%', |
|
85
|
|
|
$response->getBody(), |
|
86
|
|
|
"Contains GST modifier in the response" |
|
87
|
|
|
); |
|
88
|
|
|
$this->assertStringContainsString( |
|
89
|
|
|
'"subTotal":1008', |
|
90
|
|
|
$response->getBody(), |
|
91
|
|
|
"Contains SubTotal of 1008" |
|
92
|
|
|
); |
|
93
|
|
|
$this->assertStringContainsString( |
|
94
|
|
|
'"grandTotal":1159.2', |
|
95
|
|
|
$response->getBody(), |
|
96
|
|
|
"Contains a GrandTotal of 1159.2; the GST amount (the Flat Tax Modifier) is the difference" |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testAddToCart(): void |
|
101
|
|
|
{ |
|
102
|
|
|
// test ajax request (Product Category page) |
|
103
|
|
|
$response = $this->get(ShoppingCartController::add_item_link($this->mp3player) . "?ajax=1"); |
|
|
|
|
|
|
104
|
|
|
$this->assertEquals( |
|
105
|
|
|
200, |
|
106
|
|
|
$response->getStatusCode(), |
|
107
|
|
|
"Response status code is 200" |
|
108
|
|
|
); |
|
109
|
|
|
$this->assertEquals( |
|
110
|
|
|
"application/json; charset=utf-8", |
|
111
|
|
|
$response->getHeader("Content-Type"), |
|
112
|
|
|
"Json response header" |
|
113
|
|
|
); |
|
114
|
|
|
$this->assertJson( |
|
115
|
|
|
$response->getBody(), |
|
116
|
|
|
"Contains json in the body of the response" |
|
117
|
|
|
); |
|
118
|
|
|
$this->assertStringContainsString( |
|
119
|
|
|
"addLink", |
|
120
|
|
|
$response->getBody(), |
|
121
|
|
|
"response contains a link to add additional quantities of an item in the cart" |
|
122
|
|
|
); |
|
123
|
|
|
$this->assertStringContainsString( |
|
124
|
|
|
"removeLink", |
|
125
|
|
|
$response->getBody(), |
|
126
|
|
|
"response contains a link to reduce the quantity of an item in a cart" |
|
127
|
|
|
); |
|
128
|
|
|
$this->assertStringContainsString( |
|
129
|
|
|
"removeallLink", |
|
130
|
|
|
$response->getBody(), |
|
131
|
|
|
"response contains a link to remove all of an item from a cart" |
|
132
|
|
|
); |
|
133
|
|
|
$this->assertStringContainsString( |
|
134
|
|
|
"setquantityLink", |
|
135
|
|
|
$response->getBody(), |
|
136
|
|
|
"response contains a link to set the quantity of an item in a cart" |
|
137
|
|
|
); |
|
138
|
|
|
$this->assertStringContainsString( |
|
139
|
|
|
"unitPrice", |
|
140
|
|
|
$response->getBody(), |
|
141
|
|
|
"response contains the unit price of items in a cart" |
|
142
|
|
|
); |
|
143
|
|
|
$this->assertStringContainsString( |
|
144
|
|
|
"subTotal", |
|
145
|
|
|
$response->getBody(), |
|
146
|
|
|
"response contains a subTotal when include_totals is set to true" |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
// See what's in the cart |
|
150
|
|
|
$items = ShoppingCart::curr()->Items(); |
|
151
|
|
|
$this->assertNotNull($items); |
|
152
|
|
|
$this->assertEquals( |
|
153
|
|
|
$items->Count(), |
|
154
|
|
|
1, |
|
155
|
|
|
'There is 1 item in the cart' |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testRemoveFromCart(): void |
|
160
|
|
|
{ |
|
161
|
|
|
|
|
162
|
|
|
// add items via url to setup |
|
163
|
|
|
$this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, ['quantity' => 5])); |
|
|
|
|
|
|
164
|
|
|
$this->get(ShoppingCartController::add_item_link($this->socks)); |
|
165
|
|
|
ShoppingCart::curr()->calculate(); // recalculate the shopping cart |
|
166
|
|
|
|
|
167
|
|
|
$cart = $this->get('shoppingcart/get?ajax=1'); |
|
168
|
|
|
$this->assertStringContainsString( |
|
169
|
|
|
'"title":"Mp3 Player"', |
|
170
|
|
|
$cart->getBody(), |
|
171
|
|
|
"Contains the mp3 player" |
|
172
|
|
|
); |
|
173
|
|
|
$this->assertStringContainsString( |
|
174
|
|
|
'"title":"Socks"', |
|
175
|
|
|
$cart->getBody(), |
|
176
|
|
|
"Contains the socks" |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
// remove the one of the mp3 players via url making the total 4 |
|
180
|
|
|
$response = $this->get(ShoppingCartController::remove_item_link($this->mp3player) . "?ajax=1"); |
|
|
|
|
|
|
181
|
|
|
$this->assertEquals( |
|
182
|
|
|
200, |
|
183
|
|
|
$response->getStatusCode(), |
|
184
|
|
|
"Response status code is 200" |
|
185
|
|
|
); |
|
186
|
|
|
$this->assertEquals( |
|
187
|
|
|
"application/json; charset=utf-8", |
|
188
|
|
|
$response->getHeader("Content-Type"), |
|
189
|
|
|
"Json response header" |
|
190
|
|
|
); |
|
191
|
|
|
$this->assertJson( |
|
192
|
|
|
$response->getBody(), |
|
193
|
|
|
"Contains json in the body of the response" |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
// remove the one of the socks via url making the total NIL so it is fully removed |
|
197
|
|
|
$response = $this->get(ShoppingCartController::remove_item_link($this->socks) . "?ajax=1"); |
|
|
|
|
|
|
198
|
|
|
$this->assertEquals( |
|
199
|
|
|
200, |
|
200
|
|
|
$response->getStatusCode(), |
|
201
|
|
|
"Response status code is 200" |
|
202
|
|
|
); |
|
203
|
|
|
$this->assertEquals( |
|
204
|
|
|
"application/json; charset=utf-8", |
|
205
|
|
|
$response->getHeader("Content-Type"), |
|
206
|
|
|
"Json response header" |
|
207
|
|
|
); |
|
208
|
|
|
$this->assertJson( |
|
209
|
|
|
$response->getBody(), |
|
210
|
|
|
"Contains json in the body of the response" |
|
211
|
|
|
); |
|
212
|
|
|
$this->assertNull( |
|
213
|
|
|
$this->cart->get($this->socks), |
|
214
|
|
|
"socks completely removed" |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function testRemoveAllFromCart(): void |
|
219
|
|
|
{ |
|
220
|
|
|
// add items via url to setup |
|
221
|
|
|
$this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, ['quantity' => 5])); |
|
|
|
|
|
|
222
|
|
|
$this->get(ShoppingCartController::add_item_link($this->socks)); |
|
223
|
|
|
|
|
224
|
|
|
// remove items from cart via url |
|
225
|
|
|
$response = $this->get(ShoppingCartController::remove_all_item_link($this->mp3player) . "?ajax=1"); |
|
|
|
|
|
|
226
|
|
|
$this->assertEquals( |
|
227
|
|
|
200, |
|
228
|
|
|
$response->getStatusCode(), |
|
229
|
|
|
"Response status code is 200" |
|
230
|
|
|
); |
|
231
|
|
|
$this->assertEquals( |
|
232
|
|
|
"application/json; charset=utf-8", |
|
233
|
|
|
$response->getHeader("Content-Type"), |
|
234
|
|
|
"Json response header" |
|
235
|
|
|
); |
|
236
|
|
|
$this->assertJson( |
|
237
|
|
|
$response->getBody(), |
|
238
|
|
|
"Contains json in the body of the response" |
|
239
|
|
|
); |
|
240
|
|
|
$this->assertNull( |
|
241
|
|
|
$this->cart->get($this->mp3player), |
|
242
|
|
|
"Mp3 Players are not in the cart" |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function testSetQuantityInCart(): void |
|
247
|
|
|
{ |
|
248
|
|
|
// add items via url to setup |
|
249
|
|
|
$this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, ['quantity' => 5])); |
|
|
|
|
|
|
250
|
|
|
$this->get(ShoppingCartController::add_item_link($this->socks)); |
|
251
|
|
|
|
|
252
|
|
|
// set items via url |
|
253
|
|
|
$response = $this->get( |
|
254
|
|
|
ShoppingCartController::set_quantity_item_link($this->mp3player, ['quantity' => 3, 'ajax' => 1]) |
|
255
|
|
|
); |
|
256
|
|
|
$this->assertEquals( |
|
257
|
|
|
200, |
|
258
|
|
|
$response->getStatusCode(), |
|
259
|
|
|
"Response status code is 200" |
|
260
|
|
|
); |
|
261
|
|
|
$this->assertEquals( |
|
262
|
|
|
"application/json; charset=utf-8", |
|
263
|
|
|
$response->getHeader("Content-Type"), |
|
264
|
|
|
"Json response header" |
|
265
|
|
|
); |
|
266
|
|
|
$this->assertJson( |
|
267
|
|
|
$response->getBody(), |
|
268
|
|
|
"Contains json in the body of the response" |
|
269
|
|
|
); |
|
270
|
|
|
|
|
271
|
|
|
$this->assertStringContainsString( |
|
272
|
|
|
'Quantity has been set', |
|
273
|
|
|
$response->getBody(), |
|
274
|
|
|
"Contains a confirmation message that the quantity has been set to a new value" |
|
275
|
|
|
); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function testClearAllItemsFromTheCart(): void |
|
279
|
|
|
{ |
|
280
|
|
|
// add items via url to setup |
|
281
|
|
|
$this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, ['quantity' => 5])); |
|
|
|
|
|
|
282
|
|
|
$this->get(ShoppingCartController::add_item_link($this->socks)); |
|
283
|
|
|
|
|
284
|
|
|
// remove items via url |
|
285
|
|
|
$response = $this->get("shoppingcart/clear?ajax=1"); |
|
286
|
|
|
$this->assertEquals( |
|
287
|
|
|
200, |
|
288
|
|
|
$response->getStatusCode(), |
|
289
|
|
|
"Response status code is 200" |
|
290
|
|
|
); |
|
291
|
|
|
$this->assertEquals( |
|
292
|
|
|
"application/json; charset=utf-8", |
|
293
|
|
|
$response->getHeader("Content-Type"), |
|
294
|
|
|
"Json response header" |
|
295
|
|
|
); |
|
296
|
|
|
$this->assertJson( |
|
297
|
|
|
$response->getBody(), |
|
298
|
|
|
"Contains json in the body of the response" |
|
299
|
|
|
); |
|
300
|
|
|
|
|
301
|
|
|
$this->assertStringContainsString( |
|
302
|
|
|
'Cart was successfully cleared', |
|
303
|
|
|
$response->getBody(), |
|
304
|
|
|
"Contains a message that the cart has been cleared" |
|
305
|
|
|
); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|