Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dev/drupal#79) Fail more gracefully when upgrading on PHP 5.x #583

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion civicrm.info
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = 7.x-4.7
package = CiviCRM
core = 7.x
project = civicrm
php = 5.6
php = 7.0

files[] = civicrm.module
files[] = civicrm.install
Expand Down
18 changes: 15 additions & 3 deletions civicrm.module
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ require_once 'civicrm_user.inc';

define('CIVICRM_UF_HEAD', TRUE);

/**
* Minimum required PHP
*
* Note: This duplicates CRM_Upgrade_Form::MINIMUM_PHP_VERSION. The
* duplication helps avoid a dependency-loop. (Reading `Form::MINIMUM_PHP_VERSION`
* requires loading `civicrm.settings.php`, but that triggers a parse-error
* on PHP 5.x.)
*
* @see CRM_Upgrade_Form::MINIMUM_PHP_VERSION
* @see CiviDrupal\PhpVersionTest::testConstantMatch()
*/
define('CIVICRM_DRUPAL_PHP_MINIMUM', '7.0.0');

/**
* Adds CiviCRM CSS and JS resources into the header.
*/
Expand Down Expand Up @@ -149,9 +162,8 @@ function civicrm_page_build($page) {
*/
function civicrm_initialize() {
// Check for php version and ensure its greater than minPhpVersion
$minPhpVersion = '5.3.4';
if (version_compare(PHP_VERSION, $minPhpVersion) < 0) {
echo "CiviCRM requires PHP Version $minPhpVersion or greater. You are running PHP Version " . PHP_VERSION . "<p>";
if (version_compare(PHP_VERSION, CIVICRM_DRUPAL_PHP_MINIMUM) < 0) {
echo "CiviCRM requires PHP " . CIVICRM_DRUPAL_PHP_MINIMUM . "+. The web server is running PHP " . PHP_VERSION . ".<p>";
exit();
}
_civicrm_registerClassLoader();
Expand Down
7 changes: 7 additions & 0 deletions drush/civicrm.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,13 @@ function _civicrm_init($fail = TRUE, $load_config = TRUE) {
return $init;
}

if (!version_compare(phpversion(), CIVICRM_DRUPAL_PHP_MINIMUM, '>=')) {
return drush_set_error('CIVICRM_PHP_MINIMUM', dt('CiviCRM requires PHP @required+. Drush is running PHP @current.', [
'@current' => phpversion(),
'@required' => CIVICRM_DRUPAL_PHP_MINIMUM,
]));
}

global $cmsPath;
$cmsPath = $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
$site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT', FALSE);
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/CiviDrupal/PhpVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace CiviDrupal;

use Civi\Test\EndToEndInterface;

class PhpVersionTest extends \PHPUnit_Framework_TestCase implements EndToEndInterface {

/**
* CIVICRM_DRUPAL_PHP_MINIMUM (civicrm.module) should match MINIMUM_PHP_VERSION (CRM/Upgrade/Form.php).
*/
public function testConstantMatch() {
$constantFile = $this->getDrupalModulePath() . '/civicrm.module';
$this->assertFileExists($constantFile);
$content = file_get_contents($constantFile);
if (preg_match(";define\\('CIVICRM_DRUPAL_PHP_MINIMUM', '(.*)'\\);", $content, $m)) {
$this->assertEquals(\CRM_Upgrade_Form::MINIMUM_PHP_VERSION, $m[1]);
}
else {
$this->fail('Failed to find CIVICRM_DRUPAL_PHP_MINIMUM in ' . $constantFile);
}
}

/**
* "php" requirement (civicrm.info) should match MINIMUM_PHP_VERSION (CRM/Upgrade/Form.php).
*/
public function testInfoMatch() {
$infoFile = $this->getDrupalModulePath() . '/civicrm.info';
$this->assertFileExists($infoFile);
$info = drupal_parse_info_file($infoFile);
$expectMajorMinor = preg_replace(';^(\d+\.\d+)\..*$;', '\1', \CRM_Upgrade_Form::MINIMUM_PHP_VERSION);
$this->assertEquals($expectMajorMinor, $info['php']);
}

/**
* @return string
* Ex: '/var/www/sites/all/modules/civicrm/drupal'
*/
protected function getDrupalModulePath() {
return dirname(dirname(dirname(__DIR__)));
}

}