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