]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/xmlrpc-client.php
trailing_spaces
[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  * $Id$
11  */
12
13 /*
14   This file is part of, or distributed with, libXMLRPC - a C library for
15   xml-encoded function calls.
16
17   Author: Dan Libby (dan@libby.com)
18   Epinions.com may be contacted at feedback@epinions-inc.com
19 */
20
21 /*
22   Copyright 2001 Epinions, Inc.
23
24   Subject to the following 3 conditions, Epinions, Inc.  permits you, free
25   of charge, to (a) use, copy, distribute, modify, perform and display this
26   software and associated documentation files (the "Software"), and (b)
27   permit others to whom the Software is furnished to do so as well.
28
29   1) The above copyright notice and this permission notice shall be included
30   without modification in all copies or substantial portions of the
31   Software.
32
33   2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
34   ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
35   IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
36   PURPOSE OR NONINFRINGEMENT.
37
38   3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
39   SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
40   OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
41   NEGLIGENCE), EVEN IF EPINIONS, INC.  IS AWARE OF THE POSSIBILITY OF SUCH
42   DAMAGES.
43
44 */
45
46 $cur_dir = getcwd();
47 # Add root dir to the path
48 if (substr(PHP_OS,0,3) == 'WIN')
49     $cur_dir = str_replace("\\","/", $cur_dir);
50 $rootdir = $cur_dir . '/../../';
51 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
52 $include_path = ini_get('include_path') . $ini_sep . $rootdir . $ini_sep . $rootdir . "lib/pear";
53 ini_set('include_path', $include_path);
54
55 if ($HTTP_SERVER_VARS["SERVER_NAME"] == 'phpwiki.sourceforge.net') {
56     ini_set('include_path', ini_get('include_path') . ":/usr/share/pear");
57 }
58 define('PHPWIKI_NOMAIN', true);
59 # Quiet warnings in IniConfig.php
60 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
61 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
62 # Other needed files
63 require_once $rootdir.'index.php';
64
65 // fake a POST request to be able to load our interfaces
66 $save = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'];
67 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = "POST";
68 include($rootdir."lib/XmlRpcServer.php");
69 $GLOBALS['HTTP_SERVER_VARS']['REQUEST_METHOD'] = $save;
70 // now we have $wiki_dmap
71
72 include("./xmlrpc-servers.php");
73
74 function match($a, $b) {
75    $matches = true;
76    if (gettype($a) === "array") {
77       foreach ($a as $key => $c) {
78          $d = $b[$key];
79          if (!match($c, $d)) {
80             $matches = false;
81             break;
82          }
83       }
84    } else {
85       if ($a !== $b || xmlrpc_get_type($a) !== xmlrpc_get_type($b)) {
86          $matches = false;
87       }
88    }
89    return $matches;
90 }
91
92 function pass($method) {
93    echo "<font color=\"green\"><b>pass</b></font> $method()<br>";
94 }
95
96 function fail($method, $sent, $received) {
97    echo "<font color=\"red\"><b>fail</b></font> $method()<br>";
98    if ($sent) {
99      echo "<h3>sent</h3><xmp>";
100      var_dump($sent);
101      echo "</xmp>";
102    }
103
104    if ($received) {
105        echo "<h3>received</h3><xmp>";
106        var_dump($received);
107        echo "</xmp>";
108    }
109 }
110
111 // this needs to be fixed.
112 function check_if_matches($method, $sent, $received) {
113    if (match($sent, $received)) {
114        pass($method);
115    }
116    else {
117        fail($method, $sent, $received);
118    }
119 }
120
121 function foo($method_name, $args) {
122     xmlrpc_encode_request($method_name, $args);
123 }
124
125 function run_test($server, $debug, $output, $method, $args='', $expected='') {
126     global $HTTP_GET_VARS;
127     echo "<hr>";
128     if (!is_array($args))
129         $params = $args ? array($args) : array();
130     else
131         $params = $args;
132     if (!empty($HTTP_GET_VARS['start_debug'])) // zend ide support
133         $server['uri'] .= "?start_debug=1";
134     $result =  xu_rpc_http_concise(array('method' => $method,
135                                          'args'   => $params,
136                                          'host'   => $server['host'],
137                                          'uri'    => $server['uri'],
138                                          'port'   => $server['port'],
139                                          'debug'  => $debug,
140                                          'output' => $output));
141     check_if_matches($method, $expected, $result);
142     echo "</hr>";
143     flush();
144 }
145
146 // should return non-zero integer
147 function run_no_param_test($server, $debug, $output, $method) {
148     global $HTTP_GET_VARS;
149     echo "<hr>";
150     if (!empty($HTTP_GET_VARS['start_debug'])) // zend ide support
151         $server['uri'] .= "?start_debug=1";
152     $result =  xu_rpc_http_concise(array('method' => $method,
153                                          'host'   => $server['host'],
154                                          'uri'    => $server['uri'],
155                                          'port'   => $server['port'],
156                                          'debug'  => $debug,
157                                          'output' => $output));
158
159     if ($result && gettype($result) === "integer") {
160         pass($method);
161     }
162     else {
163         fail($method, false, $result);
164     }
165
166     flush();
167 }
168
169
170 // a method to run wiki tests against remote server. tests described at bottom.
171 function run_easy_tests($server, $debug=0, $output = null) {
172
173     //global $wiki_dmap;
174
175     run_test($server, $debug, $output, "wiki.getRPCVersionSupported", '', 1);
176
177     // getRecentChanges of the last day:
178     // Note: may crash with dba on index.php, not on RPC2.php
179     run_test($server, $debug, $output, "wiki.getRecentChanges", iso8601_encode(time()-86400));
180
181     run_test($server, $debug, $output, "wiki.getPage", "HomePage", "* What is a WikiWikiWeb? A description of this application. * Learn HowToUseWiki and learn about AddingPages. * Use the SandBox page to experiment with Wiki pages. * Please sign your name in RecentVisitors. * See RecentChanges for the latest page additions and changes. * Find out which pages are MostPopular. * Read the ReleaseNotes and RecentReleases. * Administer this wiki via PhpWikiAdministration. * See more PhpWikiDocumentation.");
182     run_test($server, $debug, $output, "wiki.getPageVersion", array("HomePage", 1));
183     run_test($server, $debug, $output, "wiki.getPageHTML", "HomePage");
184     run_test($server, $debug, $output, "wiki.getPageHTMLVersion", array("HomePage", 1));
185     run_test($server, $debug, $output, "wiki.getAllPages");
186     run_test($server, $debug, $output, "wiki.getPageInfo", "HomePage");
187     run_test($server, $debug, $output, "wiki.getPageInfoVersion", array("HomePage", 1));
188     run_test($server, $debug, $output, "wiki.listLinks", "HomePage");
189
190     run_test($server, $debug, $output, "wiki.putPage",
191              array("PutPage", "new PutPage content", "XxXx"),
192              array('code' => 200, 'version' => 1, 'message' => "Page PutPage version 1 created"));
193     run_test($server, $debug, $output, "wiki.putPage",
194              array("PutPage", "new PutPage content", "XxXx"),
195              array('code' => 400, 'version' => 1, 'message' => "Page PutPage unchanged"));
196     run_test($server, $debug, $output, "wiki.putPage",
197              array("PutPage", "new PutPage content", "disallowed"),
198              array('code' => 401, 'version' => 0, 'message' => "no permission for disallowed"));
199
200     run_test($server, $debug, $output, "wiki.rssPleaseNotify", "HomePage", 0);
201     run_test($server, $debug, $output, "wiki.mailPasswordToUser", ADMIN_USER);
202
203     run_test($server, $debug, $output, "wiki.titleSearch", "Hom");
204 }
205
206 function run_stress_tests($server, $debug=0, $output=null) {
207
208     global $wiki_dmap;
209
210     run_no_param_test($server, $debug, $output, "wiki.getRPCVersionSupported");
211     // of the last day:
212     run_test($server, $debug, $output, "wiki.getRecentChanges", iso8601_encode(time()-86400, 1));
213     /* ... */
214 }
215
216 // a method to display an html form for invoking the script
217 function print_html_form($servers_list) {
218
219    echo <<< END
220 <h1>Choose an xmlrpc wiki server to run tests against</h1>
221 END;
222
223    print_servers_form($servers_list);
224 }
225
226 // some code which determines if we are in form display or response mode.
227 $server_list = get_wiki_servers();
228 $server = get_server_from_user($server_list);
229 if ($server) {
230    $debug = $GLOBALS['HTTP_GET_VARS']['debug'] || $GLOBALS['HTTP_GET_VARS']['start_debug'];
231    $output['version'] = $GLOBALS['HTTP_GET_VARS']['version'];
232    if ($server) {
233       $title = $server['title'];
234       echo "<h2><center>Results for $title</center></h2>";
235
236       if($GLOBALS['HTTP_GET_VARS']['stress'] == 1) {
237          run_stress_tests($server, $debug, $output);
238       }
239       else {
240          run_easy_tests($server, $debug, $output);
241       }
242    }
243    else {
244       echo "<h3>invalid option</h3>";
245    }
246 }
247 else {
248    print_html_form($server_list);
249 }