QA fixes
authordeb.monish <monish.deb@jmaconsulting.biz>
Tue, 29 Aug 2017 07:36:41 +0000 (13:06 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Fri, 1 Sep 2017 10:49:48 +0000 (16:19 +0530)
CRM/Admin/Form/Setting.php
CRM/Admin/Form/Setting/Debugging.php
CRM/Core/BAO/Setting.php
CRM/Utils/Mail.php
Civi/Core/SettingsBag.php
api/v3/Job.php
api/v3/Setting.php
settings/Developer.setting.php
tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php

index c378b53d80618adfe48902a888389a92e0aa7bc3..89e2574084d2db1843f263355ebf23cdf6962be3 100644 (file)
@@ -98,23 +98,9 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
     foreach ($settingMetaData as $setting => $props) {
       if (isset($props['quick_form_type'])) {
         if (isset($props['pseudoconstant'])) {
-          if (array_key_exists('optionGroupName', $props['pseudoconstant'])) {
-            $optionValues = civicrm_api3('OptionValue', 'get', array(
-              'return' => array("label", "value"),
-              'option_group_id' => $setting,
-            ));
-            if ($optionValues['count'] > 0) {
-              foreach ($optionValues['values'] as $key => $values) {
-                $vals[$values['value']] = $values['label'];
-              }
-              $options['values'] = $vals;
-            }
-          }
-          else {
-            $options = civicrm_api3('Setting', 'getoptions', array(
-              'field' => $setting,
-            ));
-          }
+          $options = civicrm_api3('Setting', 'getoptions', array(
+            'field' => $setting,
+          ));
         }
         else {
           $options = NULL;
@@ -131,11 +117,7 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
           );
         }
         elseif ($add == 'addSelect') {
-          $element = $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
-          if (defined('CIVICRM_ENVIRONMENT')) {
-            $element->freeze();
-            CRM_Core_Session::setStatus(ts('The environment settings have been disabled because it has been overridden in the settings file.'), ts('Environment settings'), 'info');
-          }
+          $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
         }
         elseif ($add == 'addCheckBox') {
           $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('&nbsp;&nbsp;'));
index 0d7219a2f3ec417a0ef5b59f26448812ab3cf55c..5a065c567ad36ef337bf8c4a7e15673d0186df11 100644 (file)
@@ -54,29 +54,12 @@ class CRM_Admin_Form_Setting_Debugging extends CRM_Admin_Form_Setting {
     }
 
     parent::buildQuickForm();
-  }
 
-  /**
-   * Process the form submission.
-   */
-  public function postProcess() {
-    $params = $this->controller->exportValues($this->_name);
-
-    if ($params['environment'] != 'Production') {
-      $mailing = Civi::settings()->get('mailing_backend');
-      if ($mailing['outBound_option'] != 2) {
-        Civi::settings()->set('mailing_backend_store', $mailing);
-      }
-      Civi::settings()->set('mailing_backend', array('outBound_option' => CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED));
-      CRM_Core_Session::setStatus(ts('Outbound emails have been disabled. Scheduled jobs will not run unless runInNonProductionEnvironment=TRUE is added as a parameter for a specific job'), ts("Non-production environment set"), "success");
-    }
-    else {
-      $mailing = Civi::settings()->get('mailing_backend_store');
-      if ($mailing) {
-        Civi::settings()->set('mailing_backend', $mailing);
-      }
+    if (defined('CIVICRM_ENVIRONMENT')) {
+      $element = $this->getElement('environment');
+      $element->freeze();
+      CRM_Core_Session::setStatus(ts('The environment settings have been disabled because it has been overridden in the settings file.'), ts('Environment settings'), 'info');
     }
-    parent::postProcess();
   }
 
 }
index 9b45f11aa8ca0b68a7dc09739f8bdde2544e4ff5..7997c9e172c6650344e465da12cab64dfd5abe3e 100644 (file)
@@ -509,8 +509,46 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
    * @throws API_Exception
    */
   public static function isAPIJobAllowedToRun($params) {
-    if (CRM_Core_Config::environment() != 'Production' && !CRM_Utils_Array::value('runInNonProductionEnvironment', $params)) {
-      throw new Exception("Job has not been executed as it is a non-production environment.");
+    $environment = CRM_Core_Config::environment(NULL, TRUE);
+    if ($environment != 'Production') {
+      if (CRM_Utils_Array::value('runInNonProductionEnvironment', $params)) {
+        $mailing = Civi::settings()->get('mailing_backend_store');
+        if ($mailing) {
+          Civi::settings()->set('mailing_backend', $mailing);
+        }
+      }
+      else {
+        throw new Exception(ts("Job has not been executed as it is a %1 (non-production) environment.", array(1 => $environment)));
+      }
+    }
+  }
+
+  /**
+   * Setting Callback - On Change.
+   *
+   * Respond to changes in the "environment" setting.
+   *
+   * @param array $oldValue
+   *   Value of old environment mode.
+   * @param array $newValue
+   *   Value of new environment mode.
+   * @param array $metadata
+   *   Specification of the setting (per *.settings.php).
+   */
+  public static function onChangeEnvironmentSetting($oldValue, $newValue, $metadata) {
+    if ($newValue != 'Production') {
+      $mailing = Civi::settings()->get('mailing_backend');
+      if ($mailing['outBound_option'] != 2) {
+        Civi::settings()->set('mailing_backend_store', $mailing);
+      }
+      Civi::settings()->set('mailing_backend', array('outBound_option' => CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED));
+      CRM_Core_Session::setStatus(ts('Outbound emails have been disabled. Scheduled jobs will not run unless runInNonProductionEnvironment=TRUE is added as a parameter for a specific job'), ts("Non-production environment set"), "success");
+    }
+    else {
+      $mailing = Civi::settings()->get('mailing_backend_store');
+      if ($mailing) {
+        Civi::settings()->set('mailing_backend', $mailing);
+      }
     }
   }
 
index 78dc6676e7fee081c004891d7d12edea74ba4166..87e62fb0f0afc6cdecd7cd6cd8c9e44beb75aed4 100644 (file)
@@ -100,12 +100,12 @@ class CRM_Utils_Mail {
     }
     elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) {
       CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))));
