http://www.mediawiki.org/wiki/Special:Code/MediaWiki/96644

Revision: 96644
Author:   khorn
Date:     2011-09-09 02:47:34 +0000 (Fri, 09 Sep 2011)
Log Message:
-----------
Added more unit tests to DonationInterface.

Modified Paths:
--------------
    branches/fundraising/extensions/DonationInterface/donationinterface.php
    
branches/fundraising/extensions/DonationInterface/gateway_common/DonationData.php
    
branches/fundraising/extensions/DonationInterface/gateway_common/gateway.adapter.php
    
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php

Added Paths:
-----------
    branches/fundraising/extensions/DonationInterface/tests/DonationDataTest.php
    
branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php

Removed Paths:
-------------
    
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/newXMLDocument.xml
    
branches/fundraising/extensions/DonationInterface/tests/DonationInterfaceTest.php

Modified: 
branches/fundraising/extensions/DonationInterface/donationinterface.php
===================================================================
--- branches/fundraising/extensions/DonationInterface/donationinterface.php     
2011-09-09 02:13:14 UTC (rev 96643)
+++ branches/fundraising/extensions/DonationInterface/donationinterface.php     
2011-09-09 02:47:34 UTC (rev 96644)
@@ -21,6 +21,7 @@
 //This is going to be a little funky. 
 //Override this in LocalSettings.php BEFORE you include this file, if you want 
 //to disable gateways.
+//TODO: Unfunktify, if you have a better idea here for auto-loading the 
classes after LocalSettings.php runs all the way. 
 if ( !isset( $wgDonationInterfaceEnabledGateways ) ) {
        $wgDonationInterfaceEnabledGateways = array(
                'paypal',
@@ -70,3 +71,12 @@
  */
 //$wgHooks['DonationInterface_Value'][] = 'pfpGatewayValue';
 //$wgHooks['DonationInterface_Page'][] = 'pfpGatewayPage';
+
+# Unit tests
+$wgHooks['UnitTestsList'][] = 'efDonationInterfaceUnitTests';
+
+function efDonationInterfaceUnitTests( &$files ) {
+       $files[] = dirname( __FILE__ ) . '/tests/GatewayAdapterTest.php';
+       $files[] = dirname( __FILE__ ) . '/tests/DonationDataTest.php';
+       return true;
+}

Modified: 
branches/fundraising/extensions/DonationInterface/gateway_common/DonationData.php
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/gateway_common/DonationData.php
   2011-09-09 02:13:14 UTC (rev 96643)
+++ 
branches/fundraising/extensions/DonationInterface/gateway_common/DonationData.php
   2011-09-09 02:47:34 UTC (rev 96644)
@@ -11,12 +11,12 @@
        
        public $boss;
        
-       function __construct($owning_class){
+       function __construct($owning_class, $test = false, $testdata = false){
                //TODO: Actually think about this bit.
                // ...and keep in mind we can re-populate if it's a test or 
whatever. (But that may not be a good idea either)
                //maybe we should just explicitly pass in where we get the data 
from. (Test, post, API...)
                $this->boss = $owning_class;
-               $this->populateData();
+               $this->populateData($test, $testdata);
        }
        
        function populateData($test = false, $testdata = false){
@@ -24,11 +24,10 @@
                //TODO: Uh, the API should probably be able to get in this far, 
too... and have its own populate function. 
                //Maybe check for the boss class...
                if ($test){
-                       $this->populateData_Test($testdata = false);
+                       $this->populateData_Test($testdata);
                } else {
                        $this->populateData_Form();
                }
-               //TODO: Unit test the goodness out of this thing. 
                $this->doCacheStuff();
                
                
@@ -43,7 +42,6 @@
                        $this->saveContributionTracking();
                }
                
-               self::log("FORM DATA OBJECT: Data has been populated! " . 
print_r($this->normalized, true));
        }
        
        function getData(){
@@ -75,7 +73,7 @@
                        global $wgRequest; //TODO: ARRRGHARGHARGH. That is all.
 
                        $this->normalized = array(
-                               'amount' => ( $amount != "0.00" ) ? $amount : 
"35",
+                               'amount' => "35",
                                'amountOther' => '',
                                'email' => '[email protected]',
                                'fname' => 'Tester',
@@ -101,9 +99,9 @@
                                'cvv' => '001',
                                'currency' => 'USD',
                                'payment_method' => $wgRequest->getText( 
'payment_method' ),
-                               'order_id' => $order_id,
-                               'i_order_id' => $i_order_id,
-                               'numAttempt' => $numAttempt,
+                               'order_id' => '1234567890',
+                               'i_order_id' => '1234567890',
+                               'numAttempt' => 0,
                                'referrer' => 
'http://www.baz.test.com/index.php?action=foo&action=bar',
                                'utm_source' => self::getUtmSource(),
                                'utm_medium' => $wgRequest->getText( 
'utm_medium' ),
@@ -113,13 +111,13 @@
                                'comment' => $wgRequest->getText( 'comment' ),
                                'email-opt' => $wgRequest->getText( 'email-opt' 
),
                                'test_string' => $wgRequest->getText( 'process' 
),
-                               'token' => $token,
+                               'token' => '',
                                'contribution_tracking_id' => 
$wgRequest->getText( 'contribution_tracking_id' ),
                                'data_hash' => $wgRequest->getText( 'data_hash' 
),
                                'action' => $wgRequest->getText( 'action' ),
                                'gateway' => 'payflowpro',
                                'owa_session' => $wgRequest->getText( 
'owa_session', null ),
-                               'owa_ref' => $owa_ref,
+                               'owa_ref' => 'http://localhost/defaultTestData',
                        );
                }
        }
@@ -214,18 +212,6 @@
        }
        
        function setNormalizedAmount(){
-//             // find out if amount was a radio button or textbox, set amount
-//             if ( isset( $_REQUEST['amount'] ) && preg_match( 
'/^\d+(\.(\d+)?)?$/', $wgRequest->getText( 'amount' ) ) ) {
-//                     $amount = $wgRequest->getText( 'amount' );
-//             } elseif ( isset( $_REQUEST['amountGiven'] ) && preg_match( 
'/^\d+(\.(\d+)?)?$/', $wgRequest->getText( 'amountGiven' ) ) ) {
-//                     $amount = number_format( $wgRequest->getText( 
'amountGiven' ), 2, '.', '' );
-//             } elseif ( isset( $_REQUEST['amount'] ) ) {
-//                     $amount = '0.00';
-//             } elseif ( $wgRequest->getText( 'amount' ) == '-1' ) {
-//                     $amount = $wgRequest->getText( 'amountOther' );
-//             } else {
-//                     $amount = '0.00';
-//             }
                
                if (!($this->isSomething('amount')) || !(preg_match( 
'/^\d+(\.(\d+)?)?$/', $this->getVal('amount') ) )){
                        if ($this->isSomething('amountGiven') && preg_match( 
'/^\d+(\.(\d+)?)?$/', $this->getVal('amountGiven') ) ){
@@ -255,13 +241,7 @@
                
                if (!$this->isSomething('i_order_id')){ 
                        $this->setVal('i_order_id', $this->generateOrderId());
-               }
-               
-               //TODO: Take this out when you know it's okay. 
-               //In fact: TODO: Clean up the logs all over. 
-               
-               $this->log("Internal Order ID = " . 
$this->getVal('i_order_id'));
-               
+               }               
        }
        
        /**
@@ -318,12 +298,14 @@
 
                        // if we have squid caching enabled, set the maxage
                        global $wgUseSquid, $wgOut;
-                       $g = $this->boss; //the 'g' is for "Gateway"!
-                       $maxAge = $g::getGlobal('SMaxAge');
+                       if (class_exists($this->boss)){
+                               $g = $this->boss; //the 'g' is for "Gateway"!
+                               $maxAge = $g::getGlobal('SMaxAge');
 
-                       if ( $wgUseSquid ) {
-                               self::log( 
$this->getAnnoyingOrderIDLogLinePrefix() . ' Setting s-max-age: ' . $maxAge, 
LOG_DEBUG );
-                               $wgOut->setSquidMaxage( $maxAge );      
+                               if ( $wgUseSquid ) {
+                                       self::log( 
$this->getAnnoyingOrderIDLogLinePrefix() . ' Setting s-max-age: ' . $maxAge, 
LOG_DEBUG );
+                                       $wgOut->setSquidMaxage( $maxAge );      
+                               }
                        }
                } else {
                        $this->cache = false; //TODO: Kill this one in the 
face, too. (see above) 
@@ -354,8 +336,12 @@
                // make sure we have a session open for tracking a 
CSRF-prevention token
                self::ensureSession();
                
-               $g = $this->boss;
-               $gateway_ident = $g::getIdentifier();
+               if (class_exists($this->boss)){
+                       $g = $this->boss;
+                       $gateway_ident = $g::getIdentifier();
+               } else {
+                       $gateway_ident = 'DonationData';
+               }
 
                if ( !isset( $_SESSION[ $gateway_ident . 'EditToken' ] ) ) {
                        // generate unsalted token to place in the session
@@ -404,8 +390,12 @@
         * Unset the payflow edit token from a user's session
         */
        function unsetEditToken() {
-               $g = $this->boss;
-               $gateway_ident = $g::getIdentifier();
+               if (class_exists($this->boss)){
+                       $g = $this->boss;
+                       $gateway_ident = $g::getIdentifier();
+               } else {
+                       $gateway_ident = "DonationData";
+               }
                unset( $_SESSION[ $gateway_ident . 'EditToken' ] );
        }
 
