777ced84f258da242426ce5071242619ecd48d85
[civicrm-core.git] / tests / phpunit / CRM / Member / BAO / MembershipStatusTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 CRM_Member_BAO_MembershipStatusTest
30 * @group headless
31 */
32 class CRM_Member_BAO_MembershipStatusTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 /**
39 * Check function add()
40 */
41 public function testAdd() {
42 $params = array(
43 'name' => 'pending',
44 'is_active' => 1,
45 );
46
47 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
48
49 $result = $this->assertDBNotNull('CRM_Member_BAO_MembershipStatus', $membershipStatus->id,
50 'name', 'id',
51 'Database check on updated membership status record.'
52 );
53 $this->assertEquals($result, 'pending', 'Verify membership status is_active.');
54 }
55
56 public function testRetrieve() {
57
58 $params = array(
59 'name' => 'testStatus',
60 'is_active' => 1,
61 );
62
63 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
64 $defaults = array();
65 $result = CRM_Member_BAO_MembershipStatus::retrieve($params, $defaults);
66 $this->assertEquals($result->name, 'testStatus', 'Verify membership status name.');
67 CRM_Member_BAO_MembershipStatus::del($membershipStatus->id);
68 }
69
70 public function testPseudoConstantflush() {
71 $params = array(
72 'name' => 'testStatus',
73 'is_active' => 1,
74 );
75 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
76 $defaults = array();
77 $result = CRM_Member_BAO_MembershipStatus::retrieve($params, $defaults);
78 $this->assertEquals($result->name, 'testStatus', 'Verify membership status name.');
79 $updateParams = array(
80 'id' => $membershipStatus->id,
81 'name' => 'testStatus',
82 'label' => 'Changed Status',
83 'is_active' => 1,
84 );
85 $membershipStatus2 = CRM_Member_BAO_MembershipStatus::add($updateParams);
86 $result = CRM_Member_PseudoConstant::membershipStatus($membershipStatus->id, NULL, 'label', FALSE, FALSE);
87 $this->assertEquals($result, 'Changed Status', 'Verify updated membership status label From PseudoConstant.');
88 CRM_Member_BAO_MembershipStatus::del($membershipStatus->id);
89 }
90
91 public function testSetIsActive() {
92
93 $params = array(
94 'name' => 'pending',
95 'is_active' => 1,
96 );
97
98 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
99 $result = CRM_Member_BAO_MembershipStatus::setIsActive($membershipStatus->id, 0);
100 $this->assertEquals($result, TRUE, 'Verify membership status record updation.');
101
102 $isActive = $this->assertDBNotNull('CRM_Member_BAO_MembershipStatus', $membershipStatus->id,
103 'is_active', 'id',
104 'Database check on updated membership status record.'
105 );
106 $this->assertEquals($isActive, 0, 'Verify membership status is_active.');
107 }
108
109 public function testGetMembershipStatus() {
110 $params = array(
111 'name' => 'pending',
112 'is_active' => 1,
113 );
114
115 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
116 $result = CRM_Member_BAO_MembershipStatus::getMembershipStatus($membershipStatus->id);
117 $this->assertEquals($result['name'], 'pending', 'Verify membership status name.');
118 }
119
120 public function testDel() {
121 $params = array(
122 'name' => 'testStatus',
123 'is_active' => 1,
124 );
125
126 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
127 CRM_Member_BAO_MembershipStatus::del($membershipStatus->id);
128 $defaults = array();
129 $result = CRM_Member_BAO_MembershipStatus::retrieve($params, $defaults);
130 $this->assertEquals(empty($result), TRUE, 'Verify membership status record deletion.');
131 }
132
133 public function testExpiredDisabled() {
134 $result = civicrm_api3('MembershipStatus', 'get', [
135 'name' => "Expired",
136 'api.MembershipStatus.create' => ['label' => 'Expiiiired'],
137 ]);
138
139 // Calling it 'Expiiiired' is OK.
140 $result = $this->callAPISuccess('job', 'process_membership', []);
141
142 $result = civicrm_api3('MembershipStatus', 'get', [
143 'name' => "Expired",
144 'api.MembershipStatus.create' => ['is_active' => 0],
145 ]);
146
147 // Disabling 'Expired' is OK.
148 $result = $this->callAPISuccess('job', 'process_membership', []);
149
150 $result = civicrm_api3('MembershipStatus', 'get', [
151 'name' => "Expired",
152 'api.MembershipStatus.delete' => [],
153 ]);
154
155 // Deleting 'Expired' is OK.
156 $result = $this->callAPISuccess('job', 'process_membership', []);
157
158 // Put things back like normal
159 $result = civicrm_api3('MembershipStatus', 'create', [
160 'name' => 'Expired',
161 'label' => 'Expired',
162 'start_event' => 'end_date',
163 'start_event_adjust_unit' => 'month',
164 'start_event_adjust_interval' => 1,
165 'is_current_member' => 0,
166 'is_admin' => 0,
167 'weight' => 4,
168 'is_default' => 0,
169 'is_active' => 1,
170 'is_reserved' => 0,
171 ]);
172
173 }
174
175 public function testGetMembershipStatusByDate() {
176 $params = array(
177 'name' => 'Current',
178 'is_active' => 1,
179 'start_event' => 'start_date',
180 'end_event' => 'end_date',
181 );
182
183 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
184 $toDate = date('Ymd');
185
186 $result = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($toDate, $toDate, $toDate, 'today', TRUE, NULL, $params);
187 $this->assertEquals($result['name'], 'Current', 'Verify membership status record.');
188 }
189
190 public function testgetMembershipStatusCurrent() {
191 $params = array(
192 'name' => 'Current',
193 'is_active' => 1,
194 'is_current_member' => 1,
195 );
196
197 $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
198 $result = CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent();
199
200 $this->assertEquals(empty($result), FALSE, 'Verify membership status records is_current_member.');
201 }
202
203 }