|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Order Unit Tests |
|
5
|
|
|
* |
|
6
|
|
|
* @link Order |
|
7
|
|
|
* @package shop |
|
8
|
|
|
* @subpackage tests |
|
9
|
|
|
*/ |
|
10
|
|
|
class OrderTest extends SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
public static $fixture_file = 'silvershop/tests/fixtures/shop.yml'; |
|
13
|
|
|
|
|
14
|
|
|
public function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
ShopTest::setConfiguration(); |
|
18
|
|
|
$this->mp3player = $this->objFromFixture('Product', 'mp3player'); |
|
|
|
|
|
|
19
|
|
|
$this->mp3player->publish('Stage', 'Live'); |
|
20
|
|
|
$this->socks = $this->objFromFixture('Product', 'socks'); |
|
|
|
|
|
|
21
|
|
|
$this->socks->publish('Stage', 'Live'); |
|
22
|
|
|
$this->beachball = $this->objFromFixture('Product', 'beachball'); |
|
|
|
|
|
|
23
|
|
|
$this->beachball->publish('Stage', 'Live'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function tearDown() |
|
27
|
|
|
{ |
|
28
|
|
|
parent::tearDown(); |
|
29
|
|
|
unset($this->mp3player); |
|
30
|
|
|
unset($this->socks); |
|
31
|
|
|
unset($this->beachball); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCMSFields() |
|
35
|
|
|
{ |
|
36
|
|
|
singleton('Order')->getCMSFields(); |
|
37
|
|
|
$this->markTestIncomplete('assertions!'); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testSearchFields() |
|
41
|
|
|
{ |
|
42
|
|
|
singleton('Order')->scaffoldSearchFields(); |
|
43
|
|
|
$this->markTestIncomplete('assertions!'); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testDebug() |
|
47
|
|
|
{ |
|
48
|
|
|
$order = $this->objFromFixture("Order", "cart"); |
|
49
|
|
|
$order->debug(); |
|
50
|
|
|
$this->markTestIncomplete('assertions!'); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testOrderItems() |
|
54
|
|
|
{ |
|
55
|
|
|
$order = $this->objFromFixture("Order", "paid"); |
|
56
|
|
|
$items = $order->Items(); |
|
57
|
|
|
$this->assertNotNull($items); |
|
|
|
|
|
|
58
|
|
|
$this->assertDOSEquals( |
|
59
|
|
|
array( |
|
60
|
|
|
array('ProductID' => $this->mp3player->ID, 'Quantity' => 2, 'CalculatedTotal' => 400), |
|
61
|
|
|
array('ProductID' => $this->socks->ID, 'Quantity' => 1, 'CalculatedTotal' => 8), |
|
62
|
|
|
), |
|
63
|
|
|
$items |
|
64
|
|
|
); |
|
65
|
|
|
$this->assertEquals(3, $items->Quantity(), "Quantity is 3"); |
|
|
|
|
|
|
66
|
|
|
$this->assertTrue($items->Plural(), "There is more than one item"); |
|
|
|
|
|
|
67
|
|
|
$this->assertEquals(0.7, $items->Sum('Weight', true), "Total order weight sums correctly", 0.0001); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testTotals() |
|
71
|
|
|
{ |
|
72
|
|
|
$order = $this->objFromFixture("Order", "paid"); |
|
73
|
|
|
$this->assertEquals(408, $order->SubTotal(), "Subtotal is correct"); // 200 + 200 + 8 |
|
|
|
|
|
|
74
|
|
|
$this->assertEquals(408, $order->GrandTotal(), "Grand total is correct"); |
|
|
|
|
|
|
75
|
|
|
$this->assertEquals(200, $order->TotalPaid(), "Outstanding total is correct"); |
|
|
|
|
|
|
76
|
|
|
$this->assertEquals(208, $order->TotalOutstanding(), "Outstanding total is correct"); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testRounding() |
|
80
|
|
|
{ |
|
81
|
|
|
//create an order with unrounded total |
|
82
|
|
|
$order = new Order( |
|
83
|
|
|
array( |
|
84
|
|
|
'Total' => 123.257323, |
|
85
|
|
|
//NOTE: setTotal isn't called here, so un-rounded data *could* get in to the object |
|
86
|
|
|
'Status' => 'Unpaid', |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
$order->Total = 123.257323; //setTotal IS called here |
|
|
|
|
|
|
90
|
|
|
$this->assertEquals(123.26, $order->Total(), "Check total rounds appropriately"); |
|
|
|
|
|
|
91
|
|
|
$this->assertEquals(123.26, $order->TotalOutstanding(), "Check total outstanding rounds appropriately"); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testPlacedOrderImmutability() |
|
95
|
|
|
{ |
|
96
|
|
|
|
|
97
|
|
|
$order = $this->objFromFixture("Order", "paid"); |
|
98
|
|
|
$processor = OrderProcessor::create($order)->placeOrder(); |
|
|
|
|
|
|
99
|
|
|
$this->assertEquals(408, $order->Total(), "check totals"); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
//make a changes to existing products |
|
102
|
|
|
$this->mp3player->BasePrice = 100; |
|
103
|
|
|
$this->mp3player->write(); |
|
104
|
|
|
$this->socks->BasePrice = 20; |
|
105
|
|
|
$this->socks->write(); |
|
106
|
|
|
|
|
107
|
|
|
//total doesn't change |
|
108
|
|
|
$this->assertEquals(408, $order->Total()); |
|
|
|
|
|
|
109
|
|
|
$this->assertFalse($order->isCart()); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
//item values don't change |
|
112
|
|
|
$items = $order->Items() |
|
113
|
|
|
//hack join to make thigns work |
|
114
|
|
|
->innerJoin( |
|
115
|
|
|
"Product_OrderItem", |
|
116
|
|
|
'"OrderItem"."ID" = "Product_OrderItem"."ID"' |
|
117
|
|
|
); |
|
118
|
|
|
$this->assertNotNull($items); |
|
|
|
|
|
|
119
|
|
|
$this->assertDOSEquals( |
|
120
|
|
|
array( |
|
121
|
|
|
array('ProductID' => $this->mp3player->ID, 'Quantity' => 2, 'CalculatedTotal' => 400), |
|
122
|
|
|
array('ProductID' => $this->socks->ID, 'Quantity' => 1, 'CalculatedTotal' => 8), |
|
123
|
|
|
), |
|
124
|
|
|
$items |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$mp3player = $items->find('ProductID', $this->mp3player->ID);//join needed to provide ProductID |
|
128
|
|
|
$this->assertNotNull($mp3player, "MP3 player is in order"); |
|
|
|
|
|
|
129
|
|
|
$this->assertEquals(200, $mp3player->UnitPrice(), "Unit price remains the same"); |
|
|
|
|
|
|
130
|
|
|
$this->assertEquals(400, $mp3player->Total(), "Total remains the same"); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$socks = $items->find('ProductID', $this->socks->ID); |
|
133
|
|
|
$this->assertNotNull($socks, "Socks are in order"); |
|
|
|
|
|
|
134
|
|
|
$this->assertEquals(8, $socks->UnitPrice(), "Unit price remains the same"); |
|
|
|
|
|
|
135
|
|
|
$this->assertEquals(8, $socks->Total(), "Total remains the same"); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testCanFunctions() |
|
139
|
|
|
{ |
|
140
|
|
|
$order = $this->objFromFixture("Order", "cart"); |
|
141
|
|
|
$order->calculate(); |
|
142
|
|
|
$this->assertTrue($order->canPay(), "can pay when order is in cart"); |
|
|
|
|
|
|
143
|
|
|
$this->assertFalse($order->canCancel(), "can't cancel when order is in cart"); |
|
|
|
|
|
|
144
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
145
|
|
|
$this->assertTrue($order->canEdit(), "orders can be edited by anyone"); |
|
|
|
|
|
|
146
|
|
|
$this->assertFalse($order->canCreate(), "no body can create orders manually"); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
$order = $this->objFromFixture("Order", "unpaid"); |
|
149
|
|
|
$this->assertTrue($order->canPay(), "can pay an order that is unpaid"); |
|
|
|
|
|
|
150
|
|
|
$this->assertTrue($order->canCancel()); |
|
|
|
|
|
|
151
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
// Override config |
|
154
|
|
|
Config::inst()->update('Order', 'cancel_before_payment', false); |
|
155
|
|
|
$this->assertFalse($order->canCancel()); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$order = $this->objFromFixture("Order", "paid"); |
|
158
|
|
|
$this->assertFalse($order->canPay(), "paid order can't be paid for"); |
|
|
|
|
|
|
159
|
|
|
$this->assertFalse($order->canCancel(), "paid order can't be cancelled"); |
|
|
|
|
|
|
160
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
Config::inst()->update('Order', 'cancel_before_processing', true); |
|
163
|
|
|
$this->assertTrue($order->canCancel(), "paid order can be cancelled when expcicitly set via config"); |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
$order->Status = 'Processing'; |
|
166
|
|
|
$this->assertFalse($order->canPay(), "Processing order can't be paid for"); |
|
|
|
|
|
|
167
|
|
|
$this->assertFalse($order->canCancel(), "Processing order can't be cancelled"); |
|
|
|
|
|
|
168
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
Config::inst()->update('Order', 'cancel_before_sending', true); |
|
171
|
|
|
$this->assertTrue($order->canCancel(), "Processing order can be cancelled when expcicitly set via config"); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$order->Status = 'Sent'; |
|
174
|
|
|
$this->assertFalse($order->canPay(), "Sent order can't be paid for"); |
|
|
|
|
|
|
175
|
|
|
$this->assertFalse($order->canCancel(), "Sent order can't be cancelled"); |
|
|
|
|
|
|
176
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
Config::inst()->update('Order', 'cancel_after_sending', true); |
|
179
|
|
|
$this->assertTrue($order->canCancel(), "Sent order can be cancelled when expcicitly set via config"); |
|
|
|
|
|
|
180
|
|
|
Config::inst()->update('Order', 'cancel_after_sending', false); |
|
181
|
|
|
|
|
182
|
|
|
$order->Status = 'Complete'; |
|
183
|
|
|
$this->assertFalse($order->canPay(), "Complete order can't be paid for"); |
|
|
|
|
|
|
184
|
|
|
$this->assertFalse($order->canCancel(), "Complete order can't be cancelled"); |
|
|
|
|
|
|
185
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
Config::inst()->update('Order', 'cancel_after_sending', true); |
|
188
|
|
|
$this->assertTrue($order->canCancel(), "Completed order can be cancelled when expcicitly set via config"); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
$order->Status = 'AdminCancelled'; |
|
191
|
|
|
$this->assertFalse($order->canPay(), "Cancelled order can't be paid for"); |
|
|
|
|
|
|
192
|
|
|
$this->assertFalse($order->canCancel(), "Cancelled order can't be cancelled"); |
|
|
|
|
|
|
193
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
$order->Status = 'MemberCancelled'; |
|
196
|
|
|
$this->assertFalse($order->canPay(), "Cancelled order can't be paid for"); |
|
|
|
|
|
|
197
|
|
|
$this->assertFalse($order->canCancel(), "Cancelled order can't be cancelled"); |
|
|
|
|
|
|
198
|
|
|
$this->assertFalse($order->canDelete(), "never allow deleting orders"); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function testDelete() |
|
202
|
|
|
{ |
|
203
|
|
|
Config::inst()->update('FlatTaxModifier', 'rate', 0.25); |
|
204
|
|
|
Config::inst()->update('Order', 'modifiers', array('FlatTaxModifier')); |
|
205
|
|
|
|
|
206
|
|
|
$order = Order::create(); |
|
207
|
|
|
$shirt = $this->objFromFixture("Product", "tshirt"); |
|
208
|
|
|
$mp3player = $this->objFromFixture("Product", "mp3player"); |
|
209
|
|
|
$order->Items()->add($shirt->createItem(3)); |
|
|
|
|
|
|
210
|
|
|
$order->Items()->add($mp3player->createItem(1)); |
|
|
|
|
|
|
211
|
|
|
$order->write(); |
|
212
|
|
|
$order->calculate(); |
|
213
|
|
|
|
|
214
|
|
|
$statusLogId = OrderStatusLog::create(array( |
|
215
|
|
|
'Title' => 'Test status log', |
|
216
|
|
|
'OrderID' => $order->ID |
|
217
|
|
|
))->write(); |
|
218
|
|
|
|
|
219
|
|
|
$paymentId = Payment::create(array( |
|
220
|
|
|
'OrderID' => $order->ID |
|
221
|
|
|
))->init('Manual', 343.75, 'NZD')->write(); |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
$this->assertEquals(4, $order->Items()->Quantity()); |
|
|
|
|
|
|
225
|
|
|
$this->assertEquals(1, $order->Modifiers()->count()); |
|
|
|
|
|
|
226
|
|
|
$this->assertEquals(1, $order->OrderStatusLogs()->count()); |
|
|
|
|
|
|
227
|
|
|
$this->assertEquals(1, $order->Payments()->count()); |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
$itemIds = Product_OrderItem::get()->filter('OrderID', $order->ID)->column('ID'); |
|
230
|
|
|
$modifierIds = OrderModifier::get()->filter('OrderID', $order->ID)->column('ID'); |
|
231
|
|
|
|
|
232
|
|
|
$order->delete(); |
|
233
|
|
|
|
|
234
|
|
|
// Items should no longer be linked to order |
|
235
|
|
|
$this->assertEquals(0, $order->Items()->count()); |
|
|
|
|
|
|
236
|
|
|
$this->assertEquals(0, $order->Modifiers()->count()); |
|
|
|
|
|
|
237
|
|
|
$this->assertEquals(0, $order->OrderStatusLogs()->count()); |
|
|
|
|
|
|
238
|
|
|
$this->assertEquals(0, $order->Payments()->count()); |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
// Ensure the order items have been deleted! |
|
241
|
|
|
$this->assertEquals(0, Product_OrderItem::get()->filter('ID', $itemIds)->count()); |
|
|
|
|
|
|
242
|
|
|
$this->assertEquals(0, OrderModifier::get()->filter('ID', $modifierIds)->count()); |
|
|
|
|
|
|
243
|
|
|
$this->assertEquals(0, OrderStatusLog::get()->filter('ID', $statusLogId)->count()); |
|
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
// Keep the payment… it might be relevant for book keeping |
|
246
|
|
|
$this->assertEquals(1, Payment::get()->filter('ID', $paymentId)->count()); |
|
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function testStatusChange() |
|
250
|
|
|
{ |
|
251
|
|
|
Config::inst()->update('Order', 'extensions', array('OrderTest_TestStatusChangeExtension')); |
|
252
|
|
|
|
|
253
|
|
|
$order = Order::create(); |
|
254
|
|
|
$orderId = $order->write(); |
|
255
|
|
|
|
|
256
|
|
|
$order->Status = 'Unpaid'; |
|
257
|
|
|
$order->write(); |
|
258
|
|
|
|
|
259
|
|
|
$this->assertEquals(array( |
|
|
|
|
|
|
260
|
|
|
array('Cart' => 'Unpaid') |
|
261
|
|
|
), OrderTest_TestStatusChangeExtension::$stack); |
|
262
|
|
|
|
|
263
|
|
|
OrderTest_TestStatusChangeExtension::reset(); |
|
264
|
|
|
|
|
265
|
|
|
$order = Order::get()->byID($orderId); |
|
266
|
|
|
$order->Status = 'Paid'; |
|
267
|
|
|
$order->write(); |
|
268
|
|
|
|
|
269
|
|
|
$this->assertEquals(array( |
|
|
|
|
|
|
270
|
|
|
array('Unpaid' => 'Paid') |
|
271
|
|
|
), OrderTest_TestStatusChangeExtension::$stack); |
|
272
|
|
|
|
|
273
|
|
|
$this->assertTrue((boolean)$order->Paid, 'Order paid date should be set'); |
|
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
class OrderTest_TestStatusChangeExtension extends DataExtension implements TestOnly |
|
278
|
|
|
{ |
|
279
|
|
|
public static $stack = array(); |
|
280
|
|
|
|
|
281
|
|
|
public static function reset() |
|
282
|
|
|
{ |
|
283
|
|
|
self::$stack = array(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
public function onStatusChange($fromStatus, $toStatus) |
|
287
|
|
|
{ |
|
288
|
|
|
self::$stack[] = array( |
|
289
|
|
|
$fromStatus => $toStatus |
|
290
|
|
|
); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
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: