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