From 94f5d036bd168cf94c7529d3262c62294c16f8aa Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 8 Sep 2019 08:35:30 +1000 Subject: [PATCH] dev/core#1240 Ensure that Options are not duplicated when saving a contribution or event page when specifying the options --- CRM/Price/BAO/PriceField.php | 4 +- .../phpunit/CRM/Price/BAO/PriceFieldTest.php | 107 ++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/CRM/Price/BAO/PriceFieldTest.php diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 299832a3f68..b3d03a61391 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -177,10 +177,10 @@ public static function create(&$params) { } if ($opIds = CRM_Utils_Array::value('option_id', $params)) { if ($opId = CRM_Utils_Array::value($index, $opIds)) { - $optionsIds['id'] = $opId; + $options['id'] = $opId; } else { - $optionsIds['id'] = NULL; + $options['id'] = NULL; } } try { diff --git a/tests/phpunit/CRM/Price/BAO/PriceFieldTest.php b/tests/phpunit/CRM/Price/BAO/PriceFieldTest.php new file mode 100644 index 00000000000..0b57f845399 --- /dev/null +++ b/tests/phpunit/CRM/Price/BAO/PriceFieldTest.php @@ -0,0 +1,107 @@ +priceSet = civicrm_api3('PriceSet', 'create', [ + 'is_active' => 1, + 'extends' => 2, + 'is_quick_config' => 1, + 'financial_type_id' => 1, + 'name' => 'test_price_set', + 'title' => 'Test Price Set', + ]); + $this->priceFieldParams = [ + 'name' => 'contribution_amount', + 'is_active' => 1, + 'weight' => 2, + 'is_required' => 1, + 'label' => 'Contribution Amount', + 'html_type' => 'Radio', + 'financial_type_id' => 1, + 'option_label' => [ + 1 => 'Low', + 2 => 'Medium', + 3 => 'High', + ], + 'option_amount' => [ + 1 => 10, + 2 => 50, + 3 => 100, + ], + 'option_weight' => [ + 1 => 1, + 2 => 2, + 3 => 3, + ], + 'default_option' => 2, + 'price_set_id' => $this->priceSet['id'], + ]; + $this->priceField = civicrm_api3('PriceField', 'create', $this->priceFieldParams); + $this->priceFieldParams['id'] = $this->priceField['id']; + $fieldOptions = civicrm_api3('PriceFieldValue', 'get', ['price_field_id' => $this->priceField['id']]); + foreach ($fieldOptions['values'] as $fieldOption) { + if ($fieldOption['amount'] < 20) { + $key = 1; + } + elseif ($fieldOption['amount'] < 60) { + $key = 2; + } + else { + $key = 3; + } + $this->priceFieldParams['option_id'][$key] = $fieldOption['id']; + } + $this->priceFieldParams['default_option'] = 3; + $options = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $this->priceField['id']]); + $this->assertEquals(3, $options['count']); + } + +}