Skip to content

Commit

Permalink
CRM-18231 Fixed unit and api tests
Browse files Browse the repository at this point in the history
----------------------------------------
* CRM-18231: Support safe migration from production to non-production instances
  https://issues.civicrm.org/jira/browse/CRM-18231
  • Loading branch information
Edzelopez committed Jul 21, 2016
1 parent b66cfdb commit a458416
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CRM/Admin/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function buildQuickForm() {
}
elseif ($add == 'addSelect') {
$element = $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
if (CRM_Core_BAO_Setting::isEnvironmentSet($setting)) {
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');
}
Expand Down
24 changes: 6 additions & 18 deletions tests/phpunit/CRM/Core/BAO/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,13 @@ public static function _testOnChange_onChangeExample($oldValue, $newValue, $meta
*
*/
public function testSetCivicrmEnvironment() {
CRM_Core_BAO_Setting::setItem(TRUE, CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'isProductionEnvironment');
$values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'isProductionEnvironment');
$this->assertEquals(TRUE, $values);
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);

global $civicrm_setting;
$civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['isProductionEnvironment'] = FALSE;
Civi::service('settings_manager')->useMandatory();
$values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'isProductionEnvironment');
$this->assertEquals(FALSE, $values);

// check that attempt to override value set in civicrm.settings.php raises error
try {
CRM_Core_BAO_Setting::setItem(TRUE, CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'isProductionEnvironment');
$this->fail("Missed expected exception");
}
catch (Exception $e) {
$this->assertEquals(ts('CiviCRM Environment already set in civicrm.settings.php!'), $e->getMessage());
}
unset($civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['isProductionEnvironment']);
define('CIVICRM_ENVIRONMENT', 'Development');
$environment = CRM_Core_Config::environment();
$this->assertEquals('Development', $environment);
}

}
9 changes: 3 additions & 6 deletions tests/phpunit/WebTest/Admin/Form/Setting/DebuggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function setUp() {
public function testSetCivicrmEnvironment() {
$this->webtestLogin();
$this->openCiviPage('admin/setting/debug', 'reset=1');
$this->click('xpath=//tr[@class="crm-debugging-form-block-isProductionEnvironment"]/td[2]/label[contains(text(), "No")]');
$this->select('environment', 'Staging');
$this->click('_qf_Debugging_next-top');

$this->waitForPageToLoad($this->getTimeoutMsec());
Expand All @@ -48,11 +48,8 @@ public function testSetCivicrmEnvironment() {
catch (PHPUnit_Framework_AssertionFailedError$e) {
array_push($this->verificationErrors, $e->toString());
}
global $civicrm_setting;
$civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['isProductionEnvironment'] = TRUE;
$this->openCiviPage('admin/setting/debug', 'reset=1');
$disabled = $this->getAttribute("xpath=//tr[@class='crm-debugging-form-block-isProductionEnvironment']/td[2]/input[1]@disabled");
$this->assertEquals(1, $disabled);
$envi = $this->getValue("environment");
$this->assertEquals('Staging', $envi);
}

}
23 changes: 5 additions & 18 deletions tests/phpunit/api/v3/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,34 +554,21 @@ public function testDefaults() {
*/
public function testSetCivicrmEnvironment() {
$params = array(
'isProductionEnvironment' => TRUE,
'environment' => 'Staging',
);
$result = $this->callAPISuccess('Setting', 'create', $params);
$params = array(
'name' => 'isProductionEnvironment',
'name' => 'environment',
'group' => 'Developer Preferences',
);
$result = $this->callAPISuccess('Setting', 'getvalue', $params);
$this->assertEquals(TRUE, $result);
$this->assertEquals('Staging', $result);

global $civicrm_setting;
$civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['isProductionEnvironment'] = FALSE;
$civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment'] = 'Production';
Civi::service('settings_manager')->useMandatory();
$result = $this->callAPISuccess('Setting', 'getvalue', $params);
$this->assertEquals(FALSE, $result);

// check that attempt to override value set in civicrm.settings.php raises error
$params = array(
'isProductionEnvironment' => TRUE,
);
$result = $this->callAPIFailure('Setting', 'create', $params);
$params = array(
'name' => 'isProductionEnvironment',
'group' => 'Developer Preferences',
);
$result = $this->callAPISuccess('Setting', 'getvalue', $params);
$this->assertEquals(FALSE, $result);
unset($civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['isProductionEnvironment']);
$this->assertEquals('Production', $result);
}

}

0 comments on commit a458416

Please sign in to comment.