PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/_test/tests/inc/remoteapicore.test.php

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