Set default internal charset to UTF8 and change tests accordingly; add function has_e...
authorgggeek <giunta.gaetano@gmail.com>
Sun, 19 Apr 2015 20:22:22 +0000 (21:22 +0100)
committergggeek <giunta.gaetano@gmail.com>
Sun, 19 Apr 2015 20:22:22 +0000 (21:22 +0100)
NEWS
demo/server/server.php
lib/xmlrpc.inc
src/PhpXmlRpc.php
tests/1ParsingBugsTest.php
tests/3LocalhostTest.php
tests/4LocalhostMultiTest.php

diff --git a/NEWS b/NEWS
index e1045d3..7f2c261 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,9 @@ PLEASE READ CAREFULLY THE NOTES BELOW to insure a smooth upgrade.
   Backward compatibility is maintained via lib/xmlrpc.inc, lib/xmlrpcs.inc and lib/xmlrpc_wrappers.inc.
   For more details, head on to doc/api_changes_v4.md
 
+* changed: the default encoding delivered from the library to your code is now utf8.
+  It can be changed at anytime setting a value to PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding
+
 * improved: all php code is now formatted according to the PSR-2 standard
 
 * improved: no need to call anymore $client->setSSLVerifyHost(2) to silence a curl warning when using https
@@ -49,7 +52,7 @@ PLEASE READ CAREFULLY THE NOTES BELOW to insure a smooth upgrade.
 
 * fixed: the client can now successfully call methods using ISO-8859-1 or UTF-8 characters in their name
 
-* improved: echo all debug messages even when there are characters in them which php deems in a wrong encoding
+* improved: echo all debug messages even when there are characters in them which php deems to be in a wrong encoding
   (this is visible e.g. in the debugger)
 
 * changed: debug info handling
index 88938d4..aa604b8 100644 (file)
@@ -723,19 +723,18 @@ $signatures = array(
     "tests.raiseException" => array(
         "function" => array($object, "exceptionGenerator"),
     ),
-    /*
     // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
-    // We can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
+    // NB: we can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
     "tests.utf8methodname." . 'κόσμε' => array(
         "function" => "stringEcho",
         "signature" => $stringecho_sig,
         "docstring" => $stringecho_doc,
-    ),*/
-    "tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
+    ),
+    /*"tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
         "function" => "stringEcho",
         "signature" => $stringecho_sig,
         "docstring" => $stringecho_doc,
-    ),
+    ),*/
     "examples.getallheaders" => array(
         "function" => 'getallheaders_xmlrpc',
         "signature" => $getallheaders_sig,
index 2bcead0..450e881 100644 (file)
@@ -50,13 +50,15 @@ include_once(__DIR__.'/../src/Request.php');
 include_once(__DIR__.'/../src/Response.php');
 include_once(__DIR__.'/../src/Client.php');
 include_once(__DIR__.'/../src/Encoder.php');
-include_once(__DIR__.'/../src/Helper/Date.php');
 include_once(__DIR__.'/../src/Helper/Charset.php');
+include_once(__DIR__.'/../src/Helper/Date.php');
 include_once(__DIR__.'/../src/Helper/Http.php');
+include_once(__DIR__.'/../src/Helper/Logger.php');
 include_once(__DIR__.'/../src/Helper/XMLParser.php');
 
 
 /* Expose the global variables which used to be defined */
+PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1'; // old default
 PhpXmlRpc\PhpXmlRpc::exportGlobals();
 
 /* some stuff deprecated enough that we do not want to put it in the new lib version */
@@ -194,6 +196,11 @@ function guess_encoding($httpHeader='', $xmlChunk='', $encodingPrefs=null)
     return PhpXmlRpc\Helper\XMLParser::guessEncoding($httpHeader, $xmlChunk, $encodingPrefs);
 }
 
+function has_encoding($xmlChunk)
+{
+    return PhpXmlRpc\Helper\XMLParser::hasEncoding($xmlChunk);
+}
+
 function is_valid_charset($encoding, $validList)
 {
     return PhpXmlRpc\Helper\Charset::instance()->is_valid_charset($encoding, $validList);
index 88b8f70..84597e6 100644 (file)
@@ -65,8 +65,9 @@ class PhpXmlRpc
 
     // The encoding used internally by PHP.
     // String values received as xml will be converted to this, and php strings will be converted to xml
-    // as if having been coded with this
-    public static $xmlrpc_internalencoding = "ISO-8859-1"; // TODO: maybe this would be better as UTF-8
+    // as if having been coded with this.
+    // Valid also when defining names of xmlrpc methods
+    public static $xmlrpc_internalencoding = "UTF-8";
 
     public static $xmlrpcName = "XML-RPC for PHP";
     public static $xmlrpcVersion = "4.0.0.beta";
index 6a6a3f9..06fb36a 100644 (file)
@@ -40,8 +40,8 @@ class ParsingBugsTests extends PHPUnit_Framework_TestCase
         $v = new xmlrpcval('-1');
         $u = new xmlrpcval('-1', 'string');
         $t = new xmlrpcval(-1, 'string');
-        $this->assertEquals($u->scalarval(), $v->scalarval());
-        $this->assertEquals($t->scalarval(), $v->scalarval());
+        $this->assertEquals($v->scalarval(), $u->scalarval());
+        $this->assertEquals($v->scalarval(), $t->scalarval());
     }
 
     /**
@@ -49,8 +49,8 @@ class ParsingBugsTests extends PHPUnit_Framework_TestCase
      */
     public function testMinusOneInt()
     {
-        $v = new xmlrpcval(-1);
         $u = new xmlrpcval();
+        $v = new xmlrpcval(-1);
         $this->assertEquals($u->scalarval(), $v->scalarval());
     }
 
