Merge pull request #17243 from civicrm/5.25
[civicrm-core.git] / tests / phpunit / CRM / Member / BAO / MembershipLogTest.php
1 <?php
2
3 /**
4 * File for the TestActivityType class
5 *
6 * (PHP 5)
7 *
8 * @package CiviCRM
9 *
10 * This file is part of CiviCRM
11 *
12 * CiviCRM is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * CiviCRM is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public
23 * License along with this program. If not, see
24 * <http://www.gnu.org/licenses/>.
25 */
26
27 /**
28 * Test CRM/Member/BAO Membership Log add , delete functions
29 *
30 * @package CiviCRM
31 * @group headless
32 */
33 class CRM_Member_BAO_MembershipLogTest extends CiviUnitTestCase {
34
35 /**
36 * @var int
37 */
38 private $relationshipTypeID;
39
40 /**
41 * @var int
42 */
43 private $organizationContactID;
44
45 /**
46 * @var int
47 */
48 private $financialTypeID;
49
50 /**
51 * @var int
52 */
53 private $membershipStatusID;
54
55 /**
56 * @var int
57 */
58 private $membershipTypeID;
59
60 /**
61 * Set up for test.
62 *
63 * @throws \CRM_Core_Exception
64 * @throws \CiviCRM_API3_Exception
65 */
66 public function setUp() {
67 parent::setUp();
68
69 $params = [
70 'contact_type_a' => 'Individual',
71 'contact_type_b' => 'Organization',
72 'name_a_b' => 'Test Employee of',
73 'name_b_a' => 'Test Employer of',
74 ];
75 $this->relationshipTypeID = $this->relationshipTypeCreate($params);
76 $this->organizationContactID = $this->organizationCreate();
77 $this->financialTypeID = 1;
78
79 $params = [
80 'name' => 'test type',
81 'description' => NULL,
82 'minimum_fee' => 10,
83 'duration_unit' => 'year',
84 'member_of_contact_id' => $this->organizationContactID,
85 'period_type' => 'fixed',
86 'fixed_period_start_day' => '0101',
87 'fixed_period_rollover_day' => '0101',
88 'duration_interval' => 1,
89 'financial_type_id' => $this->financialTypeID,
90 'relationship_type_id' => $this->relationshipTypeID,
91 'visibility' => 'Public',
92 'is_active' => 1,
93 ];
94 $membershipType = CRM_Member_BAO_MembershipType::add($params);
95 $this->membershipTypeID = $membershipType->id;
96 $this->membershipStatusID = $this->membershipStatusCreate('test status');
97 }
98
99 /**
100 * Tears down the fixture.
101 *
102 * @throws \CRM_Core_Exception
103 */
104 public function tearDown() {
105 $this->relationshipTypeDelete($this->relationshipTypeID);
106 $this->quickCleanUpFinancialEntities();
107 $this->restoreMembershipTypes();
108 $this->contactDelete($this->organizationContactID);
109 parent::tearDown();
110 }
111
112 /**
113 * Test del function.
114 *
115 * @throws \CRM_Core_Exception
116 * @throws \CiviCRM_API3_Exception
117 */
118 public function testDel() {
119 list($contactID, $membershipID) = $this->setupMembership();
120 CRM_Member_BAO_MembershipLog::del($membershipID);
121 $this->assertDBNull('CRM_Member_BAO_MembershipLog', $membershipID, 'membership_id',
122 'id', 'Database check for deleted membership log.'
123 );
124
125 $this->membershipDelete($membershipID);
126 $this->contactDelete($contactID);
127 }
128
129 /**
130 * Test reset modified ID.
131 *
132 * @throws \CRM_Core_Exception
133 */
134 public function testResetModifiedID() {
135 list($contactID, $membershipID) = $this->setupMembership();
136 CRM_Member_BAO_MembershipLog::resetModifiedID($contactID);
137 $this->assertDBNull('CRM_Member_BAO_MembershipLog', $contactID, 'modified_id',
138 'modified_id', 'Database check for NULL modified id.'
139 );
140
141 $this->membershipDelete($membershipID);
142 $this->contactDelete($contactID);
143 }
144
145 /**
146 * Test that the value for modified_id can be set.
147 *
148 * @throws \CRM_Core_Exception
149 */
150 public function testCreateMembershipWithPassedInModifiedID() {
151 $modifier = $this->individualCreate();
152 $membershipID = $this->setupMembership($modifier)[1];
153 $this->assertEquals($modifier, $this->callAPISuccessGetValue('MembershipLog', ['membership_id' => $membershipID, 'return' => 'modified_id']));
154 }
155
156 /**
157 * Set up membership.
158 *
159 * @param int|null $modifiedID
160 *
161 * @return array
162 *
163 * @throws \CRM_Core_Exception
164 */
165 private function setupMembership($modifiedID = NULL): array {
166 $contactID = $this->individualCreate();
167 $modifiedID = $modifiedID ?? $contactID;
168
169 $params = [
170 'contact_id' => $contactID,
171 'membership_type_id' => $this->membershipTypeID,
172 'join_date' => date('Ymd', strtotime('2006-01-21')),
173 'start_date' => date('Ymd', strtotime('2006-01-21')),
174 'end_date' => date('Ymd', strtotime('2006-12-21')),
175 'source' => 'Payment',
176 'is_override' => 1,
177 'status_id' => $this->membershipStatusID,
178 'modified_id' => $modifiedID,
179 ];
180
181 $membershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
182 $this->assertEquals($modifiedID, CRM_Core_DAO::singleValueQuery(
183 'SELECT modified_id FROM civicrm_membership_log WHERE membership_id = %1',
184 [1 => [$membershipID, 'Integer']]
185 ));
186 return [$contactID, $membershipID];
187 }
188
189 }