@@ -427,10 +417,13 @@
                static $match = null;
                
                if ($match === null) {
+                       if (class_exists($this->boss)){
+                               $g = $this->boss;
+                               $salt = $g::getGlobal('Salt');
+                       } else {
+                               $salt = 'gotToBeInAUnitTest';
+                       }
                        
-                       $g = $this->boss;
-                       $salt = $g::getGlobal('Salt');
-                       
                        // establish the edit token to prevent csrf
                        $token = $this->getEditToken( $salt );
                        
@@ -452,10 +445,8 @@
        function wasPosted(){
                //TODO: Get rid of these log statements. 
                if ( $this->isSomething('posted') ){
-                       $this->log("Posted = '" . $this->getVal('posted') . 
"'");
                        return true;
                }
-               $this->log("We were NOT Posted.");
                return false;
        }
 

Modified: 
branches/fundraising/extensions/DonationInterface/gateway_common/gateway.adapter.php
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/gateway_common/gateway.adapter.php
        2011-09-09 02:13:14 UTC (rev 96643)
+++ 
branches/fundraising/extensions/DonationInterface/gateway_common/gateway.adapter.php
        2011-09-09 02:47:34 UTC (rev 96644)
@@ -94,15 +94,6 @@
        const globalprefix = 'wgDonationGateway'; //...for example. 
        
        function __construct(){
-
-               $dir = dirname( __FILE__ ) . '/';
-               require_once( $dir . '../gateway_common/DonationData.php' );
-               $this->dataObj = new DonationData(get_called_class());
-               
-               $this->postdata = $this->dataObj->getData();
-               //TODO: Fix this a bit. 
-               $this->posted = $this->dataObj->wasPosted();
-               
                global $wgDonationInterfaceTest; //this is so the forms can see 
it. 
                //TODO: Alter the forms so they don't need the global?
                if ( !self::getGlobal('Test') ) {
@@ -112,7 +103,15 @@
                        $this->url = self::getGlobal('TestingURL');
                        $wgDonationInterfaceTest = true;
                }
+
+               $dir = dirname( __FILE__ ) . '/';
+               require_once( $dir . '../gateway_common/DonationData.php' );
+               $this->dataObj = new DonationData(get_called_class(), 
$wgDonationInterfaceTest);
                
+               $this->postdata = $this->dataObj->getData();
+               //TODO: Fix this a bit. 
+               $this->posted = $this->dataObj->wasPosted();
+               
                $this->setPostDefaults();
                $this->defineTransactions();
                $this->defineVarMap();
@@ -480,6 +479,7 @@
                $c = get_called_class();
                return $c::globalprefix;
        }
+       
        static function getIdentifier() {
                $c = get_called_class();
                return $c::identifier;

Modified: 
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php
   2011-09-09 02:13:14 UTC (rev 96643)
+++ 
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php
   2011-09-09 02:47:34 UTC (rev 96644)
@@ -157,7 +157,6 @@
                                }
                        }
                        $errors[$code] = $message;
-                       self::log( "ON NOES! ERRORS: " . print_r($errors, true) 
);
                }
                return $errors;
        }

Deleted: 
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/newXMLDocument.xml
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/newXMLDocument.xml
  2011-09-09 02:13:14 UTC (rev 96643)
+++ 
branches/fundraising/extensions/DonationInterface/globalcollect_gateway/newXMLDocument.xml
  2011-09-09 02:47:34 UTC (rev 96644)
@@ -1,13 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<!--
-    Document   : newXMLDocument.xml
-    Created on : August 29, 2011, 12:35 PM
-    Author     : khorn
-    Description:
-        Purpose of the document follows.
--->
-
-<root>
-
-</root>

Added: 
branches/fundraising/extensions/DonationInterface/tests/DonationDataTest.php
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/tests/DonationDataTest.php    
                            (rev 0)
+++ 
branches/fundraising/extensions/DonationInterface/tests/DonationDataTest.php    
    2011-09-09 02:47:34 UTC (rev 96644)
