Skip to content

Commit

Permalink
CRM-18231 Added settings on developer page
Browse files Browse the repository at this point in the history
  • Loading branch information
Edzelopez authored and monishdeb committed Apr 28, 2017
1 parent 411b68f commit 95e92a7
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 6 deletions.
27 changes: 23 additions & 4 deletions CRM/Admin/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function setDefaultValues() {
$this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference();
$this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
$this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
$this->_defaults['environment'] = CRM_Core_Config::environment();
$this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
}

Expand Down Expand Up @@ -101,9 +102,23 @@ public function buildQuickForm() {
$props = $settingMetaData['values'][$setting];
if (isset($props['quick_form_type'])) {
if (isset($props['pseudoconstant'])) {
$options = civicrm_api3('Setting', 'getoptions', array(
'field' => $setting,
));
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,
));
}
}
else {
$options = NULL;
Expand All @@ -120,7 +135,11 @@ public function buildQuickForm() {
);
}
elseif ($add == 'addSelect') {
$this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
$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');
}
}
elseif ($add == 'addCheckBox') {
$this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('  '));
Expand Down
24 changes: 24 additions & 0 deletions CRM/Admin/Form/Setting/Debugging.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CRM_Admin_Form_Setting_Debugging extends CRM_Admin_Form_Setting {
'debug_enabled' => CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME,
'backtrace' => CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME,
'fatalErrorHandler' => CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME,
'environment' => CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME,
);

/**
Expand All @@ -54,4 +55,27 @@ public function buildQuickForm() {
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);
}
}
parent::postProcess();
}

}
4 changes: 4 additions & 0 deletions CRM/Admin/Page/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public function run() {
* @param null $action
*/
public function browse($action = NULL) {
// check if non-prod mode is enabled.
if (CRM_Core_Config::environment() != 'Production') {
CRM_Core_Session::setStatus(ts('Execution of scheduled jobs has been turned off by default since this is a non-production environment. You can override this for particular jobs by adding runInNonProductionEnvironment=TRUE as a parameter.'), ts("Non-production Environment"), "warning", array('expires' => 0));
}

// using Export action for Execute. Doh.
if ($this->_action & CRM_Core_Action::EXPORT) {
Expand Down
24 changes: 24 additions & 0 deletions CRM/Core/BAO/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,28 @@ public static function isUpgradeFromPreFourOneAlpha1() {
return FALSE;
}

/**
* Check if environment is explicitly set.
*
* @return bool
*/
public static function isEnvironmentSet($setting, $value = NULL) {
$environment = CRM_Core_Config::environment();
if ($setting == 'environment' && $environment) {
return TRUE;
}
return FALSE;
}

/**
* Check if job is able to be executed by API.
*
* @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.");
}
}

}
29 changes: 29 additions & 0 deletions CRM/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,35 @@ public static function domainID($domainID = NULL, $reset = FALSE) {
return $domain;
}

/**
* Function to get environment.
*
* @param string $env
* @param bool $reset
*
* @return string
*/
public static function environment($env = NULL, $reset = FALSE) {
static $environment;
if ($env) {
$environment = $env;
}
if ($reset || empty($environment)) {
if (defined('CIVICRM_ENVIRONMENT')) {
$environment = CIVICRM_ENVIRONMENT;
global $civicrm_setting;
$civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment'] = $environment;
}
else {
$environment = Civi::settings()->get('environment');
}
}
if (!$environment) {
$environment = 'Production';
}
return $environment;
}

/**
* Do general cleanup of caches, temp directories and temp tables
* CRM-8739
Expand Down
11 changes: 11 additions & 0 deletions CRM/Core/JobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ public function executeJobById($id) {
*/
public function executeJob($job) {
$this->currentJob = $job;

// CRM-18231 check if non-production environment.
try {
CRM_Core_BAO_Setting::isAPIJobAllowedToRun($job->apiParams);
}
catch (Exception $e) {
$this->logEntry('Error while executing ' . $job->name . ': ' . $e->getMessage());
$this->currentJob = FALSE;
return FALSE;
}

$this->logEntry('Starting execution of ' . $job->name);
$job->saveLastRun();

Expand Down
12 changes: 12 additions & 0 deletions CRM/Upgrade/Incremental/sql/4.7.10.mysql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ALTER TABLE civicrm_pledge_block ADD is_pledge_start_date_visible TINYINT(4) NOT
ALTER TABLE civicrm_pledge_block ADD is_pledge_start_date_editable TINYINT(4) NOT NULL DEFAULT 0 COMMENT 'If true - recurring start date is editable.';
ALTER TABLE civicrm_contribution_page ADD adjust_recur_start_date TINYINT(4) NOT NULL DEFAULT 0 COMMENT 'If true - user is able to adjust payment start date.' AFTER is_recur_installments;


-- CRM-17608 Merge to DOCx or ODT template
SELECT @option_group_id_ext := max(id) from civicrm_option_group where name = 'safe_file_extension';
SELECT @option_group_id_ext_wt := MAX(weight) FROM civicrm_option_value WHERE option_group_id = @option_group_id_ext;
Expand All @@ -21,3 +22,14 @@ INSERT INTO
`civicrm_option_value` (`option_group_id`, {localize field='label'}label{/localize}, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `is_optgroup`, `is_reserved`, `is_active`)
VALUES
(@option_group_id_ext, {localize}'{ts escape="sql"}odt{/ts}'{/localize}, @option_group_id_ext_val+1, 'odt', NULL, 0, 0, @option_group_id_ext_wt+1, 0, 1, 1);

-- CRM-18231
INSERT INTO civicrm_option_group
(name, {localize field='title'}title{/localize}, is_reserved, is_active) VALUES ('environment', {localize}'{ts escape="sql"}Environment{/ts}'{/localize}, 0, 1);

SELECT @option_group_id_env := max(id) from civicrm_option_group where name = 'environment';
INSERT INTO civicrm_option_value (option_group_id, {localize field='label'}label{/localize}, value, name, grouping, filter, is_default, weight, {localize field='description'}description{/localize}, is_optgroup, is_reserved, is_active, component_id, visibility_id)
VALUES
(@option_group_id_env, {localize}'{ts escape="sql"}Production{/ts}'{/localize}, 'Production', 'Production', NULL, 0, 1, 1, {localize}'{ts escape="sql"}Production Environment{/ts}'{/localize}, 0, 0, 1, NULL, NULL),
(@option_group_id_env, {localize}'{ts escape="sql"}Staging{/ts}'{/localize}, 'Staging', 'Staging', NULL, 0, NULL, 2, {localize}'{ts escape="sql"}Staging Environment{/ts}'{/localize}, 0, 0, 1, NULL, NULL),
(@option_group_id_env, {localize}'{ts escape="sql"}Development{/ts}'{/localize}, 'Development', 'Development', NULL, 0, NULL, 3, {localize}'{ts escape="sql"}Development Environment{/ts}'{/localize}, 0, 0, 1, NULL, NULL);
20 changes: 20 additions & 0 deletions CRM/Utils/Check/Component/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,24 @@ public function checkMbstring() {
return $messages;
}

/**
* Check if environment is Production.
* @return array
*/
public function checkEnvironment() {
$messages = array();

$environment = CRM_Core_Config::environment();
if ($environment != 'Production') {
$messages[] = new CRM_Utils_Check_Message(
__FUNCTION__,
ts('The environment of this CiviCRM instance is set to \'%1\'. Certain functionality like scheduled jobs has been disabled.', array(1 => $environment)),
ts('Non-Production Environment'),
\Psr\Log\LogLevel::ALERT,
'fa-bug'
);
}
return $messages;
}

}
1 change: 1 addition & 0 deletions api/v3/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ function civicrm_api3_job_process_batch_merge($params) {
'options' => array('limit' => 1),
));
}
$rgid = CRM_Utils_Array::value('rgid', $params);
$gid = CRM_Utils_Array::value('gid', $params);
$mode = CRM_Utils_Array::value('mode', $params, 'safe');

