Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #48 from JMAConsulting/generate_files_upgrade_phpu…
Browse files Browse the repository at this point in the history
…nit6

Update generated files and convert baseTest file to be format for php…
  • Loading branch information
monishdeb authored Feb 10, 2020
2 parents f7a37fc + 8c814c7 commit c84d3d8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 42 deletions.
107 changes: 90 additions & 17 deletions CRM/Lineitemedit/Upgrader/Base.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
use CRM_Lineitemedit_ExtensionUtil as E;

/**
* Base class which provides helpers to execute upgrade logic
*/
class CRM_Lineitemedit_Upgrader_Base {

/**
* @var varies, subclass of ttis
* @var varies, subclass of this
*/
static $instance;

Expand All @@ -32,15 +33,21 @@ class CRM_Lineitemedit_Upgrader_Base {
*/
private $revisions;

/**
* @var boolean
* Flag to clean up extension revision data in civicrm_setting
*/
private $revisionStorageIsDeprecated = FALSE;

/**
* Obtain a reference to the active upgrade handler.
*/
static public function instance() {
if (! self::$instance) {
if (!self::$instance) {
// FIXME auto-generate
self::$instance = new CRM_Lineitemedit_Upgrader(
'biz.jmaconsulting.lineitemedit',
realpath(__DIR__ .'/../../../')
realpath(__DIR__ . '/../../../')
);
}
return self::$instance;
Expand Down Expand Up @@ -91,7 +98,6 @@ public function executeCustomDataFile($relativePath) {
* @return bool
*/
protected static function executeCustomDataFileByAbsPath($xml_file) {
require_once 'CRM/Utils/Migrate/Import.php';
$import = new CRM_Utils_Migrate_Import();
$import->run($xml_file);
return TRUE;
Expand All @@ -107,7 +113,26 @@ protected static function executeCustomDataFileByAbsPath($xml_file) {
public function executeSqlFile($relativePath) {
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN,
$this->extensionDir . '/' . $relativePath
$this->extensionDir . DIRECTORY_SEPARATOR . $relativePath
);
return TRUE;
}

/**
* @param string $tplFile
* The SQL file path (relative to this extension's dir).
* Ex: "sql/mydata.mysql.tpl".
* @return bool
*/
public function executeSqlTemplate($tplFile) {
// Assign multilingual variable to Smarty.
$upgrade = new CRM_Upgrade_Form();

$tplFile = CRM_Utils_File::isAbsolute($tplFile) ? $tplFile : $this->extensionDir . DIRECTORY_SEPARATOR . $tplFile;
$smarty = CRM_Core_Smarty::singleton();
$smarty->assign('domainID', CRM_Core_Config::domainID());
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN, $smarty->fetch($tplFile), NULL, TRUE
);
return TRUE;
}
Expand All @@ -121,7 +146,7 @@ public function executeSqlFile($relativePath) {
*/
public function executeSql($query, $params = array()) {
// FIXME verify that we raise an exception on error
CRM_Core_DAO::executeSql($query, $params);
CRM_Core_DAO::executeQuery($query, $params);
return TRUE;
}

Expand Down Expand Up @@ -205,7 +230,7 @@ public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
* @return array(revisionNumbers) sorted numerically
*/
public function getRevisions() {
if (! is_array($this->revisions)) {
if (!is_array($this->revisions)) {
$this->revisions = array();

$clazz = new ReflectionClass(get_class($this));
Expand All @@ -222,31 +247,55 @@ public function getRevisions() {
}

public function getCurrentRevision() {
// return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
$revision = CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
if (!$revision) {
$revision = $this->getCurrentRevisionDeprecated();
}
return $revision;
}

private function getCurrentRevisionDeprecated() {
$key = $this->extensionName . ':version';
return CRM_Core_BAO_Setting::getItem('Extension', $key);
if ($revision = CRM_Core_BAO_Setting::getItem('Extension', $key)) {
$this->revisionStorageIsDeprecated = TRUE;
}
return $revision;
}

public function setCurrentRevision($revision) {
// We call this during hook_civicrm_install, but the underlying SQL
// UPDATE fails because the extension record hasn't been INSERTed yet.
// Instead, track revisions in our own namespace.
// CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision);

$key = $this->extensionName . ':version';
CRM_Core_BAO_Setting::setItem($revision, 'Extension', $key);
CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision);
// clean up legacy schema version store (CRM-19252)
$this->deleteDeprecatedRevision();
return TRUE;
}

private function deleteDeprecatedRevision() {
if ($this->revisionStorageIsDeprecated) {
$setting = new CRM_Core_BAO_Setting();
$setting->name = $this->extensionName . ':version';
$setting->delete();
CRM_Core_Error::debug_log_message("Migrated extension schema revision ID for {$this->extensionName} from civicrm_setting (deprecated) to civicrm_extension.\n");
}
}

// ******** Hook delegates ********

/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
public function onInstall() {
$files = glob($this->extensionDir . '/sql/*_install.sql');
if (is_array($files)) {
foreach ($files as $file) {
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
}
}
$files = glob($this->extensionDir . '/sql/*_install.mysql.tpl');
if (is_array($files)) {
foreach ($files as $file) {
$this->executeSqlTemplate($file);
}
}
$files = glob($this->extensionDir . '/xml/*_install.xml');
if (is_array($files)) {
foreach ($files as $file) {
Expand All @@ -256,13 +305,31 @@ public function onInstall() {
if (is_callable(array($this, 'install'))) {
$this->install();
}
}

/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
*/
public function onPostInstall() {
$revisions = $this->getRevisions();
if (!empty($revisions)) {
$this->setCurrentRevision(max($revisions));
}
if (is_callable(array($this, 'postInstall'))) {
$this->postInstall();
}
}

/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
*/
public function onUninstall() {
$files = glob($this->extensionDir . '/sql/*_uninstall.mysql.tpl');
if (is_array($files)) {
foreach ($files as $file) {
$this->executeSqlTemplate($file);
}
}
if (is_callable(array($this, 'uninstall'))) {
$this->uninstall();
}
Expand All @@ -272,16 +339,21 @@ public function onUninstall() {
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
}
}
$this->setCurrentRevision(NULL);
}

/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
public function onEnable() {
// stub for possible future use
if (is_callable(array($this, 'enable'))) {
$this->enable();
}
}

/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
*/
public function onDisable() {
// stub for possible future use
if (is_callable(array($this, 'disable'))) {
Expand All @@ -300,4 +372,5 @@ public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
default:
}
}

}
51 changes: 27 additions & 24 deletions lineitemedit.civix.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class CRM_Lineitemedit_ExtensionUtil {
* Translated text.
* @see ts
*/
public static function ts($text, $params = array()) {
public static function ts($text, $params = []) {
if (!array_key_exists('domain', $params)) {
$params['domain'] = array(self::LONG_NAME, NULL);
$params['domain'] = [self::LONG_NAME, NULL];
}
return ts($text, $params);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ function _lineitemedit_civix_civicrm_config(&$config = NULL) {
array_unshift($template->template_dir, $extDir);
}
else {
$template->template_dir = array($extDir, $template->template_dir);
$template->template_dir = [$extDir, $template->template_dir];
}

$include_path = $extRoot . PATH_SEPARATOR . get_include_path();
Expand Down Expand Up @@ -140,7 +140,7 @@ function _lineitemedit_civix_civicrm_install() {
function _lineitemedit_civix_civicrm_postInstall() {
_lineitemedit_civix_civicrm_config();
if ($upgrader = _lineitemedit_civix_upgrader()) {
if (is_callable(array($upgrader, 'onPostInstall'))) {
if (is_callable([$upgrader, 'onPostInstall'])) {
$upgrader->onPostInstall();
}
}
Expand All @@ -166,7 +166,7 @@ function _lineitemedit_civix_civicrm_uninstall() {
function _lineitemedit_civix_civicrm_enable() {
_lineitemedit_civix_civicrm_config();
if ($upgrader = _lineitemedit_civix_upgrader()) {
if (is_callable(array($upgrader, 'onEnable'))) {
if (is_callable([$upgrader, 'onEnable'])) {
$upgrader->onEnable();
}
}
Expand All @@ -181,7 +181,7 @@ function _lineitemedit_civix_civicrm_enable() {
function _lineitemedit_civix_civicrm_disable() {
_lineitemedit_civix_civicrm_config();
if ($upgrader = _lineitemedit_civix_upgrader()) {
if (is_callable(array($upgrader, 'onDisable'))) {
if (is_callable([$upgrader, 'onDisable'])) {
$upgrader->onDisable();
}
}
Expand Down Expand Up @@ -217,22 +217,23 @@ function _lineitemedit_civix_upgrader() {
}

/**
* Search directory tree for files which match a glob pattern
* Search directory tree for files which match a glob pattern.
*
* Note: Dot-directories (like "..", ".git", or ".svn") will be ignored.
* Note: In Civi 4.3+, delegate to CRM_Utils_File::findFiles()
*
* @param $dir string, base dir
* @param $pattern string, glob pattern, eg "*.txt"
* @param string $dir base dir
* @param string $pattern , glob pattern, eg "*.txt"
*
* @return array(string)
*/
function _lineitemedit_civix_find_files($dir, $pattern) {
if (is_callable(array('CRM_Utils_File', 'findFiles'))) {
if (is_callable(['CRM_Utils_File', 'findFiles'])) {
return CRM_Utils_File::findFiles($dir, $pattern);
}

$todos = array($dir);
$result = array();
$todos = [$dir];
$result = [];
while (!empty($todos)) {
$subdir = array_shift($todos);
foreach (_lineitemedit_civix_glob("$subdir/$pattern") as $match) {
Expand Down Expand Up @@ -296,14 +297,13 @@ function _lineitemedit_civix_civicrm_caseTypes(&$caseTypes) {
$name = preg_replace('/\.xml$/', '', basename($file));
if ($name != CRM_Case_XMLProcessor::mungeCaseType($name)) {
$errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor::mungeCaseType($name));
CRM_Core_Error::fatal($errorMessage);
// throw new CRM_Core_Exception($errorMessage);
throw new CRM_Core_Exception($errorMessage);
}
$caseTypes[$name] = array(
$caseTypes[$name] = [
'module' => E::LONG_NAME,
'name' => $name,
'file' => $file,
);
];
}
}

Expand Down Expand Up @@ -361,11 +361,12 @@ function _lineitemedit_civix_civicrm_themes(&$themes) {
*
* @link http://php.net/glob
* @param string $pattern
*
* @return array, possibly empty
*/
function _lineitemedit_civix_glob($pattern) {
$result = glob($pattern);
return is_array($result) ? $result : array();
return is_array($result) ? $result : [];
}

/**
Expand All @@ -376,16 +377,18 @@ function _lineitemedit_civix_glob($pattern) {
* 'Mailing', or 'Administer/System Settings'
* @param array $item - the item to insert (parent/child attributes will be
* filled for you)
*
* @return bool
*/
function _lineitemedit_civix_insert_navigation_menu(&$menu, $path, $item) {
// If we are done going down the path, insert menu
if (empty($path)) {
$menu[] = array(
'attributes' => array_merge(array(
$menu[] = [
'attributes' => array_merge([
'label' => CRM_Utils_Array::value('name', $item),
'active' => 1,
), $item),
);
], $item),
];
return TRUE;
}
else {
Expand All @@ -396,9 +399,9 @@ function _lineitemedit_civix_insert_navigation_menu(&$menu, $path, $item) {
foreach ($menu as $key => &$entry) {
if ($entry['attributes']['name'] == $first) {
if (!isset($entry['child'])) {
$entry['child'] = array();
$entry['child'] = [];
}
$found = _lineitemedit_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key);
$found = _lineitemedit_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item);
}
}
return $found;
Expand All @@ -409,7 +412,7 @@ function _lineitemedit_civix_insert_navigation_menu(&$menu, $path, $item) {
* (Delegated) Implements hook_civicrm_navigationMenu().
*/
function _lineitemedit_civix_navigationMenu(&$nodes) {
if (!is_callable(array('CRM_Core_BAO_Navigation', 'fixNavigationMenu'))) {
if (!is_callable(['CRM_Core_BAO_Navigation', 'fixNavigationMenu'])) {
_lineitemedit_civix_fixNavigationMenu($nodes);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Lineitemedit/Form/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @group headless
*/
class CRM_Lineitemedit_Form_BaseTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
class CRM_Lineitemedit_Form_BaseTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {

protected $_contactID;
protected $_contributionID;
Expand Down

0 comments on commit c84d3d8

Please sign in to comment.