1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DMSCheckoutControllerTest extends FunctionalTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
protected static $fixture_file = 'dms-cart/tests/DMSDocumentCartTest.yml'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var DMSCheckoutController |
9
|
|
|
*/ |
10
|
|
|
protected $controller; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var DMSDocumentCart |
14
|
|
|
*/ |
15
|
|
|
protected $cart; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DMSDocumentCartCheckoutPage |
19
|
|
|
*/ |
20
|
|
|
protected $page; |
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
DMSCheckoutController::add_extension('StubDMSDocumentCheckoutPageExtension'); |
27
|
|
|
Injector::inst()->registerService(new StubEmail(), 'Email'); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->controller = DMSCheckoutController::create(); |
30
|
|
|
$this->cart = $this->controller->getCart(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Tests DMSDocumentRequest form has a FieldList |
35
|
|
|
*/ |
36
|
|
|
public function testDMSDocumentRequestForm() |
37
|
|
|
{ |
38
|
|
|
$form = $this->controller->DMSDocumentRequestForm(); |
39
|
|
|
$this->assertInstanceOf('FieldList', $form->Fields()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Tests if the DMSDocumentRequestForm is extensible |
44
|
|
|
*/ |
45
|
|
|
public function testDMSDocumentRequestFormIsExtensible() |
46
|
|
|
{ |
47
|
|
|
$controller = $this->controller; |
48
|
|
|
$form = $controller->DMSDocumentRequestForm(); |
49
|
|
|
$this->assertNotNull( |
|
|
|
|
50
|
|
|
$form->Fields()->fieldByName('NewTextField'), |
51
|
|
|
'DMSDocumentRequestForm() is extensible as it included the field from the extension' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Tests if a Cart is received |
57
|
|
|
*/ |
58
|
|
|
public function testGetCart() |
59
|
|
|
{ |
60
|
|
|
$this->assertInstanceOf('DMSDocumentCart', $this->controller->getCart()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests whether the cart items are updated from the controller |
65
|
|
|
*/ |
66
|
|
|
public function testUpdateCartItems() |
67
|
|
|
{ |
68
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
69
|
|
|
/** @var DMSRequestItem $item */ |
70
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2); |
|
|
|
|
71
|
|
|
$this->cart->addItem($item); |
72
|
|
|
$updatedQuantities = array( |
73
|
|
|
'ItemQuantity' => array($doc->ID => 5), |
74
|
|
|
); |
75
|
|
|
$this->controller->updateCartItems($updatedQuantities); |
76
|
|
|
$this->assertEquals(6, $this->cart->getItem($item->getItemId())->getQuantity()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests whether the recipient details are updated from the controller |
81
|
|
|
*/ |
82
|
|
|
public function testUpdateCartReceiverInfo() |
83
|
|
|
{ |
84
|
|
|
$newInfo = array( |
85
|
|
|
'ReceiverName' => 'Joe Soap', |
86
|
|
|
'ReceiverPhone' => '111', |
87
|
|
|
'ReceiverEmail' => '[email protected]', |
88
|
|
|
'DeliveryAddressLine1' => 'A1', |
89
|
|
|
'DeliveryAddressLine2' => 'A2', |
90
|
|
|
'DeliveryAddressCountry' => 'NZ', |
91
|
|
|
'DeliveryAddressPostCode' => '6011', |
92
|
|
|
); |
93
|
|
|
$this->controller->updateCartReceiverInfo($newInfo); |
94
|
|
|
$this->assertEquals($newInfo, $this->cart->getReceiverInfo()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests whether emails are sent. Emails are mocked so not actually sent. |
99
|
|
|
*/ |
100
|
|
|
public function testSend() |
101
|
|
|
{ |
102
|
|
|
// Set admin email |
103
|
|
|
Config::inst()->update('Email', 'admin_email', 'admin'); |
104
|
|
|
$data = array( |
105
|
|
|
'ReceiverName' => 'Joe Soap', |
106
|
|
|
'ReceiverPhone' => '111', |
107
|
|
|
'ReceiverEmail' => '[email protected]', |
108
|
|
|
'DeliveryAddressLine1' => 'A1', |
109
|
|
|
'DeliveryAddressLine2' => 'A2', |
110
|
|
|
'DeliveryAddressCountry' => 'NZ', |
111
|
|
|
'DeliveryAddressPostCode' => '6011' |
112
|
|
|
); |
113
|
|
|
$this->cart->setReceiverInfo($data); |
114
|
|
|
$result = $this->controller->send(); |
115
|
|
|
$this->assertTrue(is_array($result)); |
116
|
|
|
$this->assertEquals('[email protected]', $result['to']); |
117
|
|
|
$this->assertEquals('admin', $result['from']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Tests whether email sending is extensible. |
122
|
|
|
*/ |
123
|
|
|
public function testSendIsExtensible() |
124
|
|
|
{ |
125
|
|
|
$result = $this->controller->send(); |
126
|
|
|
$this->assertEquals('Subject is changed', $result['subject']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Test to see whether the cart is empty after a request is sent. |
131
|
|
|
*/ |
132
|
|
|
public function testDoRequestSend() |
133
|
|
|
{ |
134
|
|
|
// Form for use later |
135
|
|
|
$form = $this->controller->DMSDocumentRequestForm(); |
136
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
137
|
|
|
// Add some an item to the cart to assert later that its empty |
138
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(15); |
|
|
|
|
139
|
|
|
$this->controller->getCart()->addItem($item); |
140
|
|
|
$data = array( |
141
|
|
|
'ReceiverName' => 'Joe Soap', |
142
|
|
|
'ReceiverPhone' => '111', |
143
|
|
|
'ReceiverEmail' => '[email protected]', |
144
|
|
|
'DeliveryAddressLine1' => 'A1', |
145
|
|
|
'DeliveryAddressLine2' => 'A2', |
146
|
|
|
'DeliveryAddressCountry' => 'NZ', |
147
|
|
|
'DeliveryAddressPostCode' => '6011', |
148
|
|
|
'ItemQuantity' => array($doc->ID => 5), |
149
|
|
|
); |
150
|
|
|
$request = new SS_HTTPRequest('POST', 'mock/url'); |
151
|
|
|
// Assert cart is empty |
152
|
|
|
$this->assertFalse($this->controller->getCart()->isCartEmpty()); |
|
|
|
|
153
|
|
|
$result = $this->controller->doRequestSend($data, $form, $request); |
154
|
|
|
$this->assertInstanceOf('SS_HTTPResponse', $result); |
|
|
|
|
155
|
|
|
$this->assertTrue($this->controller->getCart()->isCartEmpty()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Test the checkout success page shows a pretty message |
160
|
|
|
*/ |
161
|
|
|
public function testCompletePage() |
162
|
|
|
{ |
163
|
|
|
$result = (string) $this->get('checkout/complete')->getBody(); |
164
|
|
|
$this->assertContains('Thanks!', $result); |
165
|
|
|
$this->assertContains('You will receive a confirmation email', $result); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Ensure the link is "friendly", not a class name |
170
|
|
|
*/ |
171
|
|
|
public function testLink() |
172
|
|
|
{ |
173
|
|
|
$this->assertSame('checkout', $this->controller->Link()); |
174
|
|
|
$this->assertSame('checkout/complete', $this->controller->Link('complete')); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Test that the items in my cart are listed on the checkout page, and that some form fields exist |
179
|
|
|
*/ |
180
|
|
|
public function testIndexCheckoutForm() |
181
|
|
|
{ |
182
|
|
|
$backend = DMSSessionBackend::singleton(); |
183
|
|
|
$document = $this->objFromFixture('DMSDocument', 'limited_supply'); |
184
|
|
|
$requestItem = DMSRequestItem::create($document); |
185
|
|
|
$backend->addItem($requestItem); |
186
|
|
|
|
187
|
|
|
$response = $this->get('checkout'); |
188
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
189
|
|
|
|
190
|
|
|
$body = (string) $response->getBody(); |
191
|
|
|
$this->assertContains('Checkout', $body); |
192
|
|
|
$this->assertContains('Your request in summary', $body); |
193
|
|
|
$this->assertContains('Doc3', $body); |
194
|
|
|
$this->assertContains('Receiver Name', $body); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
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.