1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ilateral\SilverStripe\Notifier\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\TestMailer; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Control\Email\Mailer; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use ilateral\SilverStripe\Notifier\Model\Notification; |
11
|
|
|
use ilateral\SilverStripe\Notifier\DataObjectExtension; |
12
|
|
|
use ilateral\SilverStripe\Notifier\Model\NotificationRule; |
13
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestRule; |
14
|
|
|
use ilateral\SilverStripe\Notifier\Types\NotificationType; |
15
|
|
|
use ilateral\SilverStripe\Notifier\Types\EmailNotification; |
16
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestCreateObject; |
17
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestDeleteObject; |
18
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestUpdateObject; |
19
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestSubjectObject; |
20
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestChangeNameObject; |
21
|
|
|
use ilateral\SilverStripe\Notifier\Tests\Objects\TestStatusPaidObject; |
22
|
|
|
|
23
|
|
|
class NotificationsTest extends SapphireTest |
24
|
|
|
{ |
25
|
|
|
protected static $fixture_file = 'NotificationsTests.yml'; |
26
|
|
|
|
27
|
|
|
protected $usesDatabase = true; |
28
|
|
|
|
29
|
|
|
protected static $extra_dataobjects = [ |
30
|
|
|
TestCreateObject::class, |
31
|
|
|
TestUpdateObject::class, |
32
|
|
|
TestDeleteObject::class, |
33
|
|
|
TestChangeNameObject::class, |
34
|
|
|
TestStatusPaidObject::class, |
35
|
|
|
TestSubjectObject::class |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function testSuccessfullyRegistered() |
39
|
|
|
{ |
40
|
|
|
$this->assertContains( |
41
|
|
|
DataObjectExtension::class, |
42
|
|
|
Config::inst()->get(TestCreateObject::class, 'extensions') |
43
|
|
|
); |
44
|
|
|
$this->assertContains( |
45
|
|
|
DataObjectExtension::class, |
46
|
|
|
Config::inst()->get(TestUpdateObject::class, 'extensions') |
47
|
|
|
); |
48
|
|
|
$this->assertContains( |
49
|
|
|
DataObjectExtension::class, |
50
|
|
|
Config::inst()->get(TestDeleteObject::class, 'extensions') |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetAllowedRules() |
55
|
|
|
{ |
56
|
|
|
/** @var Notification */ |
57
|
|
|
$notification = Injector::inst()->get(Notification::class, true); |
58
|
|
|
|
59
|
|
|
$rules = $notification->getAllowedRules(); |
60
|
|
|
|
61
|
|
|
$this->assertContains(NotificationRule::class, $rules); |
62
|
|
|
$this->assertContains(TestRule::class, $rules); |
63
|
|
|
$this->assertTrue($notification->isRuleAllowed(NotificationRule::class)); |
64
|
|
|
$this->assertTrue($notification->isRuleAllowed(TestRule::class)); |
65
|
|
|
|
66
|
|
|
Config::modify()->set( |
67
|
|
|
Notification::class, |
68
|
|
|
'disallow_rules', |
69
|
|
|
[TestRule::class] |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$rules = $notification->getAllowedRules(); |
73
|
|
|
|
74
|
|
|
$this->assertContains(NotificationRule::class, $rules); |
75
|
|
|
$this->assertNotContains(TestRule::class, $rules); |
76
|
|
|
$this->assertTrue($notification->isRuleAllowed(NotificationRule::class)); |
77
|
|
|
$this->assertFalse($notification->isRuleAllowed(TestRule::class)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetAllowedTypes() |
81
|
|
|
{ |
82
|
|
|
/** @var Notification */ |
83
|
|
|
$notification = Injector::inst()->get(Notification::class, true); |
84
|
|
|
|
85
|
|
|
$types = $notification->getAllowedTypes(); |
86
|
|
|
|
87
|
|
|
$this->assertNotContains(NotificationType::class, $types); |
88
|
|
|
$this->assertContains(EmailNotification::class, $types); |
89
|
|
|
$this->assertFalse($notification->isTypeAllowed(NotificationType::class)); |
90
|
|
|
$this->assertTrue($notification->isTypeAllowed(EmailNotification::class)); |
91
|
|
|
|
92
|
|
|
Config::modify()->set( |
93
|
|
|
Notification::class, |
94
|
|
|
'disallow_types', |
95
|
|
|
[EmailNotification::class] |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$types = $notification->getAllowedTypes(); |
99
|
|
|
|
100
|
|
|
$this->assertContains(NotificationType::class, $types); |
101
|
|
|
$this->assertNotContains(EmailNotification::class, $types); |
102
|
|
|
$this->assertTrue($notification->isTypeAllowed(NotificationType::class)); |
103
|
|
|
$this->assertFalse($notification->isTypeAllowed(EmailNotification::class)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testCreatedEmails() |
107
|
|
|
{ |
108
|
|
|
TestCreateObject::create()->write(); |
109
|
|
|
|
110
|
|
|
$this->assertEmailSent( |
111
|
|
|
'[email protected]', |
112
|
|
|
'[email protected]', |
113
|
|
|
'Created Subject' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testUpdatedEmails() |
118
|
|
|
{ |
119
|
|
|
/** @var TestMailer */ |
120
|
|
|
$mailer = Injector::inst()->get(Mailer::class); |
121
|
|
|
$object = TestUpdateObject::create(); |
122
|
|
|
$object->write(); |
123
|
|
|
|
124
|
|
|
// ensure no test emails were sent on creation |
125
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
126
|
|
|
|
127
|
|
|
$object->Name = "Test name"; |
128
|
|
|
$object->write(); |
129
|
|
|
|
130
|
|
|
// Ensure update email was sent |
131
|
|
|
$this->assertEmailSent( |
132
|
|
|
'[email protected]', |
133
|
|
|
'[email protected]', |
134
|
|
|
'Updated Subject' |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testChangeFieldEmails() |
139
|
|
|
{ |
140
|
|
|
/** @var TestMailer */ |
141
|
|
|
$mailer = Injector::inst()->get(Mailer::class); |
142
|
|
|
$object = TestChangeNameObject::create(); |
143
|
|
|
$object->write(); |
144
|
|
|
|
145
|
|
|
// ensure no test emails were sent on creation |
146
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
147
|
|
|
|
148
|
|
|
$object->Name = "Test name"; |
149
|
|
|
$object->write(); |
150
|
|
|
|
151
|
|
|
// Ensure update email was sent |
152
|
|
|
$this->assertEmailSent( |
153
|
|
|
'[email protected]', |
154
|
|
|
'[email protected]', |
155
|
|
|
'Updated Name' |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$object->Name = "Different name"; |
159
|
|
|
$object->write(); |
160
|
|
|
|
161
|
|
|
// Ensure update email was sent |
162
|
|
|
$this->assertEmailSent( |
163
|
|
|
'[email protected]', |
164
|
|
|
'[email protected]', |
165
|
|
|
'Updated Name' |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$mailer->clearEmails(); |
169
|
|
|
$object->Status = "paid"; |
170
|
|
|
$object->write(); |
171
|
|
|
|
172
|
|
|
// ensure no test emails were sent on change of unchecked field |
173
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testChangeFieldValueEmails() |
177
|
|
|
{ |
178
|
|
|
/** @var TestMailer */ |
179
|
|
|
$mailer = Injector::inst()->get(Mailer::class); |
180
|
|
|
$object = TestStatusPaidObject::create(); |
181
|
|
|
$object->write(); |
182
|
|
|
|
183
|
|
|
// ensure no test emails were sent on creation |
184
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
185
|
|
|
|
186
|
|
|
$object->Name = "Test name"; |
187
|
|
|
$object->write(); |
188
|
|
|
|
189
|
|
|
// ensure no emails were sent on invalid field change |
190
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
191
|
|
|
|
192
|
|
|
$object->Status = "paid"; |
193
|
|
|
$object->write(); |
194
|
|
|
|
195
|
|
|
// Ensure update email was sent |
196
|
|
|
$this->assertEmailSent( |
197
|
|
|
'[email protected]', |
198
|
|
|
'[email protected]', |
199
|
|
|
'Updated To Status Paid' |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$mailer->clearEmails(); |
203
|
|
|
$object->Status = "cancelled"; |
204
|
|
|
$object->write(); |
205
|
|
|
|
206
|
|
|
// ensure no emails were sent on invalid status change |
207
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testDeletedEmails() |
211
|
|
|
{ |
212
|
|
|
/** @var TestMailer */ |
213
|
|
|
$mailer = Injector::inst()->get(Mailer::class); |
214
|
|
|
$object = TestDeleteObject::create(); |
215
|
|
|
$object->write(); |
216
|
|
|
|
217
|
|
|
// ensure no test emails were sent on creation |
218
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
219
|
|
|
|
220
|
|
|
$object->Name = "Test name"; |
221
|
|
|
$object->write(); |
222
|
|
|
|
223
|
|
|
// ensure no test emails were sent on update |
224
|
|
|
$this->assertNull($mailer->findEmail('[email protected]')); |
225
|
|
|
|
226
|
|
|
$object->delete(); |
227
|
|
|
|
228
|
|
|
// Ensure update email was sent |
229
|
|
|
$this->assertEmailSent( |
230
|
|
|
'[email protected]', |
231
|
|
|
'[email protected]', |
232
|
|
|
'Deleted Subject' |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function testSubjectRendered() |
237
|
|
|
{ |
238
|
|
|
$object = TestSubjectObject::create(); |
239
|
|
|
$object->write(); |
240
|
|
|
|
241
|
|
|
$object->Name = "Test name"; |
242
|
|
|
$object->write(); |
243
|
|
|
|
244
|
|
|
// Ensure update email was sent |
245
|
|
|
$this->assertEmailSent( |
246
|
|
|
'[email protected]', |
247
|
|
|
'[email protected]', |
248
|
|
|
'Field Changed To Test name' |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
} |