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