Schema - Fix boolean fields in various tables
[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 use Civi\Api4\MembershipType;
28
29 /**
30 * Test CRM/Member/BAO Membership Log add , delete functions
31 *
32 * @package CiviCRM
33 * @group headless
34 */
35 class CRM_Member_BAO_MembershipLogTest extends CiviUnitTestCase {
36
37 /**
38 * @var int
39 */
40 private $relationshipTypeID;
41
42 /**
43 * @var int
44 */
45 private $organizationContactID;
46
47 /**
48 * @var int
49 */
50 private $financialTypeID;
51
52 /**
53 * @var int
54 */
55 private $membershipStatusID;
56
57 /**
58 * @var int
59 */
60 private $membershipTypeID;
61
62 /**
63 * Set up for test.
64 *
65 * @throws \CRM_Core_Exception
66 * @throws \API_Exception
67 */
68 public function setUp(): void {
69 parent::setUp();
70
71 $params = [
72 'contact_type_a' => 'Individual',
73 'contact_type_b' => 'Organization',
74 'name_a_b' => 'Test Employee of',
75 'name_b_a' => 'Test Employer of',
76 ];
77 $this->relationshipTypeID = $this->relationshipTypeCreate($params);
78 $this->organizationContactID = $this->organizationCreate();
79 $this->financialTypeID = 1;
80
81 $params = [
82 'name' => 'test type',
83 'description' => NULL,
84 'minimum_fee' => 10,
85 'duration_unit' => 'year',
86 'member_of_contact_id' => $this->organizationContactID,
87 'period_type' => 'fixed',
88 'fixed_period_start_day' => '0101',
89 'fixed_period_rollover_day' => '0101',
90 'duration_interval' => 1,
91 'financial_type_id' => $this->financialTypeID,
92 'relationship_type_id' => $this->relationshipTypeID,
93 'visibility' => 'Public',
94 'is_active' => 1,
95 ];
96 $this->membershipTypeID = MembershipType::create()->setValues($params)->execute()->first()['id'];
97 $this->membershipStatusID = $this->membershipStatusCreate('test status');
98 }
99
100 /**
101 * Tears down the fixture.
102 *
103 * @throws \API_Exception
104 */
105 public function tearDown(): void {
106 $this->relationshipTypeDelete($this->relationshipTypeID);
107 $this->quickCleanUpFinancialEntities();
108 $this->restoreMembershipTypes();
109 $this->contactDelete($this->organizationContactID);
110 parent::tearDown();
111 }
112
113 /**
114 * Test del function.
115 *
116 * @throws \CRM_Core_Exception
117 * @throws \CiviCRM_API3_Exception
118 */
119 public function testDel() {
120 list($contactID, $membershipID) = $this->setupMembership();
121 CRM_Member_BAO_MembershipLog::del($membershipID);
122 $this->assertDBNull('CRM_Member_BAO_MembershipLog', $membershipID, 'membership_id',
123 'id', 'Database check for deleted membership log.'
124 );
125
126 $this->membershipDelete($membershipID);
127 $this->contactDelete($contactID);
128 }
129
130 /**
131 * Test reset modified ID.
132 *
133 * @throws \CRM_Core_Exception
134 */
135 public function testResetModifiedID() {
136 list($contactID, $membershipID) = $this->setupMembership();
137 CRM_Member_BAO_MembershipLog::resetModifiedID($contactID);
138 $this->assertDBNull('CRM_Member_BAO_MembershipLog', $contactID, 'modified_id',
139 'modified_id', 'Database check for NULL modified id.'
140 );
141
142 $this->membershipDelete($membershipID);
143 $this->contactDelete($contactID);
144 }
145
146 /**
147 * Test that the value for modified_id can be set.
148 *
149 * @throws \CRM_Core_Exception
150 */
151 public function testCreateMembershipWithPassedInModifiedID(): void {
152 $modifier = $this->individualCreate();
153 $membershipID = $this->setupMembership($modifier)[1];
154 $this->assertEquals($modifier, $this->callAPISuccessGetValue('MembershipLog', ['membership_id' => $membershipID, 'return' => 'modified_id']));
155 }
156
157 /**
158 * Set up membership.
159 *
160 * @param int|null $modifiedID
161 *
162 * @return array
163 *
164 * @throws \CRM_Core_Exception
165 */
166 private function setupMembership($modifiedID = NULL): array {
167 $contactID = $this->individualCreate();
168 $modifiedID = $modifiedID ?? $contactID;
169
170 $params = [
171 'contact_id' => $contactID,
172 'membership_type_id' => $this->membershipTypeID,
173 'join_date' => date('Ymd', strtotime('2006-01-21')),
174 'start_date' => date('Ymd', strtotime('2006-01-21')),
175 'end_date' => date('Ymd', strtotime('2006-12-21')),
176 'source' => 'Payment',
177 'is_override' => 1,
178 'status_id' => $this->membershipStatusID,
179 'modified_id' => $modifiedID,
180 ];
181
182 $membershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
183 $this->assertEquals($modifiedID, CRM_Core_DAO::singleValueQuery(
184 'SELECT modified_id FROM civicrm_membership_log WHERE membership_id = %1',
185 [1 => [$membershipID, 'Integer']]
186 ));
187 return [$contactID, $membershipID];
188 }
189
190 }