From b2f3c2132c099cb62169554433a114103d701df3 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 1 Dec 2023 13:05:00 +1300 Subject: [PATCH] dev/core#3994 Update membership block on membership type save & remove the same from the Manage form --- CRM/Member/BAO/MembershipBlock.php | 42 ++++++++++++++++++- CRM/Member/Form/MembershipBlock.php | 5 --- .../CRM/Member/BAO/MembershipTypeTest.php | 36 ++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/CRM/Member/BAO/MembershipBlock.php b/CRM/Member/BAO/MembershipBlock.php index d89eae0d2a..68c05e49a8 100644 --- a/CRM/Member/BAO/MembershipBlock.php +++ b/CRM/Member/BAO/MembershipBlock.php @@ -9,12 +9,16 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\MembershipBlock; +use Civi\Core\Event\PostEvent; +use Civi\Core\HookInterface; + /** * * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Member_BAO_MembershipBlock extends CRM_Member_DAO_MembershipBlock { +class CRM_Member_BAO_MembershipBlock extends CRM_Member_DAO_MembershipBlock implements HookInterface { /** * Create or update a MembershipBlock. @@ -40,4 +44,40 @@ class CRM_Member_BAO_MembershipBlock extends CRM_Member_DAO_MembershipBlock { return (bool) self::deleteRecord(['id' => $id]); } + /** + * Update MembershipBlocks if autorenew option is changed. + * + * Available auto-renew options are + * 0 - Autorenew unavailable + * 1 - Give option + * 2 - Force auto-renewal + * + * In the case of 0 or 2 we need to ensure that all membership blocks are + * set to the same value. If the option is 1 no action is required as + * all 3 options are then valid at the membership block level. + * + * https://issues.civicrm.org/jira/browse/CRM-15573 + * + * @param \Civi\Core\Event\PostEvent $event + * + * @noinspection PhpUnhandledExceptionInspection + */ + public static function on_hook_civicrm_post(PostEvent $event): void { + if ($event->entity === 'MembershipType' && $event->action === 'edit') { + $autoRenewOption = $event->object->auto_renew; + if ($event->id && $autoRenewOption !== NULL && ((int) $autoRenewOption) !== 1) { + $autoRenewOption = (int) $autoRenewOption; + $membershipBlocks = MembershipBlock::get(FALSE)->execute(); + foreach ($membershipBlocks as $membershipBlock) { + if (array_key_exists($event->id, $membershipBlock['membership_types']) + && ((int) $membershipBlock['membership_types'][$event->id]) !== $autoRenewOption + ) { + $membershipBlock['membership_types'][$event->id] = $autoRenewOption; + MembershipBlock::update(FALSE)->setValues($membershipBlock)->execute(); + } + } + } + } + } + } diff --git a/CRM/Member/Form/MembershipBlock.php b/CRM/Member/Form/MembershipBlock.php index e6b0c37dba..ede357b24a 100644 --- a/CRM/Member/Form/MembershipBlock.php +++ b/CRM/Member/Form/MembershipBlock.php @@ -151,11 +151,6 @@ class CRM_Member_Form_MembershipBlock extends CRM_Contribute_Form_ContributionPa } } - //CRM-15573 - if (!empty($params['id'])) { - $params['membership_types'] = serialize($membershipRequired); - CRM_Member_BAO_MembershipBlock::writeRecord($params); - } $this->add('hidden', 'mem_price_field_id', '', ['id' => 'mem_price_field_id']); $this->assign('is_recur', $isRecur); $this->assign('auto_renew', $renewOption); diff --git a/tests/phpunit/CRM/Member/BAO/MembershipTypeTest.php b/tests/phpunit/CRM/Member/BAO/MembershipTypeTest.php index a9074985d6..7ad00cf9e7 100644 --- a/tests/phpunit/CRM/Member/BAO/MembershipTypeTest.php +++ b/tests/phpunit/CRM/Member/BAO/MembershipTypeTest.php @@ -9,7 +9,9 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\MembershipBlock; use Civi\Api4\MembershipType; +use Civi\Test\ContributionPageTestTrait; /** * Class CRM_Member_BAO_MembershipTypeTest @@ -17,6 +19,8 @@ use Civi\Api4\MembershipType; */ class CRM_Member_BAO_MembershipTypeTest extends CiviUnitTestCase { + use ContributionPageTestTrait; + /** * @throws \CRM_Core_Exception */ @@ -325,4 +329,36 @@ class CRM_Member_BAO_MembershipTypeTest extends CiviUnitTestCase { } } + /** + * Test that when renewal settings are modified membership blocks are updated. + * + * @throws \CRM_Core_Exception + */ + public function testRenewModification(): void { + $this->contributionPageQuickConfigCreate(); + $autoRenew = MembershipBlock::get(FALSE)->execute()->first()['membership_types']; + $this->assertEquals(1, reset($autoRenew)); + + // Disable auto-renew on membership type - block should update to 0 (no auto-renew). + MembershipType::update() + ->addWhere('name', '=', 'General') + ->setValues(['auto_renew' => 0])->execute(); + $autoRenew = MembershipBlock::get(FALSE)->execute()->first()['membership_types']; + $this->assertEquals(0, reset($autoRenew)); + + // Force auto-renew on membership type - block should update to 2 (force auto-renew). + MembershipType::update()->addWhere('name', '=', 'General') + ->setValues(['auto_renew' => 2])->execute(); + $autoRenew = MembershipBlock::get(FALSE)->execute()->first()['membership_types']; + $this->assertEquals(2, reset($autoRenew)); + + // Make auto-renew optional on membership type - block should stay at 2 (force autorenew). + // If the membership type is optional if can be made more or less restrictive at + // the block level. + MembershipType::update()->addWhere('name', '=', 'General') + ->setValues(['auto_renew' => 1])->execute(); + $autoRenew = MembershipBlock::get(FALSE)->execute()->first()['membership_types']; + $this->assertEquals(2, reset($autoRenew)); + } + } -- 2.25.1