Merge pull request #15810 from eileenmcnaughton/mem_fix
[civicrm-core.git] / tests / phpunit / api / v3 / MailingContactTest.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 * File for the CiviCRM APIv3 job functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_MailingContact
17 *
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
20 */
21
22 /**
23 * Class api_v3_MailingContactTest
24 * @group headless
25 */
26 class api_v3_MailingContactTest extends CiviUnitTestCase {
27 protected $_entity = 'mailing';
28
29 public function setUp() {
30 parent::setUp();
31 $params = [
32 'first_name' => 'abc1',
33 'contact_type' => 'Individual',
34 'last_name' => 'xyz1',
35 ];
36 $this->_contact = $this->callAPISuccess("contact", "create", $params);
37 }
38
39 public function tearDown() {
40 $this->callAPISuccess("contact", "delete", ['id' => $this->_contact['id']]);
41 parent::tearDown();
42 }
43
44 /**
45 * Test that the api responds correctly to null params.
46 *
47 * Do not copy and paste.
48 *
49 * Tests like this that test the wrapper belong in the SyntaxConformance class
50 * (which already has a 'not array test)
51 * I have left this here in case 'null' isn't covered in that class
52 * but don't copy it only any other classes
53 */
54 public function testMailingNullParams() {
55 $this->callAPIFailure('MailingContact', 'get', NULL);
56 }
57
58 public function testMailingContactGetFields() {
59 $result = $this->callAPISuccess('MailingContact', 'getfields', [
60 'action' => 'get',
61 ]);
62 $this->assertEquals('Delivered', $result['values']['type']['api.default']);
63 }
64
65 /**
66 * Test for proper error when you do not supply the contact_id.
67 *
68 * Do not copy and paste.
69 *
70 * Test is of marginal if any value & testing of wrapper level functionality
71 * belongs in the SyntaxConformance class
72 */
73 public function testMailingNoContactID() {
74 $this->callAPIFailure('MailingContact', 'get', ['something' => 'This is not a real field']);
75 }
76
77 /**
78 * Test that invalid contact_id return with proper error messages.
79 *
80 * Do not copy & paste.
81 *
82 * Test is of marginal if any value & testing of wrapper level functionality
83 * belongs in the SyntaxConformance class
84 */
85 public function testMailingContactInvalidContactID() {
86 $this->callAPIFailure('MailingContact', 'get', ['contact_id' => 'This is not a number']);
87 }
88
89 /**
90 * Test that invalid types are returned with appropriate errors.
91 */
92 public function testMailingContactInvalidType() {
93 $params = [
94 'contact_id' => 23,
95 'type' => 'invalid',
96 ];
97 $this->callAPIFailure('MailingContact', 'get', $params);
98 }
99
100 /**
101 * Test for success result when there are no mailings for a the given contact.
102 */
103 public function testMailingContactNoMailings() {
104 $params = [
105 'contact_id' => $this->_contact['id'],
106 ];
107 $result = $this->callAPISuccess('MailingContact', 'get', $params);
108 $this->assertEquals($result['count'], 0);
109 $this->assertTrue(empty($result['values']));
110 }
111
112 /**
113 * Test that the API returns a mailing properly when there is only one.
114 */
115 public function testMailingContactDelivered() {
116 list($contactID, $mailingID, $eventQueueID) = $this->setupEventQueue();
117 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_event_delivered (event_queue_id) VALUES(%1)", [1 => [$eventQueueID, 'Integer']]);
118
119 $params = [
120 'contact_id' => $contactID,
121 'type' => 'Delivered',
122 ];
123
124 $result = $this->callAPISuccess('MailingContact', 'get', $params);
125 $count = $this->callAPISuccess('MailingContact', 'getcount', $params);
126 $this->assertEquals($result['count'], 1);
127 $this->assertEquals($count, 1);
128 $this->assertFalse(empty($result['values']));
129 $this->assertEquals($result['values'][1]['mailing_id'], 1);
130 $this->assertEquals($result['values'][1]['subject'], "Some Subject");
131 $this->assertEquals(CRM_Core_Session::getLoggedInContactID(), $result['values'][1]['creator_id']);
132 }
133
134 /**
135 * Test that the API returns only the "Bounced" mailings when instructed to
136 * do so.
137 *
138 * @throws \Exception
139 */
140 public function testMailingContactBounced() {
141 list($contactID, $mailingID, $eventQueueID) = $this->setupEventQueue();
142 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_event_bounce (event_queue_id, bounce_type_id) VALUES(%1, 6)", [1 => [$eventQueueID, 'Integer']]);
143
144 $params = [
145 'contact_id' => $contactID,
146 'type' => 'Bounced',
147 ];
148
149 $result = $this->callAPISuccess('MailingContact', 'get', $params)['values'];
150 $this->assertEquals(1, count($result));
151 $this->assertEquals($mailingID, $result[$mailingID]['mailing_id']);
152 $this->assertEquals('Some Subject', $result[$mailingID]['subject']);
153 $this->assertEquals(CRM_Core_Session::getLoggedInContactID(), $result[$mailingID]['creator_id'], 3);
154 }
155
156 /**
157 * @return array
158 * @throws \Exception
159 */
160 public function setupEventQueue() {
161 $contactID = $this->individualCreate(['first_name' => 'Test']);
162 $emailID = $this->callAPISuccessGetValue('Email', [
163 'return' => 'id',
164 'contact_id' => $contactID,
165 ]);
166 $this->createLoggedInUser();
167 $mailingID = $this->callAPISuccess('Mailing', 'create', [
168 'name' => 'Test Mailing',
169 'subject' => 'Some Subject',
170 ])['id'];
171 $mailingJobID = $this->callAPISuccess('MailingJob', 'create', ['mailing_id' => $mailingID])['id'];
172 $eventQueueID = $this->callAPISuccess('MailingEventQueue', 'create', [
173 'contact_id' => $contactID,
174 'mailing_id' => $mailingID,
175 'email_id' => $emailID,
176 'job_id' => $mailingJobID,
177 ])['id'];
178 return [$contactID, $mailingID, $eventQueueID];
179 }
180
181 }