Skip to content

Commit

Permalink
default not being set for price fields
Browse files Browse the repository at this point in the history
  • Loading branch information
demeritcowboy committed Jul 26, 2020
1 parent 8b6f1c7 commit 50a890f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CRM/Price/BAO/PriceFieldValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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';
$p = [1 => [$priceFieldID, 'Integer']];
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;
Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/api/v3/PriceFieldValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,17 @@ public function testCreatePriceFieldValueWithDisabledFinancialType() {
$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);
}

}

0 comments on commit 50a890f

Please sign in to comment.