From 50a890fd3e748a0356722f9da5a1f300f6322621 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Sun, 26 Jul 2020 10:12:15 -0400 Subject: [PATCH] default not being set for price fields --- CRM/Price/BAO/PriceFieldValue.php | 4 ++-- tests/phpunit/api/v3/PriceFieldValueTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CRM/Price/BAO/PriceFieldValue.php b/CRM/Price/BAO/PriceFieldValue.php index 7ad8514b75..e33b1ed83b 100644 --- a/CRM/Price/BAO/PriceFieldValue.php +++ b/CRM/Price/BAO/PriceFieldValue.php @@ -29,8 +29,6 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { * @return CRM_Price_DAO_PriceFieldValue */ public static function add($params) { - $fieldValueBAO = self::writeRecord($params); - if (!empty($params['is_default'])) { $priceFieldID = $params['price_field_id'] ?? CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $fieldValueBAO->id, 'price_field_id'); $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1'; @@ -38,6 +36,8 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { CRM_Core_DAO::executeQuery($query, $p); } + $fieldValueBAO = self::writeRecord($params); + // Reset the cached values in this function. CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE); return $fieldValueBAO; diff --git a/tests/phpunit/api/v3/PriceFieldValueTest.php b/tests/phpunit/api/v3/PriceFieldValueTest.php index 1c0f44a2c4..dca3b97ab2 100644 --- a/tests/phpunit/api/v3/PriceFieldValueTest.php +++ b/tests/phpunit/api/v3/PriceFieldValueTest.php @@ -217,4 +217,17 @@ class api_v3_PriceFieldValueTest extends CiviUnitTestCase { $this->callAPIFailure($this->_entity, 'create', $params); } + /** + * This is the same as testCreatePriceFieldValue but where is_default = 1. + */ + public function testCreatePriceFieldValueAsDefault() { + $params = $this->_params; + $params['is_default'] = 1; + $result = $this->callAPISuccess($this->_entity, 'create', $params); + $this->assertAPISuccess($result); + $this->assertEquals(1, $result['count']); + $this->assertNotNull($result['values'][$result['id']]['id']); + $this->getAndCheck($params, $result['id'], $this->_entity); + } + } -- 2.25.1