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