From: Seamus Lee Date: Tue, 11 Aug 2020 21:44:16 +0000 (+1000) Subject: dev/core#1936 Make the label column on price_field_value table not required X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d361445874f44e0703122666931c9303ca878d69;p=civicrm-core.git dev/core#1936 Make the label column on price_field_value table not required --- diff --git a/CRM/Core/I18n/SchemaStructure.php b/CRM/Core/I18n/SchemaStructure.php index ecadf83929..02a51c9a78 100644 --- a/CRM/Core/I18n/SchemaStructure.php +++ b/CRM/Core/I18n/SchemaStructure.php @@ -154,7 +154,7 @@ class CRM_Core_I18n_SchemaStructure { 'help_post' => "text COMMENT 'Description and/or help text to display after this field.'", ], 'civicrm_price_field_value' => [ - 'label' => "varchar(255) NOT NULL COMMENT 'Price field option label'", + 'label' => "varchar(255) DEFAULT NULL COMMENT 'Price field option label'", 'description' => "text DEFAULT NULL COMMENT 'Price field option description.'", 'help_pre' => "text DEFAULT NULL COMMENT 'Price field option pre help text.'", 'help_post' => "text DEFAULT NULL COMMENT 'Price field option post field help.'", @@ -586,7 +586,6 @@ class CRM_Core_I18n_SchemaStructure { 'civicrm_price_field_value' => [ 'label' => [ 'type' => "Text", - 'required' => "true", ], 'description' => [ 'type' => "TextArea", diff --git a/CRM/Price/DAO/PriceFieldValue.php b/CRM/Price/DAO/PriceFieldValue.php index d510c14b62..01aff17979 100644 --- a/CRM/Price/DAO/PriceFieldValue.php +++ b/CRM/Price/DAO/PriceFieldValue.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Price/PriceFieldValue.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:28432a14b1b1523380eb41e8e481037d) + * (GenCodeChecksum:11a02f3576be10e8c2a0ea47a19e2dac) */ /** @@ -226,10 +226,10 @@ class CRM_Price_DAO_PriceFieldValue extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_STRING, 'title' => ts('Name'), 'description' => ts('Price field option name'), - 'required' => TRUE, 'maxlength' => 255, 'size' => CRM_Utils_Type::HUGE, 'where' => 'civicrm_price_field_value.name', + 'default' => 'NULL', 'table_name' => 'civicrm_price_field_value', 'entity' => 'PriceFieldValue', 'bao' => 'CRM_Price_BAO_PriceFieldValue', @@ -244,10 +244,10 @@ class CRM_Price_DAO_PriceFieldValue extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_STRING, 'title' => ts('Label'), 'description' => ts('Price field option label'), - 'required' => TRUE, 'maxlength' => 255, 'size' => CRM_Utils_Type::HUGE, 'where' => 'civicrm_price_field_value.label', + 'default' => 'NULL', 'table_name' => 'civicrm_price_field_value', 'entity' => 'PriceFieldValue', 'bao' => 'CRM_Price_BAO_PriceFieldValue', diff --git a/CRM/Upgrade/Incremental/php/FiveTwentyEight.php b/CRM/Upgrade/Incremental/php/FiveTwentyEight.php index b035cfb16e..01c899a7de 100644 --- a/CRM/Upgrade/Incremental/php/FiveTwentyEight.php +++ b/CRM/Upgrade/Incremental/php/FiveTwentyEight.php @@ -46,6 +46,37 @@ class CRM_Upgrade_Incremental_php_FiveTwentyEight extends CRM_Upgrade_Incrementa } } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_5_28_1($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); + $this->addTask('Make label field non required on price field value', 'priceFieldValueLabelNonRequired'); + } + + /** + * Make the price field value label column non required + * @return bool + */ + public static function priceFieldValueLabelNonRequired() { + $domain = new CRM_Core_DAO_Domain(); + $domain->find(TRUE); + if ($domain->locales) { + $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); + foreach ($locales as $locale) { + CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_price_field_value CHANGE `label_{$locale}` `label_{$locale}` varchar(255) DEFAULT NULL COMMENT 'Price field option label'", [], TRUE, NULL, FALSE, FALSE); + CRM_Core_DAO::executeQuery("UPDATE civicrm_price_field_value SET label_{$locale} = NULL WHERE label_{$locale} = 'null'", [], TRUE, NULL, FALSE, FALSE); + } + } + else { + CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_price_field_value CHANGE `label` `label` varchar(255) DEFAULT NULL COMMENT 'Price field option label'", [], TRUE, NULL, FALSE, FALSE); + CRM_Core_DAO::executeQuery("UPDATE civicrm_price_field_value SET label = NULL WHERE label = 'null'", [], TRUE, NULL, FALSE, FALSE); + } + return TRUE; + } + public static function createWpFilesMessage() { if (!function_exists('civi_wp')) { return ''; diff --git a/Civi/Api4/Service/Spec/Provider/PriceFieldValueCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/PriceFieldValueCreationSpecProvider.php index 00c6b19d1e..76cfa28739 100644 --- a/Civi/Api4/Service/Spec/Provider/PriceFieldValueCreationSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/PriceFieldValueCreationSpecProvider.php @@ -29,6 +29,8 @@ class PriceFieldValueCreationSpecProvider implements Generic\SpecProviderInterfa public function modifySpec(RequestSpec $spec) { // Name will be auto-generated from label if not supplied $spec->getFieldByName('name')->setRequired(FALSE); + // Ensure that label is required this matches v3 API but doesn't match DAO because form fields allow for NULLs + $spec->getFieldByName('label')->setRequired(TRUE); } /** diff --git a/tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php b/tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php index b4b0911e81..23261b4c66 100644 --- a/tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php +++ b/tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php @@ -39,4 +39,43 @@ class CRM_Price_BAO_PriceFieldValueTest extends CiviUnitTestCase { $this->assertEquals('visibility', $fields['visibility_id']['pseudoconstant']['optionGroupName']); } + public function testEmptyStringLabel() { + // Put stuff here that should happen before all tests in this unit. + $priceSetParams = [ + 'name' => 'default_goat_priceset', + 'title' => 'Goat accommodation', + 'is_active' => 1, + 'help_pre' => "Where does your goat sleep", + 'help_post' => "thank you for your time", + 'extends' => 2, + 'financial_type_id' => 1, + 'is_quick_config' => 1, + 'is_reserved' => 1, + ]; + + $price_set = $this->callAPISuccess('price_set', 'create', $priceSetParams); + $this->priceSetID = $price_set['id']; + + $priceFieldParams = [ + 'price_set_id' => $this->priceSetID, + 'name' => 'grassvariety', + 'label' => 'Grass Variety', + 'html_type' => 'Text', + 'is_enter_qty' => 1, + 'is_active' => 1, + ]; + $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldParams); + $this->priceFieldID = $priceField['id']; + $this->_params = [ + 'price_field_id' => $this->priceFieldID, + 'name' => 'rye_grass', + 'label' => '', + 'amount' => 1, + 'financial_type_id' => 1, + ]; + $priceFieldValue = CRM_Price_BAO_PriceFieldValue::create($this->_params); + $priceFieldValue->find(TRUE); + $this->assertEquals('', $priceFieldValue->label); + } + } diff --git a/xml/schema/Price/PriceFieldValue.xml b/xml/schema/Price/PriceFieldValue.xml index 74c649b916..2290d96282 100644 --- a/xml/schema/Price/PriceFieldValue.xml +++ b/xml/schema/Price/PriceFieldValue.xml @@ -37,11 +37,11 @@ Name 255 Price field option name - true Text 3.3 + NULL label @@ -50,11 +50,11 @@ 255 true Price field option label - true Text 3.3 + NULL description