Commit | Line | Data |
---|---|---|
2ede60ec DL |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * File for the CiviCRM APIv3 job functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_MailingContact | |
35 | * | |
36 | * @copyright CiviCRM LLC (c) 2004-2013 | |
37 | * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $ | |
38 | * | |
39 | */ | |
40 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
41 | class api_v3_MailingContactTest extends CiviUnitTestCase { | |
42 | protected $_apiversion; | |
43 | ||
44 | function setUp() { | |
45 | parent::setUp(); | |
2f26d849 | 46 | $this->_apiversion = 3; |
47 | $this->_entity = 'mailing'; | |
48 | $this->_contact_params = array( | |
49 | 'first_name' => 'abc1', | |
50 | 'contact_type' => 'Individual', | |
51 | 'last_name' => 'xyz1', | |
52 | 'version' => $this->_apiversion, | |
53 | ); | |
54 | $this->_contact = civicrm_api("contact", "create", $this->_contact_params); | |
b14ce773 | 55 | |
2f26d849 | 56 | /*$this->quickCleanup( |
2ede60ec DL |
57 | array( |
58 | 'civicrm_mailing', | |
59 | 'civicrm_job', | |
60 | 'civicrm_mailing_event_queue', | |
61 | 'civicrm_mailing_event_delivered', | |
62 | 'civicrm_mailing_event_bounced', | |
63 | ) | |
2f26d849 | 64 | );*/ |
2ede60ec DL |
65 | } |
66 | ||
67 | function tearDown() { | |
68 | parent::tearDown(); | |
2f26d849 | 69 | civicrm_api("contact", "delete", $this->_contact_id); |
b14ce773 | 70 | |
2ede60ec | 71 | } |
b14ce773 | 72 | |
2f26d849 | 73 | /* |
74 | * Test that the api responds correctly to null params | |
75 | */ | |
b14ce773 | 76 | |
2f26d849 | 77 | public function testMailingNullParams() { |
d0e1eff2 | 78 | $result = $this->callAPIFailure('MailingContact', 'get', null); |
2f26d849 | 79 | } |
b14ce773 | 80 | public function testMailingContactGetFields() { |
81 | $result = civicrm_api('MailingContact', 'getfields', array( | |
82 | 'version' => 3, | |
83 | 'action' => 'get', | |
84 | ) | |
85 | ); | |
86 | $this->assertAPISuccess($result); | |
87 | $this->assertEquals('Delivered', $result['values']['type']['api.default']); | |
88 | } | |
89 | ||
2f26d849 | 90 | /* |
91 | * Test that the api will return the proper error when you do not | |
92 | * supply the contact_id | |
93 | */ | |
b14ce773 | 94 | |
2f26d849 | 95 | public function testMailingNoContactID() { |
96 | $params = array( | |
97 | 'something' => 'This is not a real field', | |
98 | 'version' => $this->_apiversion, | |
2ede60ec | 99 | ); |
b14ce773 | 100 | |
d0e1eff2 | 101 | $result = $this->callAPIFailure('MailingContact', 'get', $params); |
2ede60ec | 102 | } |
b14ce773 | 103 | |
2f26d849 | 104 | /* |
105 | * Test that invalid contact_id return with proper error messages | |
106 | */ | |
107 | public function testMailingContactInvalidContactID() { | |
108 | $params = array( | |
109 | 'contact_id' => 'This is not a number', | |
110 | 'version' => $this->_apiversion, | |
111 | ); | |
112 | ||
d0e1eff2 | 113 | $result = $this->callAPIFailure('MailingContact', 'get', $params); |
2f26d849 | 114 | } |
b14ce773 | 115 | |
2f26d849 | 116 | /* |
117 | * Test that invalid types are returned with appropriate errors | |
118 | */ | |
119 | public function testMailingContactInvalidType() { | |
120 | $params = array( | |
121 | 'contact_id' => 23, | |
122 | 'type' => 'invalid', | |
123 | 'version' => $this->_apiversion, | |
124 | ); | |
125 | ||
d0e1eff2 | 126 | $result = $this->callAPIFailure('MailingContact', 'get', $params); |
2f26d849 | 127 | } |
b14ce773 | 128 | |
129 | ||
2f26d849 | 130 | /* |
131 | * Test that the API returns properly when there are no mailings | |
132 | * for a the given contact | |
133 | */ | |
134 | public function testMailingContactNoMailings() { | |
135 | $params = array( | |
136 | 'contact_id' => $this->_contact['id'], | |
137 | 'version' => $this->_apiversion, | |
138 | ); | |
139 | ||
140 | $result = civicrm_api('MailingContact', 'get', $params); | |
fc334403 | 141 | $this->assertAPISuccess($result, "In line " . __LINE__); |
b14ce773 | 142 | |
2f26d849 | 143 | $this->assertEquals($result['count'], 0, "In line " . __LINE__); |
144 | $this->assertTrue(empty($result['values']), "In line " . __LINE__); | |
145 | } | |
b14ce773 | 146 | |
2f26d849 | 147 | /* |
148 | * Test that the API returns a mailing properly when there is only one | |
149 | */ | |
150 | public function testMailingContactDelivered() { | |
151 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
152 | //Create the User | |
153 | $op->execute($this->_dbconn, | |
154 | new PHPUnit_Extensions_Database_DataSet_XMLDataSet( | |
155 | dirname(__FILE__) . '/dataset/mailing_contact.xml' | |
156 | ) | |
157 | ); | |
158 | //~ Create the Mailing and connections to the user | |
159 | $op->execute($this->_dbconn, | |
160 | new PHPUnit_Extensions_Database_DataSet_XMLDataSet( | |
161 | dirname(__FILE__) . '/dataset/mailing_delivered.xml' | |
162 | ) | |
163 | ); | |
b14ce773 | 164 | |
2f26d849 | 165 | $params = array( |
166 | 'contact_id' => 23, | |
167 | 'type' => 'Delivered', | |
168 | 'version' => $this->_apiversion, | |
169 | ); | |
170 | ||
171 | $result = civicrm_api('MailingContact', 'get', $params); | |
fc334403 | 172 | $count = civicrm_api('MailingContact', 'getcount', $params); |
173 | $this->assertAPISuccess($result, "In line " . __LINE__); | |
2f26d849 | 174 | $this->assertEquals($result['count'], 1, "In line " . __LINE__); |
fc334403 | 175 | $this->assertEquals($count, 1, "In line " . __LINE__); |
2f26d849 | 176 | $this->assertFalse(empty($result['values']), "In line " . __LINE__); |
177 | $this->assertEquals($result['values'][1]['mailing_id'], 1, "In line " . __LINE__); | |
178 | $this->assertEquals($result['values'][1]['subject'], "Some Subject", "In line " . __LINE__); | |
179 | $this->assertEquals($result['values'][1]['creator_id'], 1, "In line " . __LINE__); | |
180 | $this->assertEquals($result['values'][1]['creator_name'], "xyz1, abc1", "In line " . __LINE__); | |
181 | } | |
b14ce773 | 182 | |
183 | ||
2f26d849 | 184 | /* |
185 | * Test that the API returns only the "Bounced" mailings when instructed to do so | |
186 | */ | |
187 | function testMailingContactBounced( ) { | |
188 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
189 | //Create the User | |
190 | $op->execute($this->_dbconn, | |
191 | new PHPUnit_Extensions_Database_DataSet_XMLDataSet( | |
192 | dirname(__FILE__) . '/dataset/mailing_contact.xml' | |
193 | ) | |
194 | ); | |
195 | //~ Create the Mailing and connections to the user | |
196 | $op->execute($this->_dbconn, | |
197 | new PHPUnit_Extensions_Database_DataSet_XMLDataSet( | |
198 | dirname(__FILE__) . '/dataset/mailing_bounced.xml' | |
199 | ) | |
200 | ); | |
b14ce773 | 201 | |
2f26d849 | 202 | $params = array( |
203 | 'contact_id' => 23, | |
204 | 'type' => 'Bounced', | |
205 | 'version' => $this->_apiversion, | |
206 | ); | |
207 | ||
208 | $result = civicrm_api('MailingContact', 'get', $params); | |
fc334403 | 209 | $this->assertAPISuccess($result, "In line " . __LINE__); |
2f26d849 | 210 | $this->assertEquals($result['count'], 1, "In line " . __LINE__); |
211 | $this->assertFalse(empty($result['values']), "In line " . __LINE__); | |
212 | $this->assertEquals($result['values'][2]['mailing_id'], 2, "In line " . __LINE__); | |
213 | $this->assertEquals($result['values'][2]['subject'], "Some Subject", "In line " . __LINE__); | |
214 | $this->assertEquals($result['values'][2]['creator_id'], 1, "In line " . __LINE__); | |
215 | $this->assertEquals($result['values'][2]['creator_name'], "xyz1, abc1", "In line " . __LINE__); | |
216 | } | |
b14ce773 | 217 | |
218 | ||
219 | ||
2f26d849 | 220 | } |