1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link ShoppingCart_Controller |
5
|
|
|
* |
6
|
|
|
* Test manipulating via urls. |
7
|
|
|
*/ |
8
|
|
|
class ShoppingCartControllerTest extends FunctionalTest |
9
|
|
|
{ |
10
|
|
|
public static $fixture_file = 'silvershop/tests/fixtures/shop.yml'; |
11
|
|
|
public static $disable_theme = true; |
12
|
|
|
public static $use_draft_site = false; |
13
|
|
|
protected $autoFollowRedirection = false; |
14
|
|
|
|
15
|
|
|
public function setUpOnce() |
16
|
|
|
{ |
17
|
|
|
parent::setUpOnce(); |
18
|
|
|
// clear session |
19
|
|
|
ShoppingCart::singleton()->clear(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
ShopTest::setConfiguration(); //reset config |
27
|
|
|
|
28
|
|
|
$this->mp3player = $this->objFromFixture('Product', 'mp3player'); |
|
|
|
|
29
|
|
|
$this->socks = $this->objFromFixture('Product', 'socks'); |
|
|
|
|
30
|
|
|
//products that can't be purchased |
31
|
|
|
$this->noPurchaseProduct = $this->objFromFixture('Product', 'beachball'); |
|
|
|
|
32
|
|
|
$this->draftProduct = $this->objFromFixture('Product', 'tshirt'); |
|
|
|
|
33
|
|
|
$this->noPriceProduct = $this->objFromFixture('Product', 'hdtv'); |
|
|
|
|
34
|
|
|
|
35
|
|
|
//publish some products |
36
|
|
|
$this->mp3player->publish('Stage', 'Live'); |
37
|
|
|
$this->socks->publish('Stage', 'Live'); |
38
|
|
|
$this->noPurchaseProduct->publish('Stage', 'Live'); |
39
|
|
|
$this->noPriceProduct->publish('Stage', 'Live'); |
40
|
|
|
//note that we don't publish 'tshirt'... we want it to remain in draft form. |
41
|
|
|
|
42
|
|
|
$this->cart = ShoppingCart::singleton(); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testAddToCart() |
46
|
|
|
{ |
47
|
|
|
// add 2 of the same items via url |
48
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->mp3player)); //add item via url |
|
|
|
|
49
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->mp3player)); //add another |
|
|
|
|
50
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); //add a different product |
|
|
|
|
51
|
|
|
$this->get( |
52
|
|
|
ShoppingCart_Controller::add_item_link($this->noPurchaseProduct) |
|
|
|
|
53
|
|
|
); //add a product that you can't add |
54
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->draftProduct)); //add a product that is draft |
|
|
|
|
55
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->noPriceProduct)); //add a product that has no price |
|
|
|
|
56
|
|
|
|
57
|
|
|
// See what's in the cart |
58
|
|
|
$items = ShoppingCart::curr()->Items(); |
|
|
|
|
59
|
|
|
$this->assertNotNull($items); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->assertEquals($items->Count(), 2, 'There are 2 items in the cart'); |
|
|
|
|
62
|
|
|
//join needed to provide ProductID |
63
|
|
|
$mp3playeritem = |
64
|
|
|
$items->innerJoin("Product_OrderItem", "\"OrderItem\".\"ID\" = \"Product_OrderItem\".\"ID\"")->find( |
65
|
|
|
'ProductID', |
66
|
|
|
$this->mp3player->ID |
67
|
|
|
); //join needed to provide ProductID |
68
|
|
|
$this->assertNotNull($mp3playeritem, "Mp3 player is in cart"); |
|
|
|
|
69
|
|
|
|
70
|
|
|
// We have the product that we asserted in our fixture file, with a quantity of 2 in the cart |
71
|
|
|
$this->assertEquals( |
|
|
|
|
72
|
|
|
$mp3playeritem->ProductID, |
73
|
|
|
$this->mp3player->ID, |
74
|
|
|
'We have the correct Product ID in the cart.' |
75
|
|
|
); |
76
|
|
|
$this->assertEquals($mp3playeritem->Quantity, 2, 'We have 2 of this product in the cart.'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
// set item quantiy |
79
|
|
|
$this->get( |
80
|
|
|
ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5)) |
|
|
|
|
81
|
|
|
); //add item via url |
82
|
|
|
$items = ShoppingCart::curr()->Items(); |
|
|
|
|
83
|
|
|
$mp3playeritem = |
84
|
|
|
$items->innerJoin("Product_OrderItem", "\"OrderItem\".\"ID\" = \"Product_OrderItem\".\"ID\"")->find( |
85
|
|
|
'ProductID', |
86
|
|
|
$this->mp3player->ID |
87
|
|
|
); //join needed to provide ProductID |
88
|
|
|
$this->assertEquals($mp3playeritem->Quantity, 5, 'We have 5 of this product in the cart.'); |
|
|
|
|
89
|
|
|
|
90
|
|
|
// non purchasable product checks |
91
|
|
|
$this->assertEquals( |
|
|
|
|
92
|
|
|
$this->noPurchaseProduct->canPurchase(), |
93
|
|
|
false, |
94
|
|
|
'non-purcahseable product is not purchaseable' |
95
|
|
|
); |
96
|
|
|
$this->assertArrayNotHasKey( |
|
|
|
|
97
|
|
|
$this->noPurchaseProduct->ID, |
98
|
|
|
$items->map('ProductID')->toArray(), |
99
|
|
|
'non-purcahable product is not in cart' |
100
|
|
|
); |
101
|
|
|
$this->assertEquals($this->draftProduct->canPurchase(), true, 'draft products can be purchased'); |
|
|
|
|
102
|
|
|
$this->assertArrayNotHasKey( |
|
|
|
|
103
|
|
|
$this->draftProduct->ID, |
104
|
|
|
$items->map('ProductID')->toArray(), |
105
|
|
|
'draft product is not in cart' |
106
|
|
|
); |
107
|
|
|
$this->assertEquals($this->noPriceProduct->canPurchase(), false, 'product without price is not purchaseable'); |
|
|
|
|
108
|
|
|
$this->assertArrayNotHasKey( |
|
|
|
|
109
|
|
|
$this->noPriceProduct->ID, |
110
|
|
|
$items->map('ProductID')->toArray(), |
111
|
|
|
'product without price is not in cart' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->cart->clear(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testRemoveFromCart() |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
// add items via url |
121
|
|
|
$this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5))); |
|
|
|
|
122
|
|
|
$this->assertTrue($this->cart->get($this->mp3player) !== false, "mp3player item now exists in cart"); |
|
|
|
|
123
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($this->socks)); |
|
|
|
|
124
|
|
|
$this->assertTrue($this->cart->get($this->socks) !== false, "socks item now exists in cart"); |
|
|
|
|
125
|
|
|
|
126
|
|
|
// remove items via url |
127
|
|
|
$this->get(ShoppingCart_Controller::remove_item_link($this->socks)); //remove one different = remove completely |
|
|
|
|
128
|
|
|
$this->assertFalse($this->cart->get($this->socks)); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$this->get(ShoppingCart_Controller::remove_item_link($this->mp3player)); //remove one product = 4 left |
|
|
|
|
131
|
|
|
|
132
|
|
|
$mp3playeritem = $this->cart->get($this->mp3player); |
133
|
|
|
$this->assertTrue($mp3playeritem !== false, "product still exists"); |
|
|
|
|
134
|
|
|
$this->assertEquals($mp3playeritem->Quantity, 4, "only 4 of item left"); |
|
|
|
|
135
|
|
|
|
136
|
|
|
$items = ShoppingCart::curr()->Items(); |
|
|
|
|
137
|
|
|
$this->assertNotNull($items, "Cart is not empty"); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$this->cart->clear(); //test clearing cart |
140
|
|
|
$this->assertEquals( |
|
|
|
|
141
|
|
|
ShoppingCart::curr(), |
142
|
|
|
null, |
143
|
|
|
'Cart is clear' |
144
|
|
|
); //items is a databoject set, and will therefore be null when cart is empty. |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testVariations() |
148
|
|
|
{ |
149
|
|
|
$this->loadFixture('silvershop/tests/fixtures/variations.yml'); |
150
|
|
|
$ballRoot = $this->objFromFixture('Product', 'ball'); |
151
|
|
|
$ballRoot->publish('Stage', 'Live'); |
152
|
|
|
$ball1 = $this->objFromFixture('ProductVariation', 'redlarge'); |
153
|
|
|
$ball2 = $this->objFromFixture('ProductVariation', 'redsmall'); |
154
|
|
|
|
155
|
|
|
// Add the two variation items |
156
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($ball1)); |
|
|
|
|
157
|
|
|
$this->get(ShoppingCart_Controller::add_item_link($ball2)); |
|
|
|
|
158
|
|
|
$items = ShoppingCart::curr()->Items(); |
|
|
|
|
159
|
|
|
$this->assertNotNull($items); |
|
|
|
|
160
|
|
|
$this->assertEquals($items->Count(), 2, 'There are 2 items in the cart'); |
|
|
|
|
161
|
|
|
|
162
|
|
|
// Remove one and see what happens |
163
|
|
|
$this->get(ShoppingCart_Controller::remove_all_item_link($ball1)); |
|
|
|
|
164
|
|
|
$this->assertEquals($items->Count(), 1, 'There is 1 item in the cart'); |
|
|
|
|
165
|
|
|
$this->assertFalse($this->cart->get($ball1), "first item not in cart"); |
|
|
|
|
166
|
|
|
$this->assertNotNull($this->cart->get($ball1), "second item is in cart"); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testSecurityToken() |
170
|
|
|
{ |
171
|
|
|
$enabled = SecurityToken::is_enabled(); |
172
|
|
|
// enable security tokens |
173
|
|
|
SecurityToken::enable(); |
174
|
|
|
|
175
|
|
|
$productId = $this->mp3player->ID; |
176
|
|
|
// link should contain the security-token |
177
|
|
|
$link = ShoppingCart_Controller::add_item_link($this->mp3player); |
178
|
|
|
$this->assertRegExp('{^shoppingcart/add/Product/'.$productId.'\?SecurityID=[a-f0-9]+$}', $link); |
|
|
|
|
179
|
|
|
|
180
|
|
|
// should redirect back to the shop |
181
|
|
|
$response = $this->get($link); |
|
|
|
|
182
|
|
|
$this->assertEquals($response->getStatusCode(), 302); |
|
|
|
|
183
|
|
|
|
184
|
|
|
// disable security token for cart-links |
185
|
|
|
Config::inst()->update('ShoppingCart_Controller', 'disable_security_token', true); |
186
|
|
|
|
187
|
|
|
$link = ShoppingCart_Controller::add_item_link($this->mp3player); |
188
|
|
|
$this->assertEquals('shoppingcart/add/Product/'.$productId, $link); |
|
|
|
|
189
|
|
|
|
190
|
|
|
// should redirect back to the shop |
191
|
|
|
$response = $this->get($link); |
|
|
|
|
192
|
|
|
$this->assertEquals($response->getStatusCode(), 302); |
|
|
|
|
193
|
|
|
|
194
|
|
|
SecurityToken::disable(); |
195
|
|
|
|
196
|
|
|
Config::inst()->update('ShoppingCart_Controller', 'disable_security_token', false); |
197
|
|
|
$link = ShoppingCart_Controller::add_item_link($this->mp3player); |
198
|
|
|
$this->assertEquals('shoppingcart/add/Product/'.$productId , $link); |
|
|
|
|
199
|
|
|
|
200
|
|
|
// should redirect back to the shop |
201
|
|
|
$response = $this->get($link); |
|
|
|
|
202
|
|
|
$this->assertEquals($response->getStatusCode(), 302); |
|
|
|
|
203
|
|
|
|
204
|
|
|
SecurityToken::enable(); |
205
|
|
|
// should now return a 400 status |
206
|
|
|
$response = $this->get($link); |
|
|
|
|
207
|
|
|
$this->assertEquals($response->getStatusCode(), 400); |
|
|
|
|
208
|
|
|
|
209
|
|
|
// restore previous setting |
210
|
|
|
if(!$enabled){ |
211
|
|
|
SecurityToken::disable(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: