]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/xmlrpc-client.php
include [all] Include and file path should be devided with single space. File path...
[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 // a method to run wiki tests against remote server. tests described at bottom.
170 function run_easy_tests($server, $debug=0, $output = null) {
171
172     //global $wiki_dmap;
173
174     run_test($server, $debug, $output, "wiki.getRPCVersionSupported", '', 1);
175
176     // getRecentChanges of the last day:
177     // Note: may crash with dba on index.php, not on RPC2.php
178     run_test($server, $debug, $output, "wiki.getRecentChanges", iso8601_encode(time()-86400));
179
180     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.");
181     run_test($server, $debug, $output, "wiki.getPageVersion", array("HomePage", 1));
182     run_test($server, $debug, $output, "wiki.getPageHTML", "HomePage");
183     run_test($server, $debug, $output, "wiki.getPageHTMLVersion", array("HomePage", 1));
184     run_test($server, $debug, $output, "wiki.getAllPages");
185     run_test($server, $debug, $output, "wiki.getPageInfo", "HomePage");
186     run_test($server, $debug, $output, "wiki.getPageInfoVersion", array("HomePage", 1));
187     run_test($server, $debug, $output, "wiki.listLinks", "HomePage");
188
189     run_test($server, $debug, $output, "wiki.putPage",
190              array("PutPage", "new PutPage content", "XxXx"),
191              array('code' => 200, 'version' => 1, 'message' => "Page PutPage version 1 created"));
192     run_test($server, $debug, $output, "wiki.putPage",
193              array("PutPage", "new PutPage content", "XxXx"),
194              array('code' => 400, 'version' => 1, 'message' => "Page PutPage unchanged"));
195     run_test($server, $debug, $output, "wiki.putPage",
196              array("PutPage", "new PutPage content", "disallowed"),
197              array('code' => 401, 'version' => 0, 'message' => "no permission for disallowed"));
198
199     run_test($server, $debug, $output, "wiki.rssPleaseNotify", "HomePage", 0);
200     run_test($server, $debug, $output, "wiki.mailPasswordToUser", ADMIN_USER);
201
202     run_test($server, $debug, $output, "wiki.titleSearch", "Hom");
203 }
204
205 function run_stress_tests($server, $debug=0, $output=null) {
206
207     global $wiki_dmap;
208
209     run_no_param_test($server, $debug, $output, "wiki.getRPCVersionSupported");
210     // of the last day:
211     run_test($server, $debug, $output, "wiki.getRecentChanges", iso8601_encode(time()-86400, 1));
212     /* ... */
213 }
214
215 // a method to display an html form for invoking the script
216 function print_html_form($servers_list) {
217
218    echo <<< END
219 <h1>Choose an xmlrpc wiki server to run tests against</h1>
220 END;
221
222    print_servers_form($servers_list);
223 }
224
225 // some code which determines if we are in form display or response mode.
226 $server_list = get_wiki_servers();
227 $server = get_server_from_user($server_list);
228 if ($server) {
229    $debug = $GLOBALS['HTTP_GET_VARS']['debug'] || $GLOBALS['HTTP_GET_VARS']['start_debug'];
230    $output['version'] = $GLOBALS['HTTP_GET_VARS']['version'];
231    if ($server) {
232       $title = $server['title'];
233       echo "<h2><center>Results for $title</center></h2>";
234
235       if($GLOBALS['HTTP_GET_VARS']['stress'] == 1) {
236          run_stress_tests($server, $debug, $output);
237       }
238       else {
239          run_easy_tests($server, $debug, $output);
240       }
241    }
242    else {
243       echo "<h3>invalid option</h3>";
244    }
245 }
246 else {
247    print_html_form($server_list);
248 }