extra setting test - hookt pu
authorEileen <eileen@fuzion.co.nz>
Sun, 5 Jan 2014 07:07:34 +0000 (20:07 +1300)
committerEileen <eileen@fuzion.co.nz>
Sun, 5 Jan 2014 07:11:03 +0000 (20:11 +1300)
CRM/Core/BAO/Setting.php
tests/phpunit/api/v3/SettingTest.php
tests/phpunit/api/v3/settings/test.setting.php [new file with mode: 0644]

index adc9e04f84a924b3b4621787a20d001bfb8cb017..87a5fdd3790e0822166380646e6a6e86abd6863c 100644 (file)
@@ -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);
index 2272085e01a29bbc60e821189faadba2cd767bac..e37f607c1f5dbd22c11acaa8470fe5b09d090117 100644 (file)
@@ -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 (file)
index 0000000..bb8d1d6
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+return array(
+  'test_key' => 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