1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class DMSDocumentCartControllerTest contains all the tests for {@link DMSDocumentCartController} |
5
|
|
|
*/ |
6
|
|
|
class DMSDocumentCartControllerTest extends FunctionalTest |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected static $fixture_file = 'dms-cart/tests/DMSDocumentCartTest.yml'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var DMSDocumentCartController |
12
|
|
|
*/ |
13
|
|
|
protected $controller; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var DMSDocumentCart |
17
|
|
|
*/ |
18
|
|
|
protected $cart; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
DMSDocumentCartController::add_extension('StubDMSDocumentCheckoutPageExtension'); |
24
|
|
|
$this->controller = DMSDocumentCartController::create(); |
25
|
|
|
$this->cart = $this->controller->getCart(); |
26
|
|
|
$this->cart->emptyCart(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Test the items method of the controller |
31
|
|
|
*/ |
32
|
|
|
public function testItems() |
33
|
|
|
{ |
34
|
|
|
/** @var DMSDocument $doc1 */ |
35
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
36
|
|
|
/** @var DMSDocument $doc2 */ |
37
|
|
|
$doc2 = $this->objFromFixture('DMSDocument', 'doc2'); |
38
|
|
|
/** @var DMSRequestItem $item */ |
39
|
|
|
$item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2); |
40
|
|
|
$item2 = DMSRequestItem::create()->setDocument($doc2)->setQuantity(5); |
41
|
|
|
$this->cart->addItem($item1); |
42
|
|
|
$this->cart->addItem($item2); |
43
|
|
|
$this->assertInstanceOf( |
|
|
|
|
44
|
|
|
'ArrayList', |
45
|
|
|
$this->controller->items(), |
46
|
|
|
'DMSDocumentCartController->Items() returned an ArrayList' |
47
|
|
|
); |
48
|
|
|
$this->assertCount( |
|
|
|
|
49
|
|
|
2, |
50
|
|
|
$this->controller->items(), |
51
|
|
|
'DMSDocumentCartController->Items()->count() returned the requisite number of items' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testReceiverInfo() |
56
|
|
|
{ |
57
|
|
|
// Now add some info |
58
|
|
|
$this->cart->setReceiverInfo(array('Name' => 'Joe', 'Surname' => 'Soap')); |
59
|
|
|
$this->assertCount(2, $this->controller->getReceiverInfo()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testIsCartEmpty() |
63
|
|
|
{ |
64
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
65
|
|
|
/** @var DMSDocument $doc */ |
66
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
67
|
|
|
/** @var DMSRequestItem $item */ |
68
|
|
|
$item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2); |
|
|
|
|
69
|
|
|
$this->cart->addItem($item1); |
70
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testAdd() |
74
|
|
|
{ |
75
|
|
|
$this->logInWithPermission(); |
76
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
77
|
|
|
// Check cart is initially empty |
78
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
79
|
|
|
// Now call controller add |
80
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 5)); |
81
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
82
|
|
|
$this->controller->add($request); |
83
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// Do it again and assert quantity was updated |
86
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7)); |
87
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
88
|
|
|
$this->controller->add($request); |
89
|
|
|
|
90
|
|
|
// Test ajax |
91
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); |
92
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
93
|
|
|
$response = $this->controller->add($request); |
94
|
|
|
$this->assertTrue($request->isAjax()); |
|
|
|
|
95
|
|
|
$this->assertJson($response, 'Confirmed that an ajax call to add() responded with json JSON'); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$item = $this->cart->getItem($doc1->ID); |
98
|
|
|
$this->assertEquals(19, $item->getQuantity()); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
public function testDeduct() |
104
|
|
|
{ |
105
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
106
|
|
|
// Check catty is initially empty |
107
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
108
|
|
|
// Now call controller add |
109
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5)); |
110
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
111
|
|
|
$this->controller->deduct($request); |
112
|
|
|
// Assert cart still empty because item doesn't exist |
113
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
114
|
|
|
// Now add item |
115
|
|
|
$this->controller->add($request); |
116
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
// Now try and deduct 2 from the items |
120
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => -2)); |
121
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
122
|
|
|
$this->controller->deduct($request); |
123
|
|
|
|
124
|
|
|
$item = $this->cart->getItem($doc1->ID); |
125
|
|
|
$this->assertEquals(3, $item->getQuantity()); |
|
|
|
|
126
|
|
|
|
127
|
|
|
// Test ajax |
128
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); |
129
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
130
|
|
|
$response = $this->controller->deduct($request); |
131
|
|
|
$this->assertTrue($request->isAjax()); |
|
|
|
|
132
|
|
|
$this->assertJson($response, 'Confirmed that an ajax call to deduct() method responded with JSON'); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testRemove() |
136
|
|
|
{ |
137
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
138
|
|
|
// Check catty is initially empty |
139
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
140
|
|
|
// Now call controller add |
141
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5)); |
142
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
143
|
|
|
// Now add item |
144
|
|
|
$this->controller->add($request); |
145
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
146
|
|
|
$this->controller->remove($request); |
147
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
148
|
|
|
|
149
|
|
|
// Test ajax |
150
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); |
151
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
152
|
|
|
$response = $this->controller->remove($request); |
153
|
|
|
$this->assertTrue($request->isAjax()); |
|
|
|
|
154
|
|
|
$this->assertJson($response, 'Confirmed that an ajax call to remove() method responded with JSON'); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Ensure that a validation error is shown when requesting to add more of a document that is allowed |
159
|
|
|
*/ |
160
|
|
|
public function testCannotAddMoreThanSuggestedQuantityOfItem() |
161
|
|
|
{ |
162
|
|
|
$document = $this->objFromFixture('DMSDocument', 'limited_supply'); |
163
|
|
|
$result = $this->get('/documentcart/add/' . $document->ID . '?quantity=5&ajax=1'); |
164
|
|
|
$this->assertContains( |
165
|
|
|
'Maximum of 3 documents exceeded for \"Doc3\", please select a lower quantity.', |
166
|
|
|
(string) $result->getBody() |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Ensure that multiple validation errors are returned in the failure message, if any |
172
|
|
|
*/ |
173
|
|
|
public function testMultipleValidationErrorsReturned() |
174
|
|
|
{ |
175
|
|
|
$document1 = $this->objFromFixture('DMSDocument', 'limited_supply'); |
176
|
|
|
$document2 = $this->objFromFixture('DMSDocument', 'very_limited_supply'); |
177
|
|
|
|
178
|
|
|
$this->cart |
179
|
|
|
->addItem(DMSRequestItem::create($document1)) |
180
|
|
|
->addItem(DMSRequestItem::create($document2)); |
181
|
|
|
|
182
|
|
|
$input = array('ItemQuantity' => array( |
183
|
|
|
$document1->ID => 15000, |
184
|
|
|
$document2->ID => 12000 |
185
|
|
|
)); |
186
|
|
|
|
187
|
|
|
$form = Form::create($this->controller, '', new FieldList, new FieldList); |
188
|
|
|
$result = $this->controller->updateCartItems($input, $form, new SS_HTTPRequest('POST', '/')); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$this->assertContains('Maximum of 3 documents exceeded for "Doc3"', $form->Message()); |
191
|
|
|
$this->assertContains('Maximum of 2 documents exceeded for "Doc5"', $form->Message()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Ensure that when a document that cannot be added to the cart is added to the cart, a validation error is |
196
|
|
|
* returned |
197
|
|
|
*/ |
198
|
|
|
public function testValidationErrorReturnedOnInvalidAdd() |
199
|
|
|
{ |
200
|
|
|
$document = $this->objFromFixture('DMSDocument', 'not_allowed_in_cart'); |
201
|
|
|
$result = $this->get('/documentcart/add/' . $document->ID . '?ajax=1'); |
202
|
|
|
$this->assertContains('You are not allowed to add this document', (string) $result->getBody()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Tests whether the cart items are updated from the controller |
207
|
|
|
* |
208
|
|
|
* @param array $quantity |
209
|
|
|
* @param int|string $expectedCount |
210
|
|
|
* @dataProvider updateCartItemsProvider |
211
|
|
|
*/ |
212
|
|
|
public function testUpdateCartItems($quantity, $expectedCount) |
213
|
|
|
{ |
214
|
|
|
$document = $this->objFromFixture('DMSDocument', 'doc1'); |
215
|
|
|
$item = DMSRequestItem::create()->setDocument($document)->setQuantity(2); |
|
|
|
|
216
|
|
|
$this->cart->addItem($item); |
217
|
|
|
|
218
|
|
|
$quantities = array('ItemQuantity' => array($document->ID => $quantity)); |
219
|
|
|
|
220
|
|
|
$form = Form::create($this->controller, 'Test', FieldList::create(), FieldList::create()); |
221
|
|
|
$request = new SS_HTTPRequest('POST', ''); |
222
|
|
|
$response = $this->controller->updateCartItems($quantities, $form, $request); |
|
|
|
|
223
|
|
|
|
224
|
|
|
$item = $this->cart->getItem($item->getItemId()); |
225
|
|
|
if (is_numeric($quantity) && $quantity <= 0) { |
226
|
|
|
$this->assertFalse($item); |
|
|
|
|
227
|
|
|
} else { |
228
|
|
|
$this->assertEquals($expectedCount, $item->getQuantity()); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return array[] |
234
|
|
|
*/ |
235
|
|
|
public function updateCartItemsProvider() |
236
|
|
|
{ |
237
|
|
|
return array( |
238
|
|
|
// Invalid quantity, leave it unchanged |
239
|
|
|
array('non-numeric', 2), |
240
|
|
|
// Existing quantity remains the same |
241
|
|
|
array(2, 2), |
242
|
|
|
// Valid quantity increase |
243
|
|
|
array(5, 5), |
244
|
|
|
// Zero, remove it |
245
|
|
|
array(0, 0), |
246
|
|
|
// Negative, treat as zero |
247
|
|
|
array(-10, 0) |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Tests DMSCartEditForm form has a FieldList |
253
|
|
|
*/ |
254
|
|
|
public function testDMSCartEditForm() |
255
|
|
|
{ |
256
|
|
|
$form = $this->controller->DMSCartEditForm(); |
257
|
|
|
$this->assertInstanceOf('FieldList', $form->Fields()); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Tests if the DMSCartEditForm is extensible |
262
|
|
|
*/ |
263
|
|
|
public function testDMSCartEditFormIsExtensible() |
264
|
|
|
{ |
265
|
|
|
$controller = $this->controller; |
266
|
|
|
$form = $controller->DMSCartEditForm(); |
267
|
|
|
$this->assertNotNull( |
|
|
|
|
268
|
|
|
$form->Fields()->fieldByName('NewTextField'), |
269
|
|
|
'DMSDocumentRequestForm() is extensible as it included the field from the extension' |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Tests that the cart summary view is returned. |
275
|
|
|
*/ |
276
|
|
|
public function testView() |
277
|
|
|
{ |
278
|
|
|
$result = $this->get('documentcart/view'); |
279
|
|
|
$this->assertInstanceOf('SS_HTTPResponse', $result); |
|
|
|
|
280
|
|
|
$this->assertContains('Updating cart items', $result->getBody()); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Ensure the link is "friendly", not a class name |
285
|
|
|
*/ |
286
|
|
|
public function testLink() |
287
|
|
|
{ |
288
|
|
|
$this->assertSame('documentcart', $this->controller->Link()); |
|
|
|
|
289
|
|
|
$this->assertSame('documentcart/view', $this->controller->Link('view')); |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Ensure that the user is notified when the cart is empty |
294
|
|
|
*/ |
295
|
|
|
public function testEmptyCartShowsNoticeOnCheckout() |
296
|
|
|
{ |
297
|
|
|
$result = $this->get('checkout'); |
298
|
|
|
$this->assertContains('Your cart is currently empty.', $result->getBody()); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
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.