From: Eileen McNaughton Date: Sat, 28 Aug 2021 23:13:50 +0000 (+1200) Subject: Fix Restore Membership types to create price set X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f8f6fc4ea4776d5ac253c68c30d256ea638b3305;p=civicrm-core.git Fix Restore Membership types to create price set --- diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 974af7a03f..ca132ddb20 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -31,6 +31,7 @@ use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; use Civi\Api4\FinancialType; use Civi\Api4\LineItem; +use Civi\Api4\MembershipType; use Civi\Api4\OptionGroup; use Civi\Api4\RelationshipType; use Civi\Payment\System; @@ -1940,7 +1941,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { /** * Reset the price set config so results exist. */ - public function restoreDefaultPriceSetConfig() { + public function restoreDefaultPriceSetConfig(): void { CRM_Core_DAO::executeQuery("DELETE FROM civicrm_price_set WHERE name NOT IN('default_contribution_amount', 'default_membership_type_amount')"); CRM_Core_DAO::executeQuery("UPDATE civicrm_price_set SET id = 1 WHERE name ='default_contribution_amount'"); CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field` (`id`, `price_set_id`, `name`, `label`, `html_type`, `is_enter_qty`, `help_pre`, `help_post`, `weight`, `is_display_amounts`, `options_per_line`, `is_active`, `is_required`, `active_on`, `expire_on`, `javascript`, `visibility_id`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', 'Text', 0, NULL, NULL, 1, 1, 1, 1, 1, NULL, NULL, NULL, 1)"); @@ -1949,16 +1950,58 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { /** * Recreate default membership types. + * + * @throws \API_Exception */ - public function restoreMembershipTypes() { - CRM_Core_DAO::executeQuery( - "REPLACE INTO civicrm_membership_type - (id, domain_id, name, description, member_of_contact_id, financial_type_id, minimum_fee, duration_unit, duration_interval, period_type, fixed_period_start_day, fixed_period_rollover_day, relationship_type_id, relationship_direction, visibility, weight, is_active) -VALUES - (1, 1, 'General', 'Regular annual membership.', 1, 2, 100.00, 'year', 2, 'rolling', NULL, NULL, 7, 'b_a', 'Public', 1, 1), - (2, 1, 'Student', 'Discount membership for full-time students.', 1, 2, 50.00, 'year', 1, 'rolling', NULL, NULL, NULL, NULL, 'Public', 2, 1), - (3, 1, 'Lifetime', 'Lifetime membership.', 1, 2, 1200.00, 'lifetime', 1, 'rolling', NULL, NULL, 7, 'b_a', 'Admin', 3, 1); - "); + public function restoreMembershipTypes(): void { + MembershipType::delete(FALSE)->addWhere('id', '>', 0)->execute(); + $this->quickCleanup(['civicrm_membership_type']); + $this->ensureMembershipPriceSetExists(); + + MembershipType::save(FALSE) + ->setRecords( + [ + [ + 'name' => 'General', + 'description' => 'Regular annual membership.', + 'minimum_fee' => 100, + 'duration_unit' => 'year', + 'duration_interval' => 2, + 'period_type' => 'rolling', + 'relationship_type_id' => 7, + 'relationship_direction' => 'b_a', + 'visibility' => 'Public', + 'is_active' => 1, + 'weight' => 1, + ], + [ + 'name' => 'Student', + 'description' => 'Discount membership for full-time students.', + 'minimum_fee' => 50, + 'duration_unit' => 1, + 'duration_interval' => 'year', + 'period_type' => 'rolling', + 'visibility' => 'Public', + ], + [ + 'name' => 'Lifetime', + 'description' => 'Lifetime membership.', + 'minimum_fee' => 1200.00, + 'duration_unit' => 1, + 'duration_interval' => 'lifetime', + 'period_type' => 'rolling', + 'relationship_type_id' => 7, + 'relationship_direction' => 'b_a', + 'visibility' => 'Admin', + ], + ] + ) + ->setDefaults([ + 'domain_id' => 1, + 'member_of_contact_id' => 1, + 'financial_type_id' => 2, + ] + )->execute(); } /* @@ -3889,4 +3932,19 @@ WHERE a1.is_primary = 0 CustomGroup::delete(FALSE)->addWhere('id', '>', 0)->execute(); } + /** + * Ensure the default price set & field exist for memberships. + */ + protected function ensureMembershipPriceSetExists(): void { + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_price_set (`id`, `name`, `title`, `extends`) + VALUES (2, 'default_membership_type_amount', 'Membership Amount', 3) + ON DUPLICATE KEY UPDATE `name` = 'default_membership_type_amount', title = 'Membership Amount'; + "); + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_price_field + (`id`, `name`, `price_set_id`, `label`, `html_type`) + VALUES (2, 1, 2, 'Membership Amount', 'Radio') + ON DUPLICATE KEY UPDATE `name` = '1', price_set_id = 1, label = 'Membership Amount', html_type = 'Radio' + "); + } + }