Merge pull request #17706 from demeritcowboy/mysql-ssl-alt
[civicrm-core.git] / tests / phpunit / api / v3 / MailingGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_mailing_group_* functions
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class api_v3_MailingGroupTest extends CiviUnitTestCase {
19 protected $_groupID;
20 protected $_email;
21 protected $_apiversion;
22
23 public function setUp() {
24 parent::setUp();
25 $this->useTransaction(TRUE);
26 $this->_apiversion = 3;
27 $this->_groupID = $this->groupCreate();
28 $this->_email = 'test@test.test';
29 }
30
31 /**
32 * Test civicrm_mailing_group_event_subscribe with wrong params.
33 */
34 public function testMailerGroupSubscribeWrongParams() {
35 $params = [
36 'email' => $this->_email,
37 'group_id' => 'Wrong Group ID',
38 'contact_id' => '2121',
39 'time_stamp' => '20111111010101',
40 'hash' => 'sasa',
41 ];
42 $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
43 }
44
45 /**
46 * Test civicrm_mailing_group_event_subscribe with given contact ID.
47 */
48 public function testMailerGroupSubscribeGivenContactId() {
49 $params = [
50 'first_name' => 'Test',
51 'last_name' => 'Test',
52 'email' => $this->_email,
53 'contact_type' => 'Individual',
54 ];
55 $contactID = $this->individualCreate($params);
56
57 $params = [
58 'email' => $this->_email,
59 'group_id' => $this->_groupID,
60 'contact_id' => $contactID,
61 'hash' => 'b15de8b64e2cec34',
62 'time_stamp' => '20101212121212',
63 ];
64 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
65 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
66
67 $this->contactDelete($contactID);
68 }
69
70 /**
71 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
72 */
73 public function testMailerGroupUnsubscribeWrongParams() {
74 $params = [
75 'job_id' => 'Wrong ID',
76 'event_queue_id' => 'Wrong ID',
77 'hash' => 'Wrong Hash',
78 'time_stamp' => '20101212121212',
79 ];
80
81 $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
82 }
83
84 /**
85 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
86 */
87 public function testMailerGroupDomainUnsubscribeWrongParams() {
88 $params = [
89 'job_id' => 'Wrong ID',
90 'event_queue_id' => 'Wrong ID',
91 'hash' => 'Wrong Hash',
92 'org_unsubscribe' => 1,
93 'time_stamp' => '20101212121212',
94 ];
95
96 $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
97 }
98
99 /**
100 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
101 */
102
103 /**
104 * Test civicrm_mailing_group_event_resubscribe with wrong params.
105 */
106 public function testMailerGroupResubscribeWrongParams() {
107 $params = [
108 'job_id' => 'Wrong ID',
109 'event_queue_id' => 'Wrong ID',
110 'hash' => 'Wrong Hash',
111 'org_unsubscribe' => 'test',
112 'time_stamp' => '20101212121212',
113 ];
114 $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
115 }
116
117 /**
118 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
119 */
120 public function testMailerProcess() {
121 $this->callAPISuccess('MailSettings', 'create', [
122 'domain_id' => 1,
123 'name' => "my mail setting",
124 'domain' => 'setting.com',
125 'localpart' => 'civicrm+',
126 'server' => "localhost",
127 'username' => 'sue',
128 'password' => 'pass',
129 'is_default' => 1,
130 ]);
131 $mut = new CiviMailUtils($this, TRUE);
132 Civi::settings()->set('include_message_id', 1);
133 $params = [
134 'first_name' => 'Test',
135 'last_name' => 'Test',
136 'email' => $this->_email,
137 'contact_type' => 'Individual',
138 ];
139 $contactID = $this->individualCreate($params);
140
141 $params = [
142 'email' => $this->_email,
143 'group_id' => $this->_groupID,
144 'contact_id' => $contactID,
145 'hash' => 'b15de8b64e2cec34',
146 'time_stamp' => '20101212121212',
147 ];
148 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
149 // Check that subscription email has been sent.
150 $msgs = $mut->getAllMessages();
151 $this->assertCount(1, $msgs, 'Subscription email failed to send');
152 $mut->checkMailLog([
153 'Message-ID: <civicrm+s',
154 'To confirm this subscription, reply to this email or click',
155 ]);
156
157 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
158
159 $params = [
160 'contact_id' => $result['values'][$result['id']]['contact_id'],
161 'subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
162 'hash' => $result['values'][$result['id']]['hash'],
163 'time_stamp' => '20101212121212',
164 'event_subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
165 ];
166
167 $this->callAPISuccess('mailing_event_confirm', 'create', $params);
168 $this->contactDelete($contactID);
169 Civi::settings()->set('include_message_id', 0);
170 }
171
172 }