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 = '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
|
|
|
$this->controller = DMSDocumentCartController::create(); |
24
|
|
|
$this->cart = $this->controller->getCart(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test the items method of the controller |
29
|
|
|
*/ |
30
|
|
|
public function testItems() |
31
|
|
|
{ |
32
|
|
|
/** @var DMSDocument $doc1 */ |
33
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
34
|
|
|
/** @var DMSDocument $doc2 */ |
35
|
|
|
$doc2 = $this->objFromFixture('DMSDocument', 'doc2'); |
36
|
|
|
/** @var DMSRequestItem $item */ |
37
|
|
|
$item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2); |
38
|
|
|
$item2 = DMSRequestItem::create()->setDocument($doc2)->setQuantity(5); |
39
|
|
|
$this->cart->addItem($item1); |
40
|
|
|
$this->cart->addItem($item2); |
41
|
|
|
$this->assertInstanceOf( |
|
|
|
|
42
|
|
|
'ArrayList', |
43
|
|
|
$this->controller->items(), |
44
|
|
|
'DMSDocumentCartController->Items() returned an ArrayList' |
45
|
|
|
); |
46
|
|
|
$this->assertCount( |
|
|
|
|
47
|
|
|
2, |
48
|
|
|
$this->controller->items(), |
49
|
|
|
'DMSDocumentCartController->Items()->count() returned the requisite number of items' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testReceiverInfo() |
54
|
|
|
{ |
55
|
|
|
// Now add some info |
56
|
|
|
$this->cart->setReceiverInfo(array('Name' => 'Joe', 'Surname' => 'Soap')); |
57
|
|
|
$this->assertCount(2, $this->controller->getReceiverInfo()); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testIsCartEmpty() |
61
|
|
|
{ |
62
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
63
|
|
|
/** @var DMSDocument $doc */ |
64
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
65
|
|
|
/** @var DMSRequestItem $item */ |
66
|
|
|
$item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2); |
|
|
|
|
67
|
|
|
$this->cart->addItem($item1); |
68
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testAdd() |
72
|
|
|
{ |
73
|
|
|
$this->logInWithPermission(); |
74
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
75
|
|
|
// Check cart is initially empty |
76
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
77
|
|
|
// Now call controller add |
78
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 5)); |
79
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
80
|
|
|
$this->controller->add($request); |
81
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
82
|
|
|
|
83
|
|
|
// Do it again and assert quantity was updated |
84
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7)); |
85
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
86
|
|
|
$this->controller->add($request); |
87
|
|
|
|
88
|
|
|
// Test ajax |
89
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); |
90
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
91
|
|
|
$response = $this->controller->add($request); |
92
|
|
|
$this->assertTrue($request->isAjax()); |
|
|
|
|
93
|
|
|
$this->assertJson($response, 'Confirmed that an ajax call to add() responded with json JSON'); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$item = $this->cart->getItem($doc1->ID); |
96
|
|
|
$this->assertEquals(19, $item->getQuantity()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
public function testDeduct() |
102
|
|
|
{ |
103
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
104
|
|
|
// Check catty is initially empty |
105
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
106
|
|
|
// Now call controller add |
107
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5)); |
108
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
109
|
|
|
$this->controller->deduct($request); |
110
|
|
|
// Assert cart still empty because item doesn't exist |
111
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
112
|
|
|
// Now add item |
113
|
|
|
$this->controller->add($request); |
114
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
// Now try and deduct 2 from the items |
118
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => -2)); |
119
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
120
|
|
|
$this->controller->deduct($request); |
121
|
|
|
|
122
|
|
|
$item = $this->cart->getItem($doc1->ID); |
123
|
|
|
$this->assertEquals(3, $item->getQuantity()); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Test ajax |
126
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); |
127
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
128
|
|
|
$response = $this->controller->deduct($request); |
129
|
|
|
$this->assertTrue($request->isAjax()); |
|
|
|
|
130
|
|
|
$this->assertJson($response, 'Confirmed that an ajax call to deduct() method responded with JSON'); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testRemove() |
134
|
|
|
{ |
135
|
|
|
$doc1 = $this->objFromFixture('DMSDocument', 'doc1'); |
136
|
|
|
// Check catty is initially empty |
137
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
138
|
|
|
// Now call controller add |
139
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5)); |
140
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
141
|
|
|
// Now add item |
142
|
|
|
$this->controller->add($request); |
143
|
|
|
$this->assertFalse($this->controller->getIsCartEmpty()); |
|
|
|
|
144
|
|
|
$this->controller->remove($request); |
145
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
146
|
|
|
|
147
|
|
|
// Test ajax |
148
|
|
|
$request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); |
149
|
|
|
$request->setRouteParams(array('ID' => $doc1->ID)); |
150
|
|
|
$response = $this->controller->remove($request); |
151
|
|
|
$this->assertTrue($request->isAjax()); |
|
|
|
|
152
|
|
|
$this->assertJson($response, 'Confirmed that an ajax call to remove() method responded with JSON'); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testCart() |
156
|
|
|
{ |
157
|
|
|
$this->assertInstanceOf('DMSDocumentCart', $this->controller->getCart()); |
|
|
|
|
158
|
|
|
//For good measure assert it's empty |
159
|
|
|
$this->assertTrue($this->controller->getIsCartEmpty()); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Ensure that a validation error is shown when requesting to add more of a document that is allowed |
164
|
|
|
*/ |
165
|
|
|
public function testCannotAddMoreThanSuggestedQuantityOfItem() |
166
|
|
|
{ |
167
|
|
|
$document = $this->objFromFixture('DMSDocument', 'limited_supply'); |
168
|
|
|
$result = $this->get('/documentcart/add/' . $document->ID . '?quantity=5&ajax=1'); |
169
|
|
|
$this->assertContains('You can\'t add 5 of this document', (string) $result->getBody()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Ensure that when a document that cannot be added to the cart is added to the cart, a validation error is |
174
|
|
|
* returned |
175
|
|
|
*/ |
176
|
|
|
public function testValidationErrorReturnedOnInvalidAdd() |
177
|
|
|
{ |
178
|
|
|
$document = $this->objFromFixture('DMSDocument', 'not_allowed_in_cart'); |
179
|
|
|
$result = $this->get('/documentcart/add/' . $document->ID . '?ajax=1'); |
180
|
|
|
$this->assertContains('You are not allowed to add this document', (string) $result->getBody()); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.