CRM-14494 add system logger to handle payment notification & test
authoreileenmcnaughton <eileen@fuzion.co.nz>
Thu, 1 May 2014 18:04:02 +0000 (18:04 +0000)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 1 May 2014 22:37:41 +0000 (15:37 -0700)
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
CRM/Utils/System.php
CRM/Utils/SystemLogger.php
extern/authorizeIPN.php
extern/googleNotify.php
extern/ipn.php
extern/pxIPN.php
tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php [new file with mode: 0644]
tests/phpunit/CRM/Core/PaymentTest.php [moved from CRM/Utils/System/SystemLogger.php with 68% similarity]
tests/phpunit/CiviTest/truncate.xml
xml/schema/Core/SystemLog.xml

index a23447718e570cbdfacb35b1aee3a917f105becc..762eb8a878aa1b7079f6b66c9ada385ae4aca17f 100644 (file)
@@ -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
index a8eab723d218c906ae2e198c118fdb5c6549d9c2..2892e5602b691b89675d11df4941d9f62c5f51f0 100644 (file)
@@ -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();
index df2a25d752aebf2c9eb08219e3eae44bf43aaa70..6b9b68065151d9e03d001841196e5b10be96289e 100644 (file)
@@ -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) {
index ed00ac1adf1ed113065d88606fcfad517822b116..d30007616ceafb839ac0af2c6a9364519d6624ae 100644 (file)
@@ -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();
index c1da8a6e379de49487a5f5bb642787f677d9bc7a..1339ee06158706edd867ae3f691865646c5ceadc 100644 (file)
@@ -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);
index 4ec25af6e68f957015bf6e937f2b142dbcdab9d6..9b1e468a62061d8f335e414baa11a27f2c6958ba 100644 (file)
@@ -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
 }
index 2a85f2cf433c21f0759d9ce15aee93faa5b26e3d..089d88dd7b3813cb322092b0d45b11051a780a2a 100644 (file)
@@ -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 (file)
index 0000000..e69de29
similarity index 68%
rename from CRM/Utils/System/SystemLogger.php
rename to tests/phpunit/CRM/Core/PaymentTest.php
index 602cf62d3381f2c40e26753def67cea092ffaae7..816cf3ba4e6ccf31c6bbe976f71002918603a738 100644 (file)
  +--------------------------------------------------------------------+
 */
 
-/**
- *
- * @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']);
   }
 }
index 64e4db6b866bd04202821b6a2a49f12929bd65e4..e1656a14608d4854ea2dd61b9b237118ff0d743d 100644 (file)
@@ -19,6 +19,8 @@
 
   <civicrm_preferences_date />
 
+  <civicrm_system_log />
+
   <civicrm_worldregion />
 
   <civicrm_component />
index 408337f913274a63fcfa4fef7f67778d3be1256f..e50a7e4d543ccba625a08cf5521ebb178b25bb8e 100644 (file)
@@ -39,7 +39,7 @@
     <comment>error level per PSR3</comment>
     <type>varchar</type>
     <length>9</length>
-    <default>info</default>
+    <default>'info'</default>
     <add>4.5</add>
   </field>
   <field>