Merge pull request #14760 from eileenmcnaughton/unsub
[civicrm-core.git] / tests / phpunit / api / v3 / MailingContactTest.php
CommitLineData
2ede60ec 1<?php
2ede60ec
DL
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
2ede60ec 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
2ede60ec
DL
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * File for the CiviCRM APIv3 job functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_MailingContact
33 *
6b83d5bd 34 * @copyright CiviCRM LLC (c) 2004-2019
2ede60ec 35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
2ede60ec 36 */
4cbe18b8
EM
37
38/**
39 * Class api_v3_MailingContactTest
acb109b7 40 * @group headless
4cbe18b8 41 */
2ede60ec 42class api_v3_MailingContactTest extends CiviUnitTestCase {
4d88a37a 43 protected $_entity = 'mailing';
4cbe18b8 44
00be9182 45 public function setUp() {
2ede60ec 46 parent::setUp();
84557ad3 47 $params = [
2f26d849 48 'first_name' => 'abc1',
49 'contact_type' => 'Individual',
50 'last_name' => 'xyz1',
84557ad3 51 ];
4d88a37a 52 $this->_contact = $this->callAPISuccess("contact", "create", $params);
2ede60ec
DL
53 }
54
00be9182 55 public function tearDown() {
84557ad3 56 $this->callAPISuccess("contact", "delete", ['id' => $this->_contact['id']]);
2ede60ec
DL
57 parent::tearDown();
58 }
b14ce773 59
c490a46a 60 /**
6d6dc885
EM
61 * Test that the api responds correctly to null params.
62 *
63 * Do not copy and paste.
64 *
65 * Tests like this that test the wrapper belong in the SyntaxConformance class
4d88a37a 66 * (which already has a 'not array test)
67 * I have left this here in case 'null' isn't covered in that class
68 * but don't copy it only any other classes
2f26d849 69 */
c490a46a 70 public function testMailingNullParams() {
6d6dc885 71 $this->callAPIFailure('MailingContact', 'get', NULL);
c490a46a
CW
72 }
73
74 public function testMailingContactGetFields() {
9099cab3 75 $result = $this->callAPISuccess('MailingContact', 'getfields', [
39b959db 76 'action' => 'get',
9099cab3 77 ]);
c490a46a
CW
78 $this->assertEquals('Delivered', $result['values']['type']['api.default']);
79 }
b14ce773 80
4d88a37a 81 /**
6d6dc885
EM
82 * Test for proper error when you do not supply the contact_id.
83 *
84 * Do not copy and paste.
85 *
86 * Test is of marginal if any value & testing of wrapper level functionality
4d88a37a 87 * belongs in the SyntaxConformance class
2f26d849 88 */
2f26d849 89 public function testMailingNoContactID() {
84557ad3 90 $this->callAPIFailure('MailingContact', 'get', ['something' => 'This is not a real field']);
2ede60ec 91 }
b14ce773 92
4d88a37a 93 /**
6d6dc885
EM
94 * Test that invalid contact_id return with proper error messages.
95 *
96 * Do not copy & paste.
97 *
98 * Test is of marginal if any value & testing of wrapper level functionality
4d88a37a 99 * belongs in the SyntaxConformance class
2f26d849 100 */
c490a46a 101 public function testMailingContactInvalidContactID() {
84557ad3 102 $this->callAPIFailure('MailingContact', 'get', ['contact_id' => 'This is not a number']);
c490a46a
CW
103 }
104
105 /**
6d6dc885 106 * Test that invalid types are returned with appropriate errors.
c490a46a
CW
107 */
108 public function testMailingContactInvalidType() {
9099cab3 109 $params = [
c490a46a
CW
110 'contact_id' => 23,
111 'type' => 'invalid',
9099cab3 112 ];
6d6dc885 113 $this->callAPIFailure('MailingContact', 'get', $params);
c490a46a
CW
114 }
115
116 /**
6d6dc885 117 * Test for success result when there are no mailings for a the given contact.
c490a46a
CW
118 */
119 public function testMailingContactNoMailings() {
9099cab3 120 $params = [
c490a46a 121 'contact_id' => $this->_contact['id'],
9099cab3 122 ];
c490a46a 123 $result = $this->callAPISuccess('MailingContact', 'get', $params);
6d6dc885
EM
124 $this->assertEquals($result['count'], 0);
125 $this->assertTrue(empty($result['values']));
c490a46a
CW
126 }
127
128 /**
6d6dc885 129 * Test that the API returns a mailing properly when there is only one.
2f26d849 130 */
c490a46a 131 public function testMailingContactDelivered() {
84557ad3 132 list($contactID, $mailingID, $eventQueueID) = $this->setupEventQueue();
133 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_event_delivered (event_queue_id) VALUES(%1)", [1 => [$eventQueueID, 'Integer']]);
c490a46a 134
21e9f92a 135 $params = [
136 'contact_id' => $contactID,
c490a46a 137 'type' => 'Delivered',
21e9f92a 138 ];
c490a46a
CW
139
140 $result = $this->callAPISuccess('MailingContact', 'get', $params);
141 $count = $this->callAPISuccess('MailingContact', 'getcount', $params);
6d6dc885
EM
142 $this->assertEquals($result['count'], 1);
143 $this->assertEquals($count, 1);
144 $this->assertFalse(empty($result['values']));
145 $this->assertEquals($result['values'][1]['mailing_id'], 1);
146 $this->assertEquals($result['values'][1]['subject'], "Some Subject");
84557ad3 147 $this->assertEquals(CRM_Core_Session::getLoggedInContactID(), $result['values'][1]['creator_id']);
c490a46a
CW
148 }
149
c490a46a 150 /**
84557ad3 151 * Test that the API returns only the "Bounced" mailings when instructed to
152 * do so.
153 *
154 * @throws \Exception
c490a46a 155 */
6c6e6187 156 public function testMailingContactBounced() {
84557ad3 157 list($contactID, $mailingID, $eventQueueID) = $this->setupEventQueue();
158 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_event_bounce (event_queue_id, bounce_type_id) VALUES(%1, 6)", [1 => [$eventQueueID, 'Integer']]);
c490a46a 159
84557ad3 160 $params = [
161 'contact_id' => $contactID,
c490a46a 162 'type' => 'Bounced',
84557ad3 163 ];
c490a46a 164
84557ad3 165 $result = $this->callAPISuccess('MailingContact', 'get', $params)['values'];
166 $this->assertEquals(1, count($result));
167 $this->assertEquals($mailingID, $result[$mailingID]['mailing_id']);
168 $this->assertEquals('Some Subject', $result[$mailingID]['subject']);
169 $this->assertEquals(CRM_Core_Session::getLoggedInContactID(), $result[$mailingID]['creator_id'], 3);
170 }
171
172 /**
173 * @return array
174 * @throws \Exception
175 */
176 public function setupEventQueue() {
177 $contactID = $this->individualCreate(['first_name' => 'Test']);
178 $emailID = $this->callAPISuccessGetValue('Email', [
179 'return' => 'id',
180 'contact_id' => $contactID,
181 ]);
182 $this->createLoggedInUser();
183 $mailingID = $this->callAPISuccess('Mailing', 'create', [
184 'name' => 'Test Mailing',
185 'subject' => 'Some Subject',
186 ])['id'];
187 $mailingJobID = $this->callAPISuccess('MailingJob', 'create', ['mailing_id' => $mailingID])['id'];
188 $eventQueueID = $this->callAPISuccess('MailingEventQueue', 'create', [
189 'contact_id' => $contactID,
190 'mailing_id' => $mailingID,
191 'email_id' => $emailID,
192 'job_id' => $mailingJobID,
193 ])['id'];
194 return [$contactID, $mailingID, $eventQueueID];
c490a46a 195 }
96025800 196
2f26d849 197}