1 | <?php |
||
8 | use SilverShop\Model\Order; |
||
9 | use SilverShop\Model\Modifiers\Tax\FlatTax; |
||
10 | use SilverShop\Page\Product; |
||
11 | use SilverShop\Page\ProductCategory; |
||
12 | use SilverShop\Cart\ShoppingCart; |
||
13 | use SilverShop\Model\Variation\Variation; |
||
14 | use SilverStripe\Dev\FunctionalTest; |
||
15 | |||
16 | /** |
||
17 | * Functional tests of json responses for shopping cart of Silverstripe Shop |
||
18 | * |
||
19 | * @package shop |
||
20 | * @subpackage tests |
||
21 | */ |
||
22 | class SilverShopJsonResponseTest extends FunctionalTest |
||
23 | { |
||
24 | protected static $fixture_file = 'vendor/silvershop/core/tests/php/Fixtures/shop.yml'; |
||
25 | |||
26 | public function setUpOnce() |
||
27 | { |
||
28 | if (!ShoppingCartController::has_extension('SilvershopJsonResponse')) { |
||
29 | ShoppingCartController::add_extension('SilvershopJsonResponse'); |
||
30 | } |
||
31 | if (!VariationForm::has_extension('SilvershopJsonResponse')) { |
||
32 | VariationForm::add_extension('SilvershopJsonResponse'); |
||
33 | } |
||
34 | parent::setUpOnce(); |
||
|
|||
35 | } |
||
36 | |||
37 | public function setUp() |
||
38 | { |
||
39 | parent::setUp(); |
||
40 | ShopTest::setConfiguration(); //reset config |
||
41 | Order::config()->modifiers = [ FlatTax::class ]; |
||
42 | |||
43 | // Needed, so that products can be published |
||
44 | $this->logInWithPermission('ADMIN'); |
||
45 | |||
46 | $this->mp3player = $this->objFromFixture(Product::class, 'mp3player'); |
||
47 | $this->socks = $this->objFromFixture(Product::class, 'socks'); |
||
48 | |||
49 | //publish some product categories and products |
||
50 | $this->objFromFixture(ProductCategory::class, 'products')->publish('Stage', 'Live'); |
||
51 | $this->objFromFixture(ProductCategory::class, 'clothing')->publish('Stage', 'Live'); |
||
52 | $this->objFromFixture(ProductCategory::class, 'clearance')->publish('Stage', 'Live'); |
||
53 | |||
54 | $this->mp3player->publish('Stage', 'Live'); |
||
55 | $this->socks->publish('Stage', 'Live'); |
||
56 | |||
57 | $this->cart = ShoppingCart::singleton(); |
||
58 | $this->cart->clear(); |
||
59 | } |
||
60 | |||
61 | public function testGet() |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | public function testAddToCart() |
||
99 | { |
||
100 | // test ajax request (Product Category page) |
||
101 | $response = $this->get(ShoppingCartController::add_item_link($this->mp3player) . "?ajax=1"); |
||
102 | $this->assertEquals( |
||
103 | 200, |
||
104 | $response->getStatusCode(), |
||
105 | "Response status code is 200" |
||
106 | ); |
||
107 | $this->assertEquals( |
||
108 | "application/json; charset=utf-8", |
||
109 | $response->getHeader("Content-Type"), |
||
110 | "Json response header" |
||
111 | ); |
||
112 | $this->assertJson( |
||
113 | $response->getBody(), |
||
114 | "Contains json in the body of the response" |
||
115 | ); |
||
116 | |||
117 | $this->assertContains( |
||
118 | "addLink", |
||
119 | $response->getBody(), |
||
120 | "response contains a link to add additional quantities of an item in the cart" |
||
121 | ); |
||
122 | $this->assertContains( |
||
123 | "removeLink", |
||
124 | $response->getBody(), |
||
125 | "response contains a link to reduce the quantity of an item in a cart" |
||
126 | ); |
||
127 | $this->assertContains( |
||
128 | "removeallLink", |
||
129 | $response->getBody(), |
||
130 | "response contains a link to remove all of an item from a cart" |
||
131 | ); |
||
132 | $this->assertContains( |
||
133 | "setquantityLink", |
||
134 | $response->getBody(), |
||
135 | "response contains a link to set the quantity of an item in a cart" |
||
136 | ); |
||
137 | $this->assertContains( |
||
138 | "unitPrice", |
||
139 | $response->getBody(), |
||
140 | "response contains the unit price of items in a cart" |
||
141 | ); |
||
142 | $this->assertContains( |
||
143 | "subTotal", |
||
144 | $response->getBody(), |
||
145 | "response contains a subTotal when include_totals is set to true" |
||
146 | ); |
||
147 | |||
148 | // See what's in the cart |
||
149 | $items = ShoppingCart::curr()->Items(); |
||
150 | $this->assertNotNull($items); |
||
151 | $this->assertEquals( |
||
152 | $items->Count(), |
||
153 | 1, |
||
154 | 'There is 1 item in the cart' |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | public function testRemoveFromCart() |
||
159 | { |
||
160 | |||
161 | // add items via url to setup |
||
162 | $this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
||
163 | $this->get(ShoppingCartController::add_item_link($this->socks)); |
||
164 | $shoppingcart = ShoppingCart::curr()->calculate(); // recalculate the shopping cart |
||
165 | |||
166 | $cart = $this->get("shoppingcart/get" . "?ajax=1"); |
||
167 | $this->assertContains( |
||
168 | '"title":"Mp3 Player"', |
||
169 | $cart->getBody(), |
||
170 | "Contains the mp3 player" |
||
171 | ); |
||
172 | $this->assertContains( |
||
173 | '"title":"Socks"', |
||
174 | $cart->getBody(), |
||
175 | "Contains the socks" |
||
176 | ); |
||
177 | |||
178 | // remove the one of the mp3 players via url making the total 4 |
||
179 | $response = $this->get(ShoppingCartController::remove_item_link($this->mp3player) . "?ajax=1"); |
||
180 | $this->assertEquals( |
||
181 | 200, |
||
182 | $response->getStatusCode(), |
||
183 | "Response status code is 200" |
||
184 | ); |
||
185 | $this->assertEquals( |
||
186 | "application/json; charset=utf-8", |
||
187 | $response->getHeader("Content-Type"), |
||
188 | "Json response header" |
||
189 | ); |
||
190 | $this->assertJson( |
||
191 | $response->getBody(), |
||
192 | "Contains json in the body of the response" |
||
193 | ); |
||
194 | |||
195 | // remove the one of the socks via url making the total NIL so it is fully removed |
||
196 | $response = $this->get(ShoppingCartController::remove_item_link($this->socks) . "?ajax=1"); |
||
197 | $this->assertEquals( |
||
198 | 200, |
||
199 | $response->getStatusCode(), |
||
200 | "Response status code is 200" |
||
201 | ); |
||
202 | $this->assertEquals( |
||
203 | "application/json; charset=utf-8", |
||
204 | $response->getHeader("Content-Type"), |
||
205 | "Json response header" |
||
206 | ); |
||
207 | $this->assertJson( |
||
208 | $response->getBody(), |
||
209 | "Contains json in the body of the response" |
||
210 | ); |
||
211 | $this->assertNull( |
||
212 | $this->cart->get($this->socks), |
||
213 | "socks completely removed" |
||
214 | ); |
||
215 | } |
||
216 | |||
217 | public function testRemoveAllFromCart() |
||
218 | { |
||
219 | // add items via url to setup |
||
220 | $this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
||
221 | $this->get(ShoppingCartController::add_item_link($this->socks)); |
||
222 | |||
223 | // remove items from cart via url |
||
224 | $response = $this->get(ShoppingCartController::remove_all_item_link($this->mp3player) . "?ajax=1"); |
||
225 | $this->assertEquals( |
||
226 | 200, |
||
227 | $response->getStatusCode(), |
||
228 | "Response status code is 200" |
||
229 | ); |
||
230 | $this->assertEquals( |
||
231 | "application/json; charset=utf-8", |
||
232 | $response->getHeader("Content-Type"), |
||
233 | "Json response header" |
||
234 | ); |
||
235 | $this->assertJson( |
||
236 | $response->getBody(), |
||
237 | "Contains json in the body of the response" |
||
238 | ); |
||
239 | $this->assertNull( |
||
240 | $this->cart->get($this->mp3player), |
||
241 | "Mp3 Players are not in the cart" |
||
242 | ); |
||
243 | } |
||
244 | |||
245 | public function testSetQuantityInCart() |
||
246 | { |
||
247 | // add items via url to setup |
||
248 | $this->get(ShoppingCartController::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
||
249 | $this->get(ShoppingCartController::add_item_link($this->socks)); |
||
250 | |||
251 | // set items via url |
||
252 | $response = $this->get( |
||
253 | ShoppingCartController::set_quantity_item_link($this->mp3player, array('quantity' => 3, 'ajax' => 1)) |
||
254 | ); |
||
255 | $this->assertEquals( |
||
256 | 200, |
||
257 | $response->getStatusCode(), |
||
258 | "Response status code is 200" |
||
259 | ); |
||
260 | $this->assertEquals( |
||
261 | "application/json; charset=utf-8", |
||
262 | $response->getHeader("Content-Type"), |
||
263 | "Json response header" |
||
264 | ); |
||
265 | $this->assertJson( |
||
266 | $response->getBody(), |
||
267 | "Contains json in the body of the response" |
||
268 | ); |
||
269 | |||
270 | $this->assertContains( |
||
271 | 'Quantity has been set', |
||
272 | $response->getBody(), |
||
273 | "Contains a confirmation message that the quantity has been set to a new value" |
||
274 | ); |
||
275 | } |
||
276 | |||
277 | public function testClearAllItemsFromTheCart() |
||
304 | ); |
||
305 | } |
||
306 | |||
307 | public function testVariations() |
||
308 | { |
||
309 | $this->loadFixture('vendor/silvershop/core/tests/php/Fixtures/variations.yml'); |
||
310 | $ballRoot = $this->objFromFixture(Product::class, 'ball'); |
||
311 | |||
312 | // add parent for categories in JSON response |
||
313 | $parent = $this->objFromFixture(ProductCategory::class, 'products'); |
||
314 | $ballRoot->ParentID = $parent->ID; |
||
315 | $ballRoot->write(); |
||
316 | |||
317 | $ballRoot->publish('Stage', 'Live'); |
||
318 | $ball1 = $this->objFromFixture(Variation::class, 'redlarge'); |
||
319 | $ball2 = $this->objFromFixture(Variation::class, 'redsmall'); |
||
320 | |||
321 | // Add the two variation items |
||
322 | $response = $this->get(ShoppingCartController::add_item_link($ball1) . "?ajax=1"); |
||
323 | $this->assertEquals( |
||
324 | 200, |
||
325 | $response->getStatusCode(), |
||
326 | "Response status code is 200" |
||
327 | ); |
||
328 | $this->assertEquals( |
||
329 | "application/json; charset=utf-8", |
||
330 | $response->getHeader("Content-Type"), |
||
331 | "Json response header" |
||
332 | ); |
||
333 | $this->assertJson( |
||
334 | $response->getBody(), |
||
335 | "Contains json in the body of the response" |
||
336 | ); |
||
337 | $this->assertContains( |
||
338 | "Size:Large, Color:Red", |
||
339 | $response->getBody(), |
||
340 | "Contains json in the body of the cart id" |
||
341 | ); |
||
342 | |||
343 | $response = $this->get(ShoppingCartController::add_item_link($ball2) . "?ajax=1"); |
||
344 | $items = ShoppingCart::curr()->Items(); |
||
345 | $this->assertEquals( |
||
346 | $items->Count(), |
||
347 | 2, |
||
348 | "There are 2 items in the cart" |
||
349 | ); |
||
350 | |||
351 | // Remove one and see what happens |
||
352 | $response = $this->get(ShoppingCartController::remove_all_item_link($ball1) . "?ajax=1"); |
||
353 | $this->assertEquals( |
||
368 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.