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