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