r98257 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98256‎ | r98257 | r98258 >
Date:21:14, 27 September 2011
Author:jpostlethwaite
Status:deferred
Tags:fundraising 
Comment:
Expanding unit tests for DonationInterface
Modified paths:
  • /branches/fundraising/extensions/DonationInterface/tests/Adapter/GatewayAdapterTestCase.php (added) (history)
  • /branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php (deleted) (history)

Diff [purge]

Index: branches/fundraising/extensions/DonationInterface/tests/GatewayAdapterTest.php
@@ -1,255 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * TODO: Something.
6 - * Something roughly test-shaped. Here.
7 - * ...to be more precise: Test that ALL the gateway adapters (Yes: All two of them)
8 - * are building the XML we think they are, and that they can process sample
9 - * return XML the way we think they should.
10 - *
11 - * TODO: Then, write all the other tests as well. :|
12 - *
13 - * @group Fundraising
14 - * @group Splunge
15 - * @group Gateways
16 - * @author Katie Horn <khorn@wikimedia.org>
17 - */
18 -class GatewayAdapterTest extends MediaWikiTestCase {
19 -
20 - function testbuildRequestXML() {
21 - $gateway = new TestAdapter();
22 - $gateway->publicCurrentTransaction( 'Test1' );
23 - $built = $gateway->buildRequestXML();
24 - $expected = '<?xml version="1.0"?>' . "\n";
25 - $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";
26 - $this->assertEquals($built, $expected, "The constructed XML for transaction type Test1 does not match our expected.");
27 -
28 - }
29 -
30 - function testParseResponseStatusXML() {
31 -
32 - $returned = $this->getTestGatewayTransactionTest2Results();
33 - $this->assertEquals($returned['status'], true, "Status should be true at this point.");
34 - }
35 -
36 - function testParseResponseErrorsXML() {
37 -
38 - $returned = $this->getTestGatewayTransactionTest2Results();
39 - $expected_errors = array(
40 - 128 => "Your shoe's untied...",
41 - 45 => "Low clearance!"
42 - );
43 - $this->assertEquals($returned['errors'], $expected_errors, "Expected errors were not found.");
44 -
45 - }
46 -
47 - function testParseResponseDataXML() {
48 -
49 - $returned = $this->getTestGatewayTransactionTest2Results();
50 - $expected_data = array(
51 - 'thing' => 'stuff',
52 - 'otherthing' => 12,
53 - );
54 - $this->assertEquals($returned['data'], $expected_data, "Expected data was not found.");
55 -
56 - }
57 -
58 - function testResponseMessage() {
59 -
60 - $returned = $this->getTestGatewayTransactionTest2Results();
61 - $this->assertEquals($returned['message'], "Test2 Transaction Successful!", "Expected message was not returned.");
62 -
63 - }
64 -
65 - function testGetGlobal(){
66 - $gateway = new TestAdapter();
67 - $found = $gateway::getGlobal("TestVar");
68 - $expected = "Hi there!";
69 - $this->assertEquals($found, $expected, "getGlobal is not functioning properly.");
70 - }
71 -
72 -
73 - function getTestGatewayTransactionTest2Results(){
74 - $gateway = new TestAdapter();
75 - return $gateway->do_transaction( 'Test2' );
76 - }
77 -
78 -}
79 -
80 -$dir = dirname( __FILE__ ) . '/';
81 -require_once( $dir . '../gateway_common/gateway.adapter.php' );
82 -
83 -class TestAdapter extends GatewayAdapter {
84 -
85 - const GATEWAY_NAME = 'Test Gateway';
86 - const IDENTIFIER = 'testgateway';
87 - const COMMUNICATION_TYPE = 'xml';
88 - const GLOBAL_PREFIX = 'wgTestAdapterGateway';
89 -
90 - function stageData(){
91 - $this->postdata['amount'] = $this->postdata['amount'] * 1000;
92 - $this->postdata['name'] = $this->postdata['fname'] . " " . $this->postdata['lname'];
93 - }
94 -
95 - function __construct( ) {
96 - global $wgTestAdapterGatewayTestVar, $wgTestAdapterGatewayUseSyslog, $wgTestAdapterGatewayTest;
97 - $wgTestAdapterGatewayTest = true;
98 - $wgTestAdapterGatewayTestVar = "Hi there!";
99 - $wgTestAdapterGatewayUseSyslog = true;
100 - parent::__construct();
101 -
102 - }
103 -
104 - function defineAccountInfo(){
105 - $this->accountInfo = array(
106 - 'MERCHANTID' => '128',
107 - 'PASSWORD' => 'k4ftw',
108 - //'IPADDRESS' => '', //TODO: Not sure if this should be OUR ip, or the user's ip. Hurm.
109 - 'VERSION' => "3.2",
110 - );
111 - }
112 -
113 - function defineVarMap(){
114 - $this->var_map = array(
115 - 'DONOR' => 'name',
116 - 'AMOUNT' => 'amount',
117 - 'CURRENCYCODE' => 'currency',
118 - 'LANGUAGECODE' => 'language',
119 - 'COUNTRYCODE' => 'country',
120 - 'OID' => 'order_id',
121 - 'RETURNURL' => 'returnto', //TODO: Fund out where the returnto URL is supposed to be coming from.
122 - );
123 - }
124 -
125 - function defineReturnValueMap(){
126 - $this->return_value_map = array(
127 - 'AOK' => true,
128 - 'WRONG' => false,
129 - );
130 - }
131 -
132 - function defineTransactions(){
133 - $this->transactions = array();
134 -
135 - $this->transactions['Test1'] = array(
136 - 'request' => array(
137 - 'REQUEST' => array(
138 - 'ACTION',
139 - 'ACCOUNT' => array(
140 - 'MERCHANTID',
141 - 'PASSWORD',
142 - 'VERSION',
143 - 'RETURNURL',
144 - ),
145 - 'DONATION' => array(
146 - 'DONOR',
147 - 'AMOUNT',
148 - 'CURRENCYCODE',
149 - 'LANGUAGECODE',
150 - 'COUNTRYCODE',
151 - //'OID', //move this to another test. It's different every time, dorkus.
152 - )
153 - )
154 - ),
155 - 'values' => array(
156 - 'ACTION' => 'Donate',
157 - ),
158 - );
159 -
160 - $this->transactions['Test2'] = array(
161 - 'request' => array(
162 - 'REQUEST' => array(
163 - 'ACTION',
164 - )
165 - ),
166 - 'values' => array(
167 - 'ACTION' => 'Donate2',
168 - ),
169 - );
170 - }
171 -
172 - /**
173 - * Take the entire response string, and strip everything we don't care about.
174 - * For instance: If it's XML, we only want correctly-formatted XML. Headers must be killed off.
175 - * return a string.
176 - */
177 - function getFormattedResponse( $rawResponse ){
178 - $xmlString = $this->stripXMLResponseHeaders($rawResponse);
179 - $displayXML = $this->formatXmlString( $xmlString );
180 - $realXML = new DomDocument( '1.0' );
181 - self::log( "Here is the Raw XML: " . $displayXML ); //I am apparently a huge fibber.
182 - $realXML->loadXML( trim( $xmlString ) );
183 - return $realXML;
184 - }
185 -
186 - /**
187 - * Parse the response to get the status. Not sure if this should return a bool, or something more... telling.
188 - */
189 - function getResponseStatus( $response ){
190 -
191 - $aok = true;
192 -
193 - foreach ( $response->getElementsByTagName( 'RESULT' ) as $node ) {
194 - if ( array_key_exists( $node->nodeValue, $this->return_value_map ) && $this->return_value_map[$node->nodeValue] !== true ) {
195 - $aok = false;
196 - }
197 - }
198 -
199 - return $aok;
200 - }
201 -
202 - /**
203 - * Parse the response to get the errors in a format we can log and otherwise deal with.
204 - * return a key/value array of codes (if they exist) and messages.
205 - */
206 - function getResponseErrors( $response ){
207 - $errors = array();
208 - foreach ( $response->getElementsByTagName( 'warning' ) as $node ) {
209 - $code = '';
210 - $message = '';
211 - foreach ( $node->childNodes as $childnode ) {
212 - if ($childnode->nodeName === "code"){
213 - $code = $childnode->nodeValue;
214 - }
215 - if ($childnode->nodeName === "message"){
216 - $message = $childnode->nodeValue;
217 - }
218 - }
219 - $errors[$code] = $message;
220 - }
221 - return $errors;
222 - }
223 -
224 - /**
225 - * Harvest the data we need back from the gateway.
226 - * return a key/value array
227 - */
228 - function getResponseData( $response ){
229 - $data = array();
230 - foreach ( $response->getElementsByTagName( 'ImportantData' ) as $node ) {
231 - foreach ( $node->childNodes as $childnode ) {
232 - if (trim($childnode->nodeValue) != ''){
233 - $data[$childnode->nodeName] = $childnode->nodeValue;
234 - }
235 - }
236 - }
237 - self::log( "Returned Data: " . print_r($data, true));
238 - return $data;
239 - }
240 -
241 - function processResponse( $response ) {
242 - //TODO: Stuff.
243 - }
244 -
245 - function publicCurrentTransaction( $transaction = '' ){
246 - $this->currentTransaction( $transaction );
247 - }
248 -
249 - function curl_transaction($data) {
250 - $data = "";
251 - $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>';
252 - return $data;
253 - }
254 -}
255 -
256 -?>
Index: branches/fundraising/extensions/DonationInterface/tests/Adapter/GatewayAdapterTestCase.php
@@ -0,0 +1,255 @@
 2+<?php
 3+
 4+/**
 5+ * TODO: Something.
 6+ * Something roughly test-shaped. Here.
 7+ * ...to be more precise: Test that ALL the gateway adapters (Yes: All two of them)
 8+ * are building the XML we think they are, and that they can process sample
 9+ * return XML the way we think they should.
 10+ *
 11+ * TODO: Then, write all the other tests as well. :|
 12+ *
 13+ * @group Fundraising
 14+ * @group Splunge
 15+ * @group Gateways
 16+ * @author Katie Horn <khorn@wikimedia.org>
 17+ */
 18+class GatewayAdapterTest extends MediaWikiTestCase {
 19+
 20+ function testbuildRequestXML() {
 21+ $gateway = new TestAdapter();
 22+ $gateway->publicCurrentTransaction( 'Test1' );
 23+ $built = $gateway->buildRequestXML();
 24+ $expected = '<?xml version="1.0"?>' . "\n";
 25+ $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";
 26+ $this->assertEquals($built, $expected, "The constructed XML for transaction type Test1 does not match our expected.");
 27+
 28+ }
 29+
 30+ function testParseResponseStatusXML() {
 31+
 32+ $returned = $this->getTestGatewayTransactionTest2Results();
 33+ $this->assertEquals($returned['status'], true, "Status should be true at this point.");
 34+ }
 35+
 36+ function testParseResponseErrorsXML() {
 37+
 38+ $returned = $this->getTestGatewayTransactionTest2Results();
 39+ $expected_errors = array(
 40+ 128 => "Your shoe's untied...",
 41+ 45 => "Low clearance!"
 42+ );
 43+ $this->assertEquals($returned['errors'], $expected_errors, "Expected errors were not found.");
 44+
 45+ }
 46+
 47+ function testParseResponseDataXML() {
 48+
 49+ $returned = $this->getTestGatewayTransactionTest2Results();
 50+ $expected_data = array(
 51+ 'thing' => 'stuff',
 52+ 'otherthing' => 12,
 53+ );
 54+ $this->assertEquals($returned['data'], $expected_data, "Expected data was not found.");
 55+
 56+ }
 57+
 58+ function testResponseMessage() {
 59+
 60+ $returned = $this->getTestGatewayTransactionTest2Results();
 61+ $this->assertEquals($returned['message'], "Test2 Transaction Successful!", "Expected message was not returned.");
 62+
 63+ }
 64+
 65+ function testGetGlobal(){
 66+ $gateway = new TestAdapter();
 67+ $found = $gateway::getGlobal("TestVar");
 68+ $expected = "Hi there!";
 69+ $this->assertEquals($found, $expected, "getGlobal is not functioning properly.");
 70+ }
 71+
 72+
 73+ function getTestGatewayTransactionTest2Results(){
 74+ $gateway = new TestAdapter();
 75+ return $gateway->do_transaction( 'Test2' );
 76+ }
 77+
 78+}
 79+
 80+$dir = dirname( __FILE__ ) . '/';
 81+require_once( $dir . '../gateway_common/gateway.adapter.php' );
 82+
 83+class TestAdapter extends GatewayAdapter {
 84+
 85+ const GATEWAY_NAME = 'Test Gateway';
 86+ const IDENTIFIER = 'testgateway';
 87+ const COMMUNICATION_TYPE = 'xml';
 88+ const GLOBAL_PREFIX = 'wgTestAdapterGateway';
 89+
 90+ function stageData(){
 91+ $this->postdata['amount'] = $this->postdata['amount'] * 1000;
 92+ $this->postdata['name'] = $this->postdata['fname'] . " " . $this->postdata['lname'];
 93+ }
 94+
 95+ function __construct( ) {
 96+ global $wgTestAdapterGatewayTestVar, $wgTestAdapterGatewayUseSyslog, $wgTestAdapterGatewayTest;
 97+ $wgTestAdapterGatewayTest = true;
 98+ $wgTestAdapterGatewayTestVar = "Hi there!";
 99+ $wgTestAdapterGatewayUseSyslog = true;
 100+ parent::__construct();
 101+
 102+ }
 103+
 104+ function defineAccountInfo(){
 105+ $this->accountInfo = array(
 106+ 'MERCHANTID' => '128',
 107+ 'PASSWORD' => 'k4ftw',
 108+ //'IPADDRESS' => '', //TODO: Not sure if this should be OUR ip, or the user's ip. Hurm.
 109+ 'VERSION' => "3.2",
 110+ );
 111+ }
 112+
 113+ function defineVarMap(){
 114+ $this->var_map = array(
 115+ 'DONOR' => 'name',
 116+ 'AMOUNT' => 'amount',
 117+ 'CURRENCYCODE' => 'currency',
 118+ 'LANGUAGECODE' => 'language',
 119+ 'COUNTRYCODE' => 'country',
 120+ 'OID' => 'order_id',
 121+ 'RETURNURL' => 'returnto', //TODO: Fund out where the returnto URL is supposed to be coming from.
 122+ );
 123+ }
 124+
 125+ function defineReturnValueMap(){
 126+ $this->return_value_map = array(
 127+ 'AOK' => true,
 128+ 'WRONG' => false,
 129+ );
 130+ }
 131+
 132+ function defineTransactions(){
 133+ $this->transactions = array();
 134+
 135+ $this->transactions['Test1'] = array(
 136+ 'request' => array(
 137+ 'REQUEST' => array(
 138+ 'ACTION',
 139+ 'ACCOUNT' => array(
 140+ 'MERCHANTID',
 141+ 'PASSWORD',
 142+ 'VERSION',
 143+ 'RETURNURL',
 144+ ),
 145+ 'DONATION' => array(
 146+ 'DONOR',
 147+ 'AMOUNT',
 148+ 'CURRENCYCODE',
 149+ 'LANGUAGECODE',
 150+ 'COUNTRYCODE',
 151+ //'OID', //move this to another test. It's different every time, dorkus.
 152+ )
 153+ )
 154+ ),
 155+ 'values' => array(
 156+ 'ACTION' => 'Donate',
 157+ ),
 158+ );
 159+
 160+ $this->transactions['Test2'] = array(
 161+ 'request' => array(
 162+ 'REQUEST' => array(
 163+ 'ACTION',
 164+ )
 165+ ),
 166+ 'values' => array(
 167+ 'ACTION' => 'Donate2',
 168+ ),
 169+ );
 170+ }
 171+
 172+ /**
 173+ * Take the entire response string, and strip everything we don't care about.
 174+ * For instance: If it's XML, we only want correctly-formatted XML. Headers must be killed off.
 175+ * return a string.
 176+ */
 177+ function getFormattedResponse( $rawResponse ){
 178+ $xmlString = $this->stripXMLResponseHeaders($rawResponse);
 179+ $displayXML = $this->formatXmlString( $xmlString );
 180+ $realXML = new DomDocument( '1.0' );
 181+ self::log( "Here is the Raw XML: " . $displayXML ); //I am apparently a huge fibber.
 182+ $realXML->loadXML( trim( $xmlString ) );
 183+ return $realXML;
 184+ }
 185+
 186+ /**
 187+ * Parse the response to get the status. Not sure if this should return a bool, or something more... telling.
 188+ */
 189+ function getResponseStatus( $response ){
 190+
 191+ $aok = true;
 192+
 193+ foreach ( $response->getElementsByTagName( 'RESULT' ) as $node ) {
 194+ if ( array_key_exists( $node->nodeValue, $this->return_value_map ) && $this->return_value_map[$node->nodeValue] !== true ) {
 195+ $aok = false;
 196+ }
 197+ }
 198+
 199+ return $aok;
 200+ }
 201+
 202+ /**
 203+ * Parse the response to get the errors in a format we can log and otherwise deal with.
 204+ * return a key/value array of codes (if they exist) and messages.
 205+ */
 206+ function getResponseErrors( $response ){
 207+ $errors = array();
 208+ foreach ( $response->getElementsByTagName( 'warning' ) as $node ) {
 209+ $code = '';
 210+ $message = '';
 211+ foreach ( $node->childNodes as $childnode ) {
 212+ if ($childnode->nodeName === "code"){
 213+ $code = $childnode->nodeValue;
 214+ }
 215+ if ($childnode->nodeName === "message"){
 216+ $message = $childnode->nodeValue;
 217+ }
 218+ }
 219+ $errors[$code] = $message;
 220+ }
 221+ return $errors;
 222+ }
 223+
 224+ /**
 225+ * Harvest the data we need back from the gateway.
 226+ * return a key/value array
 227+ */
 228+ function getResponseData( $response ){
 229+ $data = array();
 230+ foreach ( $response->getElementsByTagName( 'ImportantData' ) as $node ) {
 231+ foreach ( $node->childNodes as $childnode ) {
 232+ if (trim($childnode->nodeValue) != ''){
 233+ $data[$childnode->nodeName] = $childnode->nodeValue;
 234+ }
 235+ }
 236+ }
 237+ self::log( "Returned Data: " . print_r($data, true));
 238+ return $data;
 239+ }
 240+
 241+ function processResponse( $response ) {
 242+ //TODO: Stuff.
 243+ }
 244+
 245+ function publicCurrentTransaction( $transaction = '' ){
 246+ $this->currentTransaction( $transaction );
 247+ }
 248+
 249+ function curl_transaction($data) {
 250+ $data = "";
 251+ $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>';
 252+ return $data;
 253+ }
 254+}
 255+
 256+?>
Property changes on: branches/fundraising/extensions/DonationInterface/tests/Adapter/GatewayAdapterTestCase.php
___________________________________________________________________
Added: svn:eol-style
1257 + native

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r98255Expanding unit tests for DonationInterfacejpostlethwaite21:13, 27 September 2011

Status & tagging log