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']]);
45 * Test that the api responds correctly to null params.
47 * Do not copy and paste.
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
54 public function testMailingNullParams() {
55 $this->callAPIFailure('MailingContact', 'get', NULL);
58 public function testMailingContactGetFields() {
59 $result = $this->callAPISuccess('MailingContact', 'getfields', [
62 $this->assertEquals('Delivered', $result['values']['type']['api.default']);
66 * Test for proper error when you do not supply the contact_id.
68 * Do not copy and paste.
70 * Test is of marginal if any value & testing of wrapper level functionality
71 * belongs in the SyntaxConformance class
73 public function testMailingNoContactID() {
74 $this->callAPIFailure('MailingContact', 'get', ['something' => 'This is not a real field']);
78 * Test that invalid contact_id return with proper error messages.
80 * Do not copy & paste.
82 * Test is of marginal if any value & testing of wrapper level functionality
83 * belongs in the SyntaxConformance class
85 public function testMailingContactInvalidContactID() {
86 $this->callAPIFailure('MailingContact', 'get', ['contact_id' => 'This is not a number']);
90 * Test that invalid types are returned with appropriate errors.
92 public function testMailingContactInvalidType() {
97 $this->callAPIFailure('MailingContact', 'get', $params);
101 * Test for success result when there are no mailings for a the given contact.
103 public function testMailingContactNoMailings() {
105 'contact_id' => $this->_contact
['id'],
107 $result = $this->callAPISuccess('MailingContact', 'get', $params);
108 $this->assertEquals($result['count'], 0);
109 $this->assertTrue(empty($result['values']));
113 * Test that the API returns a mailing properly when there is only one.
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']]);
120 'contact_id' => $contactID,
121 'type' => 'Delivered',
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']);
135 * Test that the API returns only the "Bounced" mailings when instructed to
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']]);
145 'contact_id' => $contactID,
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);
160 public function setupEventQueue() {
161 $contactID = $this->individualCreate(['first_name' => 'Test']);
162 $emailID = $this->callAPISuccessGetValue('Email', [
164 'contact_id' => $contactID,
166 $this->createLoggedInUser();
167 $mailingID = $this->callAPISuccess('Mailing', 'create', [
168 'name' => 'Test Mailing',
169 'subject' => 'Some Subject',
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,
178 return [$contactID, $mailingID, $eventQueueID];