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