From f7a37fc00506cc676056fa6987b5413b62603a97 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 13 Jan 2020 13:01:59 +1100 Subject: [PATCH 1/2] Fix Failing unit tests --- CRM/Lineitemedit/Form/Edit.php | 5 ++--- tests/phpunit/CRM/Lineitemedit/Form/CancelTest.php | 3 +-- tests/phpunit/CRM/Lineitemedit/Form/EditTest.php | 7 +++---- tests/phpunit/CRM/Lineitemedit/Form/SaleTax/CancelTest.php | 7 +++---- tests/phpunit/CRM/Lineitemedit/Form/SaleTax/EditTest.php | 7 +++---- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/CRM/Lineitemedit/Form/Edit.php b/CRM/Lineitemedit/Form/Edit.php index 0ff3407..718467d 100644 --- a/CRM/Lineitemedit/Form/Edit.php +++ b/CRM/Lineitemedit/Form/Edit.php @@ -175,7 +175,7 @@ public function submit($values, $isTest = FALSE) { ); $lineItem = CRM_Price_BAO_LineItem::create($params); - $lineItem = $lineItem->toArray(); + $lineItem = civicrm_api3('LineItem', 'getsingle', ['id' => $this->_id]); // calculate balance, tax and paidamount later used to adjust transaction $updatedAmount = CRM_Price_BAO_LineItem::getLineTotal($this->_lineitemInfo['contribution_id']); @@ -238,9 +238,8 @@ protected function updateEntityRecord($lineItem) { ->fetchAll(); foreach ($getUpdatedLineItems as $updatedLineItem) { $line[] = $updatedLineItem['label'] . ' - ' . (float) $updatedLineItem['qty']; - $lineTotal += $updatedLineItem['line_total'] + $updatedLineItem['tax_amount']; + $lineTotal += $updatedLineItem['line_total'] + (float) $updatedLineItem['tax_amount']; } - $params['fee_level'] = implode(', ', $line); $params['fee_amount'] = $lineTotal; civicrm_api3('Participant', 'create', $params); diff --git a/tests/phpunit/CRM/Lineitemedit/Form/CancelTest.php b/tests/phpunit/CRM/Lineitemedit/Form/CancelTest.php index dd2a0c5..03dbbd0 100644 --- a/tests/phpunit/CRM/Lineitemedit/Form/CancelTest.php +++ b/tests/phpunit/CRM/Lineitemedit/Form/CancelTest.php @@ -95,8 +95,7 @@ public function testLineitemCancelWithPriceSet() { $params = array( 'total_amount' => 100, 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), - 'receive_date' => '04/21/2015', - 'receive_date_time' => '11:27PM', + 'receive_date' => '2015-04-21 23:27:00', 'contact_id' => $contactID, 'price_set_id' => $this->_priceSetID, 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), diff --git a/tests/phpunit/CRM/Lineitemedit/Form/EditTest.php b/tests/phpunit/CRM/Lineitemedit/Form/EditTest.php index 1fbe279..c8af57f 100644 --- a/tests/phpunit/CRM/Lineitemedit/Form/EditTest.php +++ b/tests/phpunit/CRM/Lineitemedit/Form/EditTest.php @@ -230,8 +230,7 @@ public function testLineTotalChangeWithPriceSet() { $params = array( 'total_amount' => 100, 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), - 'receive_date' => '04/21/2015', - 'receive_date_time' => '11:27PM', + 'receive_date' => '2015-04-21 23:27:00', 'contact_id' => $contactID, 'price_set_id' => $this->_priceSetID, 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), @@ -309,8 +308,8 @@ public function testParticipantRecordOnLineItemEdit() { $form->setCustomDataTypes(); $form->submit(array( - 'register_date' => 'now', - 'register_date_time' => '00:00:00', + 'register_date' => date('Y-m-d H:i:s'), + 'receive_date' => date('Y-m-d H:i:s'), 'status_id' => 1, 'role_id' => 1, 'event_id' => $form->_eventId, diff --git a/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/CancelTest.php b/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/CancelTest.php index 07fac60..e255ff1 100644 --- a/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/CancelTest.php +++ b/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/CancelTest.php @@ -104,7 +104,7 @@ public function testLineitemCancel() { $expectedFinancialTrxnEntries = array( array( 'total_amount' => 110.00, - 'net_amount' => 100.00, // @TODO this is suppose to be 110 + 'net_amount' => 110.00, 'is_payment' => 1, 'payment_instrument_id' => $check, 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), @@ -131,8 +131,7 @@ public function testLineitemCancelWithPriceSet() { 'net_amount' => 330, 'tax_amount' => 30, 'financial_type_id' => $this->_financialTypeID, - 'receive_date' => '04/21/2015', - 'receive_date_time' => '11:27PM', + 'receive_date' => '2015-04-21 23:27:00', 'contact_id' => $contactID, 'price_set_id' => $this->_priceSetID, 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), @@ -215,7 +214,7 @@ public function testLineitemCancelWithPriceSet() { ); $this->checkArrayEqualsByAttributes($expectedFinancialItemEntries, $actualFinancialItemEntries); - $actualFinancialTrxnEntries = $this->getFinancialItemsByContributionID($contribution['id']); + $actualFinancialTrxnEntries = $this->getFinancialTrxnsByContributionID($contribution['id']); $expectedFinancialTrxnEntries = array( array( 'total_amount' => 330.00, diff --git a/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/EditTest.php b/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/EditTest.php index d718c18..1992ecc 100644 --- a/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/EditTest.php +++ b/tests/phpunit/CRM/Lineitemedit/Form/SaleTax/EditTest.php @@ -107,7 +107,7 @@ public function testLineTotalIncrease() { $expectedFinancialTrxnEntries = array( array( 'total_amount' => 110.00, - 'net_amount' => 100.00, // @TODO this is suppose to be 110 + 'net_amount' => 110.00, 'is_payment' => 1, 'payment_instrument_id' => $check, 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), @@ -185,7 +185,7 @@ public function testLineTotalDecrease() { $expectedFinancialTrxnEntries = array( array( 'total_amount' => 110.00, - 'net_amount' => 100.00, // @TODO this is suppose to be 110 + 'net_amount' => 110.00, 'is_payment' => 1, 'payment_instrument_id' => $check, 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), @@ -211,8 +211,7 @@ public function testLineTotalChangeWithPriceSet() { 'net_amount' => 110, 'tax_amount' => 10, 'financial_type_id' => $this->_financialTypeID, - 'receive_date' => '04/21/2015', - 'receive_date_time' => '11:27PM', + 'receive_date' => '2015-04-21 23:27:00', 'contact_id' => $contactID, 'price_set_id' => $this->_priceSetID, 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), From 8c814c7b8be48ab8c5fdf8f81d4b438a139c1d47 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 13 Jan 2020 17:31:13 +1100 Subject: [PATCH 2/2] Update generated files and convert baseTest file to be format for phpunit6 --- CRM/Lineitemedit/Upgrader/Base.php | 107 +++++++++++++++--- lineitemedit.civix.php | 51 +++++---- .../CRM/Lineitemedit/Form/BaseTest.php | 2 +- 3 files changed, 118 insertions(+), 42 deletions(-) diff --git a/CRM/Lineitemedit/Upgrader/Base.php b/CRM/Lineitemedit/Upgrader/Base.php index 1b79815..3f9a9c2 100644 --- a/CRM/Lineitemedit/Upgrader/Base.php +++ b/CRM/Lineitemedit/Upgrader/Base.php @@ -1,6 +1,7 @@ run($xml_file); return TRUE; @@ -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; } @@ -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; } @@ -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)); @@ -222,24 +247,42 @@ 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)) { @@ -247,6 +290,12 @@ public function onInstall() { 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) { @@ -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(); } @@ -272,9 +339,11 @@ 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'))) { @@ -282,6 +351,9 @@ public function onEnable() { } } + /** + * @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'))) { @@ -300,4 +372,5 @@ public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) { default: } } + } diff --git a/lineitemedit.civix.php b/lineitemedit.civix.php index 7f97119..cdd1337 100644 --- a/lineitemedit.civix.php +++ b/lineitemedit.civix.php @@ -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); } @@ -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(); @@ -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(); } } @@ -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(); } } @@ -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(); } } @@ -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) { @@ -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, - ); + ]; } } @@ -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 : []; } /** @@ -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 { @@ -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; @@ -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); } } diff --git a/tests/phpunit/CRM/Lineitemedit/Form/BaseTest.php b/tests/phpunit/CRM/Lineitemedit/Form/BaseTest.php index d64fbf5..50eff08 100644 --- a/tests/phpunit/CRM/Lineitemedit/Form/BaseTest.php +++ b/tests/phpunit/CRM/Lineitemedit/Form/BaseTest.php @@ -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;