a6ad472de0b0ff7d4c7aa45a1100c2628fec706f
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipStatusTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29 class api_v3_MembershipStatusTest extends CiviUnitTestCase {
30
31 protected $_contactID;
32 protected $_contributionTypeID;
33 protected $_membershipTypeID;
34 protected $_membershipStatusID;
35
36 protected $_apiversion =3;
37
38 /**
39 * @return array
40 */
41 function get_info() {
42 return array(
43 'name' => 'MembershipStatus Calc',
44 'description' => 'Test all MembershipStatus Calc API methods.',
45 'group' => 'CiviCRM API Tests',
46 );
47 }
48
49 function setUp() {
50 parent::setUp();
51 $this->_contactID = $this->individualCreate();
52 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
53 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
54
55 CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE);
56 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
57 }
58
59 function tearDown() {
60 $this->membershipStatusDelete($this->_membershipStatusID);
61 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
62 $this->contactDelete($this->_contactID);
63 }
64
65 ///////////////// civicrm_membership_status_get methods
66
67
68 /**
69 * Test civicrm_membership_status_get with empty params
70 */
71 function testGetEmptyParams() {
72 $result = $this->callAPISuccess('membership_status', 'get', array());
73 // It should be 8 statuses, 7 default from mysql_data
74 // plus one test status added in setUp
75 $this->assertEquals(8, $result['count']);
76 }
77
78 /**
79 * Test civicrm_membership_status_get. Success expected.
80 */
81 function testGet() {
82 $params = array(
83 'name' => 'test status',
84 );
85 $result = $this->callAPIAndDocument('membership_status', 'get', $params, __FUNCTION__, __FILE__);
86 $this->assertEquals($result['values'][$this->_membershipStatusID]['name'], "test status", "In line " . __LINE__);
87 }
88
89 /**
90 * Test civicrm_membership_status_get. Success expected.
91 */
92 function testGetLimit() {
93 $result = $this->callAPISuccess('membership_status', 'get', array());
94 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__);
95 $params['option.limit'] = 1;
96 $result = $this->callAPISuccess('membership_status', 'get', $params);
97 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__);
98 }
99
100 function testCreateDuplicateName() {
101 $params = array('name' => 'name');
102 $result = $this->callAPISuccess('membership_status', 'create', $params);
103 $result = $this->callAPIFailure('membership_status', 'create', $params,
104 'A membership status with this name already exists.'
105 );
106 }
107
108 function testCreateWithMissingRequired() {
109 $params = array('title' => 'Does not make sense');
110 $result = $this->callAPIFailure('membership_status', 'create', $params);
111 }
112
113 function testCreate() {
114 $params = array(
115 'name' => 'test membership status',
116 );
117 $result = $this->callAPIAndDocument('membership_status', 'create', $params, __FUNCTION__, __FILE__);
118
119 $this->assertNotNull($result['id']);
120 $this->membershipStatusDelete($result['id']);
121 }
122
123 function testUpdate() {
124 $params = array(
125 'name' => 'test membership status', );
126 $result = $this->callAPISuccess('membership_status', 'create', $params);
127 $id = $result['id'];
128 $result = $this->callAPISuccess('membership_status', 'get', $params);
129 $this->assertEquals('test membership status', $result['values'][$id]['name']);
130 $newParams = array(
131 'id' => $id,
132 'name' => 'renamed', );
133 $result = $this->callAPISuccess('membership_status', 'create', $newParams);
134 $result = $this->callAPISuccess('membership_status', 'get', array('id' => $id));
135 $this->assertEquals('renamed', $result['values'][$id]['name']);
136 $this->membershipStatusDelete($result['id']);
137 }
138
139
140 ///////////////// civicrm_membership_status_delete methods
141 function testDeleteEmptyParams() {
142 $result = $this->callAPIFailure('membership_status', 'delete', array());
143 }
144
145 function testDeleteWithMissingRequired() {
146 $params = array('title' => 'Does not make sense');
147 $result = $this->callAPIFailure('membership_status', 'delete', $params);
148 }
149
150 function testDelete() {
151 $membershipID = $this->membershipStatusCreate();
152 $params = array(
153 'id' => $membershipID,
154 );
155 $result = $this->callAPISuccess('membership_status', 'delete', $params);
156 }
157 /*
158 * Test that trying to delete membership status while membership still exists creates error
159 */
160 function testDeleteWithMembershipError() {
161 $membershipStatusID = $this->membershipStatusCreate();
162 $this->_contactID = $this->individualCreate();
163 $this->_entity = 'membership';
164 $params = array(
165 'contact_id' => $this->_contactID,
166 'membership_type_id' => $this->_membershipTypeID,
167 'join_date' => '2009-01-21',
168 'start_date' => '2009-01-21',
169 'end_date' => '2009-12-21',
170 'source' => 'Payment',
171 'is_override' => 1,
172 'status_id' => $membershipStatusID,
173 );
174
175 $result = $this->callAPISuccess('membership', 'create', $params);
176 $membershipID = $result['id'];
177
178 $params = array(
179 'id' => $membershipStatusID,
180 );
181 $result = $this->callAPIFailure('membership_status', 'delete', $params);
182
183 $this->callAPISuccess('Membership', 'Delete', array(
184 'id' => $membershipID,
185 ));
186 $result = $this->callAPISuccess('membership_status', 'delete', $params);
187 }
188 }
189