1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Functional tests of json responses for shopping cart of Silverstripe Shop |
4
|
|
|
* |
5
|
|
|
* @package shop |
6
|
|
|
* @subpackage tests |
7
|
|
|
*/ |
8
|
|
|
class SilvershopJsonResponseTest extends FunctionalTest |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
protected static $fixture_file = 'silvershop/tests/fixtures/shop.yml'; |
11
|
|
|
|
12
|
|
|
public function setUpOnce() |
13
|
|
|
{ |
14
|
|
|
if (!ShoppingCart_Controller::has_extension('SilvershopJsonResponse')) { |
15
|
|
|
ShoppingCart_Controller::add_extension('SilvershopJsonResponse'); |
16
|
|
|
} |
17
|
|
|
if (!VariationForm::has_extension('SilvershopJsonResponse')) { |
18
|
|
|
VariationForm::add_extension('SilvershopJsonResponse'); |
19
|
|
|
} |
20
|
|
|
parent::setUpOnce(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
ShopTest::setConfiguration(); //reset config |
27
|
|
|
Order::config()->modifiers = array( |
28
|
|
|
"FlatTaxModifier" |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$this->mp3player = $this->objFromFixture('Product', 'mp3player'); |
|
|
|
|
32
|
|
|
$this->socks = $this->objFromFixture('Product', 'socks'); |
|
|
|
|
33
|
|
|
|
34
|
|
|
//publish some product categories and products |
35
|
|
|
$this->objFromFixture('ProductCategory', 'products')->publish('Stage', 'Live'); |
36
|
|
|
$this->objFromFixture('ProductCategory', 'clothing')->publish('Stage', 'Live'); |
37
|
|
|
$this->objFromFixture('ProductCategory', 'clearance')->publish('Stage', 'Live'); |
38
|
|
|
|
39
|
|
|
$this->mp3player->publish('Stage', 'Live'); |
40
|
|
|
$this->socks->publish('Stage', 'Live'); |
41
|
|
|
|
42
|
|
|
$this->cart = ShoppingCart::singleton(); |
|
|
|
|
43
|
|
|
$this->cart->clear(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testAddToCart() |
47
|
|
|
{ |
48
|
|
|
// test ajax request (Product Category page) |
49
|
|
|
$response = $this->get(ShoppingCart_Controller::add_item_link($this->mp3player) . "?ajax=1"); |
50
|
|
|
$this->assertEquals( |
|
|
|
|
51
|
|
|
200, |
52
|
|
|
$response->getStatusCode(), |
53
|
|
|
"Response status code is 200" |
54
|
|
|
); |
55
|
|
|
$this->assertEquals( |
|
|
|
|
56
|
|
|
"application/json; charset=utf-8", |
57
|
|
|
$response->getHeader("Content-Type"), |
58
|
|
|
"Json response header" |
59
|
|
|
); |
60
|
|
|
$this->assertJson( |
|
|
|
|
61
|
|
|
$response->getBody(), |
62
|
|
|
"Contains json in the body of the response" |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$this->assertContains( |
66
|
|
|
"addLink", |
67
|
|
|
$response->getBody(), |
68
|
|
|
"response contains a link to add additional quantities of an item in the cart" |
69
|
|
|
); |
70
|
|
|
$this->assertContains( |
71
|
|
|
"removeLink", |
72
|
|
|
$response->getBody(), |
73
|
|
|
"response contains a link to reduce the quantity of an item in a cart" |
74
|
|
|
); |
75
|
|
|
$this->assertContains( |
76
|
|
|
"removeallLink", |
77
|
|
|
$response->getBody(), |
78
|
|
|
"response contains a link to remove all of an item from a cart" |
79
|
|
|
); |
80
|
|
|
$this->assertContains( |
81
|
|
|
"setquantityLink", |
82
|
|
|
$response->getBody(), |
83
|
|
|
"response contains a link to set the quantity of an item in a cart" |
84
|
|
|
); |
85
|
|
|
$this->assertContains( |
86
|
|
|
"unitPrice", |
87
|
|
|
$response->getBody(), |
88
|
|
|
"response contains the unit price of items in a cart" |
89
|
|
|
); |
90
|
|
|
$this->assertContains( |
91
|
|
|
"subTotal", |
92
|
|
|
$response->getBody(), |
93
|
|
|
"response contains a subTotal when include_totals is set to true" |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// See what's in the cart |
97
|
|
|
$items = ShoppingCart::curr()->Items(); |
98
|
|
|
$this->assertNotNull($items); |
|
|
|
|
99
|
|
|
$this->assertEquals( |
|
|
|
|
100
|
|
|
$items->Count(), |
101
|
|
|
1, |
102
|
|
|
'There is 1 item in the cart' |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testRemoveFromCart() |
107
|
|
|
{ |
108
|
|
|
|
109
|
|
|
// add items via url to setup |
110
|
|
|
$this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
|
|
|
|
111
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); |
|
|
|
|
112
|
|
|
ShoppingCart::curr()->calculate(); // recalculate the shopping cart |
113
|
|
|
|
114
|
|
|
$cart = $this->get("shoppingcart/get" . "?ajax=1"); |
115
|
|
|
$this->assertContains( |
116
|
|
|
'"id":"10"', |
117
|
|
|
$cart->getBody(), |
118
|
|
|
"Contains the mp3 player" |
119
|
|
|
); |
120
|
|
|
$this->assertContains( |
121
|
|
|
'"id":"6"', |
122
|
|
|
$cart->getBody(), |
123
|
|
|
"Contains the socks" |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
// remove the one of the mp3 players via url making the total 4 |
127
|
|
|
$response = $this->get(ShoppingCart_Controller::remove_item_link($this->mp3player) . "?ajax=1"); |
128
|
|
|
$this->assertEquals( |
|
|
|
|
129
|
|
|
200, |
130
|
|
|
$response->getStatusCode(), |
131
|
|
|
"Response status code is 200" |
132
|
|
|
); |
133
|
|
|
$this->assertEquals( |
|
|
|
|
134
|
|
|
"application/json; charset=utf-8", |
135
|
|
|
$response->getHeader("Content-Type"), |
136
|
|
|
"Json response header" |
137
|
|
|
); |
138
|
|
|
$this->assertJson( |
|
|
|
|
139
|
|
|
$response->getBody(), |
140
|
|
|
"Contains json in the body of the response" |
141
|
|
|
); |
142
|
|
|
$this->assertContains( |
143
|
|
|
'{"id":"10"', $response->getBody(), |
144
|
|
|
"Contains json in the body of the response and id of the mp3 player" |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
// remove the one of the socks via url making the total NIL so it is fully removed |
148
|
|
|
$response = $this->get(ShoppingCart_Controller::remove_item_link($this->socks) . "?ajax=1"); |
149
|
|
|
$this->assertEquals( |
|
|
|
|
150
|
|
|
200, |
151
|
|
|
$response->getStatusCode(), |
152
|
|
|
"Response status code is 200" |
153
|
|
|
); |
154
|
|
|
$this->assertEquals( |
|
|
|
|
155
|
|
|
"application/json; charset=utf-8", |
156
|
|
|
$response->getHeader("Content-Type"), |
157
|
|
|
"Json response header" |
158
|
|
|
); |
159
|
|
|
$this->assertJson( |
|
|
|
|
160
|
|
|
$response->getBody(), |
161
|
|
|
"Contains json in the body of the response" |
162
|
|
|
); |
163
|
|
|
$this->assertFalse( |
|
|
|
|
164
|
|
|
$this->cart->get($this->socks), |
165
|
|
|
"socks completely removed" |
166
|
|
|
); |
167
|
|
|
$this->assertContains( |
168
|
|
|
'{"id":"6"', |
169
|
|
|
$response->getBody(), |
170
|
|
|
"id of Socks returned" |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testRemoveAllFromCart() |
175
|
|
|
{ |
176
|
|
|
// add items via url to setup |
177
|
|
|
$this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
|
|
|
|
178
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); |
|
|
|
|
179
|
|
|
|
180
|
|
|
// remove items from cart via url |
181
|
|
|
$response = $this->get(ShoppingCart_Controller::remove_all_item_link($this->mp3player) . "?ajax=1"); |
182
|
|
|
$this->assertEquals( |
|
|
|
|
183
|
|
|
200, |
184
|
|
|
$response->getStatusCode(), |
185
|
|
|
"Response status code is 200" |
186
|
|
|
); |
187
|
|
|
$this->assertEquals( |
|
|
|
|
188
|
|
|
"application/json; charset=utf-8", |
189
|
|
|
$response->getHeader("Content-Type"), |
190
|
|
|
"Json response header" |
191
|
|
|
); |
192
|
|
|
$this->assertJson( |
|
|
|
|
193
|
|
|
$response->getBody(), |
194
|
|
|
"Contains json in the body of the response" |
195
|
|
|
); |
196
|
|
|
$this->assertContains( |
197
|
|
|
'{"id":"10"', |
198
|
|
|
$response->getBody(), |
199
|
|
|
"Contains json in the body of the response and id of item removed" |
200
|
|
|
); |
201
|
|
|
$this->assertFalse( |
|
|
|
|
202
|
|
|
$this->cart->get($this->mp3player), |
203
|
|
|
"Mp3 Players are not in the cart" |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testSetQuantityInCart() |
208
|
|
|
{ |
209
|
|
|
// add items via url to setup |
210
|
|
|
$this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
|
|
|
|
211
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); |
|
|
|
|
212
|
|
|
|
213
|
|
|
// set items via url |
214
|
|
|
$response = $this->get( |
215
|
|
|
ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 3, 'ajax' => 1)) |
|
|
|
|
216
|
|
|
); |
217
|
|
|
$this->assertEquals( |
|
|
|
|
218
|
|
|
200, |
219
|
|
|
$response->getStatusCode(), |
220
|
|
|
"Response status code is 200" |
221
|
|
|
); |
222
|
|
|
$this->assertEquals( |
|
|
|
|
223
|
|
|
"application/json; charset=utf-8", |
224
|
|
|
$response->getHeader("Content-Type"), |
225
|
|
|
"Json response header" |
226
|
|
|
); |
227
|
|
|
$this->assertJson( |
|
|
|
|
228
|
|
|
$response->getBody(), |
229
|
|
|
"Contains json in the body of the response" |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$this->assertContains( |
233
|
|
|
'Quantity has been set', |
234
|
|
|
$response->getBody(), |
235
|
|
|
"Contains a confirmation message that the quantity has been set to a new value" |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testClearAllItemsFromTheCart() |
240
|
|
|
{ |
241
|
|
|
// add items via url to setup |
242
|
|
|
$this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
|
|
|
|
243
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); |
|
|
|
|
244
|
|
|
|
245
|
|
|
// remove items via url |
246
|
|
|
$response = $this->get("shoppingcart/clear?ajax=1"); |
247
|
|
|
$this->assertEquals( |
|
|
|
|
248
|
|
|
200, |
249
|
|
|
$response->getStatusCode(), |
250
|
|
|
"Response status code is 200" |
251
|
|
|
); |
252
|
|
|
$this->assertEquals( |
|
|
|
|
253
|
|
|
"application/json; charset=utf-8", |
254
|
|
|
$response->getHeader("Content-Type"), |
255
|
|
|
"Json response header" |
256
|
|
|
); |
257
|
|
|
$this->assertJson( |
|
|
|
|
258
|
|
|
$response->getBody(), |
259
|
|
|
"Contains json in the body of the response" |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$this->assertContains( |
263
|
|
|
'Cart was successfully cleared', |
264
|
|
|
$response->getBody(), |
265
|
|
|
"Contains a message that the cart has been cleared" |
266
|
|
|
); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testVariations() |
270
|
|
|
{ |
271
|
|
|
$this->loadFixture('silvershop/tests/fixtures/variations.yml'); |
272
|
|
|
$ballRoot = $this->objFromFixture('Product', 'ball'); |
273
|
|
|
|
274
|
|
|
// add parent for categories in JSON response |
275
|
|
|
$parent = $this->objFromFixture('ProductCategory', 'products'); |
276
|
|
|
$ballRoot->ParentID = $parent->ID; |
277
|
|
|
$ballRoot->write(); |
278
|
|
|
|
279
|
|
|
$ballRoot->publish('Stage', 'Live'); |
280
|
|
|
$ball1 = $this->objFromFixture('ProductVariation', 'redlarge'); |
281
|
|
|
$ball2 = $this->objFromFixture('ProductVariation', 'redsmall'); |
282
|
|
|
|
283
|
|
|
// Add the two variation items |
284
|
|
|
$response = $this->get(ShoppingCart_Controller::add_item_link($ball1) . "?ajax=1"); |
|
|
|
|
285
|
|
|
$this->assertEquals( |
|
|
|
|
286
|
|
|
200, |
287
|
|
|
$response->getStatusCode(), |
288
|
|
|
"Response status code is 200" |
289
|
|
|
); |
290
|
|
|
$this->assertEquals( |
|
|
|
|
291
|
|
|
"application/json; charset=utf-8", |
292
|
|
|
$response->getHeader("Content-Type"), |
293
|
|
|
"Json response header" |
294
|
|
|
); |
295
|
|
|
$this->assertJson( |
|
|
|
|
296
|
|
|
$response->getBody(), |
297
|
|
|
"Contains json in the body of the response" |
298
|
|
|
); |
299
|
|
|
$this->assertContains( |
300
|
|
|
"Size:Large, Color:Red", |
301
|
|
|
$response->getBody(), |
302
|
|
|
"Contains json in the body of the cart id" |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
$response = $this->get(ShoppingCart_Controller::add_item_link($ball2) . "?ajax=1"); |
|
|
|
|
306
|
|
|
$items = ShoppingCart::curr()->Items(); |
307
|
|
|
$this->assertEquals( |
|
|
|
|
308
|
|
|
$items->Count(), |
309
|
|
|
2, |
310
|
|
|
"There are 2 items in the cart" |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
// Remove one and see what happens |
314
|
|
|
$response = $this->get(ShoppingCart_Controller::remove_all_item_link($ball1) . "?ajax=1"); |
|
|
|
|
315
|
|
|
$this->assertEquals( |
|
|
|
|
316
|
|
|
$items->Count(), |
317
|
|
|
1, |
318
|
|
|
"There is 1 item in the cart" |
319
|
|
|
); |
320
|
|
|
$this->assertFalse( |
|
|
|
|
321
|
|
|
$this->cart->get($ball1), |
322
|
|
|
"first item not in cart" |
323
|
|
|
); |
324
|
|
|
$this->assertNotNull( |
|
|
|
|
325
|
|
|
$this->cart->get($ball1), |
326
|
|
|
"second item is in cart" |
327
|
|
|
); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function testGet() |
331
|
|
|
{ |
332
|
|
|
// add items via url to setup |
333
|
|
|
$this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
|
|
|
|
334
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); |
|
|
|
|
335
|
|
|
$shoppingcart = ShoppingCart::curr(); |
336
|
|
|
$shoppingcart->calculate(); // recalculate the shopping cart |
337
|
|
|
|
338
|
|
|
$response = $this->get("shoppingcart/get" . "?ajax=1"); |
339
|
|
|
|
340
|
|
|
$this->assertContains( |
341
|
|
|
'tableTitle', |
342
|
|
|
$response->getBody(), |
343
|
|
|
"Json contains tableTitle" |
344
|
|
|
); |
345
|
|
|
$this->assertContains( |
346
|
|
|
'tableValue', |
347
|
|
|
$response->getBody(), |
348
|
|
|
"Json contains tableValue" |
349
|
|
|
); |
350
|
|
|
$this->assertContains( |
351
|
|
|
'Tax @ 15.0%', |
352
|
|
|
$response->getBody(), |
353
|
|
|
"Contains GST modifier in the response" |
354
|
|
|
); |
355
|
|
|
$this->assertContains( |
356
|
|
|
'"subTotal":1008', |
357
|
|
|
$response->getBody(), |
358
|
|
|
"Contains SubTotal of 1008" |
359
|
|
|
); |
360
|
|
|
$this->assertContains( |
361
|
|
|
'"grandTotal":1159.2', |
362
|
|
|
$response->getBody(), |
363
|
|
|
"Contains a GrandTotal of 1159.2; the GST amount (the Flat Tax Modifier) is the difference" |
364
|
|
|
); |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.