[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Class remoteapicore_test
   5   */
   6  class remoteapicore_test extends DokuWikiTest {
   7  
   8      protected $userinfo;
   9      protected $oldAuthAcl;
  10      /** @var  RemoteAPI */
  11      protected $remote;
  12  
  13      public function setUp() {
  14          // we need a clean setup before each single test:
  15          DokuWikiTest::setUpBeforeClass();
  16  
  17          parent::setUp();
  18          global $conf;
  19          global $USERINFO;
  20          global $AUTH_ACL;
  21          global $auth;
  22          $this->oldAuthAcl = $AUTH_ACL;
  23          $this->userinfo = $USERINFO;
  24          $auth = new DokuWiki_Auth_Plugin();
  25  
  26          $conf['remote'] = 1;
  27          $conf['remoteuser'] = '@user';
  28          $conf['useacl'] = 0;
  29  
  30          $this->remote = new RemoteAPI();
  31      }
  32  
  33      public function tearDown() {
  34          parent::tearDown();
  35  
  36          global $USERINFO;
  37          global $AUTH_ACL;
  38  
  39          $USERINFO = $this->userinfo;
  40          $AUTH_ACL = $this->oldAuthAcl;
  41      }
  42  
  43      /** Delay writes of old revisions by a second. */
  44      public function handle_write(Doku_Event $event, $param) {
  45          if ($event->data[3] !== false) {
  46              $this->waitForTick();
  47          }
  48      }
  49  
  50      public function test_getVersion() {
  51          $this->assertEquals(getVersion(), $this->remote->call('dokuwiki.getVersion'));
  52      }
  53  
  54      public function test_getPageList() {
  55          $file1 = wikiFN('wiki:dokuwiki');
  56          $file2 = wikiFN('wiki:syntax');
  57          $expected = array(
  58              array(
  59                  'id' => 'wiki:dokuwiki',
  60                  'rev' => filemtime($file1),
  61                  'mtime' => filemtime($file1),
  62                  'size' => filesize($file1),
  63                  'hash' => md5(trim(rawWiki('wiki:dokuwiki')))
  64              ),
  65              array(
  66                  'id' => 'wiki:syntax',
  67                  'rev' => filemtime($file2),
  68                  'mtime' => filemtime($file2),
  69                  'size' => filesize($file2),
  70                  'hash' => md5(trim(rawWiki('wiki:syntax')))
  71              )
  72          );
  73          $params = array(
  74              'wiki:',
  75              array(
  76                  'depth' => 0, // 0 for all
  77                  'hash' => 1,
  78                  'skipacl' => 1 // is ignored
  79              )
  80          );
  81          $this->assertEquals($expected, $this->remote->call('dokuwiki.getPagelist', $params));
  82      }
  83  
  84      public function test_search() {
  85          $id = 'wiki:syntax';
  86          $file = wikiFN($id);
  87  
  88          idx_addPage($id); //full text search depends on index
  89          $expected = array(
  90              array(
  91                  'id' => $id,
  92                  'score' => 1,
  93                  'rev' => filemtime($file),
  94                  'mtime' => filemtime($file),
  95                  'size' => filesize($file),
  96                  'snippet' => ' a footnote)) by using double parentheses.
  97  
  98  ===== <strong class="search_hit">Sectioning</strong> =====
  99  
 100  You can use up to five different levels of',
 101                  'title' => 'wiki:syntax'
 102              )
 103          );
 104          $params = array('Sectioning');
 105          $this->assertEquals($expected, $this->remote->call('dokuwiki.search', $params));
 106      }
 107  
 108      public function test_getTime() {
 109          $timeexpect = time();
 110          $timeactual = $this->remote->call('dokuwiki.getTime');
 111          $this->assertTrue(($timeexpect <= $timeactual) && ($timeactual <= $timeexpect + 1));
 112      }
 113  
 114      public function test_setLocks() {
 115          $expected = array(
 116              'locked' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting'),
 117              'lockfail' => array(),
 118              'unlocked' => array(),
 119              'unlockfail' => array(),
 120          );
 121          $params = array(
 122              array(
 123                  'lock' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting'),
 124                  'unlock' => array()
 125              )
 126          );
 127          $this->assertEquals($expected, $this->remote->call('dokuwiki.setLocks', $params));
 128  
 129          $expected = array(
 130              'locked' => array(),
 131              'lockfail' => array(),
 132              'unlocked' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting'),
 133              'unlockfail' => array('nonexisting2'),
 134          );
 135          $params = array(
 136              array(
 137                  'lock' => array(),
 138                  'unlock' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting', 'nonexisting2')
 139              )
 140          );
 141          $this->assertEquals($expected, $this->remote->call('dokuwiki.setLocks', $params));
 142      }
 143  
 144      public function test_getTitle() {
 145          global $conf;
 146          $this->assertEquals($conf['title'], $this->remote->call('dokuwiki.getTitle'));
 147      }
 148  
 149      public function test_putPage() {
 150          $id = 'putpage';
 151  
 152          $content = "====Title====\nText";
 153          $params = array(
 154              $id,
 155              $content,
 156              array(
 157                  'minor' => false,
 158                  'sum' => 'Summary of nice text'
 159              )
 160          );
 161          $this->assertTrue($this->remote->call('wiki.putPage', $params));
 162          $this->assertEquals($content, rawWiki($id));
 163  
 164          //remove page
 165          $params = array(
 166              $id,
 167              '',
 168              array(
 169                  'minor' => false,
 170              )
 171          );
 172          $this->assertTrue($this->remote->call('wiki.putPage', $params));
 173          $this->assertFileNotExists(wikiFN($id));
 174      }
 175  
 176      public function test_getPage() {
 177          $id = 'getpage';
 178          $content = 'a test';
 179          saveWikiText($id, $content, 'test for getpage');
 180  
 181          $params = array($id);
 182          $this->assertEquals($content, $this->remote->call('wiki.getPage', $params));
 183      }
 184  
 185      public function test_appendPage() {
 186          $id = 'appendpage';
 187          $content = 'a test';
 188          $morecontent = "\nOther text";
 189          saveWikiText($id, $content, 'local');
 190  
 191          $params = array(
 192              $id,
 193              $morecontent,
 194              array()
 195          );
 196          $this->assertEquals(true, $this->remote->call('dokuwiki.appendPage', $params));
 197          $this->assertEquals($content . $morecontent, rawWiki($id));
 198      }
 199  
 200      public function test_getPageVersion() {
 201          $id = 'pageversion';
 202          $file = wikiFN($id);
 203  
 204          saveWikiText($id, 'first version', 'first');
 205          $rev1 = filemtime($file);
 206          clearstatcache(false, $file);
 207          $this->waitForTick(true);
 208          saveWikiText($id, 'second version', 'second');
 209          $rev2 = filemtime($file);
 210  
 211          $params = array($id, '');
 212          $this->assertEquals('second version', $this->remote->call('wiki.getPageVersion', $params), 'no revision given');
 213  
 214          $params = array($id, $rev1);
 215          $this->assertEquals('first version', $this->remote->call('wiki.getPageVersion', $params), '1st revision given');
 216  
 217          $params = array($id, $rev2);
 218          $this->assertEquals('second version', $this->remote->call('wiki.getPageVersion', $params), '2nd revision given');
 219  
 220          $params = array($id, 1234);
 221          $this->assertEquals('', $this->remote->call('wiki.getPageVersion', $params), 'Non existing revision given');
 222  
 223          $params = array('foobar', 1234);
 224          $this->assertEquals('', $this->remote->call('wiki.getPageVersion', $params), 'Non existing page given');
 225      }
 226  
 227      public function test_getPageHTML() {
 228          $id = 'htmltest';
 229          $content = "====Title====\nText";
 230          $html = "\n<h3 class=\"sectionedit1\" id=\"title\">Title</h3>\n<div class=\"level3\">\n\n<p>\nText\n</p>\n\n</div>\n";
 231  
 232          saveWikiText($id, $content, 'htmltest');
 233  
 234          $params = array($id);
 235          $this->assertEquals($html, $this->remote->call('wiki.getPageHTML', $params));
 236      }
 237  
 238      public function test_getPageHTMLVersion() {
 239          $id = 'htmltest';
 240          $file = wikiFN($id);
 241  
 242          $content1 = "====Title====\nText";
 243          $html1 = "\n<h3 class=\"sectionedit1\" id=\"title\">Title</h3>\n<div class=\"level3\">\n\n<p>\nText\n</p>\n\n</div>\n";
 244          $content2 = "====Foobar====\nText Bamm";
 245          $html2 = "\n<h3 class=\"sectionedit1\" id=\"foobar\">Foobar</h3>\n<div class=\"level3\">\n\n<p>\nText Bamm\n</p>\n\n</div>\n";
 246  
 247          saveWikiText($id, $content1, 'first');
 248          $rev1 = filemtime($file);
 249          clearstatcache(false, $file);
 250          $this->waitForTick(true);
 251          saveWikiText($id, $content2, 'second');
 252          $rev2 = filemtime($file);
 253  
 254          $params = array($id, '');
 255          $this->assertEquals($html2, $this->remote->call('wiki.getPageHTMLVersion', $params), 'no revision given');
 256  
 257          $params = array($id, $rev1);
 258          $this->assertEquals($html1, $this->remote->call('wiki.getPageHTMLVersion', $params), '1st revision given');
 259  
 260          $params = array($id, $rev2);
 261          $this->assertEquals($html2, $this->remote->call('wiki.getPageHTMLVersion', $params), '2nd revision given');
 262  
 263          $params = array($id, 1234);
 264          $this->assertEquals('', $this->remote->call('wiki.getPageHTMLVersion', $params), 'Non existing revision given');
 265  
 266          $params = array('foobar', 1234);
 267          $this->assertEquals('', $this->remote->call('wiki.getPageHTMLVersion', $params), 'Non existing page given');
 268      }
 269  
 270      public function test_getAllPages() {
 271          // all pages depends on index
 272          idx_addPage('wiki:syntax');
 273          idx_addPage('wiki:dokuwiki');
 274  
 275          $file1 = wikiFN('wiki:syntax');
 276          $file2 = wikiFN('wiki:dokuwiki');
 277  
 278          $expected = array(
 279              array(
 280                  'id' => 'wiki:syntax',
 281                  'perms' => 8,
 282                  'size' => filesize($file1),
 283                  'lastModified' => filemtime($file1)
 284              ),
 285              array(
 286                  'id' => 'wiki:dokuwiki',
 287                  'perms' => 8,
 288                  'size' => filesize($file2),
 289                  'lastModified' => filemtime($file2)
 290              )
 291          );
 292          $this->assertEquals($expected, $this->remote->call('wiki.getAllPages'));
 293      }
 294  
 295      public function test_getBacklinks() {
 296          saveWikiText('linky', '[[wiki:syntax]]', 'test');
 297          // backlinks need index
 298          idx_addPage('wiki:syntax');
 299          idx_addPage('linky');
 300  
 301          $params = array('wiki:syntax');
 302          $result = $this->remote->call('wiki.getBackLinks', $params);
 303          $this->assertTrue(count($result) > 0);
 304          $this->assertEquals(ft_backlinks('wiki:syntax'), $result);
 305      }
 306  
 307      public function test_getPageInfo() {
 308          $id = 'pageinfo';
 309          $file = wikiFN($id);
 310  
 311          saveWikiText($id, 'test', 'test');
 312  
 313          $expected = array(
 314              'name' => $id,
 315              'lastModified' => filemtime($file),
 316              'author' => clientIP(),
 317              'version' => filemtime($file)
 318          );
 319          $params = array($id);
 320          $this->assertEquals($expected, $this->remote->call('wiki.getPageInfo', $params));
 321      }
 322  
 323      public function test_getPageInfoVersion() {
 324          $id = 'pageinfo';
 325          $file = wikiFN($id);
 326  
 327          saveWikiText($id, 'first version', 'first');
 328          $rev1 = filemtime($file);
 329          clearstatcache(false, $file);
 330          $this->waitForTick(true);
 331          saveWikiText($id, 'second version', 'second');
 332          $rev2 = filemtime($file);
 333  
 334          $expected = array(
 335              'name' => $id,
 336              'lastModified' => $rev2,
 337              'author' => clientIP(),
 338              'version' => $rev2
 339          );
 340          $params = array($id, '');
 341          $this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), 'no revision given');
 342  
 343          $expected = array(
 344              'name' => $id,
 345              'lastModified' => $rev1,
 346              'author' => clientIP(),
 347              'version' => $rev1
 348          );
 349          $params = array($id, $rev1);
 350          $this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), '1st revision given');
 351  
 352          $expected = array(
 353              'name' => $id,
 354              'lastModified' => $rev2,
 355              'author' => clientIP(),
 356              'version' => $rev2
 357          );
 358          $params = array($id, $rev2);
 359          $this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), '2nd revision given');
 360      }
 361  
 362      public function test_getRecentChanges() {
 363  
 364          saveWikiText('pageone', 'test', 'test');
 365          $rev1 = filemtime(wikiFN('pageone'));
 366          saveWikiText('pagetwo', 'test', 'test');
 367          $rev2 = filemtime(wikiFN('pagetwo'));
 368  
 369          $expected = array(
 370              array(
 371                  'name' => 'pageone',
 372                  'lastModified' => $rev1,
 373                  'author' => '',
 374                  'version' => $rev1,
 375                  'perms' => 8,
 376                  'size' => 4
 377              ),
 378              array(
 379                  'name' => 'pagetwo',
 380                  'lastModified' => $rev2,
 381                  'author' => '',
 382                  'version' => $rev2,
 383                  'perms' => 8,
 384                  'size' => 4
 385              )
 386          );
 387          $params = array(strtotime("-1 year"));
 388          $this->assertEquals($expected, $this->remote->call('wiki.getRecentChanges', $params));
 389      }
 390  
 391      public function test_getPageVersions() {
 392          /** @var $EVENT_HANDLER Doku_Event_Handler */
 393          global $EVENT_HANDLER;
 394          $EVENT_HANDLER->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_write');
 395          global $conf;
 396  
 397          $id = 'revpage';
 398          $file = wikiFN($id);
 399  
 400          $rev = array();
 401          for($i = 0; $i < 6; $i++) {
 402              $this->waitForTick();
 403              saveWikiText($id, "rev$i", "rev$i");
 404              clearstatcache(false, $file);
 405              $rev[$i] = filemtime($file);
 406          }
 407  
 408          $params = array($id, 0);
 409          $versions = $this->remote->call('wiki.getPageVersions', $params);
 410          $this->assertEquals(6, count($versions));
 411          $this->assertEquals($rev[5], $versions[0]['version']);
 412          $this->assertEquals($rev[4], $versions[1]['version']);
 413          $this->assertEquals($rev[3], $versions[2]['version']);
 414          $this->assertEquals($rev[2], $versions[3]['version']);
 415          $this->assertEquals($rev[1], $versions[4]['version']);
 416          $this->assertEquals($rev[0], $versions[5]['version']);
 417  
 418          $params = array($id, 1); // offset 1
 419          $versions = $this->remote->call('wiki.getPageVersions', $params);
 420          $this->assertEquals(5, count($versions));
 421          $this->assertEquals($rev[4], $versions[0]['version']);
 422          $this->assertEquals($rev[3], $versions[1]['version']);
 423          $this->assertEquals($rev[2], $versions[2]['version']);
 424          $this->assertEquals($rev[1], $versions[3]['version']);
 425          $this->assertEquals($rev[0], $versions[4]['version']);
 426  
 427          $conf['recent'] = 3; //set number of results per page
 428  
 429          $params = array($id, 0); // first page
 430          $versions = $this->remote->call('wiki.getPageVersions', $params);
 431          $this->assertEquals(3, count($versions));
 432          $this->assertEquals($rev[5], $versions[0]['version']);
 433          $this->assertEquals($rev[4], $versions[1]['version']);
 434          $this->assertEquals($rev[3], $versions[2]['version']);
 435  
 436          $params = array($id, $conf['recent']); // second page
 437          $versions = $this->remote->call('wiki.getPageVersions', $params);
 438          $this->assertEquals(3, count($versions));
 439          $this->assertEquals($rev[2], $versions[0]['version']);
 440          $this->assertEquals($rev[1], $versions[1]['version']);
 441          $this->assertEquals($rev[0], $versions[2]['version']);
 442  
 443          $params = array($id, $conf['recent'] * 2); // third page
 444          $versions = $this->remote->call('wiki.getPageVersions', $params);
 445          $this->assertEquals(0, count($versions));
 446      }
 447  
 448      public function test_deleteUser()
 449      {
 450          global $conf, $auth;
 451          $auth = new Mock_Auth_Plugin();
 452          $conf['remote'] = 1;
 453          $conf['remoteuser'] = 'testuser';
 454          $_SERVER['REMOTE_USER'] = 'testuser';
 455          $params = [
 456              ['testuser']
 457          ];
 458          $actualCallResult = $this->remote->call('dokuwiki.deleteUsers', $params);
 459          $this->assertTrue($actualCallResult);
 460      }
 461  
 462      public function test_aclCheck() {
 463          $id = 'aclpage';
 464  
 465          $params = array($id);
 466          $this->assertEquals(AUTH_UPLOAD, $this->remote->call('wiki.aclCheck', $params));
 467  
 468          global $conf;
 469          global $AUTH_ACL, $USERINFO;
 470          $conf['useacl'] = 1;
 471          $_SERVER['REMOTE_USER'] = 'john';
 472          $USERINFO['grps'] = array('user');
 473          $AUTH_ACL = array(
 474              '*                  @ALL           0',
 475              '*                  @user          2', //edit
 476          );
 477  
 478          $params = array($id);
 479          $this->assertEquals(AUTH_EDIT, $this->remote->call('wiki.aclCheck', $params));
 480      }
 481  
 482      public function test_getXMLRPCAPIVersion() {
 483          $this->assertEquals(DOKU_API_VERSION, $this->remote->call('dokuwiki.getXMLRPCAPIVersion'));
 484      }
 485  
 486      public function test_getRPCVersionSupported() {
 487          $this->assertEquals(2, $this->remote->call('wiki.getRPCVersionSupported'));
 488      }
 489  
 490      public function test_listLinks() {
 491          $localdoku = array(
 492              'type' => 'local',
 493              'page' => 'DokuWiki',
 494              'href' => DOKU_BASE . DOKU_SCRIPT . '?id=DokuWiki'
 495          );
 496          $expected = array(  //no local links
 497                              $localdoku,
 498                              array(
 499                                  'type' => 'extern',
 500                                  'page' => 'http://www.freelists.org',
 501                                  'href' => 'http://www.freelists.org'
 502                              ),
 503                              array(
 504                                  'type' => 'extern',
 505                                  'page' => 'https://tools.ietf.org/html/rfc1855',
 506                                  'href' => 'https://tools.ietf.org/html/rfc1855'
 507                              ),
 508                              array(
 509                                  'type' => 'extern',
 510                                  'page' => 'http://www.catb.org/~esr/faqs/smart-questions.html',
 511                                  'href' => 'http://www.catb.org/~esr/faqs/smart-questions.html'
 512                              ),
 513                              $localdoku,
 514                              $localdoku
 515          );
 516          $params = array('mailinglist');
 517          $this->assertEquals($expected, $this->remote->call('wiki.listLinks', $params));
 518      }
 519  
 520      public function test_coreattachments() {
 521          global $conf;
 522          global $AUTH_ACL, $USERINFO;
 523  
 524          $filecontent = io_readFile(mediaFN('wiki:dokuwiki-128.png'), false);
 525          $params = array('test:dokuwiki-128_2.png', $filecontent, array('ow' => false));
 526          $this->assertEquals('test:dokuwiki-128_2.png', $this->remote->call('wiki.putAttachment', $params)); //prints a success div
 527  
 528          $params = array('test:dokuwiki-128_2.png');
 529          $this->assertEquals($filecontent, $this->remote->call('wiki.getAttachment', $params));
 530          $rev = filemtime(mediaFN('test:dokuwiki-128_2.png'));
 531  
 532          $expected = array(
 533              'lastModified' => $rev,
 534              'size' => 27895,
 535          );
 536          $params = array('test:dokuwiki-128_2.png');
 537          $this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
 538  
 539          $params = array(strtotime("-5 year"));
 540          $expected = array(
 541              array(
 542                  'name' => 'test:dokuwiki-128_2.png',
 543                  'lastModified' => $rev,
 544                  'author' => '',
 545                  'version' => $rev,
 546                  'perms' => 8,
 547                  'size' => 27895 //actual size, not size change
 548              )
 549          );
 550          $this->assertEquals($expected, $this->remote->call('wiki.getRecentMediaChanges', $params));
 551  
 552          $this->waitForTick(true);
 553          $conf['useacl'] = 1;
 554          $_SERVER['REMOTE_USER'] = 'john';
 555          $USERINFO['grps'] = array('user');
 556          $AUTH_ACL = array(
 557              '*                  @ALL           0',
 558              '*                  @user          16',
 559          );
 560  
 561          $params = array('test:dokuwiki-128_2.png');
 562          $this->assertEquals(0, $this->remote->call('wiki.deleteAttachment', $params));
 563  
 564          $rev2 = filemtime($conf['media_changelog']);
 565          $expected = array(
 566              'lastModified' => $rev2,
 567              'size' => 0,
 568          );
 569          $params = array('test:dokuwiki-128_2.png');
 570          $this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
 571  
 572          $expected = array(
 573              'lastModified' => 0,
 574              'size' => 0,
 575          );
 576          $params = array('test:nonexisting.png');
 577          $this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
 578  
 579          $media1 = mediaFN('wiki:dokuwiki-128.png');
 580          $expected = array(
 581              array(
 582                  'id' => 'wiki:dokuwiki-128.png',
 583                  'file' => 'dokuwiki-128.png',
 584                  'size' => filesize($media1),
 585                  'mtime' => filemtime($media1),
 586                  'writable' => 1,
 587                  'isimg' => 1,
 588                  'hash' => md5(io_readFile($media1, false)),
 589                  'perms' => 16,
 590                  'lastModified' => filemtime($media1)
 591              )
 592          );
 593          $params = array(
 594              'wiki:',
 595              array(
 596                  'depth' => 0, // 0 for all
 597                  'hash' => 1,
 598                  'skipacl' => 1, // is ignored
 599                  'showmsg' => true, //useless??
 600                  'pattern' => '/128/' //filter
 601              )
 602          );
 603          $this->assertEquals($expected, $this->remote->call('wiki.getAttachments', $params));
 604      }
 605  
 606  }