Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / SettingTest.php
index c935547cddbe541886af39a841f26f930bc0b50c..f3c6e378299af935a652dee77bbc5293d09d4146 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -25,9 +25,6 @@
  +--------------------------------------------------------------------+
  */
 
-require_once 'CiviTest/CiviUnitTestCase.php';
-
-
 /**
  *  Test APIv3 civicrm_setting_* functions
  *
@@ -38,6 +35,7 @@ require_once 'CiviTest/CiviUnitTestCase.php';
 /**
  * Class contains api test cases for civicrm settings
  *
+ * @group headless
  */
 class api_v3_SettingTest extends CiviUnitTestCase {
 
@@ -110,11 +108,12 @@ class api_v3_SettingTest extends CiviUnitTestCase {
    */
   public function testGetFieldsCaching() {
     $settingsMetadata = array();
-    CRM_Core_BAO_Cache::setItem($settingsMetadata, 'CiviCRM setting Specs', 'settingsMetadata__');
-    CRM_Core_BAO_Cache::setItem($settingsMetadata, 'CiviCRM setting Spec', 'All');
+    Civi::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config::domainID() . '_', $settingsMetadata);
+    Civi::cache('settings')->set(\Civi\Core\SettingsMetadata::ALL, $settingsMetadata);
     $result = $this->callAPISuccess('setting', 'getfields', array());
     $this->assertArrayNotHasKey('customCSSURL', $result['values']);
     $this->quickCleanup(array('civicrm_cache'));
+    Civi::cache('settings')->flush();
   }
 
   public function testGetFieldsFilters() {
@@ -134,16 +133,6 @@ class api_v3_SettingTest extends CiviUnitTestCase {
     $this->assertArrayHasKey('domain_group_id', $result['values']);
   }
 
-  /**
-   * Test that getfields will filter on another field (prefetch)
-   */
-  public function testGetFieldsPrefetchFilters() {
-    $params = array('filters' => array('prefetch' => 1));
-    $result = $this->callAPISuccess('setting', 'getfields', $params);
-    $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
-    $this->assertArrayHasKey('monetaryDecimalPoint', $result['values']);
-  }
-
   /**
    * Ensure that on_change callbacks fire.
    *
@@ -244,7 +233,7 @@ class api_v3_SettingTest extends CiviUnitTestCase {
   public function testCreateInvalidURLSettings() {
     $params = array(
       'domain_id' => $this->_domainID2,
-      'userFrameworkResourceURL' => 'dfhkdhfd',
+      'userFrameworkResourceURL' => 'dfhkd hfd',
     );
     $result = $this->callAPIFailure('setting', 'create', $params);
     $params = array(
@@ -364,9 +353,11 @@ class api_v3_SettingTest extends CiviUnitTestCase {
     // 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__');
+    Civi::cache('settings')->flush();
     $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'));
+    $this->assertEquals('keyset', Civi::settings()->get('test_key'));
     $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
     $this->assertEquals('keyset', $result);
   }
@@ -377,20 +368,19 @@ class api_v3_SettingTest extends CiviUnitTestCase {
   public function testSetConfigSetting() {
     $config = CRM_Core_Config::singleton();
     $this->assertFalse($config->debug == 1);
+
     $params = array(
       'domain_id' => $this->_domainID2,
       'debug_enabled' => 1,
     );
     $result = $this->callAPISuccess('setting', 'create', $params);
+
+    $this->assertEquals(1, Civi::settings($this->_domainID2)->get('debug_enabled'));
+
     CRM_Core_BAO_Domain::setDomain($this->_domainID2);
     $config = CRM_Core_Config::singleton(TRUE, TRUE);
     CRM_Core_BAO_Domain::resetDomain();
-    $this->assertTrue($config->debug == 1);
-    // this should NOT be stored in the settings table now - only in config
-    $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%maxFileSize%'";
-    $dao = CRM_Core_DAO::executeQuery($sql);
-    $dao->fetch();
-    $this->assertEquals($dao->c, 0);
+    $this->assertEquals(1, $config->debug);
   }
 
   /**
@@ -518,9 +508,9 @@ class api_v3_SettingTest extends CiviUnitTestCase {
   }
 
   /**
-   * Tests filling missing params.
+   * Settings should respect their defaults
    */
-  public function testFill() {
+  public function testDefaults() {
     $domparams = array(
       'name' => 'B Team Domain',
     );
@@ -540,13 +530,19 @@ class api_v3_SettingTest extends CiviUnitTestCase {
     );
     $result = $this->callAPISuccess('setting', 'get', $params);
     $this->assertAPISuccess($result, "in line " . __LINE__);
-    $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']], 'setting for domain 3 should not be set. Debug this IF domain test is passing');
+    $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
+
+    // The 'fill' operation is no longer necessary, but third parties might still use it, so let's
+    // make sure it doesn't do anything weird (crashing or breaking values).
     $result = $this->callAPISuccess('setting', 'fill', $params);
     $this->assertAPISuccess($result, "in line " . __LINE__);
     $result = $this->callAPISuccess('setting', 'get', $params);
     $this->assertAPISuccess($result, "in line " . __LINE__);
     $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
-    $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
+
+    // Setting has NULL default. Not returned.
+    //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
+
     $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
   }