jenkins-bot has submitted this change and it was merged.

Change subject: Transliterate Worldpay post to ISO-8859-1
......................................................................


Transliterate Worldpay post to ISO-8859-1

Change-Id: If04e158430c43466cbe7f7fa35b6dc5f45ef4646
---
M DonationInterface.php
A gateway_common/EncodingMangler.php
M gateway_common/gateway.adapter.php
M tests/Adapter/WorldPay/WorldPayTestCase.php
M tests/AllTests.php
A tests/EncodingManglerTestCase.php
M tests/includes/test_gateway/test.adapter.php
M worldpay_gateway/worldpay.adapter.php
8 files changed, 165 insertions(+), 4 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/DonationInterface.php b/DonationInterface.php
index a76f22e..89f8222 100644
--- a/DonationInterface.php
+++ b/DonationInterface.php
@@ -101,6 +101,7 @@
 $wgDonationInterfaceClassMap = array();
 
 $wgAutoloadClasses['DonationData'] = $donationinterface_dir . 
'gateway_common/DonationData.php';
+$wgAutoloadClasses['EncodingMangler'] = $donationinterface_dir . 
'gateway_common/EncodingMangler.php';
 $wgAutoloadClasses['GatewayAdapter'] = $donationinterface_dir . 
'gateway_common/gateway.adapter.php';
 $wgAutoloadClasses['GatewayForm'] = $donationinterface_dir . 
'gateway_common/GatewayForm.php';
 $wgAutoloadClasses['DataValidator'] = $donationinterface_dir . 
'gateway_common/DataValidator.php';
diff --git a/gateway_common/EncodingMangler.php 
b/gateway_common/EncodingMangler.php
new file mode 100644
index 0000000..d2f5fae
--- /dev/null
+++ b/gateway_common/EncodingMangler.php
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Wikimedia Foundation
+ *
+ * LICENSE
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/**
+ * Forces the languages of the world to fit into a teensy little codespace
+ * so we can send them in ISO-8859-1 encoding.
+ * Uses Transliterator if available to turn non-latin characters into something
+ * meaningful.  If not available, iconv will just replace em with question 
marks
+ */
+class EncodingMangler {
+       private static $instance = null;
+       private $use_transliterator = false;
+       private $transliterator;
+
+       private function __construct() {
+               if ( class_exists( 'Transliterator' ) ) {
+                       $this->use_transliterator = true;
+                       // Use Any-Latin to munge Cyrillic, Kanji, etc
+                       // Then convert anything outside the ISO-8859-1 range 
to nearest ASCII
+                       $this->transliterator = 
Transliterator::create('Any-Latin; [^a-ÿ] Latin-ASCII');
+               }
+       }
+
+       /**
+        * @return EncodingMangler
+        */
+       public static function singleton() {
+               if ( self::$instance == null ) {
+                       self::$instance = new EncodingMangler();
+               }
+               return self::$instance;
+       }
+
+       /**
+        * Forces string into ISO-8859-1 space
+        *
+        * @param string $value UTF-8
+        * @return string still UTF-8, but only includes chars from 00-ff
+        */
+       public function transliterate( $value ) {
+               if ( $this->use_transliterator ) {
+                       return $this->transliterator->transliterate( $value );
+               }
+               $iso = iconv( 'UTF-8', 'ISO-8859-1//TRANSLIT', $value );
+               return iconv( 'ISO-8859-1', 'UTF-8', $iso );
+       }
+}
\ No newline at end of file
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index ba58c25..cac2b51 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -821,8 +821,8 @@
         * @return string The raw transaction in xml format, ready to be 
         * curl'd off to the remote server. 
         */
-       protected function buildRequestXML( $rootElement = 'XML' ) {
-               $this->xmlDoc = new DomDocument( '1.0', 'UTF-8' );
+       protected function buildRequestXML( $rootElement = 'XML', $encoding = 
'UTF-8' ) {
+               $this->xmlDoc = new DomDocument( '1.0', $encoding );
                $node = $this->xmlDoc->createElement( $rootElement );
 
                // Look up the request structure for our current transaction 
type in the transactions array
diff --git a/tests/Adapter/WorldPay/WorldPayTestCase.php 
b/tests/Adapter/WorldPay/WorldPayTestCase.php
index 0f4074e..9c3f589 100644
--- a/tests/Adapter/WorldPay/WorldPayTestCase.php
+++ b/tests/Adapter/WorldPay/WorldPayTestCase.php
@@ -263,4 +263,24 @@
                $staged = $gateway->_getData_Staged( 'merchant_reference_2' );
                $this->assertEquals( 'little teapot short stout com', $staged );
        }
+
+       function testTransliterateUtf8forEurocentricProcessor() {
+               $options = $this->getDonorTestData();
+               $options['fname'] = 'Barnabáš';
+               $options['lname'] = 'Voříšek';
+               $options['street'] = 'Truhlářská 320/62';
+               $options['city'] = 'České Budějovice';
+               $class = $this->testAdapterClass;
+
+               $_SERVER['REQUEST_URI'] = 
GatewayFormChooser::buildPaymentsFormURL( 'testytest', array ( 'gateway' => 
$class::getIdentifier() ) );
+               $gateway = $this->getFreshGatewayObject( $options );
+
+               $gateway->_stageData();
+               $gateway->do_transaction( 'AuthorizeAndDepositPayment' );
+               $xml = new SimpleXMLElement( preg_replace( '/StringIn=/', '', 
$gateway->curled ) );
+               $this->assertEquals( 'Barnabás', $xml->FirstName );
+               $this->assertEquals( 'Vorísek', $xml->LastName );
+               $this->assertEquals( 'Truhlárská 320/62', $xml->Address1 );
+               $this->assertEquals( 'Ceské Budejovice', $xml->City );
+       }
 }
