Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OptionValue BAO to call pre/post hooks to prevent force-reset of managed option values #23130

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CRM/Core/BAO/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public static function add(&$params, $ids = []) {
$params['option_group_id'], 'name', 'id'
);

$op = $id ? 'edit' : 'create';
CRM_Utils_Hook::pre($op, 'OptionValue', $id, $params);

// action is taken depending upon the mode
$optionValue = new CRM_Core_DAO_OptionValue();
$optionValue->copyValues($params);
Expand Down Expand Up @@ -218,6 +221,8 @@ public static function add(&$params, $ids = []) {
$optionValue->save();
CRM_Core_PseudoConstant::flush();

CRM_Utils_Hook::post($op, 'OptionValue', $id, $optionValue);

// Create relationship for payment instrument options
if (!empty($params['financial_account_id'])) {
$optionName = civicrm_api3('OptionGroup', 'getvalue', [
Expand Down
19 changes: 16 additions & 3 deletions tests/phpunit/api/v4/Entity/ManagedEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,21 @@ public function testOptionGroupAndValues() {
\CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();

$values = OptionValue::get(FALSE)
->addSelect('*', 'local_modified_date', 'has_base')
->addWhere('option_group_id.name', '=', 'testManagedOptionGroup')
->execute();

$this->assertCount(1, $values);
$this->assertEquals('Option Value 1', $values[0]['label']);
$this->assertNull($values[0]['local_modified_date']);
$this->assertTrue($values[0]['has_base']);

// Update option 1, now it should have a local_modified_date
// And the new label should persist after a reconcile
$result = OptionValue::update(FALSE)
->addWhere('id', '=', $values[0]['id'])
->addValue('label', '1 New Label')
->execute();

$optionValue2 = [
'module' => 'civicrm',
Expand All @@ -333,8 +343,8 @@ public function testOptionGroupAndValues() {
],
],
];

$this->_managedEntities[] = $optionValue2;

\CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();

$values = OptionValue::get(FALSE)
Expand All @@ -344,9 +354,12 @@ public function testOptionGroupAndValues() {
->execute();

$this->assertCount(2, $values);
$this->assertEquals('Option Value 2', $values[1]['label']);
$this->assertNull($values[0]['local_modified_date']);
$this->assertEquals('1 New Label', $values[0]['label']);
$this->assertNotNull($values[0]['local_modified_date']);
$this->assertTrue($values[0]['has_base']);
$this->assertEquals('Option Value 2', $values[1]['label']);
$this->assertNull($values[1]['local_modified_date']);
$this->assertTrue($values[1]['has_base']);

$this->_managedEntities = [];
\CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
Expand Down