PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/tests/php/101_MailchimpSeed.php

https://gitlab.com/x33n/platform
PHP | 253 lines | 203 code | 26 blank | 24 comment | 19 complexity | c029483def25b81295b4e5891bb1dc42 MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__) . '/base.php');
  3. class MailchimpSeedTests extends UnitTestCase {
  4. var $test_list_id;
  5. private $mailchimp_connection_id,
  6. $api_list_id=false,
  7. $api_key=false,
  8. $cash_user_id=1; // arbitrary user id so settings/queries match
  9. function __construct() {
  10. echo "Testing MailChimp Seed\n";
  11. // add a new admin user for this
  12. $user_add_request = new CASHRequest(
  13. array(
  14. 'cash_request_type' => 'system',
  15. 'cash_action' => 'addlogin',
  16. 'address' => 'email@thisisjustatest.com',
  17. 'password' => 'thiswillneverbeused',
  18. 'is_admin' => 1
  19. )
  20. );
  21. $this->cash_user_id = $user_add_request->response['payload'];
  22. // add a new connection
  23. $this->api_key = getTestEnv("MAILCHIMP_API_KEY");
  24. $this->api_list_id = getTestEnv("MAILCHIMP_LIST_ID");
  25. if (!$this->api_key || !$this->api_list_id) {
  26. echo "Mailchimp api key not found, skipping mailchimp tests\n";
  27. }
  28. $c = new CASHConnection($this->cash_user_id); // the '1' sets a user id=1
  29. $this->mailchimp_connection_id = $c->setSettings('MailChimp', 'com.mailchimp',
  30. array( "key" => $this->api_key, "list" => $this->api_list_id ) );
  31. if ($this->mailchimp_connection_id) {
  32. // add a new list
  33. $list_add_request = new CASHRequest(
  34. array(
  35. 'cash_request_type' => 'people',
  36. 'cash_action' => 'addlist',
  37. 'name' => 'Test List',
  38. 'description' => 'Test Description',
  39. 'user_id' => $this->cash_user_id,
  40. 'connection_id' => $this->mailchimp_connection_id
  41. )
  42. );
  43. // should work fine with no description or connection_id
  44. $this->test_list_id = $list_add_request->response['payload'];
  45. }
  46. }
  47. function testSetIDs() {
  48. // only run if key / list have been set properly
  49. if ($this->api_key && $this->api_list_id) {
  50. // make sure the added connection has been set
  51. $this->assertTrue($this->mailchimp_connection_id);
  52. $this->assertTrue($this->test_list_id);
  53. }
  54. }
  55. function testMailchimpSeed(){
  56. $time = time();
  57. // only run if key / list have been set properly
  58. if ($this->api_key && $this->api_list_id) {
  59. $mc = new MailchimpSeed($this->cash_user_id, $this->mailchimp_connection_id); // the '1' sets a user id=1
  60. $this->assertIsa($mc, 'MailchimpSeed');
  61. $this->assertTrue($mc->lists());
  62. $webhooks = $mc->listWebhooks();
  63. $this->assertTrue(isset($webhooks));
  64. $members = $mc->listMembers();
  65. $this->assertTrue($members);
  66. $total1 = $members['total'];
  67. $this->assertTrue($total1);
  68. $this->assertTrue($members['data'][0]['email'] == 'duke@leto.net');
  69. $test_email = "dev+$time@cashmusic.org";
  70. $rc = $mc->listSubscribe($test_email, array('double_optin' => false));
  71. $this->assertTrue($rc);
  72. if (!$rc) {
  73. fwrite(STDERR,"Failed to add $test_email to list " . $this->api_list_id);
  74. exit(1);
  75. }
  76. $members2 = $mc->listMembers();
  77. $this->assertTrue($members2);
  78. $this->assertTrue($members2['total'] > $total1 );
  79. $rc = $mc->listUnsubscribe($test_email);
  80. $this->assertTrue($rc);
  81. if (!$rc) {
  82. fwrite(STDERR,"Failed to remove $test_email from list $test_id");
  83. exit(1);
  84. }
  85. $members3 = $mc->listMembers();
  86. $this->assertTrue($members3);
  87. $this->assertTrue($members3['total'] == $total1 );
  88. }
  89. }
  90. function testMailchimpWebhooks(){
  91. $time = time();
  92. // only run if key / list have been set properly
  93. if ($this->api_key && $this->api_list_id) {
  94. // A valid 200 OK for an external server (don't rely on true API URL in case of localhost):
  95. $webhook_test_url = 'http://cashmusic.org/';
  96. $mc = new MailchimpSeed($this->cash_user_id, $this->mailchimp_connection_id);
  97. $webhooks1 = $mc->listWebhooks();
  98. $initial_webhooks = count($webhooks1);
  99. //$this->assertTrue(count($webhooks1) == 0, 'zero webhooks initially');
  100. $rc = $mc->listWebhookAdd($webhook_test_url);
  101. $this->assertTrue($rc);
  102. $webhooks2 = $mc->listWebhooks();
  103. $this->assertIsa($webhooks2, 'Array');
  104. $this->assertTrue($webhooks2);
  105. $this->assertTrue(count($webhooks2) == $initial_webhooks + 1, 'incorrect webhook count');
  106. $this->assertTrue($webhooks2[$initial_webhooks]); // using $initial_webhooks as index — zero if none, course corrects to our "new" webhook
  107. $this->assertTrue($webhooks2[$initial_webhooks]['url']);
  108. $this->assertEqual($webhook_test_url, $webhooks2[$initial_webhooks]['url'], 'urls do not match');
  109. $rc = $mc->listWebhookDel($webhook_test_url);
  110. $this->assertTrue($rc);
  111. $webhooks3 = $mc->listWebhooks();
  112. $this->assertEqual($webhooks1, $webhooks3, 'webhooks get deleted properly');
  113. }
  114. }
  115. function testProcessWebhooks(){
  116. $time = time();
  117. // only run if key / list have been set properly
  118. if ($this->api_key && $this->api_list_id) {
  119. $data_request = new CASHRequest(
  120. array(
  121. 'cash_request_type' => 'system',
  122. 'cash_action' => 'getapicredentials',
  123. 'user_id' => $this->cash_user_id
  124. )
  125. );
  126. $api_credentials = $data_request->response['payload'];
  127. // valid API url, but likely localhost
  128. $webhook_api_url = CASH_API_URL . '/verbose/people/processwebhook/origin/com.mailchimp/list_id/' . $this->test_list_id . '/api_key/' . $api_credentials['api_key'];
  129. // make sure we're rejecting bad keys
  130. $bad_webhook_api_url = CASH_API_URL . '/verbose/people/processwebhook/origin/com.mailchimp/list_id/' . $this->test_list_id . '/api_key/incorrect';
  131. $response = json_decode(CASHSystem::getURLContents($bad_webhook_api_url,array('sample'=>'data'),true));
  132. // TODO: this is currently returning 400, we need to get that to 403, but we'll test for not-200
  133. // which at least proves we're not accepting bad keys
  134. $this->assertNotEqual($response->status_code,200);
  135. $test_address = 'dev+shouldnotsubscribe' . $time . '@cashmusic.org';
  136. $add_post_data = array(
  137. "type" => "subscribe",
  138. "fired_at" => "2009-03-26 21:35:57",
  139. "data" => array (
  140. "id" => "8a25ff1d98",
  141. "list_id" => "a6b5da1054",
  142. "email" => $test_address,
  143. "email_type" => "html",
  144. "merges" => null,
  145. "ip_opt" => "10.20.10.30",
  146. "ip_signup" => "10.20.10.30"
  147. )
  148. );
  149. CASHSystem::getURLContents($webhook_api_url,$add_post_data,true);
  150. $list_request = new CASHRequest(
  151. array(
  152. 'cash_request_type' => 'people',
  153. 'cash_action' => 'getaddresslistinfo',
  154. 'list_id' => $this->test_list_id,
  155. 'address' => $test_address
  156. )
  157. );
  158. // make sure that the address has been added to the local list
  159. $this->assertTrue($list_request->response['payload']);
  160. $remove_post_data = array(
  161. "type" => "unsubscribe",
  162. "fired_at" => "2009-03-26 21:36:52",
  163. "data" => array (
  164. "id" => "8a25ff1d98",
  165. "action" => "unsub",
  166. "reason" => "manual",
  167. "list_id" => "a6b5da1054",
  168. "email" => $test_address,
  169. "email_type" => "html",
  170. "merges" => null,
  171. "ip_opt" => "10.20.10.30",
  172. "ip_signup" => "10.20.10.30"
  173. )
  174. );
  175. CASHSystem::getURLContents($webhook_api_url,$remove_post_data,true);
  176. $list_request = new CASHRequest(
  177. array(
  178. 'cash_request_type' => 'people',
  179. 'cash_action' => 'getaddresslistinfo',
  180. 'list_id' => $this->test_list_id,
  181. 'address' => $test_address
  182. )
  183. );
  184. // now make sure that the address has been removed
  185. $this->assertEqual($list_request->response['payload']['active'],0);
  186. }
  187. }
  188. function testListAddSync(){
  189. $time = time();
  190. // only run if key / list have been set properly
  191. if ($this->api_key && $this->api_list_id) {
  192. $test_address = 'dev+testlistaddsync' . $time . '@cashmusic.org';
  193. $add_request = new CASHRequest(
  194. array(
  195. 'cash_request_type' => 'people',
  196. 'cash_action' => 'addaddresstolist',
  197. 'address' => $test_address,
  198. 'list_id' => $this->test_list_id,
  199. 'do_not_verify' => true,
  200. 'service_opt_in' => false
  201. )
  202. );
  203. $this->assertTrue($add_request->response['payload']);
  204. $mc = new MailchimpSeed($this->cash_user_id, $this->mailchimp_connection_id);
  205. $members = $mc->listMembers();
  206. $member_count = count($members['data']);
  207. // this is a little weird because it's testing the last member of the subcriber list
  208. // pretty much *should* work but down the line a recursive search would be better to
  209. // avoid problems when 2 people test at once.
  210. $this->assertTrue($members['data'][$member_count - 1]['email'] == $test_address);
  211. $remove_request = new CASHRequest(
  212. array(
  213. 'cash_request_type' => 'people',
  214. 'cash_action' => 'removeaddress',
  215. 'address' => $test_address,
  216. 'list_id' => $this->test_list_id
  217. )
  218. );
  219. // test that it's been removed on our end
  220. $this->assertTrue($remove_request->response['payload']);
  221. $members = $mc->listMembers();
  222. // post-add total members - post-remove total members should equal one if it's been
  223. // removed from the subscribers list correctly on the mailchimp end
  224. $this->assertEqual($member_count - count($members['data']),1);
  225. }
  226. }
  227. }