1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* simple class to see that everything is working ... |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class CampaignMonitorAPIConnector_TestController extends Controller |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
private static $url_segment = "create-send-test"; |
|
|
|
|
13
|
|
|
|
14
|
|
|
private static $allowed_actions = array( |
|
|
|
|
15
|
|
|
"testall" => "CMS_ACCESS_CMSMain", |
16
|
|
|
"testlists" => "CMS_ACCESS_CMSMain", |
17
|
|
|
"testcampaigns" => "CMS_ACCESS_CMSMain", |
18
|
|
|
"testsubscribers" => "CMS_ACCESS_CMSMain" |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* example data |
23
|
|
|
* @var Array |
24
|
|
|
*/ |
25
|
|
|
protected $egData = array( |
26
|
|
|
"limit" => 10, |
27
|
|
|
"listID" => "", |
28
|
|
|
"listIDtoDelete" => "", |
29
|
|
|
"campaignID" => "", |
30
|
|
|
"templateID" => "", |
31
|
|
|
"listTitle" => "Test List 9", |
32
|
|
|
"unsubscribePage" => "http://unsub", |
33
|
|
|
"confirmedOptIn" => false, |
34
|
|
|
"confirmationSuccessPage" => "http://confirmed", |
35
|
|
|
"unsubscribeSetting" => null, |
36
|
|
|
"addUnsubscribesToSuppList" => true, |
37
|
|
|
"scrubActiveWithSuppList" => true, |
38
|
|
|
"oldEmailAddress" => "[email protected]", |
39
|
|
|
"newEmailAddress" => "[email protected]" |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* contains API once started |
44
|
|
|
* @var CampaignMonitorAPIConnector |
45
|
|
|
*/ |
46
|
|
|
protected $api = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* should we show as much as possible? |
50
|
|
|
* @var Boolean |
51
|
|
|
*/ |
52
|
|
|
protected $showAll = false; |
53
|
|
|
|
54
|
|
|
public function init() |
55
|
|
|
{ |
56
|
|
|
parent::init(); |
57
|
|
|
if (!Config::inst()->get("CampaignMonitorAPIConnector", "client_id")) { |
58
|
|
|
user_error("To use the campaign monitor module you must set the basic authentication credentials such as CampaignMonitorAPIConnector.client_id"); |
59
|
|
|
} |
60
|
|
|
$this->egData["listTitle"] = $this->egData["listTitle"].rand(0, 999999999999); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* link for controller |
65
|
|
|
* we add baseURL to make it work for all set ups. |
66
|
|
|
* @var String |
67
|
|
|
*/ |
68
|
|
|
public function Link($action = null) |
69
|
|
|
{ |
70
|
|
|
$link = Director::baseURL().$this->Config()->get("url_segment")."/"; |
71
|
|
|
if ($action) { |
72
|
|
|
$link .= $action . "/"; |
73
|
|
|
} |
74
|
|
|
return $link; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* run all tests |
79
|
|
|
*/ |
80
|
|
|
public function testall() |
81
|
|
|
{ |
82
|
|
|
$this->testlists(); |
83
|
|
|
$this->testcampaigns(); |
84
|
|
|
$this->testsubscribers(); |
85
|
|
|
$this->index(); |
86
|
|
|
die("<h1>THE END</h1>"); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function index() |
90
|
|
|
{ |
91
|
|
|
echo " |
92
|
|
|
<hr /><hr /><hr /><hr /><hr /> |
93
|
|
|
<ul> |
94
|
|
|
<li><a href=\"".$this->Link("testlists")."\">test lists</a></li> |
95
|
|
|
<li><a href=\"".$this->Link("testcampaigns")."\">test campaigns</a></li> |
96
|
|
|
<li><a href=\"".$this->Link("testsubscribers")."\">test subscribers</a></li> |
97
|
|
|
<li><a href=\"".$this->Link("testall")."\">test all</a></li> |
98
|
|
|
</ul> |
99
|
|
|
<hr /><hr /><hr /><hr /><hr /> |
100
|
|
|
"; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testlists() |
104
|
|
|
{ |
105
|
|
|
$this->setupTests(); |
106
|
|
|
|
107
|
|
|
//create list |
108
|
|
|
$result = $this->api->createList( |
109
|
|
|
$this->egData["listTitle"], |
110
|
|
|
$this->egData["unsubscribePage"], |
111
|
|
|
$this->egData["confirmedOptIn"], |
112
|
|
|
$this->egData["confirmationSuccessPage"], |
113
|
|
|
$this->egData["unsubscribeSetting"] |
114
|
|
|
); |
115
|
|
|
$this->egData["listIDtoDelete"] = $result; |
116
|
|
|
|
117
|
|
|
//update list |
118
|
|
|
$result = $this->api->updateList( |
|
|
|
|
119
|
|
|
$this->egData["listIDtoDelete"], |
120
|
|
|
$this->egData["listTitle"]."updated_22", |
121
|
|
|
$this->egData["unsubscribePage"]."updated", |
122
|
|
|
$this->egData["confirmedOptIn"], |
123
|
|
|
$this->egData["confirmationSuccessPage"]."updated", |
124
|
|
|
$this->egData["unsubscribeSetting"], |
125
|
|
|
$addUnsubscribesToSuppList = true, |
126
|
|
|
$scrubActiveWithSuppList = true |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
//delete list |
130
|
|
|
if ($this->egData["listIDtoDelete"]) { |
131
|
|
|
$result = $this->api->deleteList($this->egData["listIDtoDelete"]); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//getList |
135
|
|
|
$result = $this->api->getList($this->egData["listID"]); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$result = $this->api->getActiveSubscribers( |
|
|
|
|
138
|
|
|
$this->egData["listID"], |
139
|
|
|
$daysAgo = 3650, |
140
|
|
|
$page = 1, |
141
|
|
|
$pageSize = $this->egData["limit"], |
142
|
|
|
$sortByField = "DATE", |
143
|
|
|
$sortDirection = "DESC" |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$result = $this->api->getUnconfirmedSubscribers( |
|
|
|
|
147
|
|
|
$this->egData["listID"], |
148
|
|
|
$daysAgo = 3650, |
149
|
|
|
$page = 1, |
150
|
|
|
$pageSize = $this->egData["limit"], |
151
|
|
|
$sortByField = "DATE", |
152
|
|
|
$sortDirection = "DESC" |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$result = $this->api->getBouncedSubscribers( |
|
|
|
|
156
|
|
|
$this->egData["listID"], |
157
|
|
|
$daysAgo = 3650, |
158
|
|
|
$page = 1, |
159
|
|
|
$pageSize = $this->egData["limit"], |
160
|
|
|
$sortByField = "DATE", |
161
|
|
|
$sortDirection = "DESC" |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$result = $this->api->getUnsubscribedSubscribers( |
|
|
|
|
165
|
|
|
$this->egData["listID"], |
166
|
|
|
$daysAgo = 3650, |
167
|
|
|
$page = 1, |
168
|
|
|
$pageSize = $this->egData["limit"], |
169
|
|
|
$sortByField = "DATE", |
170
|
|
|
$sortDirection = "DESC" |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$result = $this->api->getSegments($this->egData["listID"]); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$result = $this->api->getListStats($this->egData["listID"]); |
|
|
|
|
176
|
|
|
|
177
|
|
|
$result = $this->api->getListCustomFields($this->egData["listID"]); |
|
|
|
|
178
|
|
|
|
179
|
|
|
echo "<h2>end of list tests</h2>"; |
180
|
|
|
$this->index(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testcampaigns() |
184
|
|
|
{ |
185
|
|
|
$this->setupTests(); |
186
|
|
|
|
187
|
|
|
$this->api->getTemplates(); |
188
|
|
|
flush(); |
189
|
|
|
ob_flush(); |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
//campaign summary |
193
|
|
|
|
194
|
|
|
$result = $this->api->getCampaigns(); |
|
|
|
|
195
|
|
|
|
196
|
|
|
$result = $this->api->getDrafts(); |
|
|
|
|
197
|
|
|
|
198
|
|
|
$result = $this->api->getSummary($this->egData["campaignID"]); |
|
|
|
|
199
|
|
|
|
200
|
|
|
$result = $this->api->getEmailClientUsage($this->egData["campaignID"]); |
|
|
|
|
201
|
|
|
|
202
|
|
|
$result = $this->api->getUnsubscribes( |
|
|
|
|
203
|
|
|
$this->egData["campaignID"], |
204
|
|
|
$daysAgo = 3650, |
205
|
|
|
$page =1, |
206
|
|
|
$pageSize = $this->egData["limit"], |
207
|
|
|
$sortByField = "EMAIL", |
208
|
|
|
$sortDirection = "ASC" |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
echo "<h3>creating a campaign without template</h3>"; |
212
|
|
|
$obj = CampaignMonitorCampaign::create(); |
213
|
|
|
$randNumber = rand(0, 9999999); |
214
|
|
|
$obj->Name = "test only ".$randNumber; |
|
|
|
|
215
|
|
|
$obj->Subject = "test only ".$randNumber; |
|
|
|
|
216
|
|
|
$obj->CreateAsTemplate = false; |
|
|
|
|
217
|
|
|
$obj->CreateFromWebsite = true; |
|
|
|
|
218
|
|
|
$obj->write(); |
219
|
|
|
$result = $this->api->getSummary($obj->CampaignID); |
|
|
|
|
220
|
|
|
echo "<h3>deleting campaign without template</h3>"; |
221
|
|
|
$obj->delete(); |
222
|
|
|
|
223
|
|
|
echo "<h3>creating a campaign with template</h3>"; |
224
|
|
|
$obj = CampaignMonitorCampaign::create(); |
225
|
|
|
$randNumber = rand(0, 9999999); |
226
|
|
|
$obj->Name = "test only ".$randNumber; |
|
|
|
|
227
|
|
|
$obj->Subject = "test only ".$randNumber; |
|
|
|
|
228
|
|
|
$obj->CreateAsTemplate = true; |
|
|
|
|
229
|
|
|
$obj->CreateFromWebsite = true; |
|
|
|
|
230
|
|
|
$obj->write(); |
231
|
|
|
$result = $this->api->getSummary($obj->TemplateID); |
|
|
|
|
232
|
|
|
echo "<h3>deleting campaign with template</h3>"; |
233
|
|
|
$obj->delete(); |
234
|
|
|
|
235
|
|
|
echo "<h2>end of campaign tests</h2>"; |
236
|
|
|
$this->index(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
public function testsubscribers() |
241
|
|
|
{ |
242
|
|
|
$this->setupTests(); |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
//create list |
246
|
|
|
$result = $this->api->createList( |
247
|
|
|
$this->egData["listTitle"], |
248
|
|
|
$this->egData["unsubscribePage"], |
249
|
|
|
$this->egData["confirmedOptIn"], |
250
|
|
|
$this->egData["confirmationSuccessPage"], |
251
|
|
|
$this->egData["unsubscribeSetting"] |
252
|
|
|
); |
253
|
|
|
$this->egData["tempListID"] = $result; |
254
|
|
|
|
255
|
|
|
$customFieldKey = $this->api->createCustomField( |
256
|
|
|
$this->egData["tempListID"], |
|
|
|
|
257
|
|
|
$visible = true, |
258
|
|
|
$type = "multi_select_one", |
259
|
|
|
$title = "are you happy?", |
260
|
|
|
$options = array("YES", "NO") |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
for ($i = 0; $i < 5; $i++) { |
264
|
|
|
$member[$i] = new Member(); |
|
|
|
|
265
|
|
|
$email = "test_".$i."_".$this->egData["oldEmailAddress"]; |
266
|
|
|
$member[$i] = Member::get()->filter(array("Email" => $email))->First(); |
|
|
|
|
267
|
|
|
if (!$member[$i]) { |
268
|
|
|
$member[$i] = new Member(); |
269
|
|
|
$member[$i]->Email = $email; |
270
|
|
|
$member[$i]->FirstName = "First Name $i"; |
271
|
|
|
$member[$i]->Surname = "Surname $i"; |
272
|
|
|
$member[$i]->write(); |
273
|
|
|
} |
274
|
|
|
$result = $this->api->addSubscriber( |
|
|
|
|
275
|
|
|
$this->egData["tempListID"], |
|
|
|
|
276
|
|
|
$member[$i], |
|
|
|
|
277
|
|
|
$customFields = array($customFieldKey => "NO"), |
278
|
|
|
$resubscribe = true, |
279
|
|
|
$restartSubscriptionBasedAutoResponders = false |
280
|
|
|
); |
281
|
|
|
$result = $this->api->updateSubscriber( |
|
|
|
|
282
|
|
|
$this->egData["tempListID"], |
|
|
|
|
283
|
|
|
$email, |
284
|
|
|
$member[$i], |
|
|
|
|
285
|
|
|
$customFields = array($customFieldKey => "YES"), |
286
|
|
|
$resubscribe = true, |
287
|
|
|
$restartSubscriptionBasedAutoResponders = false |
288
|
|
|
); |
289
|
|
|
sleep(1); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
/* |
|
|
|
|
294
|
|
|
$result = $this->api->addSubscribers( |
295
|
|
|
$this->egData["tempListID"], |
296
|
|
|
$membersSet, |
297
|
|
|
$customFields = array(), |
298
|
|
|
$resubscribe, |
299
|
|
|
$queueSubscriptionBasedAutoResponders = false, |
300
|
|
|
$restartSubscriptionBasedAutoResponders = false |
301
|
|
|
); |
302
|
|
|
*/ |
303
|
|
|
|
304
|
|
|
$result = $this->api->deleteSubscriber( |
|
|
|
|
305
|
|
|
$this->egData["tempListID"], |
|
|
|
|
306
|
|
|
$member[2] |
307
|
|
|
); |
308
|
|
|
|
309
|
|
|
$result = $this->api->unsubscribeSubscriber( |
|
|
|
|
310
|
|
|
$this->egData["tempListID"], |
|
|
|
|
311
|
|
|
$member[3] |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
for ($i = 0; $i < 5; $i++) { |
316
|
|
|
$result = $this->api->getSubscriberExistsForThisList( |
|
|
|
|
317
|
|
|
$this->egData["tempListID"], |
|
|
|
|
318
|
|
|
$member[$i] |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
$result = $this->api->getListsForEmail($member[$i]); |
|
|
|
|
322
|
|
|
|
323
|
|
|
$result = $this->api->getSubscriberCanReceiveEmailsForThisList( |
|
|
|
|
324
|
|
|
$this->egData["tempListID"], |
|
|
|
|
325
|
|
|
$member[$i] |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
$result = $this->api->getSubscriberCanNoLongerReceiveEmailsForThisList( |
|
|
|
|
329
|
|
|
$this->egData["tempListID"], |
|
|
|
|
330
|
|
|
$member[$i] |
331
|
|
|
); |
332
|
|
|
|
333
|
|
|
$result = $this->api->getSubscriber( |
|
|
|
|
334
|
|
|
$this->egData["tempListID"], |
|
|
|
|
335
|
|
|
$member[$i] |
336
|
|
|
); |
337
|
|
|
|
338
|
|
|
$result = $this->api->getHistory( |
|
|
|
|
339
|
|
|
$this->egData["tempListID"], |
|
|
|
|
340
|
|
|
$member[$i] |
341
|
|
|
); |
342
|
|
|
$result = $this->api->deleteSubscriber( |
|
|
|
|
343
|
|
|
$this->egData["tempListID"], |
|
|
|
|
344
|
|
|
$member[$i] |
345
|
|
|
); |
346
|
|
|
$member[$i]->delete(); |
347
|
|
|
sleep(1); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
//delete list |
351
|
|
|
if ($this->egData["tempListID"]) { |
352
|
|
|
$this->api->deleteCustomField($this->egData["tempListID"], $customFieldKey); |
|
|
|
|
353
|
|
|
$result = $this->api->deleteList($this->egData["tempListID"]); |
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
echo "<h2>end of subscriber tests</h2>"; |
357
|
|
|
$this->index(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
protected function setupTests() |
361
|
|
|
{ |
362
|
|
|
$this->api = CampaignMonitorAPIConnector::create(); |
363
|
|
|
$this->api->init(); |
364
|
|
|
|
365
|
|
|
if ($this->showAll) { |
366
|
|
|
$this->api->setDebug(true); |
367
|
|
|
$this->egData["limit"] = 100; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
//getLists |
371
|
|
|
$result = $this->api->getLists(); |
372
|
|
|
$this->egData["listID"] = $result[0]->ListID; |
373
|
|
|
|
374
|
|
|
//getCampaigns |
375
|
|
|
$result = $this->api->getCampaigns(); |
376
|
|
|
if (isset($result[0])) { |
377
|
|
|
$this->egData["campaignID"] = $result[0]->CampaignID; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$this->api->setDebug(true); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
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.