3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * File for the CiviCRM APIv3 job functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_MailingContact
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
23 * Class api_v3_MailingContactTest
26 class api_v3_MailingContactTest
extends CiviUnitTestCase
{
27 protected $_entity = 'mailing';
29 public function setUp() {
32 'first_name' => 'abc1',
33 'contact_type' => 'Individual',
34 'last_name' => 'xyz1',
36 $this->_contact
= $this->callAPISuccess("contact", "create", $params);
39 public function tearDown() {
40 $this->callAPISuccess("contact", "delete", ['id' => $this->_contact
['id']]);
44 public function testMailingContactGetFields() {
45 $result = $this->callAPISuccess('MailingContact', 'getfields', [
48 $this->assertEquals('Delivered', $result['values']['type']['api.default']);
52 * Test for proper error when you do not supply the contact_id.
54 * Do not copy and paste.
56 * Test is of marginal if any value & testing of wrapper level functionality
57 * belongs in the SyntaxConformance class
59 public function testMailingNoContactID() {
60 $this->callAPIFailure('MailingContact', 'get', ['something' => 'This is not a real field']);
64 * Test that invalid contact_id return with proper error messages.
66 * Do not copy & paste.
68 * Test is of marginal if any value & testing of wrapper level functionality
69 * belongs in the SyntaxConformance class
71 public function testMailingContactInvalidContactID() {
72 $this->callAPIFailure('MailingContact', 'get', ['contact_id' => 'This is not a number']);
76 * Test that invalid types are returned with appropriate errors.
78 public function testMailingContactInvalidType() {
83 $this->callAPIFailure('MailingContact', 'get', $params);
87 * Test for success result when there are no mailings for a the given contact.
89 public function testMailingContactNoMailings() {
91 'contact_id' => $this->_contact
['id'],
93 $result = $this->callAPISuccess('MailingContact', 'get', $params);
94 $this->assertEquals($result['count'], 0);
95 $this->assertTrue(empty($result['values']));
99 * Test that the API returns a mailing properly when there is only one.
101 public function testMailingContactDelivered() {
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']]);
106 'contact_id' => $contactID,
107 'type' => 'Delivered',
110 $result = $this->callAPISuccess('MailingContact', 'get', $params);
111 $count = $this->callAPISuccess('MailingContact', 'getcount', $params);
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");
117 $this->assertEquals(CRM_Core_Session
::getLoggedInContactID(), $result['values'][1]['creator_id']);
121 * Test that the API returns only the "Bounced" mailings when instructed to
126 public function testMailingContactBounced() {
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']]);
131 'contact_id' => $contactID,
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);
146 public function setupEventQueue() {
147 $contactID = $this->individualCreate(['first_name' => 'Test']);
148 $emailID = $this->callAPISuccessGetValue('Email', [
150 'contact_id' => $contactID,
152 $this->createLoggedInUser();
153 $mailingID = $this->callAPISuccess('Mailing', 'create', [
154 'name' => 'Test Mailing',
155 'subject' => 'Some Subject',
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,
164 return [$contactID, $mailingID, $eventQueueID];