Merge pull request #4622 from civicrm/4.5
[civicrm-core.git] / tests / phpunit / api / v3 / MailingContactTest.php
CommitLineData
2ede60ec 1<?php
2ede60ec
DL
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
2ede60ec 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 *
06a1bc01 34 * @copyright CiviCRM LLC (c) 2004-2014
2ede60ec
DL
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
36 *
37 */
38require_once 'CiviTest/CiviUnitTestCase.php';
4cbe18b8
EM
39
40/**
41 * Class api_v3_MailingContactTest
42 */
2ede60ec 43class api_v3_MailingContactTest extends CiviUnitTestCase {
4d88a37a 44 protected $_apiversion = 3;
45 protected $_entity = 'mailing';
4cbe18b8 46
2ede60ec
DL
47 function setUp() {
48 parent::setUp();
4d88a37a 49 $params = array(
2f26d849 50 'first_name' => 'abc1',
51 'contact_type' => 'Individual',
52 'last_name' => 'xyz1',
2f26d849 53 );
4d88a37a 54 $this->_contact = $this->callAPISuccess("contact", "create", $params);
2ede60ec
DL
55 }
56
57 function tearDown() {
4d88a37a 58 $this->callAPISuccess("contact", "delete", array('id' => $this->_contact['id']));
2ede60ec
DL
59 parent::tearDown();
60 }
b14ce773 61
2f26d849 62 /*
63 * Test that the api responds correctly to null params
4d88a37a 64 * Note to copy & pasters - tests like this that test the wrapper belong in the SyntaxConformance class
65 * (which already has a 'not array test)
66 * I have left this here in case 'null' isn't covered in that class
67 * but don't copy it only any other classes
2f26d849 68 */
2f26d849 69 public function testMailingNullParams() {
d0e1eff2 70 $result = $this->callAPIFailure('MailingContact', 'get', null);
2f26d849 71 }
4d88a37a 72
b14ce773 73 public function testMailingContactGetFields() {
4d88a37a 74 $result = $this->callAPISuccess('MailingContact', 'getfields', array(
75 'action' => 'get',
b14ce773 76 )
77 );
b14ce773 78 $this->assertEquals('Delivered', $result['values']['type']['api.default']);
79 }
80
4d88a37a 81 /**
2f26d849 82 * Test that the api will return the proper error when you do not
83 * supply the contact_id
4d88a37a 84 * Note to copy & pasters - test is of marginal if any value & testing of wrapper level functionaliy
85 * belongs in the SyntaxConformance class
2f26d849 86 */
b14ce773 87
2f26d849 88 public function testMailingNoContactID() {
89 $params = array(
90 'something' => 'This is not a real field',
2ede60ec 91 );
d0e1eff2 92 $result = $this->callAPIFailure('MailingContact', 'get', $params);
2ede60ec 93 }
b14ce773 94
4d88a37a 95 /**
2f26d849 96 * Test that invalid contact_id return with proper error messages
4d88a37a 97 * Note to copy & pasters - test is of marginal if any value & testing of wrapper level functionaliy
98 * belongs in the SyntaxConformance class
2f26d849 99 */
100 public function testMailingContactInvalidContactID() {
4d88a37a 101 $params = array('contact_id' => 'This is not a number',);
102 $result = $this->callAPIFailure('MailingContact', 'get', $params);
2f26d849 103 }
b14ce773 104
4d88a37a 105 /**
2f26d849 106 * Test that invalid types are returned with appropriate errors
107 */
108 public function testMailingContactInvalidType() {
4d88a37a 109 $params = array(
110 'contact_id' => 23,
111 'type' => 'invalid',
112 );
113 $result = $this->callAPIFailure('MailingContact', 'get', $params);
2f26d849 114 }
b14ce773 115
4d88a37a 116 /**
2f26d849 117 * Test that the API returns properly when there are no mailings
118 * for a the given contact
119 */
120 public function testMailingContactNoMailings() {
121 $params = array(
122 'contact_id' => $this->_contact['id'],
2f26d849 123 );
4d88a37a 124 $result = $this->callAPISuccess('MailingContact', 'get', $params);
2f26d849 125 $this->assertEquals($result['count'], 0, "In line " . __LINE__);
126 $this->assertTrue(empty($result['values']), "In line " . __LINE__);
127 }
b14ce773 128
2f26d849 129 /*
130 * Test that the API returns a mailing properly when there is only one
131 */
132 public function testMailingContactDelivered() {
133 $op = new PHPUnit_Extensions_Database_Operation_Insert();
134 //Create the User
135 $op->execute($this->_dbconn,
bbfd46a5 136 $this->createXMLDataSet(
2f26d849 137 dirname(__FILE__) . '/dataset/mailing_contact.xml'
138 )
139 );
140 //~ Create the Mailing and connections to the user
141 $op->execute($this->_dbconn,
bbfd46a5 142 $this->createXMLDataSet(
2f26d849 143 dirname(__FILE__) . '/dataset/mailing_delivered.xml'
144 )
145 );
b14ce773 146
2f26d849 147 $params = array(
148 'contact_id' => 23,
149 'type' => 'Delivered',
2f26d849 150 );
151
4d88a37a 152 $result = $this->callAPISuccess('MailingContact', 'get', $params);
153 $count = $this->callAPISuccess('MailingContact', 'getcount', $params);
2f26d849 154 $this->assertEquals($result['count'], 1, "In line " . __LINE__);
fc334403 155 $this->assertEquals($count, 1, "In line " . __LINE__);
2f26d849 156 $this->assertFalse(empty($result['values']), "In line " . __LINE__);
157 $this->assertEquals($result['values'][1]['mailing_id'], 1, "In line " . __LINE__);
158 $this->assertEquals($result['values'][1]['subject'], "Some Subject", "In line " . __LINE__);
159 $this->assertEquals($result['values'][1]['creator_id'], 1, "In line " . __LINE__);
160 $this->assertEquals($result['values'][1]['creator_name'], "xyz1, abc1", "In line " . __LINE__);
161 }
b14ce773 162
163
2f26d849 164 /*
165 * Test that the API returns only the "Bounced" mailings when instructed to do so
166 */
167 function testMailingContactBounced( ) {
168 $op = new PHPUnit_Extensions_Database_Operation_Insert();
169 //Create the User
170 $op->execute($this->_dbconn,
bbfd46a5 171 $this->createXMLDataSet(
2f26d849 172 dirname(__FILE__) . '/dataset/mailing_contact.xml'
173 )
174 );
175 //~ Create the Mailing and connections to the user
176 $op->execute($this->_dbconn,
bbfd46a5 177 $this->createXMLDataSet(
2f26d849 178 dirname(__FILE__) . '/dataset/mailing_bounced.xml'
179 )
180 );
b14ce773 181
2f26d849 182 $params = array(
183 'contact_id' => 23,
184 'type' => 'Bounced',
2f26d849 185 );
186
4d88a37a 187 $result = $this->callAPISuccess('MailingContact', 'get', $params);
2f26d849 188 $this->assertEquals($result['count'], 1, "In line " . __LINE__);
189 $this->assertFalse(empty($result['values']), "In line " . __LINE__);
190 $this->assertEquals($result['values'][2]['mailing_id'], 2, "In line " . __LINE__);
191 $this->assertEquals($result['values'][2]['subject'], "Some Subject", "In line " . __LINE__);
192 $this->assertEquals($result['values'][2]['creator_id'], 1, "In line " . __LINE__);
193 $this->assertEquals($result['values'][2]['creator_name'], "xyz1, abc1", "In line " . __LINE__);
194 }
2f26d849 195}