Expand Down
17 changes: 17 additions & 0 deletions settings/Developer.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
'is_contact' => 0,
'description' => "Set this value to Yes if you want to display a backtrace listing when a fatal error is encountered. This feature should NOT be enabled for production sites",
),
'environment' => array(
'group_name' => 'Developer Preferences',
'group' => 'developer',
'name' => 'environment',
'type' => 'String',
'html_type' => 'Select',
'quick_form_type' => 'Select',
'default' => 'Production',
'pseudoconstant' => array(
'optionGroupName' => 'environment',
),
'add' => '4.7',
'title' => 'Environment',
'is_domain' => 1,
'is_contact' => 0,
'description' => "Setting to define the environment in which this CiviCRM instance is running.",
),
'fatalErrorHandler' => array(
'group_name' => 'Developer Preferences',
'group' => 'developer',
Expand Down
5 changes: 5 additions & 0 deletions templates/CRM/Admin/Form/Setting/Debugging.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<td>{$form.backtrace.html}<br />
<span class="description">{ts}<strong>This feature should NOT be enabled for production sites.</strong><br />Set this value to <strong>Yes</strong> if you want to display a backtrace listing when a fatal error is encountered.{/ts}</span></td>
</tr>
<tr class="crm-debugging-form-block-environment">
<td class="label">{$form.environment.label}</td>
<td>{$form.environment.html}<br />
<span class="description">{ts}Set this value to <strong>Staging/Development</strong> to prevent cron jobs & mailings from being executed.{/ts}</span></td>
</tr>
<tr class="crm-debugging-form-block-fatalErrorHandler">
<td class="label">{$form.fatalErrorHandler.label}</td>
<td>{$form.fatalErrorHandler.html}<br />
Expand Down
11 changes: 11 additions & 0 deletions templates/CRM/common/civicrm.settings.php.template
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ if (!defined('CIVICRM_DOMAIN_ID')) {
define( 'CIVICRM_DOMAIN_ID', 1);
}

/**
* Setting to define the environment in which this CiviCRM instance is running.
* Note the setting here must be value from the option group 'Environment',
* (see Administration > System Settings > Option Groups, Options beside Environment)
* which by default has three option values: 'Production', 'Staging', 'Development'.
* NB: defining a value from CIVICRM_ENVIRONMENT here prevents it from being set
* via the browser.
*/

// define( 'CIVICRM_ENVIRONMENT', 'Production' );

/**
* Settings to enable external caching using a cache server. This is an
* advanced feature, and you should read and understand the documentation
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/CRM/Core/BAO/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,18 @@ public static function _testOnChange_onChangeExample($oldValue, $newValue, $meta
$_testOnChange_hookCalls['metadata'] = $metadata;
}

/**
* Test to set isProductionEnvironment
*
*/
public function testSetCivicrmEnvironment() {
CRM_Core_BAO_Setting::setItem('Staging', CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'environment');
$values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'environment');
$this->assertEquals('Staging', $values);

define('CIVICRM_ENVIRONMENT', 'Development');
$environment = CRM_Core_Config::environment();
$this->assertEquals('Development', $environment);
}

}
55 changes: 55 additions & 0 deletions tests/phpunit/WebTest/Admin/Form/Setting/DebuggingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2015 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License along with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

require_once 'CiviTest/CiviSeleniumTestCase.php';

/**
* Class WebTest_Admin_Form_Setting_LocalizationTest
*/
class WebTest_Admin_Form_Setting_DebuggingTest extends CiviSeleniumTestCase {

protected function setUp() {
parent::setUp();
}

public function testSetCivicrmEnvironment() {
$this->webtestLogin();
$this->openCiviPage('admin/setting/debug', 'reset=1');
$this->select('environment', 'Staging');
$this->click('_qf_Debugging_next-top');

$this->waitForPageToLoad($this->getTimeoutMsec());
try {
$this->assertFalse($this->isTextPresent('Your changes have been saved.'));
}
catch (PHPUnit_Framework_AssertionFailedError$e) {
array_push($this->verificationErrors, $e->toString());
}
$envi = $this->getValue("environment");
$this->assertEquals('Staging', $envi);
}

}
Loading

0 comments on commit 95e92a7

Please sign in to comment.