From 414e3596b0b790b76e83b0adac00b68fc664b798 Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Thu, 1 May 2014 18:04:02 +0000 Subject: [PATCH] CRM-14494 add system logger to handle payment notification & test CRM-14449 log legacy style ipns to system_log CRM-14449 logging request seems more consistent with others CRM-14449 stding message value in payment notification logs CRM-14449 log ipaddress on ipn calls CRM-14449 add fn exists as legacy non-boostrapped methods get to this point --- CRM/Core/Payment.php | 11 +++-- CRM/Utils/System.php | 19 ++++---- CRM/Utils/SystemLogger.php | 3 ++ extern/authorizeIPN.php | 2 + extern/googleNotify.php | 2 + extern/ipn.php | 4 +- extern/pxIPN.php | 3 +- .../CRM/Core/Payment/AuthorizeNetIPNTest.php | 0 .../phpunit/CRM/Core/PaymentTest.php | 46 ++++++++++--------- tests/phpunit/CiviTest/truncate.xml | 2 + xml/schema/Core/SystemLog.xml | 2 +- 11 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php rename CRM/Utils/System/SystemLogger.php => tests/phpunit/CRM/Core/PaymentTest.php (68%) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index a23447718e..762eb8a878 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -130,15 +130,16 @@ abstract class CRM_Core_Payment { * @return mixed */ public static function logPaymentNotification($params) { - $message = ''; + $message = 'payment_notification '; if (!empty($params['processor_name'])) { - $message = 'processor_name=' . $params['processor_name']; + $message .= 'processor_name=' . $params['processor_name']; } if (!empty($params['processor_id'])) { $message .= 'processor_id=' . $params['processor_id']; } - $log = CRM_Utils_SystemLogger(); - $log->log('alert', $message, $_REQUEST); + + $log = new CRM_Utils_SystemLogger(); + $log->alert($message, $_REQUEST); } /** @@ -182,7 +183,7 @@ abstract class CRM_Core_Payment { /** * This function checks to see if we have the right config values * - * @param string $mode the mode we are operating in (live or test) + * @internal param string $mode the mode we are operating in (live or test) * * @return string the error message if any * @public diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index a8eab723d2..2892e5602b 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -58,9 +58,9 @@ class CRM_Utils_System { * (optional) Whether to include the reset GET string (if present). * @param bool $includeForce * (optional) Whether to include the force GET string (if present). - * @param string $path + * @param string $path * (optional) The path to use for the new url. - * @param string $absolute + * @param string $absolute * (optional) Whether to return an absolute URL. * * @return string @@ -178,7 +178,7 @@ class CRM_Utils_System { * * @param string $content * The content that will be themed. - * @param bool $print + * @param bool $print * (optional) Are we displaying to the screen or bypassing theming? * @param bool $maintenance * (optional) For maintenance mode. @@ -720,7 +720,7 @@ class CRM_Utils_System { return substr_replace($number, $replace, 0, -$keep); } - /** + /** * Determine which PHP modules are loaded. * * @return array @@ -956,7 +956,7 @@ class CRM_Utils_System { * @param int $ver * The major version of PHP that is required. * @param bool $abort - * (optional) Whether to fatally abort if the version requirement is not + * (optional) Whether to fatally abort if the version requirement is not * met. Defaults to TRUE. * @return bool * Returns TRUE if the requirement is met, FALSE if the requirement is not @@ -1173,9 +1173,10 @@ class CRM_Utils_System { $address = CRM_Utils_Array::value('REMOTE_ADDR', $_SERVER); $config = CRM_Core_Config::singleton(); - if ($config->userSystem->is_drupal) { - //drupal function handles the server being behind a proxy securely - $address = ip_address(); + if ($config->userSystem->is_drupal && function_exists('ip_address')) { + //drupal function handles the server being behind a proxy securely. We still have legacy ipn methods + // that reach this point without bootstrapping hence the check that the fn exists + $address = ip_address(); } // hack for safari @@ -1731,7 +1732,7 @@ class CRM_Utils_System { // getPluginList() /** - * + * */ static function executeScheduledJobs() { $facility = new CRM_Core_JobManager(); diff --git a/CRM/Utils/SystemLogger.php b/CRM/Utils/SystemLogger.php index df2a25d752..6b9b680651 100644 --- a/CRM/Utils/SystemLogger.php +++ b/CRM/Utils/SystemLogger.php @@ -34,6 +34,9 @@ */ class CRM_Utils_SystemLogger extends Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterface { public function log($level, $message, array $context = array()) { + if(!isset($context['hostname'])) { + $context['hostname'] = CRM_Utils_System::ipAddress(); + } $rec = new CRM_Core_DAO_SystemLog(); $separateFields = array('contact_id', 'hostname'); foreach ($separateFields as $separateField) { diff --git a/extern/authorizeIPN.php b/extern/authorizeIPN.php index ed00ac1adf..d30007616c 100644 --- a/extern/authorizeIPN.php +++ b/extern/authorizeIPN.php @@ -35,6 +35,8 @@ session_start(); require_once '../civicrm.config.php'; $config = CRM_Core_Config::singleton(); +$log = new CRM_Utils_SystemLogger(); +$log->log('alert', 'payment_notification processor_name=AuthNet', $_REQUEST); $authorizeNetIPN = new CRM_Core_Payment_AuthorizeNetIPN(); $authorizeNetIPN->main(); diff --git a/extern/googleNotify.php b/extern/googleNotify.php index c1da8a6e37..1339ee0615 100644 --- a/extern/googleNotify.php +++ b/extern/googleNotify.php @@ -36,6 +36,8 @@ session_start(); require_once '../civicrm.config.php'; $config = CRM_Core_Config::singleton(); +$log = new CRM_Utils_SystemLogger(); +$log->log('alert', 'payment_notification processor_name=Google_Checkout', $_REQUEST); $rawPostData = file_get_contents('php://input'); CRM_Core_Payment_GoogleIPN::main($rawPostData); diff --git a/extern/ipn.php b/extern/ipn.php index 4ec25af6e6..9b1e468a62 100644 --- a/extern/ipn.php +++ b/extern/ipn.php @@ -38,11 +38,13 @@ require_once '../civicrm.config.php'; /* Cache the real UF, override it with the SOAP environment */ $config = CRM_Core_Config::singleton(); - +$log = new CRM_Utils_SystemLogger(); if (empty($_GET)) { + $log->log('alert', 'payment_notification processor_name=PayPal', $_REQUEST); $paypalIPN = new CRM_Core_Payment_PayPalProIPN($_REQUEST); } else { + $log->log('alert', 'payment_notification PayPal_Standard', $_REQUEST); $paypalIPN = new CRM_Core_Payment_PayPalIPN(); // @todo upgrade standard per Pro } diff --git a/extern/pxIPN.php b/extern/pxIPN.php index 2a85f2cf43..089d88dd7b 100644 --- a/extern/pxIPN.php +++ b/extern/pxIPN.php @@ -17,7 +17,8 @@ require_once '../civicrm.config.php'; require_once 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); - +$log = new CRM_Utils_SystemLogger(); +$log->log('alert', 'payment_notification processor_name=Payment_Express', $_REQUEST); /* * Get the password from the Payment Processor's table based on the DPS user id * being passed back from the server diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/CRM/Utils/System/SystemLogger.php b/tests/phpunit/CRM/Core/PaymentTest.php similarity index 68% rename from CRM/Utils/System/SystemLogger.php rename to tests/phpunit/CRM/Core/PaymentTest.php index 602cf62d33..816cf3ba4e 100644 --- a/CRM/Utils/System/SystemLogger.php +++ b/tests/phpunit/CRM/Core/PaymentTest.php @@ -25,28 +25,30 @@ +--------------------------------------------------------------------+ */ -/** - * - * @package CRM - * @copyright CiviCRM LLC (c) 2004-2014 - * $Id$ - * - */ -class CRM_Utils_SystemLogger implements \Psr\Log\LoggerInterface { - static public function log($level, $message, array $context = array()) { - echo 'k'; - $rec = new CRM_Core_DAO_SystemLog(); - $separateFields = array('contact_id', 'hostname'); - foreach ($separateFields as $separateField) { - if (isset($context[$separateField])) { - $rec->{$separateField} = $context[$separateField]; - unset($context[$separateField]); - } + +require_once 'CiviTest/CiviUnitTestCase.php'; + +class CRM_Core_PaymentTest extends CiviUnitTestCase { + function get_info() { + return array( + 'name' => 'Payment Test', + 'description' => 'Test Payment methods.', + 'group' => 'Payment Processor Tests', + ); + } + + /** + * test the payment method is adequately logged - we don't expect the processing to succeed + */ + function testHandlePaymentMethodLogging() { + $params = array('processor_name' => 'Paypal', 'data' => 'blah'); + try { + CRM_Core_Payment::handlePaymentMethod('method', $params); + } + catch (Exception $e) { + } - print_r($rec); - $rec->level = $level; - $rec->message = $message; - $rec->context = json_encode($context); - $rec->save(); + $log = $this->callAPISuccess('SystemLog', 'get', array()); + $this->assertEquals('payment_notification processor_name=Paypal', $log['values'][$log->id]['message']); } } diff --git a/tests/phpunit/CiviTest/truncate.xml b/tests/phpunit/CiviTest/truncate.xml index 64e4db6b86..e1656a1460 100644 --- a/tests/phpunit/CiviTest/truncate.xml +++ b/tests/phpunit/CiviTest/truncate.xml @@ -19,6 +19,8 @@ + + diff --git a/xml/schema/Core/SystemLog.xml b/xml/schema/Core/SystemLog.xml index 408337f913..e50a7e4d54 100644 --- a/xml/schema/Core/SystemLog.xml +++ b/xml/schema/Core/SystemLog.xml @@ -39,7 +39,7 @@ error level per PSR3 varchar 9 - info + 'info' 4.5 -- 2.25.1