[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

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