[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

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