@@ -0,0 +1,275 @@
+<?php
+
+/**
+ * @group Fundraising
+ * @group Splunge
+ * @group Gateways
+ * @author Katie Horn <[email protected]>
+ */
+$dir = dirname( __FILE__ ) . '/';
+require_once( $dir . '../gateway_common/DonationData.php' );
+class DonationDataTest extends MediaWikiTestCase {
+       
+       function __construct(){
+               parent::__construct();
+               $this->testData = array(
+                       'amount' => '128',
+                       'email' => '[email protected]',
+                       'fname' => 'Testocres',
+                       'mname' => 'S.',
+                       'lname' => 'McTestingyou',
+                       'street' => '123 Fake Street',
+                       'city' => 'Springfield',
+                       'state' => 'US',
+                       'zip' => '99999',
+                       'country' => 'US',
+                       'fname2' => 'Testor',
+                       'lname2' => 'Testeyton',
+                       'street2' => '123 W. Grand Ave.',
+                       'city2' => 'Oakland',
+                       'state2' => 'CA',
+                       'zip2' => '94607',
+                       'country2' => 'US',
+                       'size' => 'Big mcLargeHuge',
+                       'premium_language' => 'fr',
+                       'card_num' => '42',
+                       'card' => 'visa',
+                       'expiration' => '1138',
+                       'cvv' => '665',
+                       'currency' => 'USD',
+                       'payment_method' => '',
+                       'i_order_id' => '1234567890',
+                       'numAttempt' => '5',
+                       'referrer' => 'http://www.testing.com/',
+                       'utm_source' => '..dd',
+                       'utm_medium' => 'large',
+                       'utm_campaign' => 'yes',
+                       'comment-option' => '',
+                       'comment' => 'My hovercraft is full of eels',
+                       'email-opt' => '',
+                       'test_string' => '',
+                       'token' => '113811',
+                       'contribution_tracking_id' => '',
+                       'data_hash' => '',
+                       'action' => '',
+                       'gateway' => 'chainlink',
+                       'owa_session' => '',
+                       'owa_ref' => 'http://localhost/importedTestData',
+               );
+               
+       }
+       
+       
+       function testConstruct(){
+               $ddObj = new DonationData(''); //as if we were posted.
+               $returned = $ddObj->getData();
+               $expected = array(  'posted' => '',
+                       'amount' => '0.00',
+                       'email' => '',
+                       'fname' => '',
+                       'mname' => '',
+                       'lname' => '',
+                       'street' => '',
+                       'city' => '',
+                       'state' => '',
+                       'zip' => '',
+                       'country' => '',
+                       'fname2' => '',
+                       'lname2' => '',
+                       'street2' => '',
+                       'city2' => '',
+                       'state2' => '',
+                       'zip2' => '',
+                       'country2' => '',
+                       'size' => '',
+                       'premium_language' => 'en',
+                       'card_num' => '',
+                       'card' => '',
+                       'expiration' => '',
+                       'cvv' => '',
+                       'currency' => '',
+                       'payment_method' => '',
+                       'numAttempt' => '0',
+                       'referrer' => '',
+                       'utm_source' => '..cc',
+                       'utm_medium' => '',
+                       'utm_campaign' => '',
+                       'language' => '',
+                       'comment-option' => '',
+                       'comment' => '',
+                       'email-opt' => '',
+                       'test_string' => '',
+                       '_cache_' => '',
+                       'token' => '',
+                       'contribution_tracking_id' => '',
+                       'data_hash' => '',
+                       'action' => '',
+                       'gateway' => '',
+                       'owa_session' => '',
+                       'owa_ref' => '',
+               );
+               unset($returned['order_id']);
+               unset($returned['i_order_id']);
+               $this->assertEquals($returned, $expected, "Staged post data 
does not match expected (largely empty).");
+       }
+       
+       function testConstructAsTest(){
+               $ddObj = new DonationData('', true); //test mode from the 
start, no data
+               $returned = $ddObj->getData();
+               $expected = array(
+                       'amount' => '35',
+                       'email' => '[email protected]',
+                       'fname' => 'Tester',
+                       'mname' => 'T.',
+                       'lname' => 'Testington',
+                       'street' => '548 Market St.',
+                       'city' => 'San Francisco',
+                       'state' => 'CA',
+                       'zip' => '94104',
+                       'country' => 'US',
+                       'fname2' => 'Testy',
+                       'lname2' => 'Testerson',
+                       'street2' => '123 Telegraph Ave.',
+                       'city2' => 'Berkeley',
+                       'state2' => 'CA',
+                       'zip2' => '94703',
+                       'country2' => 'US',
+                       'size' => 'small',
+                       'premium_language' => 'es',
+                       'card_num' => '378282246310005',
+                       'card' => 'american',
+                       'expiration' => '1012',
+                       'cvv' => '001',
+                       'currency' => 'USD',
+                       'payment_method' => '',
+                       'i_order_id' => '1234567890',
+                       'numAttempt' => '0',
+                       'referrer' => 
'http://www.baz.test.com/index.php?action=foo&amp;action=bar',
+                       'utm_source' => '..cc',
+                       'utm_medium' => '',
+                       'utm_campaign' => '',
+                       'language' => 'en',
+                       'comment-option' => '',
+                       'comment' => '',
+                       'email-opt' => '',
+                       'test_string' => '',
+                       'token' => '',
+                       'contribution_tracking_id' => '',
+                       'data_hash' => '',
+                       'action' => '',
+                       'gateway' => 'payflowpro',
+                       'owa_session' => '',
+                       'owa_ref' => 'http://localhost/defaultTestData',
+               );
+               unset($returned['order_id']);
+               
+               $this->assertEquals($expected, $returned, "Staged default test 
data does not match expected.");
+       }
+       
+       function testRepopulate(){
+               $expected = $this->testData;
+               //just unset a handfull... doesn't matter what, really. 
+               unset($expected['comment-option']);
+               unset($expected['email-opt']);
+               unset($expected['test_string']);
+                               
+               $ddObj = new DonationData('');
+               $ddObj->populateData(true, $expected); //change to test mode 
with explicit test data
+               $returned = $ddObj->getData();
+               //unset these, because they're always new
+               unset($returned['order_id']);
+               unset($expected['order_id']);
+               $this->assertEquals($returned, $expected, "The forced test data 
did not populate as expected.");
+       }
+       
+       function testIsSomething(){
+               $data = $this->testData;
+               unset($data['zip']);
+               $ddObj = new DonationData('', true, $data); 
+               $this->assertEquals($ddObj->isSomething('zip'), false, "Zip 
should currently be nothing.");
+               $this->assertEquals($ddObj->isSomething('lname'), true, "Lname 
should currently be something.");
+       }
+       
+       function testGetVal(){
+               $data = $this->testData;
+               unset($data['zip']);
+               $ddObj = new DonationData('', true, $data);
+               $this->assertEquals($ddObj->getVal('zip'), null, "Zip should 
currently be nothing.");
+               $this->assertEquals($ddObj->getVal('lname'), 'McTestingyou', 
"Lname should currently be 'McTestingyou'.");
+       }
+       
+       function testSetNormalizedAmount_amtGiven() {
+               $data = $this->testData;
+               $data['amount'] = 'this is not a number';
+               $data['amountGiven'] = 42.50;
+               //unset($data['zip']);
+               $ddObj = new DonationData('', true, $data);
+               $returned = $ddObj->getData();
+               $this->assertEquals($returned['amount'], '42.50', "Amount was 
not properly reset");
+               $this->assertTrue(!(array_key_exists('amountGiven', 
$returned)), "amountGiven should have been removed from the data");
+       }
+       
+       function testSetNormalizedAmount_amount() {
+               $data = $this->testData;
+               $data['amount'] = 88.15;
+               $data['amountGiven'] = 42.50;
+               //unset($data['zip']);
+               $ddObj = new DonationData('', true, $data);
+               $returned = $ddObj->getData();
+               $this->assertEquals($returned['amount'], 88.15, "Amount was not 
properly reset");
+               $this->assertTrue(!(array_key_exists('amountGiven', 
$returned)), "amountGiven should have been removed from the data");
+       }
+       
+       function testSetNormalizedAmount_neagtiveAmount() {
+               $data = $this->testData;
+               $data['amount'] = -1;
+               $data['amountOther'] = 3.25;
+               //unset($data['zip']);
+               $ddObj = new DonationData('', true, $data);
+               $returned = $ddObj->getData();
+               $this->assertEquals($returned['amount'], 3.25, "Amount was not 
properly reset");
+               $this->assertTrue(!(array_key_exists('amountOther', 
$returned)), "amountOther should have been removed from the data");
+       }
+       
+       function testSetNormalizedAmount_noGoodAmount() {
+               $data = $this->testData;
+               $data['amount'] = 'splunge';
+               $data['amountGiven'] = 'wombat';
+               $data['amountOther'] = 'macedonia';
+               //unset($data['zip']);
+               $ddObj = new DonationData('', true, $data);
+               $returned = $ddObj->getData();
+               $this->assertEquals($returned['amount'], 0.00, "Amount was not 
properly reset");
+               $this->assertTrue(!(array_key_exists('amountOther', 
$returned)), "amountOther should have been removed from the data");
+               $this->assertTrue(!(array_key_exists('amountGiven', 
$returned)), "amountGiven should have been removed from the data");
+       }
+
+       /*
+        * TODO: Make sure ALL these functions in DonationData are tested, 
either directly or through a calling function. 
+        * I know that's more regression-ish, but I stand by it. :p
+       function isCache(){
+       function setOwaRefId(){
+       function setNormalizedOrderIDs(){
+       function generateOrderId() {
+       public function sanitizeInput( &$value, $key, $flags=ENT_COMPAT, 
$double_encode=false ) {
+       function setGateway(){
+       function doCacheStuff(){
+       function getAnnoyingOrderIDLogLinePrefix(){
+       public function getEditToken( $salt = '' ) {
+       public static function generateToken( $salt = '' ) {
+       function matchEditToken( $val, $salt = '' ) {
+       function unsetEditToken() {
+       public static function ensureSession() {
+       public function checkTokens() {
+       function wasPosted(){
+       public static function getUtmSource( $utm_source = null, $utm_source_id 
= null ) {
+       public function getOptOuts() {
+       public function getCleanTrackingData( $clean_optouts = false ) {
+       function saveContributionTracking() {
+       public static function insertContributionTracking( $tracking_data ) {
+       public function updateContributionTracking( $force = false ) {
+       
+       */
+}
+
+?>


Property changes on: 
branches/fundraising/extensions/DonationInterface/tests/DonationDataTest.php
___________________________________________________________________
Added: svn:eol-style
   + native

Deleted: 
branches/fundraising/extensions/DonationInterface/tests/DonationInterfaceTest.php
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/tests/DonationInterfaceTest.php
   2011-09-09 02:13:14 UTC (rev 96643)
+++ 
branches/fundraising/extensions/DonationInterface/tests/DonationInterfaceTest.php
   2011-09-09 02:47:34 UTC (rev 96644)
@@ -1,233 +0,0 @@
-<?php
-
-/**
- * TODO: Something. 
- * Something roughly test-shaped. Here.
- * ...to be more precise: Test that ALL the gateway adapters (Yes: All two of 
them)
- * are building the XML we think they are, and that they can process sample 
- * return XML the way we think they should. 
- * 
- * TODO: Then, write all the other tests as well. :|
- * 
- * @group Fundraising
- * @group Splunge
- * @group Gateways
- * @author Katie Horn <[email protected]>
- */
-class DonationInterfaceTest extends MediaWikiTestCase {
-
-       function testbuildRequestXML() {
-               $gateway = new TestAdapter();
-               $gateway->publicCurrentTransaction( 'Test1' );
-               $built = $gateway->buildRequestXML();
-               $expected = '<?xml version="1.0"?>' . "\n";
-               $expected .= 
'<XML><REQUEST><ACTION>Donate</ACTION><ACCOUNT><MERCHANTID>128</MERCHANTID><PASSWORD>k4ftw</PASSWORD><VERSION>3.2</VERSION><RETURNURL>http://localhost/index.php/Donate-thanks/en</RETURNURL></ACCOUNT><DONATION><DONOR>
 
</DONOR><AMOUNT>0</AMOUNT><CURRENCYCODE>USD</CURRENCYCODE><LANGUAGECODE>en</LANGUAGECODE><COUNTRYCODE>US</COUNTRYCODE></DONATION></REQUEST></XML>'
 . "\n";
-               $this->assertEquals($built, $expected, "The constructed XML for 
transaction type Test1 does not match our expected.");
-               
-       }
-       
-       function testParseResponseXML() {
-               $gateway = new TestAdapter();
-               $returned = $gateway->do_transaction( 'Test2' );
-               
-               $expected_errors = array(
-                       128 => "Your shoe's untied...",
-                       45 => "Low clearance!"
-               );
-               
-               $expected_data = array(
-                       'thing' => 'stuff',
-                       'otherthing' => 12,
-               );
-               
-               $this->assertEquals($returned['status'], true, "Status should 
be true at this point.");
-               
-               $this->assertEquals($returned['errors'], $expected_errors, 
"Expected errors were not found.");
-               
-               $this->assertEquals($returned['data'], $expected_data, 
"Expected data was not found.");
-               
-               $this->assertEquals($returned['message'], "Test2 Transaction 
Successful!", "Expected message was not returned.");
-                               
-       }
-
-}
-
-$dir = dirname( __FILE__ ) . '/';
-require_once( $dir . '../gateway_common/gateway.adapter.php' );
-
-class TestAdapter extends GatewayAdapter {
-
-       const gatewayname = 'Test Gateway';
-       const identifier = 'testgateway';
-       const communicationtype = 'xml';
-       const globalprefix = 'wgTestAdapterGateway';
-
-       function stageData(){
-               $this->postdata['amount'] = $this->postdata['amount'] * 1000;
-               $this->postdata['name'] = $this->postdata['fname'] . " " . 
$this->postdata['lname'];
-       }
-       
-       function __construct( ) {
-               global $wgTestAdapterGatewayTestVar, 
$wgTestAdapterGatewayUseSyslog;
-               $wgTestAdapterGatewayTestVar = "Hi there!";
-               $wgTestAdapterGatewayUseSyslog = true;
-               $this->classlocation = __FILE__;
-               parent::__construct();
-               
-       }
-       
-       function defineAccountInfo(){
-               $this->accountInfo = array(
-                       'MERCHANTID' => '128',
-                       'PASSWORD' => 'k4ftw',
-                       //'IPADDRESS' => '', //TODO: Not sure if this should be 
OUR ip, or the user's ip. Hurm. 
-                       'VERSION' => "3.2",
-               );
-       }
-       
-       function defineVarMap(){
-               $this->var_map = array(
-                       'DONOR' => 'name',
-                       'AMOUNT' => 'amount',
-                       'CURRENCYCODE' => 'currency',
-                       'LANGUAGECODE' => 'language',
-                       'COUNTRYCODE' => 'country',
-                       'OID' => 'order_id',
-                       'RETURNURL' => 'returnto', //TODO: Fund out where the 
returnto URL is supposed to be coming from. 
-               );
-       }
-       
-       function defineReturnValueMap(){
-               $this->return_value_map = array(
-                       'AOK' => true,
-                       'WRONG' => false,
-               );
-       }
-       
-       function defineTransactions(){
-               $this->transactions = array();
-               
-               $this->transactions['Test1'] = array(
-                       'request' => array(
-                               'REQUEST' => array(
-                                       'ACTION',
-                                       'ACCOUNT' => array(
-                                               'MERCHANTID',
-                                               'PASSWORD',
-                                               'VERSION',
-                                               'RETURNURL',
-                                       ),
-                                       'DONATION' => array(
-                                               'DONOR',
-                                               'AMOUNT',
-                                               'CURRENCYCODE',
-                                               'LANGUAGECODE',
-                                               'COUNTRYCODE',
-                                               //'OID', //move this to another 
test. It's different every time, dorkus.
-                                       )
-                               )
-                       ),
-                       'values' => array(
-                               'ACTION' => 'Donate',
-                       ),
-               );
-               
-               $this->transactions['Test2'] = array(
-                       'request' => array(
-                               'REQUEST' => array(
-                                       'ACTION',
-                               )
-                       ),
-                       'values' => array(
-                               'ACTION' => 'Donate2',
-                       ),
-               );
-               self::log(print_r($this->transactions, true));
-       }
-
-       /**
-        * Take the entire response string, and strip everything we don't care 
about.
-        * For instance: If it's XML, we only want correctly-formatted XML. 
Headers must be killed off. 
-        * return a string.
-        */
-       function getFormattedResponse( $rawResponse ){
-               $xmlString = $this->stripXMLResponseHeaders($rawResponse);
-               $displayXML = $this->formatXmlString( $xmlString );
-               $realXML = new DomDocument( '1.0' );
-               self::log( "Here is the Raw XML: " . $displayXML ); //I am 
apparently a huge fibber.
-               $realXML->loadXML( trim( $xmlString ) );
-               return $realXML;
-       }
-       
-       /**
-        * Parse the response to get the status. Not sure if this should return 
a bool, or something more... telling.
-        */
-       function getResponseStatus( $response ){
-
-               $aok = true;
-
-               foreach ( $response->getElementsByTagName( 'RESULT' ) as $node 
) {
-                       if ( array_key_exists( $node->nodeValue, 
$this->return_value_map ) && $this->return_value_map[$node->nodeValue] !== true 
) {
-                               $aok = false;
-                       }
-               }
-               
-               return $aok;            
-       }
-       
-       /**
-        * Parse the response to get the errors in a format we can log and 
otherwise deal with.
-        * return a key/value array of codes (if they exist) and messages. 
-        */
-       function getResponseErrors( $response ){
-               $errors = array();
-               foreach ( $response->getElementsByTagName( 'warning' ) as $node 
) {
-                       $code = '';
-                       $message = '';
-                       foreach ( $node->childNodes as $childnode ) {
-                               if ($childnode->nodeName === "code"){
-                                       $code = $childnode->nodeValue;
-                               }
-                               if ($childnode->nodeName === "message"){
-                                       $message = $childnode->nodeValue;
-                               }
-                       }
-                       $errors[$code] = $message;
-                       self::log( "ON NOES! ERRORS: " . print_r($errors, true) 
);
-               }
-               return $errors;
-       }
-       
-       /**
-        * Harvest the data we need back from the gateway. 
-        * return a key/value array
-        */
-       function getResponseData( $response ){
-               $data = array();
-               foreach ( $response->getElementsByTagName( 'ImportantData' ) as 
$node ) {
-                       foreach ( $node->childNodes as $childnode ) {
-                               if (trim($childnode->nodeValue) != ''){
-                                       $data[$childnode->nodeName] = 
$childnode->nodeValue;
-                               }
-                       }
-               }
-               self::log( "Returned Data: " . print_r($data, true));
-               return $data;
-       }
-       
-       function processResponse( $response ) {
-               //TODO: Stuff. 
-       }
-       
-       function publicCurrentTransaction( $transaction = '' ){
-               $this->currentTransaction( $transaction );
-       }
-       
-       function curl_transaction($data) {
-               $data = "";
-               $data['result'] = 'BLAH BLAH BLAH BLAH whatever something blah 
blah<?xml version="1.0"?>' . "\n" . 
'<XML><Response><Status>AOK</Status><ImportantData><thing>stuff</thing><otherthing>12</otherthing></ImportantData><errorswarnings><warning><code>128</code><message>Your
 shoe\'s untied...</message></warning><warning><code>45</code><message>Low 
clearance!</message></warning></errorswarnings></Response></XML>';
-               return $data;
-       }
-}
-
-?>

Copied: 
branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php 
(from rev 96595, 
branches/fundraising/extensions/DonationInterface/tests/DonationInterfaceTest.php)
===================================================================
--- 
branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php  
                            (rev 0)
+++ 
branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php  
    2011-09-09 02:47:34 UTC (rev 96644)
@@ -0,0 +1,256 @@
+<?php
+
+/**
+ * TODO: Something. 
+ * Something roughly test-shaped. Here.
+ * ...to be more precise: Test that ALL the gateway adapters (Yes: All two of 
them)
+ * are building the XML we think they are, and that they can process sample 
+ * return XML the way we think they should. 
+ * 
+ * TODO: Then, write all the other tests as well. :|
+ * 
+ * @group Fundraising
+ * @group Splunge
+ * @group Gateways
+ * @author Katie Horn <[email protected]>
+ */
+class GatewayAdapterTest extends MediaWikiTestCase {
+
+       function testbuildRequestXML() {
+               $gateway = new TestAdapter();
+               $gateway->publicCurrentTransaction( 'Test1' );
+               $built = $gateway->buildRequestXML();
+               $expected = '<?xml version="1.0"?>' . "\n";
+               $expected .= 
'<XML><REQUEST><ACTION>Donate</ACTION><ACCOUNT><MERCHANTID>128</MERCHANTID><PASSWORD>k4ftw</PASSWORD><VERSION>3.2</VERSION><RETURNURL>http://localhost/index.php/Donate-thanks/en</RETURNURL></ACCOUNT><DONATION><DONOR>Tester
 
Testington</DONOR><AMOUNT>35000</AMOUNT><CURRENCYCODE>USD</CURRENCYCODE><LANGUAGECODE>en</LANGUAGECODE><COUNTRYCODE>US</COUNTRYCODE></DONATION></REQUEST></XML>'
 . "\n";
+               $this->assertEquals($built, $expected, "The constructed XML for 
transaction type Test1 does not match our expected.");
+               
+       }
+       
+       function testParseResponseStatusXML() {
+               
+               $returned = $this->getTestGatewayTransactionTest2Results();
+               $this->assertEquals($returned['status'], true, "Status should 
be true at this point.");
+       }
+       
+       function testParseResponseErrorsXML() {
+               
+               $returned = $this->getTestGatewayTransactionTest2Results();
+               $expected_errors = array(
+                       128 => "Your shoe's untied...",
+                       45 => "Low clearance!"
+               );
+               $this->assertEquals($returned['errors'], $expected_errors, 
"Expected errors were not found.");
+                               
+       }
+       
+       function testParseResponseDataXML() {
+               
+               $returned = $this->getTestGatewayTransactionTest2Results();
+               $expected_data = array(
+                       'thing' => 'stuff',
+                       'otherthing' => 12,
+               );
+               $this->assertEquals($returned['data'], $expected_data, 
"Expected data was not found.");
+                               
+       }
+       
+       function testResponseMessage() {
+               
+               $returned = $this->getTestGatewayTransactionTest2Results();
+               $this->assertEquals($returned['message'], "Test2 Transaction 
Successful!", "Expected message was not returned.");
+                               
+       }
+       
+       function testGetGlobal(){
+               $gateway = new TestAdapter();
+               $found = $gateway::getGlobal("TestVar");
+               $expected = "Hi there!";
+               $this->assertEquals($found, $expected, "getGlobal is not 
functioning properly.");
+       }
+       
+       
+       function getTestGatewayTransactionTest2Results(){
+               $gateway = new TestAdapter();
+               return $gateway->do_transaction( 'Test2' );
+       }
+
+}
+
+$dir = dirname( __FILE__ ) . '/';
+require_once( $dir . '../gateway_common/gateway.adapter.php' );
+
+class TestAdapter extends GatewayAdapter {
+
+       const gatewayname = 'Test Gateway';
+       const identifier = 'testgateway';
+       const communicationtype = 'xml';
+       const globalprefix = 'wgTestAdapterGateway';
+
+       function stageData(){
+               $this->postdata['amount'] = $this->postdata['amount'] * 1000;
+               $this->postdata['name'] = $this->postdata['fname'] . " " . 
$this->postdata['lname'];
+       }
+       
+       function __construct( ) {
+               global $wgTestAdapterGatewayTestVar, 
$wgTestAdapterGatewayUseSyslog, $wgTestAdapterGatewayTest;
+               $wgTestAdapterGatewayTest = true;
+               $wgTestAdapterGatewayTestVar = "Hi there!";
+               $wgTestAdapterGatewayUseSyslog = true;
+               $this->classlocation = __FILE__;
+               parent::__construct();
+               
+       }
+       
+       function defineAccountInfo(){
+               $this->accountInfo = array(
+                       'MERCHANTID' => '128',
+                       'PASSWORD' => 'k4ftw',
+                       //'IPADDRESS' => '', //TODO: Not sure if this should be 
OUR ip, or the user's ip. Hurm. 
+                       'VERSION' => "3.2",
+               );
+       }
+       
+       function defineVarMap(){
+               $this->var_map = array(
+                       'DONOR' => 'name',
+                       'AMOUNT' => 'amount',
+                       'CURRENCYCODE' => 'currency',
+                       'LANGUAGECODE' => 'language',
+                       'COUNTRYCODE' => 'country',
+                       'OID' => 'order_id',
+                       'RETURNURL' => 'returnto', //TODO: Fund out where the 
returnto URL is supposed to be coming from. 
+               );
+       }
+       
+       function defineReturnValueMap(){
+               $this->return_value_map = array(
+                       'AOK' => true,
+                       'WRONG' => false,
+               );
+       }
+       
+       function defineTransactions(){
+               $this->transactions = array();
+               
+               $this->transactions['Test1'] = array(
+                       'request' => array(
+                               'REQUEST' => array(
+                                       'ACTION',
+                                       'ACCOUNT' => array(
+                                               'MERCHANTID',
+                                               'PASSWORD',
+                                               'VERSION',
+                                               'RETURNURL',
+                                       ),
+                                       'DONATION' => array(
+                                               'DONOR',
+                                               'AMOUNT',
+                                               'CURRENCYCODE',
+                                               'LANGUAGECODE',
+                                               'COUNTRYCODE',
+                                               //'OID', //move this to another 
test. It's different every time, dorkus.
+                                       )
+                               )
+                       ),
+                       'values' => array(
+                               'ACTION' => 'Donate',
+                       ),
+               );
+               
+               $this->transactions['Test2'] = array(
+                       'request' => array(
+                               'REQUEST' => array(
+                                       'ACTION',
+                               )
+                       ),
+                       'values' => array(
+                               'ACTION' => 'Donate2',
+                       ),
+               );
+       }
+
+       /**
+        * Take the entire response string, and strip everything we don't care 
about.
+        * For instance: If it's XML, we only want correctly-formatted XML. 
Headers must be killed off. 
+        * return a string.
+        */
+       function getFormattedResponse( $rawResponse ){
+               $xmlString = $this->stripXMLResponseHeaders($rawResponse);
+               $displayXML = $this->formatXmlString( $xmlString );
+               $realXML = new DomDocument( '1.0' );
+               self::log( "Here is the Raw XML: " . $displayXML ); //I am 
apparently a huge fibber.
+               $realXML->loadXML( trim( $xmlString ) );
+               return $realXML;
+       }
+       
+       /**
+        * Parse the response to get the status. Not sure if this should return 
a bool, or something more... telling.
+        */
+       function getResponseStatus( $response ){
+
+               $aok = true;
+
+               foreach ( $response->getElementsByTagName( 'RESULT' ) as $node 
) {
+                       if ( array_key_exists( $node->nodeValue, 
$this->return_value_map ) && $this->return_value_map[$node->nodeValue] !== true 
) {
+                               $aok = false;
+                       }
+               }
+               
+               return $aok;            
+       }
+       
+       /**
+        * Parse the response to get the errors in a format we can log and 
otherwise deal with.
+        * return a key/value array of codes (if they exist) and messages. 
+        */
+       function getResponseErrors( $response ){
+               $errors = array();
+               foreach ( $response->getElementsByTagName( 'warning' ) as $node 
) {
+                       $code = '';
+                       $message = '';
+                       foreach ( $node->childNodes as $childnode ) {
+                               if ($childnode->nodeName === "code"){
+                                       $code = $childnode->nodeValue;
+                               }
+                               if ($childnode->nodeName === "message"){
+                                       $message = $childnode->nodeValue;
+                               }
+                       }
+                       $errors[$code] = $message;
+               }
+               return $errors;
+       }
+       
+       /**
+        * Harvest the data we need back from the gateway. 
+        * return a key/value array
+        */
+       function getResponseData( $response ){
+               $data = array();
+               foreach ( $response->getElementsByTagName( 'ImportantData' ) as 
$node ) {
+                       foreach ( $node->childNodes as $childnode ) {
+                               if (trim($childnode->nodeValue) != ''){
+                                       $data[$childnode->nodeName] = 
$childnode->nodeValue;
+                               }
+                       }
+               }
+               self::log( "Returned Data: " . print_r($data, true));
+               return $data;
+       }
+       
+       function processResponse( $response ) {
+               //TODO: Stuff. 
+       }
+       
+       function publicCurrentTransaction( $transaction = '' ){
+               $this->currentTransaction( $transaction );
+       }
+       
+       function curl_transaction($data) {
+               $data = "";
+               $data['result'] = 'BLAH BLAH BLAH BLAH whatever something blah 
blah<?xml version="1.0"?>' . "\n" . 
'<XML><Response><Status>AOK</Status><ImportantData><thing>stuff</thing><otherthing>12</otherthing></ImportantData><errorswarnings><warning><code>128</code><message>Your
 shoe\'s untied...</message></warning><warning><code>45</code><message>Low 
clearance!</message></warning></errorswarnings></Response></XML>';
+               return $data;
+       }
+}
+
+?>


Property changes on: 
branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to