diff --git a/tests/AllTests.php b/tests/AllTests.php
index 255ffd5..139785b 100644
--- a/tests/AllTests.php
+++ b/tests/AllTests.php
@@ -26,6 +26,7 @@
 require_once 'IntegrationTestCase.php';
 require_once 'FraudFiltersTestCase.php';
 require_once 'GatewayFormTestCase.php';
+require_once 'EncodingManglerTestCase.php';
 
 /**
  * AllTests
@@ -60,6 +61,7 @@
                $suite->addTestSuite( 'DonationInterface_IntegrationTestCase' );
                $suite->addTestSuite( 'DonationInterface_FraudFiltersTestCase' 
);
                $suite->addTestSuite( 'GatewayFormTestCase' );
+               $suite->addTestSuite( 'EncodingManglerTestCase' );
 //             $suite->addTest(DonationInterface_Adapter_AllTests::suite());
 
                return $suite;
diff --git a/tests/EncodingManglerTestCase.php 
b/tests/EncodingManglerTestCase.php
new file mode 100644
index 0000000..acbd1d4
--- /dev/null
+++ b/tests/EncodingManglerTestCase.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Wikimedia Foundation
+ *
+ * LICENSE
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/**
+ * @group      Fundraising
+ * @group      DonationInterface
+ * @group      EncodingMangler
+ * @category   UnitTesting
+ */
+class EncodingManglerTestCase extends PHPUnit_Framework_TestCase {
+
+       /**
+        * Keep the accented characters that we can represent
+        */
+       public function testRetainsAccents() {
+               $input = 
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ';
+               $output = EncodingMangler::singleton()->transliterate( $input );
+               $this->assertEquals( $input, $output );
+       }
+
+       /**
+        * Make eastern European characters NATO-friendly
+        */
+       public function testMangleCzech() {
+               $output = EncodingMangler::singleton()->transliterate( 
'ČčŘřŠšŽž' );
+               $this->assertEquals( 'CcRrSsZz', $output );
+       }
+
+       /**
+        * Ditto for Turkish
+        */
+       public function testMangleTurkish() {
+               $output = EncodingMangler::singleton()->transliterate( 'İĞğŞş' 
);
+               $this->assertEquals( 'IGgSs', $output );
+       }
+
+}
diff --git a/tests/includes/test_gateway/test.adapter.php 
b/tests/includes/test_gateway/test.adapter.php
index 318641b..ddddf69 100644
--- a/tests/includes/test_gateway/test.adapter.php
+++ b/tests/includes/test_gateway/test.adapter.php
@@ -493,6 +493,7 @@
 class TestingWorldPayAdapter extends WorldPayAdapter {
 
        public $testlog = array ( );
+       public $curled = '';
 
        //@TODO: That minfraud jerk needs its own isolated tests.
        function runAntifraudHooks() {
@@ -534,6 +535,11 @@
                $this->dummyCurlResponseCode = $code;
        }
 
+       protected function curl_transaction( $data ) {
+               $this->curled = $data;
+               return parent::curl_transaction( $data );
+       }
+
        /**
         * Load in some dummy response XML so we can test proper response 
processing
         */
diff --git a/worldpay_gateway/worldpay.adapter.php 
b/worldpay_gateway/worldpay.adapter.php
index 623c8cb..90124a0 100644
--- a/worldpay_gateway/worldpay.adapter.php
+++ b/worldpay_gateway/worldpay.adapter.php
@@ -836,8 +836,27 @@
                return $data;
        }
 
-       protected function buildRequestXML( $rootElement = 'TMSTN' ) {
-               return 'StringIn=' . str_replace( "\n", '', 
parent::buildRequestXML( $rootElement ) );
+       // WorldPay is apparently not very worldly in the ways of alphabets
+       protected function buildRequestXML( $rootElement = 'TMSTN', $encoding = 
'ISO-8859-1' ) {
+               $xml = parent::buildRequestXML( $rootElement, $encoding );
+               return 'StringIn=' . str_replace( "\n", '', $xml );
+       }
+
+       // override the charset from the parent function
+       protected function getTransactionSpecificValue( $gateway_field_name, 
$token = false ) {
+               $original = parent::getTransactionSpecificValue( 
$gateway_field_name, $token );
+               return EncodingMangler::singleton()->transliterate( $original );
+       }
+
+       // override the charset from the parent function
+       function getCurlBaseHeaders() {
+               $headers = parent::getCurlBaseHeaders();
+               foreach ( $headers as $index => $header ) {
+                       if ( substr( $header, 0, 13 ) == 'Content-Type:' ) {
+                               $headers[$index] = preg_replace( 
'/\bcharset=utf-8\b/', 'charset=iso-8859-1', $header, 1 );
+                       }
+               }
+               return $headers;
        }
 
        protected function stage_returnto( $type = 'request' ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/155190
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If04e158430c43466cbe7f7fa35b6dc5f45ef4646
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to