@@ -63,7 +63,7 @@ class ParsingBugsTests extends PHPUnit_Framework_TestCase
         $m = $this->newMsg('dummy');
         $r = $m->parseResponse($r);
         $v = $r->value();
-        $this->assertEquals($v->structmemexists($str), true);
+        $this->assertEquals(true, $v->structmemexists($str));
     }
 
     public function testUnicodeInErrorString()
index e6cc42d..b782319 100644 (file)
@@ -122,7 +122,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
         if (is_array($errorCode)) {
             $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
         } else {
-            $this->assertEquals($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
+            $this->assertEquals($errorCode, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
         }
         if (!$r->faultCode()) {
             if ($returnResponse) {
@@ -176,7 +176,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
         }
     }
 
-    public function testLatin1Method()
+    /*public function testLatin1Method()
     {
         $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array(
             new xmlrpcval('hello')
@@ -185,10 +185,11 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
         if ($v) {
             $this->assertEquals('hello', $v->scalarval());
         }
-    }
+    }*/
 
-    /*public function testUtf8Method()
+    public function testUtf8Method()
     {
+        PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
         $f = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array(
             new xmlrpcval('hello')
         ));
@@ -196,7 +197,8 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
         if ($v) {
             $this->assertEquals('hello', $v->scalarval());
         }
-    }*/
+        PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
+    }
 
     public function testAddingDoubles()
     {
@@ -247,9 +249,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
                 new xmlrpcval(true, 'boolean'),
                 new xmlrpcval(false, 'boolean'),
                 new xmlrpcval(1, 'boolean'),
-                new xmlrpcval(0, 'boolean'),
-                //new xmlrpcval('true', 'boolean'),
-                //new xmlrpcval('false', 'boolean')
+                new xmlrpcval(0, 'boolean')
             ),
                 'array'
             ),));
@@ -518,7 +518,7 @@ And turned it into nylon';
         ));
         $v = $this->send($f);
         if ($v) {
-            $this->assertEquals($v->scalarval(), true);
+            $this->assertEquals(true, $v->scalarval());
         }
     }
 
@@ -547,7 +547,6 @@ And turned it into nylon';
         $f = new xmlrpcmsg('system.MethodHelp');
         $f->payload = "<?xml version=\"1.0\"?><methodCall><methodName>validator1.echoStructTest</methodName><params><param><value><struct><member><name>','')); echo('gotcha!'); die(); //</name></member></struct></value></param></params></methodCall>";
         $v = $this->send($f);
-        //$v = $r->faultCode();
         if ($v) {
             $this->assertEquals(0, $v->structsize());
         }
@@ -661,7 +660,7 @@ And turned it into nylon';
             $cookies[$cookie] = (string)$cookies[$cookie];
         }
         $r = $this->client->send($f, $this->timeout, $this->method);
-        $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
+        $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
         if (!$r->faultCode()) {
             $v = $r->value();
             $v = php_xmlrpc_decode($v);
@@ -672,7 +671,7 @@ And turned it into nylon';
             }
 
             // on IIS and Apache getallheaders returns something slightly different...
-            $this->assertEquals($v, $cookies);
+            $this->assertEquals($cookies, $v);
         }
     }
 
@@ -683,9 +682,8 @@ And turned it into nylon';
         ));
         $v1 = $this->send($f);
         $v2 = $this->send($f);
-        //$v = $r->faultCode();
         if ($v1 && $v2) {
-            $this->assertEquals($v2, $v1);
+            $this->assertEquals($v1, $v2);
         }
     }
 }
index 51a8933..5ef6a3c 100644 (file)
@@ -15,9 +15,10 @@ class LocalhostMultiTest extends LocalhostTest
      */
     function _runtests()
     {
+        $unsafeMethods = array('testHttps', 'testCatchExceptions', 'testUtf8Method');
         foreach(get_class_methods('LocalhostTest') as $method)
         {
-            if(strpos($method, 'test') === 0 && $method != 'testHttps' && $method != 'testCatchExceptions')
+            if(strpos($method, 'test') === 0 && !in_array($method, $unsafeMethods))
             {
                 if (!isset(self::$failed_tests[$method]))
                     $this->$method();