1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DMSDocumentCartCheckoutPageTest extends FunctionalTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
protected static $fixture_file = 'DMSDocumentCartTest.yml'; |
6
|
|
|
/** |
7
|
|
|
* @var DMSDocumentCartCheckoutPage_Controller |
8
|
|
|
*/ |
9
|
|
|
protected $controller; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var DMSDocumentCart |
13
|
|
|
*/ |
14
|
|
|
protected $cart; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
$this->controller = DMSDocumentCartCheckoutPage_Controller::create(); |
20
|
|
|
$this->cart = $this->controller->getCart(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/* |
24
|
|
|
* Tests DMSDocumentRequest form has a FieldList |
25
|
|
|
*/ |
26
|
|
|
public function testDMSDocumentRequestForm() |
27
|
|
|
{ |
28
|
|
|
$form = $this->controller->DMSDocumentRequestForm(); |
29
|
|
|
$this->assertInstanceOf('FieldList', $form->Fields()); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Tests if the DMSDocumentRequestForm is extensible |
34
|
|
|
*/ |
35
|
|
|
public function testDMSDocumentRequestFormIsExtensible() |
36
|
|
|
{ |
37
|
|
|
DMSDocumentCartCheckoutPage_Controller::add_extension('StubDMSDocumentCheckoutPageExtension'); |
38
|
|
|
|
39
|
|
|
$controller = DMSDocumentCartCheckoutPage_Controller::create(); |
40
|
|
|
$form = $controller->DMSDocumentRequestForm(); |
41
|
|
|
$this->assertNotNull( |
|
|
|
|
42
|
|
|
$form->Fields()->fieldByName('NewTextField'), |
43
|
|
|
'DMSDocumentRequestForm() is extensible as it included the field from the extension' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Tests if print requests are incremented |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function testTrackTimestampedPrintRequest() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
53
|
|
|
/** @var DMSRequestItem $item */ |
54
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2); |
|
|
|
|
55
|
|
|
$this->cart->addItem($item); |
56
|
|
|
|
57
|
|
|
$this->controller->trackTimestampedPrintRequest(); |
58
|
|
|
$this->assertEquals(1, $item->getDocument()->PrintRequestCount); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Tests if a Cart is received |
63
|
|
|
*/ |
64
|
|
|
public function testGetCart() |
65
|
|
|
{ |
66
|
|
|
$this->assertInstanceOf('DMSDocumentCart', $this->controller->getCart()); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tests whether the cart items are updated from the controller |
71
|
|
|
*/ |
72
|
|
|
public function testUpdateCartItems() |
73
|
|
|
{ |
74
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
75
|
|
|
/** @var DMSRequestItem $item */ |
76
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2); |
|
|
|
|
77
|
|
|
$this->cart->addItem($item); |
78
|
|
|
$updatedQuantities = array( |
79
|
|
|
'ItemQuantity' => array($doc->ID => 5), |
80
|
|
|
); |
81
|
|
|
$this->controller->updateCartItems($updatedQuantities); |
82
|
|
|
$this->assertEquals(6, $this->cart->getItem($item->getItemId())->getQuantity()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Tests whether the recipient details are updated from the controller |
87
|
|
|
*/ |
88
|
|
|
public function testUpdateCartReceiverInfo() |
89
|
|
|
{ |
90
|
|
|
$newInfo = array( |
91
|
|
|
'ReceiverName' => 'Joe Soap', |
92
|
|
|
'ReceiverPhone' => '111', |
93
|
|
|
'ReceiverEmail' => '[email protected]', |
94
|
|
|
'DeliveryAddressLine1' => 'A1', |
95
|
|
|
'DeliveryAddressLine2' => 'A2', |
96
|
|
|
'DeliveryAddressCountry' => 'NZ', |
97
|
|
|
'DeliveryAddressPostCode' => '6011', |
98
|
|
|
); |
99
|
|
|
$this->controller->updateCartReceiverInfo($newInfo); |
100
|
|
|
$this->assertEquals($newInfo, $this->cart->getReceiverInfo()); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Tests whether emails are sent. Emails are mocked so not actually sent. |
105
|
|
|
*/ |
106
|
|
|
public function testSend() |
107
|
|
|
{ |
108
|
|
|
// Set admin email |
109
|
|
|
Config::inst()->update('Email', 'admin_email', 'admin'); |
110
|
|
|
// Temporarily replace default email service |
111
|
|
|
Injector::inst()->registerService(new StubEmail(), 'Email'); |
|
|
|
|
112
|
|
|
$page = $this->objFromFixture('DMSDocumentCartCheckoutPage', 'page1'); |
113
|
|
|
/** @var DMSDocumentCartCheckoutPage_Controller $controller */ |
114
|
|
|
$controller = ModelAsController::controller_for($page); |
|
|
|
|
115
|
|
|
$data = array( |
116
|
|
|
'ReceiverName' => 'Joe Soap', |
117
|
|
|
'ReceiverPhone' => '111', |
118
|
|
|
'ReceiverEmail' => '[email protected]', |
119
|
|
|
'DeliveryAddressLine1' => 'A1', |
120
|
|
|
'DeliveryAddressLine2' => 'A2', |
121
|
|
|
'DeliveryAddressCountry' => 'NZ', |
122
|
|
|
'DeliveryAddressPostCode' => '6011' |
123
|
|
|
); |
124
|
|
|
$this->cart->setReceiverInfo($data); |
125
|
|
|
$result = $controller->send(); |
126
|
|
|
$this->assertTrue(is_array($result)); |
|
|
|
|
127
|
|
|
$this->assertEquals('[email protected]', $result['to']); |
|
|
|
|
128
|
|
|
$this->assertEquals('admin', $result['from']); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Tests whether email sending is extensible. |
133
|
|
|
*/ |
134
|
|
|
public function testSendIsExtensible() |
135
|
|
|
{ |
136
|
|
|
DMSDocumentCartCheckoutPage_Controller::add_extension('StubDMSDocumentCheckoutPageExtension'); |
137
|
|
|
Injector::inst()->registerService(new StubEmail(), 'Email'); |
|
|
|
|
138
|
|
|
$page = $this->objFromFixture('DMSDocumentCartCheckoutPage', 'page1'); |
139
|
|
|
/** @var DMSDocumentCartCheckoutPage_Controller $controller */ |
140
|
|
|
$controller = ModelAsController::controller_for($page); |
|
|
|
|
141
|
|
|
$result = $controller->send(); |
142
|
|
|
$this->assertEquals('Subject is changed', $result['subject']); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Test to see whether the cart is empty after a request is sent. |
147
|
|
|
*/ |
148
|
|
|
public function testDoRequestSend() |
149
|
|
|
{ |
150
|
|
|
Injector::inst()->registerService(new StubEmail(), 'Email'); |
|
|
|
|
151
|
|
|
$page = $this->objFromFixture('DMSDocumentCartCheckoutPage', 'page1'); |
152
|
|
|
/** @var DMSDocumentCartCheckoutPage_Controller $controller */ |
153
|
|
|
$controller = ModelAsController::controller_for($page); |
|
|
|
|
154
|
|
|
// Form for use later |
155
|
|
|
$form = $controller->DMSDocumentRequestForm(); |
156
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
157
|
|
|
// Add some an item to the cart to assert later that its empty |
158
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(15); |
|
|
|
|
159
|
|
|
$controller->getCart()->addItem($item); |
160
|
|
|
$data = array( |
161
|
|
|
'ReceiverName' => 'Joe Soap', |
162
|
|
|
'ReceiverPhone' => '111', |
163
|
|
|
'ReceiverEmail' => '[email protected]', |
164
|
|
|
'DeliveryAddressLine1' => 'A1', |
165
|
|
|
'DeliveryAddressLine2' => 'A2', |
166
|
|
|
'DeliveryAddressCountry' => 'NZ', |
167
|
|
|
'DeliveryAddressPostCode' => '6011', |
168
|
|
|
'ItemQuantity' => array($doc->ID => 5), |
169
|
|
|
); |
170
|
|
|
$request = new SS_HTTPRequest('POST', 'mock/url'); |
171
|
|
|
// Assert cart is empty |
172
|
|
|
$this->assertFalse($controller->getCart()->isCartEmpty()); |
|
|
|
|
173
|
|
|
$result = $controller->doRequestSend($data, $form, $request); |
174
|
|
|
$this->assertInstanceOf('SS_HTTPResponse', $result); |
|
|
|
|
175
|
|
|
$this->assertTrue($controller->getCart()->isCartEmpty()); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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.