]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/xmlrpc-client.php
add the xmlrpc testsuite (wiki and interop) from xmlrpc-epi
[SourceForge/phpwiki.git] / tests / xmlrpc / xmlrpc-client.php
1 <?php // #!/usr/local/bin/php -Cq
2 /*
3  * Test the wiki XMLRPC interface methods.
4  * This is a client app used to query most methods from our server.
5
6  * The interface specification is that discussed at 
7  * http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface
8
9  * Author: Reini Urban
10  */
11
12 /*
13   This file is part of, or distributed with, libXMLRPC - a C library for 
14   xml-encoded function calls.
15
16   Author: Dan Libby (dan@libby.com)
17   Epinions.com may be contacted at feedback@epinions-inc.com
18 */
19
20 /*  
21   Copyright 2001 Epinions, Inc. 
22
23   Subject to the following 3 conditions, Epinions, Inc.  permits you, free 
24   of charge, to (a) use, copy, distribute, modify, perform and display this 
25   software and associated documentation files (the "Software"), and (b) 
26   permit others to whom the Software is furnished to do so as well.  
27
28   1) The above copyright notice and this permission notice shall be included 
29   without modification in all copies or substantial portions of the 
30   Software.  
31
32   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
33   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
34   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
35   PURPOSE OR NONINFRINGEMENT.  
36
37   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, 
38   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT 
39   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING 
40   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH 
41   DAMAGES.    
42
43 */
44
45 $cur_dir = getcwd();
46 # Add root dir to the path
47 if (substr(PHP_OS,0,3) == 'WIN')
48     $cur_dir = str_replace("\\","/", $cur_dir);
49 $rootdir = $cur_dir . '/../../';
50 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
51 $include_path = ini_get('include_path') . $ini_sep . $rootdir . $ini_sep . $rootdir . "lib/pear";
52 ini_set('include_path', $include_path);
53
54 if ($HTTP_SERVER_VARS["SERVER_NAME"] == 'phpwiki.sourceforge.net') {
55     ini_set('include_path', ini_get('include_path') . ":/usr/share/pear");
56 }
57 define('PHPWIKI_NOMAIN', true);
58 # Quiet warnings in IniConfig.php
59 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
60 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
61 # Other needed files
62 require_once $rootdir.'index.php';
63
64 // fake a POST request to be able to load our interfaces
65 $save = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'];
66 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = "POST";
67 include($rootdir."lib/XmlRpcServer.php");
68 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = $save;
69 // now we have $wiki_dmap
70
71 include("./xmlrpc-servers.php");
72
73 function match($a, $b) {
74    $matches = true;
75    if (gettype($a) === "array") {
76       foreach ($a as $key => $c) {
77          $d = $b[$key];
78          if (!match($c, $d)) {
79             $matches = false;
80             break;
81          }
82       }
83    } else {
84       if ($a !== $b || xmlrpc_get_type($a) !== xmlrpc_get_type($b)) {
85          $matches = false;
86       }
87    }
88    return $matches;
89 }
90
91 function pass($method) {
92    echo "<font color='green'><b>pass</b></font> $method()<br>";
93 }
94
95 function fail($method, $sent, $received) {
96    echo "<font color='red'><b>fail</b></font> $method()<br>";
97    if ($sent) {
98      echo "<h3>sent</h3><xmp>";
99      var_dump($sent);
100      echo "</xmp>";
101    }
102
103    if ($received) {
104        echo "<h3>received</h3><xmp>";
105        var_dump($received);
106        echo "</xmp>";
107    }
108 }
109
110 function check_if_matches($method, $sent, $received) {
111    if (match($sent, $received)) {
112        pass($method);
113    }
114    else {
115        fail($method, $sent, $received);
116    }
117 }
118
119 function foo($method_name, $args) {
120     xmlrpc_encode_request($method_name, $args);
121 }
122
123 function run_test($server, $debug, $output, $method, $args) {
124     global $HTTP_GET_VARS;
125     echo "<hr>";
126     $params = array($args);
127     if (!empty($HTTP_GET_VARS['start_debug'])) // zend ide support
128         $server['uri'] .= "?start_debug=1";
129     $result =  xu_rpc_http_concise(array('method' => $method,
130                                          'args'   => $params, 
131                                          'host'   => $server['host'], 
132                                          'uri'    => $server['uri'], 
133                                          'port'   => $server['port'], 
134                                          'debug'  => $debug,
135                                          'output' => $output));
136     check_if_matches($method, $args, $result);
137     echo "</hr>";
138     flush();
139 }
140
141 function run_no_param_test($server, $debug, $output, $method) {
142     global $HTTP_GET_VARS;
143     echo "<hr>";
144     if (!empty($HTTP_GET_VARS['start_debug'])) // zend ide support
145         $server['uri'] .= "?start_debug=1";
146     $result =  xu_rpc_http_concise(array('method' => $method,
147                                          'host'   => $server['host'], 
148                                          'uri'    => $server['uri'], 
149                                          'port'   => $server['port'], 
150                                          'debug'  => $debug,
151                                          'output' => $output));
152
153     if ($result && gettype($result) === "integer") {
154         pass($method);
155     }
156     else {
157         fail($method, false, $result);
158     }
159    
160     flush();
161 }
162
163
164 // a method to run wiki tests against remote server. tests described at bottom.
165 function run_easy_tests($server, $debug=0, $output = null) {
166
167     global $wiki_dmap;
168
169     run_no_param_test($server, $debug, $output, "wiki.getRPCVersionSupported");
170     // of the last day:
171     run_test($server, $debug, $output, "wiki.getRecentChanges", iso8601_encode(time()-86400, 1));
172     /* ... */
173 }
174
175 function run_stress_tests($server, $debug=0, $output=null) {
176
177     global $wiki_dmap;
178
179     run_no_param_test($server, $debug, $output, "wiki.getRPCVersionSupported");
180     // of the last day:
181     run_test($server, $debug, $output, "wiki.getRecentChanges", iso8601_encode(time()-86400, 1));
182     /* ... */
183 }
184
185 // a method to display an html form for invoking the script
186 function print_html_form($servers_list) {
187
188    echo <<< END
189 <h1>Choose an xmlrpc wiki server to run tests against</h1>
190 END;
191
192    print_servers_form($servers_list);
193 }
194
195 // some code which determines if we are in form display or response mode.
196 $server_list = get_wiki_servers();
197 $server = get_server_from_user($server_list);
198 if ($server) {
199    $debug = $GLOBALS['HTTP_GET_VARS']['debug'] || $GLOBALS['HTTP_GET_VARS']['start_debug'];
200    $output['version'] = $GLOBALS['HTTP_GET_VARS']['version'];
201    if ($server) {
202       $title = $server['title'];
203       echo "<h2><CENTER>Results for $title</CENTER></H2>";
204       
205       if($GLOBALS['HTTP_GET_VARS']['stress'] == 1) {
206          run_stress_tests($server, $debug, $output);
207       }
208       else {
209          run_easy_tests($server, $debug, $output);
210       }
211    }
212    else {
213       echo "<h3>invalid option</h3>";
214    }
215 }
216 else {
217    print_html_form($server_list);
218 }