From ee7b49c4ce27fa4e8a12dad79b79b0edd1c43591 Mon Sep 17 00:00:00 2001 From: Eileen Date: Sun, 5 Jan 2014 20:07:34 +1300 Subject: [PATCH] extra setting test - hookt pu --- CRM/Core/BAO/Setting.php | 8 ++- tests/phpunit/api/v3/SettingTest.php | 27 ++++++++ .../phpunit/api/v3/settings/test.setting.php | 62 +++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/api/v3/settings/test.setting.php diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index adc9e04f84..87a5fdd379 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -511,7 +511,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $fields = civicrm_api('setting','getfields', $getFieldsParams); $invalidParams = (array_diff_key($settingParams, $fields['values'])); if (!empty($invalidParams)) { - throw new API_Exception(implode(',', $invalidParams) . " not valid settings"); + throw new api_Exception(implode(',', $invalidParams) . " not valid settings"); } if (!empty($settingParams)) { $filteredFields = array_intersect_key($settingParams, $fields['values']); @@ -539,7 +539,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { else { list($class,$fn) = explode('::',$fieldSpec['validate_callback']); if (!$class::$fn($value,$fieldSpec)) { - throw new API_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}"); + throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}"); } } } @@ -552,7 +552,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { */ static function validateBoolSetting(&$value, $fieldSpec) { if (!CRM_Utils_Rule::boolean($value)) { - throw new API_Exception("Boolean value required for {$fieldSpec['name']}"); + throw new api_Exception("Boolean value required for {$fieldSpec['name']}"); } if (!$value) { $value = 0; @@ -630,6 +630,8 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $cacheString .= "_{$filterField}_{$filterString}"; } $cached = 1; + // the caching into 'All' seems to be a duplicate of caching to + // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook $settingsMetadata = CRM_Core_BAO_Cache::getItem('CiviCRM setting Specs', $cacheString, $componentID); if ($settingsMetadata === NULL) { $settingsMetadata = CRM_Core_BAO_Cache::getItem('CiviCRM setting Spec', 'All', $componentID); diff --git a/tests/phpunit/api/v3/SettingTest.php b/tests/phpunit/api/v3/SettingTest.php index 2272085e01..e37f607c1f 100644 --- a/tests/phpunit/api/v3/SettingTest.php +++ b/tests/phpunit/api/v3/SettingTest.php @@ -84,14 +84,25 @@ class api_v3_SettingTest extends CiviUnitTestCase { } $this->_domainID3 = $result['id']; $this->_currentDomain = CRM_Core_Config::domainID(); + $this->hookClass = CRM_Utils_Hook::singleton(); } function tearDown() { + CRM_Utils_Hook::singleton()->reset(); parent::tearDown(); $this->callAPISuccess('system','flush', array()); $this->quickCleanup(array('civicrm_domain')); } + /** + * Set additional settings into metadata (implements hook) + * @param array $metaDataFolders + */ + function setExtensionMetadata(&$metaDataFolders) { + global $civicrm_root; + $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings'; + } + /** /** * check getfields works */ @@ -361,6 +372,22 @@ class api_v3_SettingTest extends CiviUnitTestCase { $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetSettingCurrentDomain'); $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']); } + + /** + * Check that setting defined in extension can be retrieved + */ + function testGetExtensionSetting() { + $this->hookClass->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata')); + $data = NULL; + // the caching of data to all duplicates the caching of data to the empty string + CRM_Core_BAO_Cache::setItem($data, 'CiviCRM setting Spec', 'All'); + CRM_Core_BAO_Cache::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__'); + $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings'))); + $this->assertArrayHasKey('test_key', $fields['values']); + $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset')); + $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings')); + $this->assertEquals('keyset', $result); + } /** * setting api should set & fetch settings stored in config as well as those in settings table */ diff --git a/tests/phpunit/api/v3/settings/test.setting.php b/tests/phpunit/api/v3/settings/test.setting.php new file mode 100644 index 0000000000..bb8d1d6397 --- /dev/null +++ b/tests/phpunit/api/v3/settings/test.setting.php @@ -0,0 +1,62 @@ + array( + 'group_name' => 'Test Settings', + 'group' => 'test', + 'name' => 'test_key', + 'type' => 'String', + 'add' => '4.4', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'Test Key', + 'title' => 'Test Key', + 'help_text' => '', + 'html_type' => 'Text', + 'quick_form_type' => 'Element', + ), + 'test_secret' => array( + 'group_name' => 'Test Settings', + 'group' => 'test', + 'name' => 'test_secret', + 'type' => 'String', + 'add' => '4.4', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'Test Secret', + 'title' => 'Test Secret', + 'help_text' => '', + 'html_type' => 'Text', + 'quick_form_type' => 'Element', + ), + 'test_public_certificate' => array( + 'group_name' => 'Test Settings', + 'group' => 'test', + 'name' => 'test_public_certificate', + 'type' => 'Text', + 'add' => '4.4', + 'is_domain' => 1, + 'is_contact' => 0, + 'title' => 'Test Public Certificate Path', + 'description' => 'Enter the full path to your public Certificate', + 'help_text' => '', + 'html_type' => 'Text', + 'html_attributes' => array( + 'size' => 50, + ), + 'quick_form_type' => 'Element', + ), + 'test_private_key' => array( + 'group_name' => 'Test Settings', + 'group' => 'test', + 'name' => 'test_private_key', + 'type' => 'String', + 'add' => '4.4', + 'is_domain' => 1, + 'is_contact' => 0, + 'title' => 'Test Private Key Path', + 'description' => 'Enter the full path to your private Key', + 'help_text' => '', + 'html_type' => 'Text', + 'quick_form_type' => 'Element', + ), + ); \ No newline at end of file -- 2.25.1