3 use dokuwiki\test\mock\AuthPlugin
;
4 use dokuwiki\Extension\RemotePlugin
;
5 use dokuwiki\Remote\Api
;
6 use dokuwiki\Remote\RemoteException
;
8 class RemoteAPICoreTest
{
10 function getRemoteInfo() {
12 'wiki.stringTestMethod' => array(
15 'doc' => 'Test method',
16 'name' => 'stringTestMethod',
17 ), 'wiki.intTestMethod' => array(
20 'doc' => 'Test method',
21 'name' => 'intTestMethod',
22 ), 'wiki.floatTestMethod' => array(
25 'doc' => 'Test method',
26 'name' => 'floatTestMethod',
27 ), 'wiki.dateTestMethod' => array(
30 'doc' => 'Test method',
31 'name' => 'dateTestMethod',
32 ), 'wiki.fileTestMethod' => array(
35 'doc' => 'Test method',
36 'name' => 'fileTestMethod',
37 ), 'wiki.voidTestMethod' => array(
40 'doc' => 'Test method',
41 'name' => 'voidTestMethod',
42 ), 'wiki.oneStringArgMethod' => array(
43 'args' => array('string'),
45 'doc' => 'Test method',
46 'name' => 'oneStringArgMethod',
47 ), 'wiki.twoArgMethod' => array(
48 'args' => array('string', 'int'),
50 'doc' => 'Test method',
51 'name' => 'twoArgMethod',
52 ), 'wiki.twoArgWithDefaultArg' => array(
53 'args' => array('string', 'string'),
55 'doc' => 'Test method',
56 'name' => 'twoArgWithDefaultArg',
57 ), 'wiki.publicCall' => array(
59 'return' => 'boolean',
60 'doc' => 'testing for public access',
61 'name' => 'publicCall',
66 function stringTestMethod() { return 'success'; }
67 function intTestMethod() { return 42; }
68 function floatTestMethod() { return 3.14159265; }
69 function dateTestMethod() { return 2623452346; }
70 function fileTestMethod() { return 'file content'; }
71 function voidTestMethod() { return null; }
72 function oneStringArgMethod($arg) {return $arg; }
73 function twoArgMethod($string, $int) { return array($string, $int); }
74 function twoArgWithDefaultArg($string1, $string2 = 'default') { return array($string1, $string2); }
75 function publicCall() {return true;}
79 class remote_plugin_testplugin
extends RemotePlugin
{
80 function _getMethods() {
85 ), 'methodString' => array(
88 ), 'method2' => array(
89 'args' => array('string', 'int'),
92 ), 'method2ext' => array(
93 'args' => array('string', 'int', 'bool'),
96 ), 'publicCall' => array(
98 'return' => 'boolean',
99 'doc' => 'testing for public access',
100 'name' => 'publicCall',
106 function method1() { return null; }
107 function methodString() { return 'success'; }
108 function method2($str, $int, $bool = false) { return array($str, $int, $bool); }
109 function publicCall() {return true;}
112 class remote_plugin_testplugin2
extends RemotePlugin
{
114 * This is a dummy method
116 * @param string $str some more parameter description
119 * @param Object $unknown
122 public function commented($str, $int, $bool, $unknown) { return array($str, $int, $bool); }
124 private function privateMethod() {return true;}
125 protected function protectedMethod() {return true;}
126 public function _underscore() {return true;}
131 class remote_test
extends DokuWikiTest
{
138 function setUp() : void
{
140 global $plugin_controller;
147 // mock plugin controller to return our test plugins
148 $pluginManager = $this->createMock('dokuwiki\Extension\PluginController');
149 $pluginManager->method('getList')->willReturn(array('testplugin', 'testplugin2'));
150 $pluginManager->method('load')->willReturnCallback(
151 function($type, $plugin) {
152 if($plugin == 'testplugin2') {
153 return new remote_plugin_testplugin2();
155 return new remote_plugin_testplugin();
159 $plugin_controller = $pluginManager;
162 $conf['remoteuser'] = '!!not set!!';
165 $this->userinfo
= $USERINFO;
166 $this->remote
= new Api();
168 $auth = new AuthPlugin();
171 function tearDown() : void
{
173 $USERINFO = $this->userinfo
;
177 function test_pluginMethods() {
178 $methods = $this->remote
->getPluginMethods();
179 $actual = array_keys($methods);
182 'plugin.testplugin.method1',
183 'plugin.testplugin.method2',
184 'plugin.testplugin.methodString',
185 'plugin.testplugin.method2ext',
186 'plugin.testplugin.publicCall',
188 'plugin.testplugin2.commented'
191 $this->assertEquals($expect,$actual);
194 function test_pluginDescriptors() {
195 $methods = $this->remote
->getPluginMethods();
196 $this->assertEquals(array('string','int','bool','string'), $methods['plugin.testplugin2.commented']['args']);
197 $this->assertEquals('array', $methods['plugin.testplugin2.commented']['return']);
198 $this->assertEquals(0, $methods['plugin.testplugin2.commented']['public']);
199 $this->assertStringContainsString('This is a dummy method', $methods['plugin.testplugin2.commented']['doc']);
200 $this->assertStringContainsString('string $str some more parameter description', $methods['plugin.testplugin2.commented']['doc']);
203 function test_hasAccessSuccess() {
205 $conf['remoteuser'] = '';
206 $this->assertTrue($this->remote
->hasAccess());
209 function test_hasAccessFail() {
212 // the hasAccess() should throw a Exception to keep the same semantics with xmlrpc.php.
213 // because the user(xmlrpc) check remote before .--> (!$conf['remote']) die('XML-RPC server not enabled.');
214 // so it must be a Exception when get here.
215 $this->expectException(\dokuwiki\Remote\AccessDeniedException
::class);
216 $this->remote
->hasAccess();
219 function test_hasAccessFailAcl() {
222 $this->assertFalse($this->remote
->hasAccess());
225 function test_hasAccessSuccessAclEmptyRemoteUser() {
228 $conf['remoteuser'] = '';
230 $this->assertTrue($this->remote
->hasAccess());
233 function test_hasAccessSuccessAcl() {
237 $conf['remoteuser'] = '@grp,@grp2';
238 $USERINFO['grps'] = array('grp');
239 $this->assertTrue($this->remote
->hasAccess());
242 function test_hasAccessFailAcl2() {
246 $conf['remoteuser'] = '@grp';
247 $USERINFO['grps'] = array('grp1');
249 $this->assertFalse($this->remote
->hasAccess());
253 function test_forceAccessSuccess() {
256 $conf['remoteuser'] = '';
257 $this->remote
->forceAccess(); // no exception should occur
258 $this->assertTrue(true); // avoid being marked as risky for having no assertion
261 function test_forceAccessFail() {
266 $this->remote
->forceAccess();
267 $this->fail('Expects RemoteException to be raised');
268 } catch (RemoteException
$th) {
269 $this->assertEquals(-32604, $th->getCode());
273 function test_generalCoreFunctionWithoutArguments() {
277 $conf['remoteuser'] = '';
279 $USERINFO['grps'] = array('grp');
280 $remoteApi = new Api();
281 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
283 $this->assertEquals($remoteApi->call('wiki.stringTestMethod'), 'success');
284 $this->assertEquals($remoteApi->call('wiki.intTestMethod'), 42);
285 $this->assertEquals($remoteApi->call('wiki.floatTestMethod'), 3.14159265);
286 $this->assertEquals($remoteApi->call('wiki.dateTestMethod'), 2623452346);
287 $this->assertEquals($remoteApi->call('wiki.fileTestMethod'), 'file content');
288 $this->assertEquals($remoteApi->call('wiki.voidTestMethod'), null);
291 function test_generalCoreFunctionOnArgumentMismatch() {
294 $remoteApi = new Api();
295 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
298 $remoteApi->call('wiki.voidTestMethod', array('something'));
299 $this->fail('Expects RemoteException to be raised');
300 } catch (RemoteException
$th) {
301 $this->assertEquals(-32604, $th->getCode());
305 function test_generalCoreFunctionWithArguments() {
309 $conf['remoteuser'] = '';
312 $remoteApi = new Api();
313 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
315 $this->assertEquals($remoteApi->call('wiki.oneStringArgMethod', array('string')), 'string');
316 $this->assertEquals($remoteApi->call('wiki.twoArgMethod', array('string', 1)), array('string' , 1));
317 $this->assertEquals($remoteApi->call('wiki.twoArgWithDefaultArg', array('string')), array('string', 'default'));
318 $this->assertEquals($remoteApi->call('wiki.twoArgWithDefaultArg', array('string', 'another')), array('string', 'another'));
321 function test_generalCoreFunctionOnArgumentMissing() {
324 $conf['remoteuser'] = '';
325 $remoteApi = new Api();
326 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
329 $remoteApi->call('wiki.twoArgWithDefaultArg', array());
330 $this->fail('Expects RemoteException to be raised');
331 } catch (RemoteException
$th) {
332 $this->assertEquals(-32603, $th->getCode());
336 function test_pluginCallMethods() {
340 $conf['remoteuser'] = '';
343 $remoteApi = new Api();
344 $this->assertEquals($remoteApi->call('plugin.testplugin.method1'), null);
345 $this->assertEquals($remoteApi->call('plugin.testplugin.method2', array('string', 7)), array('string', 7, false));
346 $this->assertEquals($remoteApi->call('plugin.testplugin.method2ext', array('string', 7, true)), array('string', 7, true));
347 $this->assertEquals($remoteApi->call('plugin.testplugin.methodString'), 'success');
350 function test_pluginCallMethodsOnArgumentMissing() {
353 $conf['remoteuser'] = '';
354 $remoteApi = new Api();
355 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
358 $remoteApi->call('plugin.testplugin.method2', array());
359 $this->fail('Expects RemoteException to be raised');
360 } catch (RemoteException
$th) {
361 $this->assertEquals(-32603, $th->getCode());
365 function test_notExistingCall() {
369 $this->expectException(RemoteException
::class);
370 $this->expectExceptionCode(-32603);
372 $remoteApi = new Api();
373 $remoteApi->call('invalid method'); // no '.'
374 $remoteApi->call('does.not exist'); // unknown method type
377 function test_publicCallCore() {
380 $remoteApi = new Api();
381 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
382 $this->assertTrue($remoteApi->call('wiki.publicCall'));
385 function test_publicCallPlugin() {
388 $remoteApi = new Api();
389 $this->assertTrue($remoteApi->call('plugin.testplugin.publicCall'));
392 function test_publicCallCoreDeny() {
395 $this->expectException(\dokuwiki\Remote\AccessDeniedException
::class);
396 $remoteApi = new Api();
397 $remoteApi->getCoreMethods(new RemoteAPICoreTest());
398 $remoteApi->call('wiki.stringTestMethod');
401 function test_publicCallPluginDeny() {
404 $this->expectException(\dokuwiki\Remote\AccessDeniedException
::class);
405 $remoteApi = new Api();
406 $remoteApi->call('plugin.testplugin.methodString');
409 function test_pluginCallCustomPath() {
413 $conf['remoteuser'] = '';
415 global $EVENT_HANDLER;
416 $EVENT_HANDLER->register_hook('RPC_CALL_ADD', 'BEFORE', $this, 'pluginCallCustomPathRegister');
418 $remoteApi = new Api();
419 $result = $remoteApi->call('custom.path');
420 $this->assertEquals($result, 'success');
423 function pluginCallCustomPathRegister(&$event, $param) {
424 $event->data
['custom.path'] = array('testplugin', 'methodString');