Completed
Push — master ( 453ef0...e0d9af )
by Will
27s queued 11s
created

OrderStatusLogTest::testOrderIsRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 18
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace SilverShop\Tests\Model;
4
5
use SilverShop\Checkout\OrderProcessor;
6
use SilverShop\Model\Order;
7
use SilverShop\Model\OrderStatusLog;
8
use SilverShop\Tests\ShopTest;
9
use SilverStripe\Control\Email\Mailer;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\TestMailer;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Dev\SapphireTest;
15
16
/**
17
 * @link OrderStatusLog
18
 * @package shop_statuschangeemail
19
 * @subpackage tests
20
 */
21
class OrderStatusLogTest extends SapphireTest
22
{
23
    protected static $fixture_file = array(
24
        __DIR__ . '/../Fixtures/Orders.yml',
25
        __DIR__ . '/../Fixtures/ShopMembers.yml',
26
        __DIR__ . '/../Fixtures/Pages.yml'
27
    );
28
29
    public function setUp()
30
    {
31
        parent::setUp();
32
        ShopTest::setConfiguration();
33
        Config::modify()->set(Order::class, 'log_status', array('Processing', 'Sent', 'AdminCancelled', 'MemberCancelled'));
34
    }
35
36
    public function testOrderStatusLogItemsWithMember()
37
    {
38
        // start a new order
39
        $order = $this->objFromFixture(Order::class, "cart1");
40
        $member = $this->objFromFixture(Member::class, 'jeremyperemy');
41
        $order->MemberID = $member->ID;
42
43
        $no_log_generated_with_order_status_cart = OrderStatusLog::get()->sort('ID')->last();
44
        $this->assertNull(
45
            $no_log_generated_with_order_status_cart,
46
            "no log generated with Status of 'Cart'"
47
        );
48
49
        $order->Status = "Unpaid";
50
        $order->write();
51
52
        $no_log_generated_with_order_status_unpaid = OrderStatusLog::get()->sort('ID')->last();
53
        $this->assertNull(
54
            $no_log_generated_with_order_status_unpaid,
55
            "no log generated with Status of 'Unpaid'"
56
        );
57
58
        $processor = OrderProcessor::create($order);
59
        $response = $processor->makePayment("Manual", array());
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
60
        $order->Status = "Paid";
61
        $order->write();
62
63
        $log_order_status_paid = OrderStatusLog::get()->sort('ID')->last();
64
        $this->assertNull(
65
            $log_order_status_paid,
66
            "no log generated with Status of 'Unpaid'"
67
        );
68
69
        $order->Status = "Processing";
70
        $order->write();
71
72
        $log_order_status_processing = OrderStatusLog::get()->sort('ID')->last();
73
        $this->assertEquals(OrderStatusLog::get()->count(), '1', "One items in the OrderStatusLog");
74
        $this->assertNotNull(
75
            $log_order_status_processing,
76
            "a log when changing to 'Processing' status (and PaymentMethod is 'Manual')"
77
        );
78
        $this->assertSame(
79
            $log_order_status_processing->Order()->ID,
80
            $order->ID,
81
            "Log conatins an Order"
82
        );
83
84
        $this->assertContains(
85
            "Processing",
86
            $log_order_status_processing->Note,
87
            "Processing note is recorded"
88
        );
89
        $this->assertContains(
90
            'changed to "Processing"',
91
            $log_order_status_processing->Title,
92
            'Processing title is recorded'
93
        );
94
        $this->assertEmailSent(
95
            '[email protected]',
96
            '[email protected]',
97
            'Silvershop - ' . $log_order_status_processing->Title
98
        );
99
        $order->Status = "Sent";
100
        $order->write();
101
102
        $log_order_status_sent = OrderStatusLog::get()->sort('ID')->last();
103
        $this->assertEquals(
104
            OrderStatusLog::get()->count(),
105
            '2',
106
            "Three items in the OrderStatusLog"
107
        );
108
        $this->assertNotNull(
109
            $log_order_status_sent,
110
            "an log should be recorded when an order's status is changed to 'Sent' (and PaymentMethod is 'Manual')"
111
        );
112
        $this->assertSame(
113
            $log_order_status_sent->Order()->ID,
114
            $order->ID,
115
            "Log conatins an Order"
116
        );
117
        $this->assertContains(
118
            "sent",
119
            $log_order_status_sent->Note,
120
            "Sent note is recorded"
121
        );
122
        $this->assertContains(
123
            'changed to "Sent"',
124
            $log_order_status_sent->Title,
125
            "Sent title is recorded"
126
        );
127
128
        $this->assertEmailSent(
129
            "[email protected]",
130
            "[email protected]",
131
            'Silvershop - ' . $log_order_status_sent->Title
132
        );
133
134
        $order->Status = "Complete";
135
        $order->write();
136
        $this->assertEquals(
137
            OrderStatusLog::get()->count(),
138
            '2',
139
            "Additional item in the OrderStatusLog has not been created"
140
        );
141
142
        $order->Status = "AdminCancelled";
143
        $order->write();
144
145
        $log_order_status_admin_cancelled = OrderStatusLog::get()->sort('ID')->last();
146
        $this->assertEquals(
147
            OrderStatusLog::get()->count(),
148
            '3',
149
            "Three items in the OrderStatusLog"
150
        );
151
        $this->assertNotNull(
152
            $log_order_status_admin_cancelled,
153
            "a log should be recorded with change to 'Admin Cancelled' status (and PaymentMethod is 'Manual')"
154
        );
155
        $this->assertSame(
156
            $log_order_status_admin_cancelled->Order()->ID,
157
            $order->ID,
158
            "Log conatins an Order"
159
        );
160
        $this->assertContains(
161
            "cancelled",
162
            $log_order_status_admin_cancelled->Note,
163
            "Admin Cancelled note is recorded"
164
        );
165
        $this->assertContains(
166
            'changed to "Cancelled by admin"',
167
            $log_order_status_admin_cancelled->Title,
168
            "Admin Cancelled title is recorded"
169
        );
170
        $this->assertEmailSent(
171
            "[email protected]",
172
            "[email protected]",
173
            'Silvershop - ' . $log_order_status_admin_cancelled->Title
174
        );
175
176
        $order->Status = "MemberCancelled";
177
        $order->write();
178
        $log_order_status_member_cancelled = OrderStatusLog::get()->sort('ID')->last();
179
        $this->assertEquals(
180
            OrderStatusLog::get()->count(),
181
            '4',
182
            "Four items in the OrderStatusLog"
183
        );
184
        $this->assertNotNull(
185
            $log_order_status_member_cancelled,
186
            "a log should be recorded for change to 'Member Cancelled' status (and PaymentMethod is 'Manual')"
187
        );
188
        $this->assertSame(
189
            $log_order_status_member_cancelled->Order()->ID,
190
            $order->ID,
191
            "Log conatins an Order"
192
        );
193
        $this->assertSame(
194
            "Your cancellation of the order has been noted.  Please contact us if you have any questions.",
195
            $log_order_status_member_cancelled->Note,
196
            "Member Cancelled note is recorded"
197
        );
198
        $this->assertContains(
199
            ' changed to "Cancelled by member"',
200
            $log_order_status_member_cancelled->Title,
201
            "Member Cancelled title is recorded"
202
        );
203
        $this->assertEmailSent(
204
            '[email protected]',
205
            '[email protected]',
206
            'Silvershop - ' . $log_order_status_member_cancelled->Title
207
        );
208
    }
209
210
    public function testEmailSentOnce()
211
    {
212
        $order = $this->objFromFixture(Order::class, "cart1");
213
        $member = $this->objFromFixture(Member::class, 'jeremyperemy');
214
        $order->MemberID = $member->ID;
215
216
        $order->Status = 'Processing';
217
        $order->write();
218
219
        $logEntry = OrderStatusLog::get()->sort('ID')->last();
220
221
        $this->assertEquals(
222
            OrderStatusLog::get()->count(),
223
            1,
224
            "An item has been added to the status-log"
225
        );
226
227
        $this->assertEmailSent(
228
            '[email protected]',
229
            '[email protected]',
230
            'Silvershop - ' . $logEntry->Title
231
        );
232
233
        // clear sent emails
234
        /**
235
         * @var TestMailer $mailer
236
         */
237
        $mailer = Injector::inst()->get(Mailer::class);
238
        $mailer->clearEmails();
239
240
        // force another write on the order
241
        $order->Notes = 'Random Test Notes';
242
        $order->write();
243
244
        // Status hasn't changed, so there should be just one log entry still
245
        $this->assertEquals(
246
            OrderStatusLog::get()->count(),
247
            1,
248
            "An item has been added to the status-log"
249
        );
250
251
        $this->assertFalse(
252
            (bool)$this->findEmail('[email protected]', '[email protected]'),
253
            'No additional email should be sent'
254
        );
255
256
        // Try re-writing the Log entry
257
        $logEntry->Note = 'Some random notes';
258
        $logEntry->write();
259
260
        $this->assertFalse(
261
            (bool)$this->findEmail('[email protected]', '[email protected]'),
262
            'No additional email should be sent'
263
        );
264
    }
265
266
    public function testOrderPlacedByGuest()
267
    {
268
        // start a new order
269
        $order = $this->objFromFixture(Order::class, "cart1");
270
        $order->FirstName = "Edmund";
271
        $order->Surname = "Hillary";
272
        $order->Email = "[email protected]";
273
        $order->Status = "Unpaid";
274
        $order->write();
275
276
        $no_log_generated_with_order_status_unpaid = OrderStatusLog::get()->sort('ID')->last();
277
        $this->assertNull(
278
            $no_log_generated_with_order_status_unpaid,
279
            "no log generated with Status of 'Unpaid'"
280
        );
281
282
        $processor_guest = OrderProcessor::create($order);
283
        $response = $processor_guest->makePayment("Manual", array());
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
284
        $order->Status = "Paid";
285
        $order->write();
286
287
        $log_order_status_paid = OrderStatusLog::get()->sort('ID')->last();
288
        $this->assertNull(
289
            $log_order_status_paid,
290
            "no log generated with Status of 'Unpaid'"
291
        );
292
293
        $order->Status = "Processing";
294
        $order->write();
295
296
        $log_order_status_processing = OrderStatusLog::get()->sort('ID')->last();
297
        $this->assertEquals(OrderStatusLog::get()->count(), '1', "One items in the OrderStatusLog");
298
        $this->assertNotNull(
299
            $log_order_status_processing,
300
            "a log when changing to 'Processing' status (and PaymentMethod is 'Manual')"
301
        );
302
        $this->assertSame(
303
            $log_order_status_processing->Order()->ID,
304
            $order->ID,
305
            "Log conatins an Order"
306
        );
307
308
        $this->assertContains(
309
            "Processing",
310
            $log_order_status_processing->Note,
311
            "Processing note is recorded"
312
        );
313
        $this->assertContains(
314
            ' changed to "Processing"',
315
            $log_order_status_processing->Title,
316
            "Processing title is recorded"
317
        );
318
        $this->assertEmailSent(
319
            "[email protected]",
320
            "[email protected]",
321
            'Silvershop - ' . $log_order_status_processing->Title
322
        );
323
    }
324
325
    public function testOrderIsRequired()
326
    {
327
        $log = new OrderStatusLog([
328
            'Title' => 'Test',
329
            'OrderID' => 1
330
        ]);
331
        $log->write();
332
        $this->assertTrue($log->exists());
333
334
        // Now we make sure we don't need to set an OrderID
335
        Config::modify()->set(OrderStatusLog::class, 'order_is_required', false);
336
337
        $log = new OrderStatusLog([
338
            'Title' => 'Test'
339
        ]);
340
341
        $log->write();
342
        $this->assertTrue($log->exists());
343
    }
344
}
345