[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/inc/ -> remote.test.php (source)

   1  <?php
   2  
   3  use dokuwiki\test\mock\AuthPlugin;
   4  use dokuwiki\Extension\RemotePlugin;
   5  use dokuwiki\Remote\Api;
   6  use dokuwiki\Remote\RemoteException;
   7  
   8  class RemoteAPICoreTest {
   9  
  10      function __getRemoteInfo() {
  11          return array(
  12              'wiki.stringTestMethod' => array(
  13                  'args' => array(),
  14                  'return' => 'string',
  15                  'doc' => 'Test method',
  16                  'name' => 'stringTestMethod',
  17              ), 'wiki.intTestMethod' => array(
  18                  'args' => array(),
  19                  'return' => 'int',
  20                  'doc' => 'Test method',
  21                  'name' => 'intTestMethod',
  22              ), 'wiki.floatTestMethod' => array(
  23                  'args' => array(),
  24                  'return' => 'float',
  25                  'doc' => 'Test method',
  26                  'name' => 'floatTestMethod',
  27              ), 'wiki.dateTestMethod' => array(
  28                  'args' => array(),
  29                  'return' => 'date',
  30                  'doc' => 'Test method',
  31                  'name' => 'dateTestMethod',
  32              ), 'wiki.fileTestMethod' => array(
  33                  'args' => array(),
  34                  'return' => 'file',
  35                  'doc' => 'Test method',
  36                  'name' => 'fileTestMethod',
  37              ), 'wiki.voidTestMethod' => array(
  38                  'args' => array(),
  39                  'return' => 'void',
  40                  'doc' => 'Test method',
  41                  'name' => 'voidTestMethod',
  42              ),  'wiki.oneStringArgMethod' => array(
  43                  'args' => array('string'),
  44                  'return' => 'string',
  45                  'doc' => 'Test method',
  46                  'name' => 'oneStringArgMethod',
  47              ), 'wiki.twoArgMethod' => array(
  48                  'args' => array('string', 'int'),
  49                  'return' => 'array',
  50                  'doc' => 'Test method',
  51                  'name' => 'twoArgMethod',
  52              ), 'wiki.twoArgWithDefaultArg' => array(
  53                  'args' => array('string', 'string'),
  54                  'return' => 'string',
  55                  'doc' => 'Test method',
  56                  'name' => 'twoArgWithDefaultArg',
  57              ), 'wiki.publicCall' => array(
  58                  'args' => array(),
  59                  'return' => 'boolean',
  60                  'doc' => 'testing for public access',
  61                  'name' => 'publicCall',
  62                  'public' => 1
  63              )
  64          );
  65      }
  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;}
  76  
  77  }
  78  
  79  class remote_plugin_testplugin extends RemotePlugin {
  80      function _getMethods() {
  81          return array(
  82              'method1' => array(
  83                  'args' => array(),
  84                  'return' => 'void'
  85              ), 'methodString' => array(
  86                  'args' => array(),
  87                  'return' => 'string'
  88              ), 'method2' => array(
  89                  'args' => array('string', 'int'),
  90                  'return' => 'array',
  91                  'name' => 'method2',
  92              ), 'method2ext' => array(
  93                  'args' => array('string', 'int', 'bool'),
  94                  'return' => 'array',
  95                  'name' => 'method2',
  96              ), 'publicCall' => array(
  97                  'args' => array(),
  98                  'return' => 'boolean',
  99                  'doc' => 'testing for public access',
 100                  'name' => 'publicCall',
 101                  'public' => 1
 102              )
 103          );
 104      }
 105  
 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;}
 110  }
 111  
 112  class remote_plugin_testplugin2 extends RemotePlugin {
 113      /**
 114       * This is a dummy method
 115       *
 116       * @param string $str some more parameter description
 117       * @param int $int
 118       * @param bool $bool
 119       * @param Object $unknown
 120       * @return array
 121       */
 122      public function commented($str, $int, $bool, $unknown) { return array($str, $int, $bool); }
 123  
 124      private function privateMethod() {return true;}
 125      protected function protectedMethod() {return true;}
 126      public function _underscore() {return true;}
 127  }
 128  
 129  
 130  
 131  class remote_test extends DokuWikiTest {
 132  
 133      protected $userinfo;
 134  
 135      /** @var  Api */
 136      protected $remote;
 137  
 138      function setUp() {
 139          parent::setUp();
 140          global $plugin_controller;
 141          global $conf;
 142          global $USERINFO;
 143          global $auth;
 144  
 145          parent::setUp();
 146  
 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();
 154                  } else {
 155                      return new remote_plugin_testplugin();
 156                  }
 157              }
 158          );
 159          $plugin_controller = $pluginManager;
 160  
 161          $conf['remote'] = 1;
 162          $conf['remoteuser'] = '!!not set!!';
 163          $conf['useacl'] = 0;
 164  
 165          $this->userinfo = $USERINFO;
 166          $this->remote = new Api();
 167  
 168          $auth = new AuthPlugin();
 169      }
 170  
 171      function tearDown() {
 172          global $USERINFO;
 173          $USERINFO = $this->userinfo;
 174  
 175      }
 176  
 177      function test_pluginMethods() {
 178          $methods = $this->remote->getPluginMethods();
 179          $actual = array_keys($methods);
 180          sort($actual);
 181          $expect = array(
 182              'plugin.testplugin.method1',
 183              'plugin.testplugin.method2',
 184              'plugin.testplugin.methodString',
 185              'plugin.testplugin.method2ext',
 186              'plugin.testplugin.publicCall',
 187  
 188              'plugin.testplugin2.commented'
 189          );
 190          sort($expect);
 191          $this->assertEquals($expect,$actual);
 192      }
 193  
 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->assertContains('This is a dummy method', $methods['plugin.testplugin2.commented']['doc']);
 200          $this->assertContains('string $str some more parameter description', $methods['plugin.testplugin2.commented']['doc']);
 201      }
 202  
 203      function test_hasAccessSuccess() {
 204          global $conf;
 205          $conf['remoteuser'] = '';
 206          $this->assertTrue($this->remote->hasAccess());
 207      }
 208  
 209      /**
 210       * @expectedException dokuwiki\Remote\AccessDeniedException
 211       */
 212      function test_hasAccessFail() {
 213          global $conf;
 214          $conf['remote'] = 0;
 215          // the hasAccess() should throw a Exception to keep the same semantics with xmlrpc.php.
 216          // because the user(xmlrpc) check remote before .--> (!$conf['remote']) die('XML-RPC server not enabled.');
 217          // so it must be a Exception when get here.
 218          $this->remote->hasAccess();
 219      }
 220  
 221      function test_hasAccessFailAcl() {
 222          global $conf;
 223          $conf['useacl'] = 1;
 224          $this->assertFalse($this->remote->hasAccess());
 225      }
 226  
 227      function test_hasAccessSuccessAclEmptyRemoteUser() {
 228          global $conf;
 229          $conf['useacl'] = 1;
 230          $conf['remoteuser'] = '';
 231  
 232          $this->assertTrue($this->remote->hasAccess());
 233      }
 234  
 235      function test_hasAccessSuccessAcl() {
 236          global $conf;
 237          global $USERINFO;
 238          $conf['useacl'] = 1;
 239          $conf['remoteuser'] = '@grp,@grp2';
 240          $USERINFO['grps'] = array('grp');
 241          $this->assertTrue($this->remote->hasAccess());
 242      }
 243  
 244      function test_hasAccessFailAcl2() {
 245          global $conf;
 246          global $USERINFO;
 247          $conf['useacl'] = 1;
 248          $conf['remoteuser'] = '@grp';
 249          $USERINFO['grps'] = array('grp1');
 250  
 251          $this->assertFalse($this->remote->hasAccess());
 252      }
 253  
 254  
 255      function test_forceAccessSuccess() {
 256          global $conf;
 257          $conf['remote'] = 1;
 258          $conf['remoteuser'] = '';
 259          $this->remote->forceAccess(); // no exception should occur
 260          $this->assertTrue(true); // avoid being marked as risky for having no assertion
 261      }
 262  
 263      function test_forceAccessFail() {
 264          global $conf;
 265          $conf['remote'] = 0;
 266  
 267          try {
 268              $this->remote->forceAccess();
 269              $this->fail('Expects RemoteException to be raised');
 270          } catch (RemoteException $th) {
 271              $this->assertEquals(-32604, $th->getCode());
 272          }
 273      }
 274  
 275      function test_generalCoreFunctionWithoutArguments() {
 276          global $conf;
 277          global $USERINFO;
 278          $conf['remote'] = 1;
 279          $conf['remoteuser'] = '';
 280          $conf['useacl'] = 1;
 281          $USERINFO['grps'] = array('grp');
 282          $remoteApi = new Api();
 283          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 284  
 285          $this->assertEquals($remoteApi->call('wiki.stringTestMethod'), 'success');
 286          $this->assertEquals($remoteApi->call('wiki.intTestMethod'), 42);
 287          $this->assertEquals($remoteApi->call('wiki.floatTestMethod'), 3.14159265);
 288          $this->assertEquals($remoteApi->call('wiki.dateTestMethod'), 2623452346);
 289          $this->assertEquals($remoteApi->call('wiki.fileTestMethod'), 'file content');
 290          $this->assertEquals($remoteApi->call('wiki.voidTestMethod'), null);
 291      }
 292  
 293      function test_generalCoreFunctionOnArgumentMismatch() {
 294          global $conf;
 295          $conf['remote'] = 1;
 296          $remoteApi = new Api();
 297          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 298  
 299          try {
 300              $remoteApi->call('wiki.voidTestMethod', array('something'));
 301              $this->fail('Expects RemoteException to be raised');
 302          } catch (RemoteException $th) {
 303              $this->assertEquals(-32604, $th->getCode());
 304          }
 305      }
 306  
 307      function test_generalCoreFunctionWithArguments() {
 308          global $conf;
 309          global $USERINFO;
 310          $conf['remote'] = 1;
 311          $conf['remoteuser'] = '';
 312          $conf['useacl'] = 1;
 313  
 314          $remoteApi = new Api();
 315          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 316  
 317          $this->assertEquals($remoteApi->call('wiki.oneStringArgMethod', array('string')), 'string');
 318          $this->assertEquals($remoteApi->call('wiki.twoArgMethod', array('string', 1)), array('string' , 1));
 319          $this->assertEquals($remoteApi->call('wiki.twoArgWithDefaultArg', array('string')), array('string', 'default'));
 320          $this->assertEquals($remoteApi->call('wiki.twoArgWithDefaultArg', array('string', 'another')), array('string', 'another'));
 321      }
 322  
 323      function test_generalCoreFunctionOnArgumentMissing() {
 324          global $conf;
 325          $conf['remote'] = 1;
 326          $conf['remoteuser'] = '';
 327          $remoteApi = new Api();
 328          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 329  
 330          try {
 331              $remoteApi->call('wiki.twoArgWithDefaultArg', array());
 332              $this->fail('Expects RemoteException to be raised');
 333          } catch (RemoteException $th) {
 334              $this->assertEquals(-32603, $th->getCode());
 335          }
 336      }
 337  
 338      function test_pluginCallMethods() {
 339          global $conf;
 340          global $USERINFO;
 341          $conf['remote'] = 1;
 342          $conf['remoteuser'] = '';
 343          $conf['useacl'] = 1;
 344  
 345          $remoteApi = new Api();
 346          $this->assertEquals($remoteApi->call('plugin.testplugin.method1'), null);
 347          $this->assertEquals($remoteApi->call('plugin.testplugin.method2', array('string', 7)), array('string', 7, false));
 348          $this->assertEquals($remoteApi->call('plugin.testplugin.method2ext', array('string', 7, true)), array('string', 7, true));
 349          $this->assertEquals($remoteApi->call('plugin.testplugin.methodString'), 'success');
 350      }
 351  
 352      function test_pluginCallMethodsOnArgumentMissing() {
 353          global $conf;
 354          $conf['remote'] = 1;
 355          $conf['remoteuser'] = '';
 356          $remoteApi = new Api();
 357          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 358  
 359          try {
 360              $remoteApi->call('plugin.testplugin.method2', array());
 361              $this->fail('Expects RemoteException to be raised');
 362          } catch (RemoteException $th) {
 363              $this->assertEquals(-32603, $th->getCode());
 364          }
 365      }
 366  
 367      function test_notExistingCall() {
 368          global $conf;
 369          $conf['remote'] = 1;
 370  
 371          $remoteApi = new Api();
 372          try {
 373              $remoteApi->call('dose not exist');
 374              $this->fail('Expects RemoteException to be raised');
 375          } catch (RemoteException $th) {
 376              $this->assertEquals(-32603, $th->getCode());
 377          }
 378      }
 379  
 380      function test_publicCallCore() {
 381          global $conf;
 382          $conf['useacl'] = 1;
 383          $remoteApi = new Api();
 384          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 385          $this->assertTrue($remoteApi->call('wiki.publicCall'));
 386      }
 387  
 388      function test_publicCallPlugin() {
 389          global $conf;
 390          $conf['useacl'] = 1;
 391          $remoteApi = new Api();
 392          $this->assertTrue($remoteApi->call('plugin.testplugin.publicCall'));
 393      }
 394  
 395      /**
 396       * @expectedException dokuwiki\Remote\AccessDeniedException
 397       */
 398      function test_publicCallCoreDeny() {
 399          global $conf;
 400          $conf['useacl'] = 1;
 401          $remoteApi = new Api();
 402          $remoteApi->getCoreMethods(new RemoteAPICoreTest());
 403          $remoteApi->call('wiki.stringTestMethod');
 404      }
 405  
 406      /**
 407       * @expectedException dokuwiki\Remote\AccessDeniedException
 408       */
 409      function test_publicCallPluginDeny() {
 410          global $conf;
 411          $conf['useacl'] = 1;
 412          $remoteApi = new Api();
 413          $remoteApi->call('plugin.testplugin.methodString');
 414      }
 415  
 416      function test_pluginCallCustomPath() {
 417          global $conf;
 418          global $USERINFO;
 419          $conf['remote'] = 1;
 420          $conf['remoteuser'] = '';
 421          $conf['useacl'] = 1;
 422          global $EVENT_HANDLER;
 423          $EVENT_HANDLER->register_hook('RPC_CALL_ADD', 'BEFORE', $this, 'pluginCallCustomPathRegister');
 424  
 425          $remoteApi = new Api();
 426          $result = $remoteApi->call('custom.path');
 427          $this->assertEquals($result, 'success');
 428      }
 429  
 430      function pluginCallCustomPathRegister(&$event, $param) {
 431          $event->data['custom.path'] = array('testplugin', 'methodString');
 432      }
 433  }