From 06f258d2ad78fa1d90c410d714376f8c06fbe073 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 1 Sep 2017 10:43:50 +1000 Subject: [PATCH] CRM-20237 Don't add in extra copy of ids and only use most recent id copied from in title and don't put copy of details in name for pricesets --- CRM/Price/BAO/PriceSet.php | 9 +++--- tests/phpunit/CRM/Price/BAO/PriceSetTest.php | 33 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index a83d7b6658..27fdb9014e 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -1240,12 +1240,13 @@ WHERE id = %1"; */ public static function copy($id) { $maxId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_price_set"); - + $oldTitle = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $id, 'title', 'id'); + $newTitle = preg_replace('/\[Copy id \d+\]$/', "", $oldTitle); $title = ts('[Copy id %1]', array(1 => $maxId + 1)); $fieldsFix = array( - 'suffix' => array( - 'title' => ' ' . $title, - 'name' => '__Copy_id_' . ($maxId + 1) . '_', + 'replace' => array( + 'title' => trim($newTitle) . ' ' . $title, + 'name' => 'Price_Set__id_' . ($maxId + 1) . '_', ), ); diff --git a/tests/phpunit/CRM/Price/BAO/PriceSetTest.php b/tests/phpunit/CRM/Price/BAO/PriceSetTest.php index 74c36421d6..f2e7700cd3 100644 --- a/tests/phpunit/CRM/Price/BAO/PriceSetTest.php +++ b/tests/phpunit/CRM/Price/BAO/PriceSetTest.php @@ -57,6 +57,39 @@ class CRM_Price_BAO_PriceSetTest extends CiviUnitTestCase { $params = array('priceSetId' => $priceSetID, 'price_' . $field['id'] => 1); $amountLevel = CRM_Price_BAO_PriceSet::getAmountLevelText($params); $this->assertEquals(CRM_Core_DAO::VALUE_SEPARATOR . 'Price Field - 1' . CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel); + $priceFieldValue = $this->callAPISuccess('pricefieldvalue', 'getsingle', array('price_field_id' => $field['id'])); + $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $priceFieldValue['id'])); + $this->callAPISuccess('PriceField', 'delete', array('id' => $field['id'])); + $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceSetID)); + } + + /** + * CRM-20237 Test that Copied price set does not generate long name and unneded information + */ + public function testCopyPriceSet() { + $priceSetID = $this->eventPriceSetCreate(9); + $oldPriceSetInfo = $this->callAPISuccess('PriceSet', 'getsingle', array('id' => $priceSetID)); + $newPriceSet = CRM_Price_BAO_PriceSet::copy($priceSetID); + $this->assertEquals('Price_Set__id_' . $newPriceSet->id . '_', $newPriceSet->name); + $this->assertEquals($oldPriceSetInfo['title'] . ' [Copy id ' . $newPriceSet->id . ']', $newPriceSet->title); + $new2PriceSet = CRM_Price_BAO_PriceSet::copy($newPriceSet->id); + $this->assertEquals('Price_Set__id_' . $new2PriceSet->id . '_', $new2PriceSet->name); + $this->assertEquals($oldPriceSetInfo['title'] . ' [Copy id ' . $new2PriceSet->id . ']', $new2PriceSet->title); + $oldPriceField = $this->callAPISuccess('priceField', 'getsingle', array('price_set_id' => $priceSetID)); + $oldPriceFieldValue = $this->callAPISuccess('priceFieldValue', 'getsingle', array('price_field_id' => $oldPriceField['id'])); + $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $oldPriceFieldValue['id'])); + $this->callAPISuccess('PriceField', 'delete', array('id' => $oldPriceField['id'])); + $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceSetID)); + $newPriceField = $this->callAPISuccess('PriceField', 'getsingle', array('price_set_id' => $newPriceSet->id)); + $newPriceFieldValue = $this->callAPISuccess('PriceFieldValue', 'getsingle', array('price_field_id' => $newPriceField['id'])); + $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $newPriceFieldValue['id'])); + $this->callAPISuccess('PriceField', 'delete', array('id' => $newPriceField['id'])); + $this->callAPISuccess('PriceSet', 'delete', array('id' => $newPriceSet->id)); + $new2PriceField = $this->callAPISuccess('PriceField', 'getsingle', array('price_set_id' => $new2PriceSet->id)); + $new2PriceFieldValue = $this->callAPISuccess('PriceFieldValue', 'getsingle', array('price_field_id' => $new2PriceField['id'])); + $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $new2PriceFieldValue['id'])); + $this->callAPISuccess('PriceField', 'delete', array('id' => $new2PriceField['id'])); + $this->callAPISuccess('PriceSet', 'delete', array('id' => $new2PriceSet->id)); } /** -- 2.25.1