-      CRM_Core_Session::setStatus(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))));
+      CRM_Core_Error::statusBounce(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))));
     }
     else {
       CRM_Core_Error::debug_log_message(ts('There is no valid SMTP server Setting Or SendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))));
-      CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or sendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))));
       CRM_Core_Error::debug_var('mailing_info', $mailingInfo);
+      CRM_Core_Error::statusBounce(ts('There is no valid SMTP server Setting Or sendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))));
     }
     return $mailer;
   }
index 14289499244f2cfe8061e2704c0909815102716c..2aee1eb1c205b2bf799e1f256dc67e9654f564f9 100644 (file)
@@ -352,7 +352,9 @@ class SettingsBag {
     }
     $dao->find(TRUE);
 
-    if (isset($metadata['on_change']) && !($value == 0 && ($dao->value === NULL || unserialize($dao->value) == 0))) {
+    // string comparison with 0 always return true, so to be ensure the type use ===
+    // ref - https://stackoverflow.com/questions/8671942/php-string-comparasion-to-0-integer-returns-true
+    if (isset($metadata['on_change']) && !($value === 0 && ($dao->value === NULL || unserialize($dao->value) == 0))) {
       foreach ($metadata['on_change'] as $callback) {
         call_user_func(
           \Civi\Core\Resolver::singleton()->get($callback),
index bbc62b881999aea07a82cb2e0793265950a00ff1..1d21b3496500c3216e3038d38e833fa972d2f9f2 100644 (file)
@@ -314,6 +314,13 @@ function civicrm_api3_job_process_pledge($params) {
 function civicrm_api3_job_process_mailing($params) {
   $mailsProcessedOrig = CRM_Mailing_BAO_MailingJob::$mailsProcessed;
 
+  try {
+    CRM_Core_BAO_Setting::isAPIJobAllowedToRun($params);
+  }
+  catch (Exception $e) {
+    return civicrm_api3_create_error($e->getMessage());
+  }
+
   if (!CRM_Mailing_BAO_Mailing::processQueue()) {
     return civicrm_api3_create_error('Process Queue failed');
   }
index e8d85801e17809dafc91fbec8d8dae7accdf50cf..0677d2ffa0491704be94f768839c1fa07af34254 100644 (file)
@@ -160,6 +160,12 @@ function civicrm_api3_setting_getoptions($params) {
     $values = Civi\Core\Resolver::singleton()->call($pseudoconstant['callback'], array());
     return civicrm_api3_create_success($values, $params, 'Setting', 'getoptions');
   }
+  elseif (!empty($pseudoconstant['optionGroupName'])) {
+    return civicrm_api3_create_success(
+      CRM_Core_OptionGroup::values($pseudoconstant['optionGroupName'], FALSE, FALSE, TRUE),
+      $params, 'Setting', 'getoptions'
+    );
+  }
 
   throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
 }
index 2c0902ba6a2452fa799fc8196eec8c2e896d08c2..9a47e7916e62a4395ed69f6d499bc3e0fd649f06 100644 (file)
@@ -116,6 +116,9 @@ return array(
     'is_domain' => 1,
     'is_contact' => 0,
     'description' => "Setting to define the environment in which this CiviCRM instance is running.",
+    'on_change' => array(
+      'CRM_Core_BAO_Setting::onChangeEnvironmentSetting',
+    ),
   ),
   'fatalErrorHandler' => array(
     'group_name' => 'Developer Preferences',
index 4fec7e95061ba92a31052a1c882df3c84fd539bf..37bf968c14041c289115c1b17138aca0a5c74498 100644 (file)
@@ -395,7 +395,7 @@ abstract class CRM_Mailing_BaseMailingSystemTest extends CiviUnitTestCase {
     $mailingParams = array_merge($this->defaultParams, $params);
     $this->callAPISuccess('mailing', 'create', $mailingParams);
     $this->_mut->assertRecipients(array());
-    $this->callAPISuccess('job', 'process_mailing', array());
+    $this->callAPISuccess('job', 'process_mailing', array('runInNonProductionEnvironment' => TRUE));
 
     $allMessages = $this->_mut->getAllMessages('ezc');
     // There are exactly two contacts produced by setUp().