Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3670 lines
92KB

  1. <?php
  2. /**
  3. * ApiTest class.
  4. */
  5. namespace Friendica\Test;
  6. use Friendica\App;
  7. use Friendica\Core\Config;
  8. use Friendica\Core\PConfig;
  9. use Friendica\Network\BadRequestException;
  10. use Friendica\Network\HTTPException;
  11. use Friendica\Render\FriendicaSmarty;
  12. /**
  13. * Tests for the API functions.
  14. *
  15. * Functions that use header() need to be tested in a separate process.
  16. * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
  17. */
  18. class ApiTest extends DatabaseTest
  19. {
  20. /**
  21. * Create variables used by tests.
  22. */
  23. protected function setUp()
  24. {
  25. global $a;
  26. parent::setUp();
  27. // User data that the test database is populated with
  28. $this->selfUser = [
  29. 'id' => 42,
  30. 'name' => 'Self contact',
  31. 'nick' => 'selfcontact',
  32. 'nurl' => 'http://localhost/profile/selfcontact'
  33. ];
  34. $this->otherUser = [
  35. 'id' => 43,
  36. 'name' => 'othercontact',
  37. 'nick' => 'othercontact',
  38. 'nurl' => 'http://localhost/profile/othercontact'
  39. ];
  40. // User ID that we know is not in the database
  41. $this->wrongUserId = 666;
  42. // Most API require login so we force the session
  43. $_SESSION = [
  44. 'allow_api' => true,
  45. 'authenticated' => true,
  46. 'uid' => $this->selfUser['id']
  47. ];
  48. // Reusable App object
  49. $this->app = new App(__DIR__.'/../');
  50. $a = $this->app;
  51. // Default config
  52. Config::set('config', 'hostname', 'localhost');
  53. Config::set('system', 'throttle_limit_day', 100);
  54. Config::set('system', 'throttle_limit_week', 100);
  55. Config::set('system', 'throttle_limit_month', 100);
  56. Config::set('system', 'theme', 'system_theme');
  57. }
  58. /**
  59. * Assert that an user array contains expected keys.
  60. * @param array $user User array
  61. * @return void
  62. */
  63. private function assertSelfUser(array $user)
  64. {
  65. $this->assertEquals($this->selfUser['id'], $user['uid']);
  66. $this->assertEquals($this->selfUser['id'], $user['cid']);
  67. $this->assertEquals(1, $user['self']);
  68. $this->assertEquals('Friendica', $user['location']);
  69. $this->assertEquals($this->selfUser['name'], $user['name']);
  70. $this->assertEquals($this->selfUser['nick'], $user['screen_name']);
  71. $this->assertEquals('dfrn', $user['network']);
  72. $this->assertTrue($user['verified']);
  73. }
  74. /**
  75. * Assert that an user array contains expected keys.
  76. * @param array $user User array
  77. * @return void
  78. */
  79. private function assertOtherUser(array $user)
  80. {
  81. $this->assertEquals($this->otherUser['id'], $user['id']);
  82. $this->assertEquals($this->otherUser['id'], $user['id_str']);
  83. $this->assertEquals(0, $user['self']);
  84. $this->assertEquals($this->otherUser['name'], $user['name']);
  85. $this->assertEquals($this->otherUser['nick'], $user['screen_name']);
  86. $this->assertFalse($user['verified']);
  87. }
  88. /**
  89. * Assert that a status array contains expected keys.
  90. * @param array $status Status array
  91. * @return void
  92. */
  93. private function assertStatus(array $status)
  94. {
  95. $this->assertInternalType('string', $status['text']);
  96. $this->assertInternalType('int', $status['id']);
  97. // We could probably do more checks here.
  98. }
  99. /**
  100. * Assert that a list array contains expected keys.
  101. * @param array $list List array
  102. * @return void
  103. */
  104. private function assertList(array $list)
  105. {
  106. $this->assertInternalType('string', $list['name']);
  107. $this->assertInternalType('int', $list['id']);
  108. $this->assertInternalType('string', $list['id_str']);
  109. $this->assertContains($list['mode'], ['public', 'private']);
  110. // We could probably do more checks here.
  111. }
  112. /**
  113. * Assert that the string is XML and contain the root element.
  114. * @param string $result XML string
  115. * @param string $root_element Root element name
  116. * @return void
  117. */
  118. private function assertXml($result, $root_element)
  119. {
  120. $this->assertStringStartsWith('<?xml version="1.0"?>', $result);
  121. $this->assertContains('<'.$root_element, $result);
  122. // We could probably do more checks here.
  123. }
  124. /**
  125. * Get the path to a temporary empty PNG image.
  126. * @return string Path
  127. */
  128. private function getTempImage()
  129. {
  130. $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
  131. file_put_contents(
  132. $tmpFile,
  133. base64_decode(
  134. // Empty 1x1 px PNG image
  135. 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
  136. )
  137. );
  138. return $tmpFile;
  139. }
  140. /**
  141. * Test the api_user() function.
  142. * @return void
  143. */
  144. public function testApiUser()
  145. {
  146. $this->assertEquals($this->selfUser['id'], api_user());
  147. }
  148. /**
  149. * Test the api_user() function with an unallowed user.
  150. * @return void
  151. */
  152. public function testApiUserWithUnallowedUser()
  153. {
  154. $_SESSION = ['allow_api' => false];
  155. $this->assertEquals(false, api_user());
  156. }
  157. /**
  158. * Test the api_source() function.
  159. * @return void
  160. */
  161. public function testApiSource()
  162. {
  163. $this->assertEquals('api', api_source());
  164. }
  165. /**
  166. * Test the api_source() function with a Twidere user agent.
  167. * @return void
  168. */
  169. public function testApiSourceWithTwidere()
  170. {
  171. $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
  172. $this->assertEquals('Twidere', api_source());
  173. }
  174. /**
  175. * Test the api_source() function with a GET parameter.
  176. * @return void
  177. */
  178. public function testApiSourceWithGet()
  179. {
  180. $_GET['source'] = 'source_name';
  181. $this->assertEquals('source_name', api_source());
  182. }
  183. /**
  184. * Test the api_date() function.
  185. * @return void
  186. */
  187. public function testApiDate()
  188. {
  189. $this->assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
  190. }
  191. /**
  192. * Test the api_register_func() function.
  193. * @return void
  194. */
  195. public function testApiRegisterFunc()
  196. {
  197. global $API;
  198. $this->assertNull(
  199. api_register_func(
  200. 'api_path',
  201. function () {
  202. },
  203. true,
  204. 'method'
  205. )
  206. );
  207. $this->assertTrue($API['api_path']['auth']);
  208. $this->assertEquals('method', $API['api_path']['method']);
  209. $this->assertTrue(is_callable($API['api_path']['func']));
  210. }
  211. /**
  212. * Test the api_login() function without any login.
  213. * @return void
  214. * @runInSeparateProcess
  215. * @expectedException Friendica\Network\HTTPException\UnauthorizedException
  216. */
  217. public function testApiLoginWithoutLogin()
  218. {
  219. api_login($this->app);
  220. }
  221. /**
  222. * Test the api_login() function with a bad login.
  223. * @return void
  224. * @runInSeparateProcess
  225. * @expectedException Friendica\Network\HTTPException\UnauthorizedException
  226. */
  227. public function testApiLoginWithBadLogin()
  228. {
  229. $_SERVER['PHP_AUTH_USER'] = 'user@server';
  230. api_login($this->app);
  231. }
  232. /**
  233. * Test the api_login() function with oAuth.
  234. * @return void
  235. */
  236. public function testApiLoginWithOauth()
  237. {
  238. $this->markTestIncomplete('Can we test this easily?');
  239. }
  240. /**
  241. * Test the api_login() function with authentication provided by an addon.
  242. * @return void
  243. */
  244. public function testApiLoginWithAddonAuth()
  245. {
  246. $this->markTestIncomplete('Can we test this easily?');
  247. }
  248. /**
  249. * Test the api_login() function with a correct login.
  250. * @return void
  251. * @runInSeparateProcess
  252. */
  253. public function testApiLoginWithCorrectLogin()
  254. {
  255. $_SERVER['PHP_AUTH_USER'] = 'Test user';
  256. $_SERVER['PHP_AUTH_PW'] = 'password';
  257. api_login($this->app);
  258. }
  259. /**
  260. * Test the api_login() function with a remote user.
  261. * @return void
  262. * @runInSeparateProcess
  263. * @expectedException Friendica\Network\HTTPException\UnauthorizedException
  264. */
  265. public function testApiLoginWithRemoteUser()
  266. {
  267. $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
  268. api_login($this->app);
  269. }
  270. /**
  271. * Test the api_check_method() function.
  272. * @return void
  273. */
  274. public function testApiCheckMethod()
  275. {
  276. $this->assertFalse(api_check_method('method'));
  277. }
  278. /**
  279. * Test the api_check_method() function with a correct method.
  280. * @return void
  281. */
  282. public function testApiCheckMethodWithCorrectMethod()
  283. {
  284. $_SERVER['REQUEST_METHOD'] = 'method';
  285. $this->assertTrue(api_check_method('method'));
  286. }
  287. /**
  288. * Test the api_check_method() function with a wildcard.
  289. * @return void
  290. */
  291. public function testApiCheckMethodWithWildcard()
  292. {
  293. $this->assertTrue(api_check_method('*'));
  294. }
  295. /**
  296. * Test the api_call() function.
  297. * @return void
  298. * @runInSeparateProcess
  299. */
  300. public function testApiCall()
  301. {
  302. global $API;
  303. $API['api_path'] = [
  304. 'method' => 'method',
  305. 'func' => function () {
  306. return ['data' => ['some_data']];
  307. }
  308. ];
  309. $_SERVER['REQUEST_METHOD'] = 'method';
  310. $_GET['callback'] = 'callback_name';
  311. $this->app->query_string = 'api_path';
  312. $this->assertEquals(
  313. 'callback_name(["some_data"])',
  314. api_call($this->app)
  315. );
  316. }
  317. /**
  318. * Test the api_call() function with the profiled enabled.
  319. * @return void
  320. * @runInSeparateProcess
  321. */
  322. public function testApiCallWithProfiler()
  323. {
  324. global $API;
  325. $API['api_path'] = [
  326. 'method' => 'method',
  327. 'func' => function () {
  328. return ['data' => ['some_data']];
  329. }
  330. ];
  331. $_SERVER['REQUEST_METHOD'] = 'method';
  332. Config::set('system', 'profiler', true);
  333. Config::set('rendertime', 'callstack', true);
  334. $this->app->callstack = [
  335. 'database' => ['some_function' => 200],
  336. 'database_write' => ['some_function' => 200],
  337. 'cache' => ['some_function' => 200],
  338. 'cache_write' => ['some_function' => 200],
  339. 'network' => ['some_function' => 200]
  340. ];
  341. $this->app->query_string = 'api_path';
  342. $this->assertEquals(
  343. '["some_data"]',
  344. api_call($this->app)
  345. );
  346. }
  347. /**
  348. * Test the api_call() function without any result.
  349. * @return void
  350. * @runInSeparateProcess
  351. */
  352. public function testApiCallWithNoResult()
  353. {
  354. global $API;
  355. $API['api_path'] = [
  356. 'method' => 'method',
  357. 'func' => function () {
  358. return false;
  359. }
  360. ];
  361. $_SERVER['REQUEST_METHOD'] = 'method';
  362. $this->app->query_string = 'api_path';
  363. $this->assertEquals(
  364. '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
  365. api_call($this->app)
  366. );
  367. }
  368. /**
  369. * Test the api_call() function with an unimplemented API.
  370. * @return void
  371. * @runInSeparateProcess
  372. */
  373. public function testApiCallWithUninplementedApi()
  374. {
  375. $this->assertEquals(
  376. '{"status":{"error":"Not Implemented","code":"501 Not Implemented","request":""}}',
  377. api_call($this->app)
  378. );
  379. }
  380. /**
  381. * Test the api_call() function with a JSON result.
  382. * @return void
  383. * @runInSeparateProcess
  384. */
  385. public function testApiCallWithJson()
  386. {
  387. global $API;
  388. $API['api_path'] = [
  389. 'method' => 'method',
  390. 'func' => function () {
  391. return ['data' => ['some_data']];
  392. }
  393. ];
  394. $_SERVER['REQUEST_METHOD'] = 'method';
  395. $this->app->query_string = 'api_path.json';
  396. $this->assertEquals(
  397. '["some_data"]',
  398. api_call($this->app)
  399. );
  400. }
  401. /**
  402. * Test the api_call() function with an XML result.
  403. * @return void
  404. * @runInSeparateProcess
  405. */
  406. public function testApiCallWithXml()
  407. {
  408. global $API;
  409. $API['api_path'] = [
  410. 'method' => 'method',
  411. 'func' => function () {
  412. return 'some_data';
  413. }
  414. ];
  415. $_SERVER['REQUEST_METHOD'] = 'method';
  416. $this->app->query_string = 'api_path.xml';
  417. $this->assertEquals(
  418. 'some_data',
  419. api_call($this->app)
  420. );
  421. }
  422. /**
  423. * Test the api_call() function with an RSS result.
  424. * @return void
  425. * @runInSeparateProcess
  426. */
  427. public function testApiCallWithRss()
  428. {
  429. global $API;
  430. $API['api_path'] = [
  431. 'method' => 'method',
  432. 'func' => function () {
  433. return 'some_data';
  434. }
  435. ];
  436. $_SERVER['REQUEST_METHOD'] = 'method';
  437. $this->app->query_string = 'api_path.rss';
  438. $this->assertEquals(
  439. '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.
  440. 'some_data',
  441. api_call($this->app)
  442. );
  443. }
  444. /**
  445. * Test the api_call() function with an Atom result.
  446. * @return void
  447. * @runInSeparateProcess
  448. */
  449. public function testApiCallWithAtom()
  450. {
  451. global $API;
  452. $API['api_path'] = [
  453. 'method' => 'method',
  454. 'func' => function () {
  455. return 'some_data';
  456. }
  457. ];
  458. $_SERVER['REQUEST_METHOD'] = 'method';
  459. $this->app->query_string = 'api_path.atom';
  460. $this->assertEquals(
  461. '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.
  462. 'some_data',
  463. api_call($this->app)
  464. );
  465. }
  466. /**
  467. * Test the api_call() function with an unallowed method.
  468. * @return void
  469. * @runInSeparateProcess
  470. */
  471. public function testApiCallWithWrongMethod()
  472. {
  473. global $API;
  474. $API['api_path'] = ['method' => 'method'];
  475. $this->app->query_string = 'api_path';
  476. $this->assertEquals(
  477. '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
  478. api_call($this->app)
  479. );
  480. }
  481. /**
  482. * Test the api_call() function with an unauthorized user.
  483. * @return void
  484. * @runInSeparateProcess
  485. */
  486. public function testApiCallWithWrongAuth()
  487. {
  488. global $API;
  489. $API['api_path'] = [
  490. 'method' => 'method',
  491. 'auth' => true
  492. ];
  493. $_SERVER['REQUEST_METHOD'] = 'method';
  494. $_SESSION['authenticated'] = false;
  495. $this->app->query_string = 'api_path';
  496. $this->assertEquals(
  497. '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
  498. api_call($this->app)
  499. );
  500. }
  501. /**
  502. * Test the api_error() function with a JSON result.
  503. * @return void
  504. * @runInSeparateProcess
  505. */
  506. public function testApiErrorWithJson()
  507. {
  508. $this->assertEquals(
  509. '{"status":{"error":"error_message","code":"200 Friendica\\\\Network\\\\HTTP","request":""}}',
  510. api_error('json', new HTTPException('error_message'))
  511. );
  512. }
  513. /**
  514. * Test the api_error() function with an XML result.
  515. * @return void
  516. * @runInSeparateProcess
  517. */
  518. public function testApiErrorWithXml()
  519. {
  520. $this->assertEquals(
  521. '<?xml version="1.0"?>'.PHP_EOL.
  522. '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
  523. 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
  524. 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
  525. ' <error>error_message</error>'.PHP_EOL.
  526. ' <code>200 Friendica\Network\HTTP</code>'.PHP_EOL.
  527. ' <request/>'.PHP_EOL.
  528. '</status>'.PHP_EOL,
  529. api_error('xml', new HTTPException('error_message'))
  530. );
  531. }
  532. /**
  533. * Test the api_error() function with an RSS result.
  534. * @return void
  535. * @runInSeparateProcess
  536. */
  537. public function testApiErrorWithRss()
  538. {
  539. $this->assertEquals(
  540. '<?xml version="1.0"?>'.PHP_EOL.
  541. '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
  542. 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
  543. 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
  544. ' <error>error_message</error>'.PHP_EOL.
  545. ' <code>200 Friendica\Network\HTTP</code>'.PHP_EOL.
  546. ' <request/>'.PHP_EOL.
  547. '</status>'.PHP_EOL,
  548. api_error('rss', new HTTPException('error_message'))
  549. );
  550. }
  551. /**
  552. * Test the api_error() function with an Atom result.
  553. * @return void
  554. * @runInSeparateProcess
  555. */
  556. public function testApiErrorWithAtom()
  557. {
  558. $this->assertEquals(
  559. '<?xml version="1.0"?>'.PHP_EOL.
  560. '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
  561. 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
  562. 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
  563. ' <error>error_message</error>'.PHP_EOL.
  564. ' <code>200 Friendica\Network\HTTP</code>'.PHP_EOL.
  565. ' <request/>'.PHP_EOL.
  566. '</status>'.PHP_EOL,
  567. api_error('atom', new HTTPException('error_message'))
  568. );
  569. }
  570. /**
  571. * Test the api_rss_extra() function.
  572. * @return void
  573. */
  574. public function testApiRssExtra()
  575. {
  576. $user_info = ['url' => 'user_url'];
  577. $result = api_rss_extra($this->app, [], $user_info);
  578. $this->assertEquals($user_info, $result['$user']);
  579. $this->assertEquals($user_info['url'], $result['$rss']['alternate']);
  580. $this->assertArrayHasKey('self', $result['$rss']);
  581. $this->assertArrayHasKey('base', $result['$rss']);
  582. $this->assertArrayHasKey('updated', $result['$rss']);
  583. $this->assertArrayHasKey('atom_updated', $result['$rss']);
  584. $this->assertArrayHasKey('language', $result['$rss']);
  585. $this->assertArrayHasKey('logo', $result['$rss']);
  586. }
  587. /**
  588. * Test the api_rss_extra() function without any user info.
  589. * @return void
  590. * @runInSeparateProcess
  591. */
  592. public function testApiRssExtraWithoutUserInfo()
  593. {
  594. $result = api_rss_extra($this->app, [], null);
  595. $this->assertInternalType('array', $result['$user']);
  596. $this->assertArrayHasKey('alternate', $result['$rss']);
  597. $this->assertArrayHasKey('self', $result['$rss']);
  598. $this->assertArrayHasKey('base', $result['$rss']);
  599. $this->assertArrayHasKey('updated', $result['$rss']);
  600. $this->assertArrayHasKey('atom_updated', $result['$rss']);
  601. $this->assertArrayHasKey('language', $result['$rss']);
  602. $this->assertArrayHasKey('logo', $result['$rss']);
  603. }
  604. /**
  605. * Test the api_unique_id_to_nurl() function.
  606. * @return void
  607. */
  608. public function testApiUniqueIdToNurl()
  609. {
  610. $this->assertFalse(api_unique_id_to_nurl($this->wrongUserId));
  611. }
  612. /**
  613. * Test the api_unique_id_to_nurl() function with a correct ID.
  614. * @return void
  615. */
  616. public function testApiUniqueIdToNurlWithCorrectId()
  617. {
  618. $this->assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
  619. }
  620. /**
  621. * Test the api_get_user() function.
  622. * @return void
  623. * @runInSeparateProcess
  624. */
  625. public function testApiGetUser()
  626. {
  627. $user = api_get_user($this->app);
  628. $this->assertSelfUser($user);
  629. $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
  630. $this->assertEquals('6fdbe8', $user['profile_link_color']);
  631. $this->assertEquals('ededed', $user['profile_background_color']);
  632. }
  633. /**
  634. * Test the api_get_user() function with a Frio schema.
  635. * @return void
  636. * @runInSeparateProcess
  637. */
  638. public function testApiGetUserWithFrioSchema()
  639. {
  640. PConfig::set($this->selfUser['id'], 'frio', 'schema', 'red');
  641. $user = api_get_user($this->app);
  642. $this->assertSelfUser($user);
  643. $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
  644. $this->assertEquals('6fdbe8', $user['profile_link_color']);
  645. $this->assertEquals('ededed', $user['profile_background_color']);
  646. }
  647. /**
  648. * Test the api_get_user() function with a custom Frio schema.
  649. * @return void
  650. * @runInSeparateProcess
  651. */
  652. public function testApiGetUserWithCustomFrioSchema()
  653. {
  654. PConfig::set($this->selfUser['id'], 'frio', 'schema', '---');
  655. PConfig::set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
  656. PConfig::set($this->selfUser['id'], 'frio', 'link_color', '#123456');
  657. PConfig::set($this->selfUser['id'], 'frio', 'background_color', '#123456');
  658. $user = api_get_user($this->app);
  659. $this->assertSelfUser($user);
  660. $this->assertEquals('123456', $user['profile_sidebar_fill_color']);
  661. $this->assertEquals('123456', $user['profile_link_color']);
  662. $this->assertEquals('123456', $user['profile_background_color']);
  663. }
  664. /**
  665. * Test the api_get_user() function with an empty Frio schema.
  666. * @return void
  667. * @runInSeparateProcess
  668. */
  669. public function testApiGetUserWithEmptyFrioSchema()
  670. {
  671. PConfig::set($this->selfUser['id'], 'frio', 'schema', '---');
  672. $user = api_get_user($this->app);
  673. $this->assertSelfUser($user);
  674. $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
  675. $this->assertEquals('6fdbe8', $user['profile_link_color']);
  676. $this->assertEquals('ededed', $user['profile_background_color']);
  677. }
  678. /**
  679. * Test the api_get_user() function with an user that is not allowed to use the API.
  680. * @return void
  681. * @runInSeparateProcess
  682. */
  683. public function testApiGetUserWithoutApiUser()
  684. {
  685. $_SERVER['PHP_AUTH_USER'] = 'Test user';
  686. $_SERVER['PHP_AUTH_PW'] = 'password';
  687. $_SESSION['allow_api'] = false;
  688. $this->assertFalse(api_get_user($this->app));
  689. }
  690. /**
  691. * Test the api_get_user() function with an user ID in a GET parameter.
  692. * @return void
  693. * @runInSeparateProcess
  694. */
  695. public function testApiGetUserWithGetId()
  696. {
  697. $_GET['user_id'] = $this->otherUser['id'];
  698. $this->assertOtherUser(api_get_user($this->app));
  699. }
  700. /**
  701. * Test the api_get_user() function with a wrong user ID in a GET parameter.
  702. * @return void
  703. * @runInSeparateProcess
  704. * @expectedException Friendica\Network\HTTPException\BadRequestException
  705. */
  706. public function testApiGetUserWithWrongGetId()
  707. {
  708. $_GET['user_id'] = $this->wrongUserId;
  709. $this->assertOtherUser(api_get_user($this->app));
  710. }
  711. /**
  712. * Test the api_get_user() function with an user name in a GET parameter.
  713. * @return void
  714. * @runInSeparateProcess
  715. */
  716. public function testApiGetUserWithGetName()
  717. {
  718. $_GET['screen_name'] = $this->selfUser['nick'];
  719. $this->assertSelfUser(api_get_user($this->app));
  720. }
  721. /**
  722. * Test the api_get_user() function with a profile URL in a GET parameter.
  723. * @return void
  724. * @runInSeparateProcess
  725. */
  726. public function testApiGetUserWithGetUrl()
  727. {
  728. $_GET['profileurl'] = $this->selfUser['nurl'];
  729. $this->assertSelfUser(api_get_user($this->app));
  730. }
  731. /**
  732. * Test the api_get_user() function with an user ID in the API path.
  733. * @return void
  734. * @runInSeparateProcess
  735. */
  736. public function testApiGetUserWithNumericCalledApi()
  737. {
  738. global $called_api;
  739. $called_api = ['api_path'];
  740. $this->app->argv[1] = $this->otherUser['id'].'.json';
  741. $this->assertOtherUser(api_get_user($this->app));
  742. }
  743. /**
  744. * Test the api_get_user() function with the $called_api global variable.
  745. * @return void
  746. * @runInSeparateProcess
  747. */
  748. public function testApiGetUserWithCalledApi()
  749. {
  750. global $called_api;
  751. $called_api = ['api_path'];
  752. $this->assertSelfUser(api_get_user($this->app));
  753. }
  754. /**
  755. * Test the api_get_user() function with a valid user.
  756. * @return void
  757. * @runInSeparateProcess
  758. */
  759. public function testApiGetUserWithCorrectUser()
  760. {
  761. $this->assertOtherUser(api_get_user($this->app, $this->otherUser['id']));
  762. }
  763. /**
  764. * Test the api_get_user() function with a wrong user ID.
  765. * @return void
  766. * @runInSeparateProcess
  767. * @expectedException Friendica\Network\HTTPException\BadRequestException
  768. */
  769. public function testApiGetUserWithWrongUser()
  770. {
  771. $this->assertOtherUser(api_get_user($this->app, $this->wrongUserId));
  772. }
  773. /**
  774. * Test the api_get_user() function with a 0 user ID.
  775. * @return void
  776. * @runInSeparateProcess
  777. */
  778. public function testApiGetUserWithZeroUser()
  779. {
  780. $this->assertSelfUser(api_get_user($this->app, 0));
  781. }
  782. /**
  783. * Test the api_item_get_user() function.
  784. * @return void
  785. * @runInSeparateProcess
  786. */
  787. public function testApiItemGetUser()
  788. {
  789. $users = api_item_get_user($this->app, []);
  790. $this->assertSelfUser($users[0]);
  791. }
  792. /**
  793. * Test the api_item_get_user() function with a different item parent.
  794. * @return void
  795. */
  796. public function testApiItemGetUserWithDifferentParent()
  797. {
  798. $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
  799. $this->assertSelfUser($users[0]);
  800. $this->assertEquals($users[0], $users[1]);
  801. }
  802. /**
  803. * Test the api_walk_recursive() function.
  804. * @return void
  805. */
  806. public function testApiWalkRecursive()
  807. {
  808. $array = ['item1'];
  809. $this->assertEquals(
  810. $array,
  811. api_walk_recursive(
  812. $array,
  813. function () {
  814. // Should we test this with a callback that actually does something?
  815. return true;
  816. }
  817. )
  818. );
  819. }
  820. /**
  821. * Test the api_walk_recursive() function with an array.
  822. * @return void
  823. */
  824. public function testApiWalkRecursiveWithArray()
  825. {
  826. $array = [['item1'], ['item2']];
  827. $this->assertEquals(
  828. $array,
  829. api_walk_recursive(
  830. $array,
  831. function () {
  832. // Should we test this with a callback that actually does something?
  833. return true;
  834. }
  835. )
  836. );
  837. }
  838. /**
  839. * Test the api_reformat_xml() function.
  840. * @return void
  841. */
  842. public function testApiReformatXml()
  843. {
  844. $item = true;
  845. $key = '';
  846. $this->assertTrue(api_reformat_xml($item, $key));
  847. $this->assertEquals('true', $item);
  848. }
  849. /**
  850. * Test the api_reformat_xml() function with a statusnet_api key.
  851. * @return void
  852. */
  853. public function testApiReformatXmlWithStatusnetKey()
  854. {
  855. $item = '';
  856. $key = 'statusnet_api';
  857. $this->assertTrue(api_reformat_xml($item, $key));
  858. $this->assertEquals('statusnet:api', $key);
  859. }
  860. /**
  861. * Test the api_reformat_xml() function with a friendica_api key.
  862. * @return void
  863. */
  864. public function testApiReformatXmlWithFriendicaKey()
  865. {
  866. $item = '';
  867. $key = 'friendica_api';
  868. $this->assertTrue(api_reformat_xml($item, $key));
  869. $this->assertEquals('friendica:api', $key);
  870. }
  871. /**
  872. * Test the api_create_xml() function.
  873. * @return void
  874. */
  875. public function testApiCreateXml()
  876. {
  877. $this->assertEquals(
  878. '<?xml version="1.0"?>'.PHP_EOL.
  879. '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
  880. 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
  881. 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
  882. ' <data>some_data</data>'.PHP_EOL.
  883. '</root_element>'.PHP_EOL,
  884. api_create_xml(['data' => ['some_data']], 'root_element')
  885. );
  886. }
  887. /**
  888. * Test the api_create_xml() function without any XML namespace.
  889. * @return void
  890. */
  891. public function testApiCreateXmlWithoutNamespaces()
  892. {
  893. $this->assertEquals(
  894. '<?xml version="1.0"?>'.PHP_EOL.
  895. '<ok>'.PHP_EOL.
  896. ' <data>some_data</data>'.PHP_EOL.
  897. '</ok>'.PHP_EOL,
  898. api_create_xml(['data' => ['some_data']], 'ok')
  899. );
  900. }
  901. /**
  902. * Test the api_format_data() function.
  903. * @return void
  904. */
  905. public function testApiFormatData()
  906. {
  907. $data = ['some_data'];
  908. $this->assertEquals($data, api_format_data('root_element', 'json', $data));
  909. }
  910. /**
  911. * Test the api_format_data() function with an XML result.
  912. * @return void
  913. */
  914. public function testApiFormatDataWithXml()
  915. {
  916. $this->assertEquals(
  917. '<?xml version="1.0"?>'.PHP_EOL.
  918. '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
  919. 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
  920. 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
  921. ' <data>some_data</data>'.PHP_EOL.
  922. '</root_element>'.PHP_EOL,
  923. api_format_data('root_element', 'xml', ['data' => ['some_data']])
  924. );
  925. }
  926. /**
  927. * Test the api_account_verify_credentials() function.
  928. * @return void
  929. */
  930. public function testApiAccountVerifyCredentials()
  931. {
  932. $this->assertArrayHasKey('user', api_account_verify_credentials('json'));
  933. }
  934. /**
  935. * Test the api_account_verify_credentials() function without an authenticated user.
  936. * @return void
  937. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  938. */
  939. public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
  940. {
  941. $_SESSION['authenticated'] = false;
  942. api_account_verify_credentials('json');
  943. }
  944. /**
  945. * Test the requestdata() function.
  946. * @return void
  947. */
  948. public function testRequestdata()
  949. {
  950. $this->assertNull(requestdata('variable_name'));
  951. }
  952. /**
  953. * Test the requestdata() function with a POST parameter.
  954. * @return void
  955. */
  956. public function testRequestdataWithPost()
  957. {
  958. $_POST['variable_name'] = 'variable_value';
  959. $this->assertEquals('variable_value', requestdata('variable_name'));
  960. }
  961. /**
  962. * Test the requestdata() function with a GET parameter.
  963. * @return void
  964. */
  965. public function testRequestdataWithGet()
  966. {
  967. $_GET['variable_name'] = 'variable_value';
  968. $this->assertEquals('variable_value', requestdata('variable_name'));
  969. }
  970. /**
  971. * Test the api_statuses_mediap() function.
  972. * @return void
  973. */
  974. public function testApiStatusesMediap()
  975. {
  976. $this->app->argc = 2;
  977. $_FILES = [
  978. 'media' => [
  979. 'id' => 666,
  980. 'size' => 666,
  981. 'width' => 666,
  982. 'height' => 666,
  983. 'tmp_name' => $this->getTempImage(),
  984. 'type' => 'image/png'
  985. ]
  986. ];
  987. $_GET['status'] = '<b>Status content</b>';
  988. $result = api_statuses_mediap('json');
  989. $this->assertStatus($result['status']);
  990. }
  991. /**
  992. * Test the api_statuses_mediap() function without an authenticated user.
  993. * @return void
  994. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  995. */
  996. public function testApiStatusesMediapWithoutAuthenticatedUser()
  997. {
  998. $_SESSION['authenticated'] = false;
  999. api_statuses_mediap('json');
  1000. }
  1001. /**
  1002. * Test the api_statuses_update() function.
  1003. * @return void
  1004. */
  1005. public function testApiStatusesUpdate()
  1006. {
  1007. $_GET['status'] = 'Status content';
  1008. $_GET['in_reply_to_status_id'] = -1;
  1009. $_GET['lat'] = 48;
  1010. $_GET['long'] = 7;
  1011. $_FILES = [
  1012. 'media' => [
  1013. 'id' => 666,
  1014. 'size' => 666,
  1015. 'width' => 666,
  1016. 'height' => 666,
  1017. 'tmp_name' => $this->getTempImage(),
  1018. 'type' => 'image/png'
  1019. ]
  1020. ];
  1021. $result = api_statuses_update('json');
  1022. $this->assertStatus($result['status']);
  1023. }
  1024. /**
  1025. * Test the api_statuses_update() function with an HTML status.
  1026. * @return void
  1027. */
  1028. public function testApiStatusesUpdateWithHtml()
  1029. {
  1030. $_GET['htmlstatus'] = '<b>Status content</b>';
  1031. $result = api_statuses_update('json');
  1032. $this->assertStatus($result['status']);
  1033. }
  1034. /**
  1035. * Test the api_statuses_update() function without an authenticated user.
  1036. * @return void
  1037. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1038. */
  1039. public function testApiStatusesUpdateWithoutAuthenticatedUser()
  1040. {
  1041. $_SESSION['authenticated'] = false;
  1042. api_statuses_update('json');
  1043. }
  1044. /**
  1045. * Test the api_statuses_update() function with a parent status.
  1046. * @return void
  1047. */
  1048. public function testApiStatusesUpdateWithParent()
  1049. {
  1050. $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
  1051. }
  1052. /**
  1053. * Test the api_statuses_update() function with a media_ids parameter.
  1054. * @return void
  1055. */
  1056. public function testApiStatusesUpdateWithMediaIds()
  1057. {
  1058. $this->markTestIncomplete();
  1059. }
  1060. /**
  1061. * Test the api_statuses_update() function with the throttle limit reached.
  1062. * @return void
  1063. */
  1064. public function testApiStatusesUpdateWithDayThrottleReached()
  1065. {
  1066. $this->markTestIncomplete();
  1067. }
  1068. /**
  1069. * Test the api_media_upload() function.
  1070. * @return void
  1071. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1072. */
  1073. public function testApiMediaUpload()
  1074. {
  1075. api_media_upload();
  1076. }
  1077. /**
  1078. * Test the api_media_upload() function without an authenticated user.
  1079. * @return void
  1080. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1081. */
  1082. public function testApiMediaUploadWithoutAuthenticatedUser()
  1083. {
  1084. $_SESSION['authenticated'] = false;
  1085. api_media_upload();
  1086. }
  1087. /**
  1088. * Test the api_media_upload() function with an invalid uploaded media.
  1089. * @return void
  1090. * @expectedException Friendica\Network\HTTPException\InternalServerErrorException
  1091. */
  1092. public function testApiMediaUploadWithMedia()
  1093. {
  1094. $_FILES = [
  1095. 'media' => [
  1096. 'id' => 666
  1097. ]
  1098. ];
  1099. api_media_upload();
  1100. }
  1101. /**
  1102. * Test the api_media_upload() function with an valid uploaded media.
  1103. * @return void
  1104. */
  1105. public function testApiMediaUploadWithValidMedia()
  1106. {
  1107. $_FILES = [
  1108. 'media' => [
  1109. 'id' => 666,
  1110. 'size' => 666,
  1111. 'width' => 666,
  1112. 'height' => 666,
  1113. 'tmp_name' => $this->getTempImage(),
  1114. 'type' => 'image/png'
  1115. ]
  1116. ];
  1117. $app = get_app();
  1118. $app->argc = 2;
  1119. $result = api_media_upload();
  1120. $this->assertEquals('image/png', $result['media']['image']['image_type']);
  1121. $this->assertEquals(1, $result['media']['image']['w']);
  1122. $this->assertEquals(1, $result['media']['image']['h']);
  1123. }
  1124. /**
  1125. * Test the api_status_show() function.
  1126. * @return void
  1127. */
  1128. public function testApiStatusShow()
  1129. {
  1130. $result = api_status_show('json');
  1131. $this->assertStatus($result['status']);
  1132. }
  1133. /**
  1134. * Test the api_status_show() function with an XML result.
  1135. * @return void
  1136. */
  1137. public function testApiStatusShowWithXml()
  1138. {
  1139. $result = api_status_show('xml');
  1140. $this->assertXml($result, 'statuses');
  1141. }
  1142. /**
  1143. * Test the api_status_show() function with a raw result.
  1144. * @return void
  1145. */
  1146. public function testApiStatusShowWithRaw()
  1147. {
  1148. $this->assertStatus(api_status_show('raw'));
  1149. }
  1150. /**
  1151. * Test the api_users_show() function.
  1152. * @return void
  1153. */
  1154. public function testApiUsersShow()
  1155. {
  1156. $result = api_users_show('json');
  1157. // We can't use assertSelfUser() here because the user object is missing some properties.
  1158. $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
  1159. $this->assertEquals('Friendica', $result['user']['location']);
  1160. $this->assertEquals($this->selfUser['name'], $result['user']['name']);
  1161. $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
  1162. $this->assertEquals('dfrn', $result['user']['network']);
  1163. $this->assertTrue($result['user']['verified']);
  1164. }
  1165. /**
  1166. * Test the api_users_show() function with an XML result.
  1167. * @return void
  1168. */
  1169. public function testApiUsersShowWithXml()
  1170. {
  1171. $result = api_users_show('xml');
  1172. $this->assertXml($result, 'statuses');
  1173. }
  1174. /**
  1175. * Test the api_users_search() function.
  1176. * @return void
  1177. */
  1178. public function testApiUsersSearch()
  1179. {
  1180. $_GET['q'] = 'othercontact';
  1181. $result = api_users_search('json');
  1182. $this->assertOtherUser($result['users'][0]);
  1183. }
  1184. /**
  1185. * Test the api_users_search() function with an XML result.
  1186. * @return void
  1187. */
  1188. public function testApiUsersSearchWithXml()
  1189. {
  1190. $_GET['q'] = 'othercontact';
  1191. $result = api_users_search('xml');
  1192. $this->assertXml($result, 'users');
  1193. }
  1194. /**
  1195. * Test the api_users_search() function without a GET q parameter.
  1196. * @return void
  1197. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1198. */
  1199. public function testApiUsersSearchWithoutQuery()
  1200. {
  1201. api_users_search('json');
  1202. }
  1203. /**
  1204. * Test the api_users_lookup() function.
  1205. * @return void
  1206. * @expectedException Friendica\Network\HTTPException\NotFoundException
  1207. */
  1208. public function testApiUsersLookup()
  1209. {
  1210. api_users_lookup('json');
  1211. }
  1212. /**
  1213. * Test the api_users_lookup() function with an user ID.
  1214. * @return void
  1215. */
  1216. public function testApiUsersLookupWithUserId()
  1217. {
  1218. $_REQUEST['user_id'] = $this->otherUser['id'];
  1219. $result = api_users_lookup('json');
  1220. $this->assertOtherUser($result['users'][0]);
  1221. }
  1222. /**
  1223. * Test the api_search() function.
  1224. * @return void
  1225. */
  1226. public function testApiSearch()
  1227. {
  1228. $_REQUEST['q'] = 'reply';
  1229. $_REQUEST['max_id'] = 10;
  1230. $result = api_search('json');
  1231. foreach ($result['status'] as $status) {
  1232. $this->assertStatus($status);
  1233. $this->assertContains('reply', $status['text'], null, true);
  1234. }
  1235. }
  1236. /**
  1237. * Test the api_search() function a count parameter.
  1238. * @return void
  1239. */
  1240. public function testApiSearchWithCount()
  1241. {
  1242. $_REQUEST['q'] = 'reply';
  1243. $_REQUEST['count'] = 20;
  1244. $result = api_search('json');
  1245. foreach ($result['status'] as $status) {
  1246. $this->assertStatus($status);
  1247. $this->assertContains('reply', $status['text'], null, true);
  1248. }
  1249. }
  1250. /**
  1251. * Test the api_search() function with an rpp parameter.
  1252. * @return void
  1253. */
  1254. public function testApiSearchWithRpp()
  1255. {
  1256. $_REQUEST['q'] = 'reply';
  1257. $_REQUEST['rpp'] = 20;
  1258. $result = api_search('json');
  1259. foreach ($result['status'] as $status) {
  1260. $this->assertStatus($status);
  1261. $this->assertContains('reply', $status['text'], null, true);
  1262. }
  1263. }
  1264. /**
  1265. * Test the api_search() function without an authenticated user.
  1266. * @return void
  1267. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1268. */
  1269. public function testApiSearchWithUnallowedUser()
  1270. {
  1271. $_SESSION['allow_api'] = false;
  1272. $_GET['screen_name'] = $this->selfUser['nick'];
  1273. api_search('json');
  1274. }
  1275. /**
  1276. * Test the api_search() function without any GET query parameter.
  1277. * @return void
  1278. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1279. */
  1280. public function testApiSearchWithoutQuery()
  1281. {
  1282. api_search('json');
  1283. }
  1284. /**
  1285. * Test the api_statuses_home_timeline() function.
  1286. * @return void
  1287. */
  1288. public function testApiStatusesHomeTimeline()
  1289. {
  1290. $_REQUEST['max_id'] = 10;
  1291. $_REQUEST['exclude_replies'] = true;
  1292. $_REQUEST['conversation_id'] = 1;
  1293. $result = api_statuses_home_timeline('json');
  1294. $this->assertNotEmpty($result['status']);
  1295. foreach ($result['status'] as $status) {
  1296. $this->assertStatus($status);
  1297. }
  1298. }
  1299. /**
  1300. * Test the api_statuses_home_timeline() function with a negative page parameter.
  1301. * @return void
  1302. */
  1303. public function testApiStatusesHomeTimelineWithNegativePage()
  1304. {
  1305. $_REQUEST['page'] = -2;
  1306. $result = api_statuses_home_timeline('json');
  1307. $this->assertNotEmpty($result['status']);
  1308. foreach ($result['status'] as $status) {
  1309. $this->assertStatus($status);
  1310. }
  1311. }
  1312. /**
  1313. * Test the api_statuses_home_timeline() with an unallowed user.
  1314. * @return void
  1315. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1316. */
  1317. public function testApiStatusesHomeTimelineWithUnallowedUser()
  1318. {
  1319. $_SESSION['allow_api'] = false;
  1320. $_GET['screen_name'] = $this->selfUser['nick'];
  1321. api_statuses_home_timeline('json');
  1322. }
  1323. /**
  1324. * Test the api_statuses_home_timeline() function with an RSS result.
  1325. * @return void
  1326. */
  1327. public function testApiStatusesHomeTimelineWithRss()
  1328. {
  1329. $result = api_statuses_home_timeline('rss');
  1330. $this->assertXml($result, 'statuses');
  1331. }
  1332. /**
  1333. * Test the api_statuses_public_timeline() function.
  1334. * @return void
  1335. */
  1336. public function testApiStatusesPublicTimeline()
  1337. {
  1338. $_REQUEST['max_id'] = 10;
  1339. $_REQUEST['conversation_id'] = 1;
  1340. $result = api_statuses_public_timeline('json');
  1341. $this->assertNotEmpty($result['status']);
  1342. foreach ($result['status'] as $status) {
  1343. $this->assertStatus($status);
  1344. }
  1345. }
  1346. /**
  1347. * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
  1348. * @return void
  1349. */
  1350. public function testApiStatusesPublicTimelineWithExcludeReplies()
  1351. {
  1352. $_REQUEST['max_id'] = 10;
  1353. $_REQUEST['exclude_replies'] = true;
  1354. $result = api_statuses_public_timeline('json');
  1355. $this->assertNotEmpty($result['status']);
  1356. foreach ($result['status'] as $status) {
  1357. $this->assertStatus($status);
  1358. }
  1359. }
  1360. /**
  1361. * Test the api_statuses_public_timeline() function with a negative page parameter.
  1362. * @return void
  1363. */
  1364. public function testApiStatusesPublicTimelineWithNegativePage()
  1365. {
  1366. $_REQUEST['page'] = -2;
  1367. $result = api_statuses_public_timeline('json');
  1368. $this->assertNotEmpty($result['status']);
  1369. foreach ($result['status'] as $status) {
  1370. $this->assertStatus($status);
  1371. }
  1372. }
  1373. /**
  1374. * Test the api_statuses_public_timeline() function with an unallowed user.
  1375. * @return void
  1376. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1377. */
  1378. public function testApiStatusesPublicTimelineWithUnallowedUser()
  1379. {
  1380. $_SESSION['allow_api'] = false;
  1381. $_GET['screen_name'] = $this->selfUser['nick'];
  1382. api_statuses_public_timeline('json');
  1383. }
  1384. /**
  1385. * Test the api_statuses_public_timeline() function with an RSS result.
  1386. * @return void
  1387. */
  1388. public function testApiStatusesPublicTimelineWithRss()
  1389. {
  1390. $result = api_statuses_public_timeline('rss');
  1391. $this->assertXml($result, 'statuses');
  1392. }
  1393. /**
  1394. * Test the api_statuses_networkpublic_timeline() function.
  1395. * @return void
  1396. */
  1397. public function testApiStatusesNetworkpublicTimeline()
  1398. {
  1399. $_REQUEST['max_id'] = 10;
  1400. $result = api_statuses_networkpublic_timeline('json');
  1401. $this->assertNotEmpty($result['status']);
  1402. foreach ($result['status'] as $status) {
  1403. $this->assertStatus($status);
  1404. }
  1405. }
  1406. /**
  1407. * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
  1408. * @return void
  1409. */
  1410. public function testApiStatusesNetworkpublicTimelineWithNegativePage()
  1411. {
  1412. $_REQUEST['page'] = -2;
  1413. $result = api_statuses_networkpublic_timeline('json');
  1414. $this->assertNotEmpty($result['status']);
  1415. foreach ($result['status'] as $status) {
  1416. $this->assertStatus($status);
  1417. }
  1418. }
  1419. /**
  1420. * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
  1421. * @return void
  1422. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1423. */
  1424. public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
  1425. {
  1426. $_SESSION['allow_api'] = false;
  1427. $_GET['screen_name'] = $this->selfUser['nick'];
  1428. api_statuses_networkpublic_timeline('json');
  1429. }
  1430. /**
  1431. * Test the api_statuses_networkpublic_timeline() function with an RSS result.
  1432. * @return void
  1433. */
  1434. public function testApiStatusesNetworkpublicTimelineWithRss()
  1435. {
  1436. $result = api_statuses_networkpublic_timeline('rss');
  1437. $this->assertXml($result, 'statuses');
  1438. }
  1439. /**
  1440. * Test the api_statuses_show() function.
  1441. * @return void
  1442. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1443. */
  1444. public function testApiStatusesShow()
  1445. {
  1446. api_statuses_show('json');
  1447. }
  1448. /**
  1449. * Test the api_statuses_show() function with an ID.
  1450. * @return void
  1451. */
  1452. public function testApiStatusesShowWithId()
  1453. {
  1454. $this->app->argv[3] = 1;
  1455. $result = api_statuses_show('json');
  1456. $this->assertStatus($result['status']);
  1457. }
  1458. /**
  1459. * Test the api_statuses_show() function with the conversation parameter.
  1460. * @return void
  1461. */
  1462. public function testApiStatusesShowWithConversation()
  1463. {
  1464. $this->app->argv[3] = 1;
  1465. $_REQUEST['conversation'] = 1;
  1466. $result = api_statuses_show('json');
  1467. $this->assertNotEmpty($result['status']);
  1468. foreach ($result['status'] as $status) {
  1469. $this->assertStatus($status);
  1470. }
  1471. }
  1472. /**
  1473. * Test the api_statuses_show() function with an unallowed user.
  1474. * @return void
  1475. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1476. */
  1477. public function testApiStatusesShowWithUnallowedUser()
  1478. {
  1479. $_SESSION['allow_api'] = false;
  1480. $_GET['screen_name'] = $this->selfUser['nick'];
  1481. api_statuses_show('json');
  1482. }
  1483. /**
  1484. * Test the api_conversation_show() function.
  1485. * @return void
  1486. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1487. */
  1488. public function testApiConversationShow()
  1489. {
  1490. api_conversation_show('json');
  1491. }
  1492. /**
  1493. * Test the api_conversation_show() function with an ID.
  1494. * @return void
  1495. */
  1496. public function testApiConversationShowWithId()
  1497. {
  1498. $this->app->argv[3] = 1;
  1499. $_REQUEST['max_id'] = 10;
  1500. $_REQUEST['page'] = -2;
  1501. $result = api_conversation_show('json');
  1502. $this->assertNotEmpty($result['status']);
  1503. foreach ($result['status'] as $status) {
  1504. $this->assertStatus($status);
  1505. }
  1506. }
  1507. /**
  1508. * Test the api_conversation_show() function with an unallowed user.
  1509. * @return void
  1510. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1511. */
  1512. public function testApiConversationShowWithUnallowedUser()
  1513. {
  1514. $_SESSION['allow_api'] = false;
  1515. $_GET['screen_name'] = $this->selfUser['nick'];
  1516. api_conversation_show('json');
  1517. }
  1518. /**
  1519. * Test the api_statuses_repeat() function.
  1520. * @return void
  1521. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1522. */
  1523. public function testApiStatusesRepeat()
  1524. {
  1525. api_statuses_repeat('json');
  1526. }
  1527. /**
  1528. * Test the api_statuses_repeat() function without an authenticated user.
  1529. * @return void
  1530. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1531. */
  1532. public function testApiStatusesRepeatWithoutAuthenticatedUser()
  1533. {
  1534. $_SESSION['authenticated'] = false;
  1535. api_statuses_repeat('json');
  1536. }
  1537. /**
  1538. * Test the api_statuses_repeat() function with an ID.
  1539. * @return void
  1540. */
  1541. public function testApiStatusesRepeatWithId()
  1542. {
  1543. $this->app->argv[3] = 1;
  1544. $result = api_statuses_repeat('json');
  1545. $this->assertStatus($result['status']);
  1546. // Also test with a shared status
  1547. $this->app->argv[3] = 5;
  1548. $result = api_statuses_repeat('json');
  1549. $this->assertStatus($result['status']);
  1550. }
  1551. /**
  1552. * Test the api_statuses_destroy() function.
  1553. * @return void
  1554. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1555. */
  1556. public function testApiStatusesDestroy()
  1557. {
  1558. api_statuses_destroy('json');
  1559. }
  1560. /**
  1561. * Test the api_statuses_destroy() function without an authenticated user.
  1562. * @return void
  1563. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1564. */
  1565. public function testApiStatusesDestroyWithoutAuthenticatedUser()
  1566. {
  1567. $_SESSION['authenticated'] = false;
  1568. api_statuses_destroy('json');
  1569. }
  1570. /**
  1571. * Test the api_statuses_destroy() function with an ID.
  1572. * @return void
  1573. */
  1574. public function testApiStatusesDestroyWithId()
  1575. {
  1576. $this->app->argv[3] = 1;
  1577. $result = api_statuses_destroy('json');
  1578. $this->assertStatus($result['status']);
  1579. }
  1580. /**
  1581. * Test the api_statuses_mentions() function.
  1582. * @return void
  1583. */
  1584. public function testApiStatusesMentions()
  1585. {
  1586. $this->app->user = ['nickname' => $this->selfUser['nick']];
  1587. $_REQUEST['max_id'] = 10;
  1588. $result = api_statuses_mentions('json');
  1589. $this->assertEmpty($result['status']);
  1590. // We should test with mentions in the database.
  1591. }
  1592. /**
  1593. * Test the api_statuses_mentions() function with a negative page parameter.
  1594. * @return void
  1595. */
  1596. public function testApiStatusesMentionsWithNegativePage()
  1597. {
  1598. $_REQUEST['page'] = -2;
  1599. $result = api_statuses_mentions('json');
  1600. $this->assertEmpty($result['status']);
  1601. }
  1602. /**
  1603. * Test the api_statuses_mentions() function with an unallowed user.
  1604. * @return void
  1605. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1606. */
  1607. public function testApiStatusesMentionsWithUnallowedUser()
  1608. {
  1609. $_SESSION['allow_api'] = false;
  1610. $_GET['screen_name'] = $this->selfUser['nick'];
  1611. api_statuses_mentions('json');
  1612. }
  1613. /**
  1614. * Test the api_statuses_mentions() function with an RSS result.
  1615. * @return void
  1616. */
  1617. public function testApiStatusesMentionsWithRss()
  1618. {
  1619. $result = api_statuses_mentions('rss');
  1620. $this->assertXml($result, 'statuses');
  1621. }
  1622. /**
  1623. * Test the api_statuses_user_timeline() function.
  1624. * @return void
  1625. */
  1626. public function testApiStatusesUserTimeline()
  1627. {
  1628. $_REQUEST['max_id'] = 10;
  1629. $_REQUEST['exclude_replies'] = true;
  1630. $_REQUEST['conversation_id'] = 1;
  1631. $result = api_statuses_user_timeline('json');
  1632. $this->assertNotEmpty($result['status']);
  1633. foreach ($result['status'] as $status) {
  1634. $this->assertStatus($status);
  1635. }
  1636. }
  1637. /**
  1638. * Test the api_statuses_user_timeline() function with a negative page parameter.
  1639. * @return void
  1640. */
  1641. public function testApiStatusesUserTimelineWithNegativePage()
  1642. {
  1643. $_REQUEST['page'] = -2;
  1644. $result = api_statuses_user_timeline('json');
  1645. $this->assertNotEmpty($result['status']);
  1646. foreach ($result['status'] as $status) {
  1647. $this->assertStatus($status);
  1648. }
  1649. }
  1650. /**
  1651. * Test the api_statuses_user_timeline() function with an RSS result.
  1652. * @return void
  1653. */
  1654. public function testApiStatusesUserTimelineWithRss()
  1655. {
  1656. $result = api_statuses_user_timeline('rss');
  1657. $this->assertXml($result, 'statuses');
  1658. }
  1659. /**
  1660. * Test the api_statuses_user_timeline() function with an unallowed user.
  1661. * @return void
  1662. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1663. */
  1664. public function testApiStatusesUserTimelineWithUnallowedUser()
  1665. {
  1666. $_SESSION['allow_api'] = false;
  1667. $_GET['screen_name'] = $this->selfUser['nick'];
  1668. api_statuses_user_timeline('json');
  1669. }
  1670. /**
  1671. * Test the api_favorites_create_destroy() function.
  1672. * @return void
  1673. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1674. */
  1675. public function testApiFavoritesCreateDestroy()
  1676. {
  1677. api_favorites_create_destroy('json');
  1678. }
  1679. /**
  1680. * Test the api_favorites_create_destroy() function with an invalid ID.
  1681. * @return void
  1682. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1683. */
  1684. public function testApiFavoritesCreateDestroyWithInvalidId()
  1685. {
  1686. // This triggers a very specific condition ($action_argv_id + 2)
  1687. $this->app->argv[1] = '1.1';
  1688. $this->app->argc = 5;
  1689. api_favorites_create_destroy('json');
  1690. }
  1691. /**
  1692. * Test the api_favorites_create_destroy() function with an invalid action.
  1693. * @return void
  1694. * @expectedException Friendica\Network\HTTPException\BadRequestException
  1695. */
  1696. public function testApiFavoritesCreateDestroyWithInvalidAction()
  1697. {
  1698. $this->app->argv[1] = '1.1';
  1699. $this->app->argc = 10;
  1700. $_REQUEST['id'] = 1;
  1701. api_favorites_create_destroy('json');
  1702. }
  1703. /**
  1704. * Test the api_favorites_create_destroy() function with the create action.
  1705. * @return void
  1706. */
  1707. public function testApiFavoritesCreateDestroyWithCreateAction()
  1708. {
  1709. $this->app->argv[1] = '1.1';
  1710. $this->app->argv[3] = 'create';
  1711. $this->app->argc = 10;
  1712. $_REQUEST['id'] = 1;
  1713. $result = api_favorites_create_destroy('json');
  1714. $this->assertStatus($result['status']);
  1715. }
  1716. /**
  1717. * Test the api_favorites_create_destroy() function with the create action and an RSS result.
  1718. * @return void
  1719. */
  1720. public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
  1721. {
  1722. $this->app->argv[1] = '1.1';
  1723. $this->app->argv[3] = 'create';
  1724. $this->app->argc = 10;
  1725. $_REQUEST['id'] = 1;
  1726. $result = api_favorites_create_destroy('rss');
  1727. $this->assertXml($result, 'status');
  1728. }
  1729. /**
  1730. * Test the api_favorites_create_destroy() function with the destroy action.
  1731. * @return void
  1732. */
  1733. public function testApiFavoritesCreateDestroyWithDestroyAction()
  1734. {
  1735. $this->app->argv[1] = '1.1';
  1736. $this->app->argv[3] = 'destroy';
  1737. $this->app->argc = 10;
  1738. $_REQUEST['id'] = 1;
  1739. $result = api_favorites_create_destroy('json');
  1740. $this->assertStatus($result['status']);
  1741. }
  1742. /**
  1743. * Test the api_favorites_create_destroy() function without an authenticated user.
  1744. * @return void
  1745. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1746. */
  1747. public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
  1748. {
  1749. $_SESSION['authenticated'] = false;
  1750. api_favorites_create_destroy('json');
  1751. }
  1752. /**
  1753. * Test the api_favorites() function.
  1754. * @return void
  1755. */
  1756. public function testApiFavorites()
  1757. {
  1758. $_REQUEST['page'] = -1;
  1759. $_REQUEST['max_id'] = 10;
  1760. $result = api_favorites('json');
  1761. foreach ($result['status'] as $status) {
  1762. $this->assertStatus($status);
  1763. }
  1764. }
  1765. /**
  1766. * Test the api_favorites() function with an RSS result.
  1767. * @return void
  1768. */
  1769. public function testApiFavoritesWithRss()
  1770. {
  1771. $result = api_favorites('rss');
  1772. $this->assertXml($result, 'statuses');
  1773. }
  1774. /**
  1775. * Test the api_favorites() function with an unallowed user.
  1776. * @return void
  1777. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  1778. */
  1779. public function testApiFavoritesWithUnallowedUser()
  1780. {
  1781. $_SESSION['allow_api'] = false;
  1782. $_GET['screen_name'] = $this->selfUser['nick'];
  1783. api_favorites('json');
  1784. }
  1785. /**
  1786. * Test the api_format_messages() function.
  1787. * @return void
  1788. */
  1789. public function testApiFormatMessages()
  1790. {
  1791. $result = api_format_messages(
  1792. ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
  1793. ['id' => 2, 'screen_name' => 'recipient_name'],
  1794. ['id' => 3, 'screen_name' => 'sender_name']
  1795. );
  1796. $this->assertEquals('item_title'.PHP_EOL.'item_body', $result['text']);
  1797. $this->assertEquals(1, $result['id']);
  1798. $this->assertEquals(2, $result['recipient_id']);
  1799. $this->assertEquals(3, $result['sender_id']);
  1800. $this->assertEquals('recipient_name', $result['recipient_screen_name']);
  1801. $this->assertEquals('sender_name', $result['sender_screen_name']);
  1802. }
  1803. /**
  1804. * Test the api_format_messages() function with HTML.
  1805. * @return void
  1806. */
  1807. public function testApiFormatMessagesWithHtmlText()
  1808. {
  1809. $_GET['getText'] = 'html';
  1810. $result = api_format_messages(
  1811. ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
  1812. ['id' => 2, 'screen_name' => 'recipient_name'],
  1813. ['id' => 3, 'screen_name' => 'sender_name']
  1814. );
  1815. $this->assertEquals('item_title', $result['title']);
  1816. $this->assertEquals('<strong>item_body</strong>', $result['text']);
  1817. }
  1818. /**
  1819. * Test the api_format_messages() function with plain text.
  1820. * @return void
  1821. */
  1822. public function testApiFormatMessagesWithPlainText()
  1823. {
  1824. $_GET['getText'] = 'plain';
  1825. $result = api_format_messages(
  1826. ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
  1827. ['id' => 2, 'screen_name' => 'recipient_name'],
  1828. ['id' => 3, 'screen_name' => 'sender_name']
  1829. );
  1830. $this->assertEquals('item_title', $result['title']);
  1831. $this->assertEquals('item_body', $result['text']);
  1832. }
  1833. /**
  1834. * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
  1835. * @return void
  1836. */
  1837. public function testApiFormatMessagesWithoutUserObjects()
  1838. {
  1839. $_GET['getUserObjects'] = 'false';
  1840. $result = api_format_messages(
  1841. ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
  1842. ['id' => 2, 'screen_name' => 'recipient_name'],
  1843. ['id' => 3, 'screen_name' => 'sender_name']
  1844. );
  1845. $this->assertNull($result['sender']);
  1846. $this->assertNull($result['recipient']);
  1847. }
  1848. /**
  1849. * Test the api_convert_item() function.
  1850. * @return void
  1851. */
  1852. public function testApiConvertItem()
  1853. {
  1854. $result = api_convert_item(
  1855. [
  1856. 'network' => 'feed',
  1857. 'title' => 'item_title',
  1858. // We need a long string to test that it is correctly cut
  1859. 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui '.
  1860. 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor '.
  1861. 'officiis pariatur doloremque nemo culpa aut quidem qui dolore '.
  1862. 'laudantium atque commodi alias voluptatem non possimus aperiam '.
  1863. 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium '.
  1864. 'repellendus quibusdam et et inventore mollitia rerum sit autem '.
  1865. 'pariatur maiores ipsum accusantium perferendis vel sit possimus '.
  1866. 'veritatis nihil distinctio qui eum repellat officia illum quos '.
  1867. 'impedit quam iste esse unde qui suscipit aut facilis ut inventore '.
  1868. 'omnis exercitationem quo magnam consequatur maxime aut illum '.
  1869. 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam '.
  1870. 'temporibus corporis ratione blanditiis perspiciatis impedit '.
  1871. 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus '.
  1872. 'sunt consequatur inventore dolor officiis pariatur doloremque '.
  1873. 'nemo culpa aut quidem qui dolore laudantium atque commodi alias '.
  1874. 'voluptatem non possimus aperiam ipsum rerum consequuntur aut '.
  1875. 'amet fugit quia aliquid praesentium repellendus quibusdam et et '.
  1876. 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium '.
  1877. 'perferendis vel sit possimus veritatis nihil distinctio qui eum '.
  1878. 'repellat officia illum quos impedit quam iste esse unde qui '.
  1879. 'suscipit aut facilis ut inventore omnis exercitationem quo magnam '.
  1880. 'consequatur maxime aut illum soluta quaerat natus unde aspernatur '.
  1881. 'et sed beatae nihil ullam temporibus corporis ratione blanditiis'
  1882. ]
  1883. );
  1884. $this->assertStringStartsWith('item_title', $result['text']);
  1885. $this->assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
  1886. }
  1887. /**
  1888. * Test the api_convert_item() function with an empty item body.
  1889. * @return void
  1890. */
  1891. public function testApiConvertItemWithoutBody()
  1892. {
  1893. $result = api_convert_item(
  1894. [
  1895. 'network' => 'feed',
  1896. 'title' => 'item_title',
  1897. 'body' => '',
  1898. 'plink' => 'item_plink'
  1899. ]
  1900. );
  1901. $this->assertEquals('item_title', $result['text']);
  1902. $this->assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
  1903. }
  1904. /**
  1905. * Test the api_convert_item() function with the title in the body.
  1906. * @return void
  1907. */
  1908. public function testApiConvertItemWithTitleInBody()
  1909. {
  1910. $result = api_convert_item(
  1911. [
  1912. 'title' => 'item_title',
  1913. 'body' => 'item_title item_body'
  1914. ]
  1915. );
  1916. $this->assertEquals('item_title item_body', $result['text']);
  1917. $this->assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
  1918. }
  1919. /**
  1920. * Test the api_get_attachments() function.
  1921. * @return void
  1922. */
  1923. public function testApiGetAttachments()
  1924. {
  1925. $body = 'body';
  1926. $this->assertEmpty(api_get_attachments($body));
  1927. }
  1928. /**
  1929. * Test the api_get_attachments() function with an img tag.
  1930. * @return void
  1931. */
  1932. public function testApiGetAttachmentsWithImage()
  1933. {
  1934. $body = '[img]img_url[/img]';
  1935. $this->assertInternalType('array', api_get_attachments($body));
  1936. }
  1937. /**
  1938. * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
  1939. * @return void
  1940. */
  1941. public function testApiGetAttachmentsWithImageAndAndStatus()
  1942. {
  1943. $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
  1944. $body = '[img]img_url[/img]';
  1945. $this->assertInternalType('array', api_get_attachments($body));
  1946. }
  1947. /**
  1948. * Test the api_get_entitities() function.
  1949. * @return void
  1950. */
  1951. public function testApiGetEntitities()
  1952. {
  1953. $text = 'text';
  1954. $this->assertInternalType('array', api_get_entitities($text, 'bbcode'));
  1955. }
  1956. /**
  1957. * Test the api_get_entitities() function with the include_entities parameter.
  1958. * @return void
  1959. */
  1960. public function testApiGetEntititiesWithIncludeEntities()
  1961. {
  1962. $_REQUEST['include_entities'] = 'true';
  1963. $text = 'text';
  1964. $result = api_get_entitities($text, 'bbcode');
  1965. $this->assertInternalType('array', $result['hashtags']);
  1966. $this->assertInternalType('array', $result['symbols']);
  1967. $this->assertInternalType('array', $result['urls']);
  1968. $this->assertInternalType('array', $result['user_mentions']);
  1969. }
  1970. /**
  1971. * Test the api_format_items_embeded_images() function.
  1972. * @return void
  1973. */
  1974. public function testApiFormatItemsEmbededImages()
  1975. {
  1976. $this->assertEquals(
  1977. 'text http://localhost/display/item_guid',
  1978. api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
  1979. );
  1980. }
  1981. /**
  1982. * Test the api_contactlink_to_array() function.
  1983. * @return void
  1984. */
  1985. public function testApiContactlinkToArray()
  1986. {
  1987. $this->assertEquals(
  1988. [
  1989. 'name' => 'text',
  1990. 'url' => '',
  1991. ],
  1992. api_contactlink_to_array('text')
  1993. );
  1994. }
  1995. /**
  1996. * Test the api_contactlink_to_array() function with an URL.
  1997. * @return void
  1998. */
  1999. public function testApiContactlinkToArrayWithUrl()
  2000. {
  2001. $this->assertEquals(
  2002. [
  2003. 'name' => ['link_text'],
  2004. 'url' => ['url'],
  2005. ],
  2006. api_contactlink_to_array('text <a href="url">link_text</a>')
  2007. );
  2008. }
  2009. /**
  2010. * Test the api_format_items_activities() function.
  2011. * @return void
  2012. */
  2013. public function testApiFormatItemsActivities()
  2014. {
  2015. $item = [];
  2016. $result = api_format_items_activities($item);
  2017. $this->assertArrayHasKey('like', $result);
  2018. $this->assertArrayHasKey('dislike', $result);
  2019. $this->assertArrayHasKey('attendyes', $result);
  2020. $this->assertArrayHasKey('attendno', $result);
  2021. $this->assertArrayHasKey('attendmaybe', $result);
  2022. }
  2023. /**
  2024. * Test the api_format_items_activities() function with an XML result.
  2025. * @return void
  2026. */
  2027. public function testApiFormatItemsActivitiesWithXml()
  2028. {
  2029. $item = [];
  2030. $result = api_format_items_activities($item, 'xml');
  2031. $this->assertArrayHasKey('friendica:like', $result);
  2032. $this->assertArrayHasKey('friendica:dislike', $result);
  2033. $this->assertArrayHasKey('friendica:attendyes', $result);
  2034. $this->assertArrayHasKey('friendica:attendno', $result);
  2035. $this->assertArrayHasKey('friendica:attendmaybe', $result);
  2036. }
  2037. /**
  2038. * Test the api_format_items_profiles() function.
  2039. * @return void
  2040. */
  2041. public function testApiFormatItemsProfiles()
  2042. {
  2043. $profile_row = [
  2044. 'id' => 'profile_id',
  2045. 'profile-name' => 'profile_name',
  2046. 'is-default' => true,
  2047. 'hide-friends' => true,
  2048. 'photo' => 'profile_photo',
  2049. 'thumb' => 'profile_thumb',
  2050. 'publish' => true,
  2051. 'net-publish' => true,
  2052. 'pdesc' => 'description',
  2053. 'dob' => 'date_of_birth',
  2054. 'address' => 'address',
  2055. 'locality' => 'city',
  2056. 'region' => 'region',
  2057. 'postal-code' => 'postal_code',
  2058. 'country-name' => 'country',
  2059. 'hometown' => 'hometown',
  2060. 'gender' => 'gender',
  2061. 'marital' => 'marital',
  2062. 'with' => 'marital_with',
  2063. 'howlong' => 'marital_since',
  2064. 'sexual' => 'sexual',
  2065. 'politic' => 'politic',
  2066. 'religion' => 'religion',
  2067. 'pub_keywords' => 'public_keywords',
  2068. 'prv_keywords' => 'private_keywords',
  2069. 'likes' => 'likes',
  2070. 'dislikes' => 'dislikes',
  2071. 'about' => 'about',
  2072. 'music' => 'music',
  2073. 'book' => 'book',
  2074. 'tv' => 'tv',
  2075. 'film' => 'film',
  2076. 'interest' => 'interest',
  2077. 'romance' => 'romance',
  2078. 'work' => 'work',
  2079. 'education' => 'education',
  2080. 'contact' => 'social_networks',
  2081. 'homepage' => 'homepage'
  2082. ];
  2083. $result = api_format_items_profiles($profile_row);
  2084. $this->assertEquals(
  2085. [
  2086. 'profile_id' => 'profile_id',
  2087. 'profile_name' => 'profile_name',
  2088. 'is_default' => true,
  2089. 'hide_friends' => true,
  2090. 'profile_photo' => 'profile_photo',
  2091. 'profile_thumb' => 'profile_thumb',
  2092. 'publish' => true,
  2093. 'net_publish' => true,
  2094. 'description' => 'description',
  2095. 'date_of_birth' => 'date_of_birth',
  2096. 'address' => 'address',
  2097. 'city' => 'city',
  2098. 'region' => 'region',
  2099. 'postal_code' => 'postal_code',
  2100. 'country' => 'country',
  2101. 'hometown' => 'hometown',
  2102. 'gender' => 'gender',
  2103. 'marital' => 'marital',
  2104. 'marital_with' => 'marital_with',
  2105. 'marital_since' => 'marital_since',
  2106. 'sexual' => 'sexual',
  2107. 'politic' => 'politic',
  2108. 'religion' => 'religion',
  2109. 'public_keywords' => 'public_keywords',
  2110. 'private_keywords' => 'private_keywords',
  2111. 'likes' => 'likes',
  2112. 'dislikes' => 'dislikes',
  2113. 'about' => 'about',
  2114. 'music' => 'music',
  2115. 'book' => 'book',
  2116. 'tv' => 'tv',
  2117. 'film' => 'film',
  2118. 'interest' => 'interest',
  2119. 'romance' => 'romance',
  2120. 'work' => 'work',
  2121. 'education' => 'education',
  2122. 'social_networks' => 'social_networks',
  2123. 'homepage' => 'homepage',
  2124. 'users' => null
  2125. ],
  2126. $result
  2127. );
  2128. }
  2129. /**
  2130. * Test the api_format_items() function.
  2131. * @return void
  2132. */
  2133. public function testApiFormatItems()
  2134. {
  2135. $items = [
  2136. [
  2137. 'item_network' => 'item_network',
  2138. 'source' => 'web',
  2139. 'coord' => '5 7'
  2140. ]
  2141. ];
  2142. $result = api_format_items($items, [], true);
  2143. foreach ($result as $status) {
  2144. $this->assertStatus($status);
  2145. }
  2146. }
  2147. /**
  2148. * Test the api_format_items() function with an XML result.
  2149. * @return void
  2150. */
  2151. public function testApiFormatItemsWithXml()
  2152. {
  2153. $items = [
  2154. [
  2155. 'coord' => '5 7'
  2156. ]
  2157. ];
  2158. $result = api_format_items($items, [], true, 'xml');
  2159. foreach ($result as $status) {
  2160. $this->assertStatus($status);
  2161. }
  2162. }
  2163. /**
  2164. * Test the api_format_items() function.
  2165. * @return void
  2166. */
  2167. public function testApiAccountRateLimitStatus()
  2168. {
  2169. $result = api_account_rate_limit_status('json');
  2170. $this->assertEquals(150, $result['hash']['remaining_hits']);
  2171. $this->assertEquals(150, $result['hash']['hourly_limit']);
  2172. $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
  2173. }
  2174. /**
  2175. * Test the api_format_items() function with an XML result.
  2176. * @return void
  2177. */
  2178. public function testApiAccountRateLimitStatusWithXml()
  2179. {
  2180. $result = api_account_rate_limit_status('xml');
  2181. $this->assertXml($result, 'hash');
  2182. }
  2183. /**
  2184. * Test the api_help_test() function.
  2185. * @return void
  2186. */
  2187. public function testApiHelpTest()
  2188. {
  2189. $result = api_help_test('json');
  2190. $this->assertEquals(['ok' => 'ok'], $result);
  2191. }
  2192. /**
  2193. * Test the api_help_test() function with an XML result.
  2194. * @return void
  2195. */
  2196. public function testApiHelpTestWithXml()
  2197. {
  2198. $this->markTestIncomplete('Triggers this error: "key() expects parameter 1 to be array, string given"');
  2199. $result = api_help_test('xml');
  2200. $this->assertXml($result, 'ok');
  2201. }
  2202. /**
  2203. * Test the api_lists_list() function.
  2204. * @return void
  2205. */
  2206. public function testApiListsList()
  2207. {
  2208. $result = api_lists_list('json');
  2209. $this->assertEquals(['lists_list' => []], $result);
  2210. }
  2211. /**
  2212. * Test the api_lists_ownerships() function.
  2213. * @return void
  2214. */
  2215. public function testApiListsOwnerships()
  2216. {
  2217. $result = api_lists_ownerships('json');
  2218. foreach ($result['lists']['lists'] as $list) {
  2219. $this->assertList($list);
  2220. }
  2221. }
  2222. /**
  2223. * Test the api_lists_ownerships() function without an authenticated user.
  2224. * @return void
  2225. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2226. */
  2227. public function testApiListsOwnershipsWithoutAuthenticatedUser()
  2228. {
  2229. $_SESSION['authenticated'] = false;
  2230. api_lists_ownerships('json');
  2231. }
  2232. /**
  2233. * Test the api_lists_statuses() function.
  2234. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2235. * @return void
  2236. */
  2237. public function testApiListsStatuses()
  2238. {
  2239. api_lists_statuses('json');
  2240. }
  2241. /**
  2242. * Test the api_lists_statuses() function with a list ID.
  2243. * @return void
  2244. */
  2245. public function testApiListsStatusesWithListId()
  2246. {
  2247. $_REQUEST['list_id'] = 1;
  2248. $_REQUEST['page'] = -1;
  2249. $_REQUEST['max_id'] = 10;
  2250. $result = api_lists_statuses('json');
  2251. foreach ($result['status'] as $status) {
  2252. $this->assertStatus($status);
  2253. }
  2254. }
  2255. /**
  2256. * Test the api_lists_statuses() function with a list ID and a RSS result.
  2257. * @return void
  2258. */
  2259. public function testApiListsStatusesWithListIdAndRss()
  2260. {
  2261. $_REQUEST['list_id'] = 1;
  2262. $result = api_lists_statuses('rss');
  2263. $this->assertXml($result, 'statuses');
  2264. }
  2265. /**
  2266. * Test the api_lists_statuses() function with an unallowed user.
  2267. * @return void
  2268. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2269. */
  2270. public function testApiListsStatusesWithUnallowedUser()
  2271. {
  2272. $_SESSION['allow_api'] = false;
  2273. $_GET['screen_name'] = $this->selfUser['nick'];
  2274. api_lists_statuses('json');
  2275. }
  2276. /**
  2277. * Test the api_statuses_f() function.
  2278. * @return void
  2279. */
  2280. public function testApiStatusesFWithFriends()
  2281. {
  2282. $_GET['page'] = -1;
  2283. $result = api_statuses_f('friends');
  2284. $this->assertArrayHasKey('user', $result);
  2285. }
  2286. /**
  2287. * Test the api_statuses_f() function.
  2288. * @return void
  2289. */
  2290. public function testApiStatusesFWithFollowers()
  2291. {
  2292. $result = api_statuses_f('followers');
  2293. $this->assertArrayHasKey('user', $result);
  2294. }
  2295. /**
  2296. * Test the api_statuses_f() function.
  2297. * @return void
  2298. */
  2299. public function testApiStatusesFWithBlocks()
  2300. {
  2301. $result = api_statuses_f('blocks');
  2302. $this->assertArrayHasKey('user', $result);
  2303. }
  2304. /**
  2305. * Test the api_statuses_f() function.
  2306. * @return void
  2307. */
  2308. public function testApiStatusesFWithIncoming()
  2309. {
  2310. $result = api_statuses_f('incoming');
  2311. $this->assertArrayHasKey('user', $result);
  2312. }
  2313. /**
  2314. * Test the api_statuses_f() function an undefined cursor GET variable.
  2315. * @return void
  2316. */
  2317. public function testApiStatusesFWithUndefinedCursor()
  2318. {
  2319. $_GET['cursor'] = 'undefined';
  2320. $this->assertFalse(api_statuses_f('friends'));
  2321. }
  2322. /**
  2323. * Test the api_statuses_friends() function.
  2324. * @return void
  2325. */
  2326. public function testApiStatusesFriends()
  2327. {
  2328. $result = api_statuses_friends('json');
  2329. $this->assertArrayHasKey('user', $result);
  2330. }
  2331. /**
  2332. * Test the api_statuses_friends() function an undefined cursor GET variable.
  2333. * @return void
  2334. */
  2335. public function testApiStatusesFriendsWithUndefinedCursor()
  2336. {
  2337. $_GET['cursor'] = 'undefined';
  2338. $this->assertFalse(api_statuses_friends('json'));
  2339. }
  2340. /**
  2341. * Test the api_statuses_followers() function.
  2342. * @return void
  2343. */
  2344. public function testApiStatusesFollowers()
  2345. {
  2346. $result = api_statuses_followers('json');
  2347. $this->assertArrayHasKey('user', $result);
  2348. }
  2349. /**
  2350. * Test the api_statuses_followers() function an undefined cursor GET variable.
  2351. * @return void
  2352. */
  2353. public function testApiStatusesFollowersWithUndefinedCursor()
  2354. {
  2355. $_GET['cursor'] = 'undefined';
  2356. $this->assertFalse(api_statuses_followers('json'));
  2357. }
  2358. /**
  2359. * Test the api_blocks_list() function.
  2360. * @return void
  2361. */
  2362. public function testApiBlocksList()
  2363. {
  2364. $result = api_blocks_list('json');
  2365. $this->assertArrayHasKey('user', $result);
  2366. }
  2367. /**
  2368. * Test the api_blocks_list() function an undefined cursor GET variable.
  2369. * @return void
  2370. */
  2371. public function testApiBlocksListWithUndefinedCursor()
  2372. {
  2373. $_GET['cursor'] = 'undefined';
  2374. $this->assertFalse(api_blocks_list('json'));
  2375. }
  2376. /**
  2377. * Test the api_friendships_incoming() function.
  2378. * @return void
  2379. */
  2380. public function testApiFriendshipsIncoming()
  2381. {
  2382. $result = api_friendships_incoming('json');
  2383. $this->assertArrayHasKey('id', $result);
  2384. }
  2385. /**
  2386. * Test the api_friendships_incoming() function an undefined cursor GET variable.
  2387. * @return void
  2388. */
  2389. public function testApiFriendshipsIncomingWithUndefinedCursor()
  2390. {
  2391. $_GET['cursor'] = 'undefined';
  2392. $this->assertFalse(api_friendships_incoming('json'));
  2393. }
  2394. /**
  2395. * Test the api_statusnet_config() function.
  2396. * @return void
  2397. */
  2398. public function testApiStatusnetConfig()
  2399. {
  2400. $result = api_statusnet_config('json');
  2401. $this->assertEquals('localhost', $result['config']['site']['server']);
  2402. $this->assertEquals('default', $result['config']['site']['theme']);
  2403. $this->assertEquals('http://localhost/images/friendica-64.png', $result['config']['site']['logo']);
  2404. $this->assertTrue($result['config']['site']['fancy']);
  2405. $this->assertEquals('en', $result['config']['site']['language']);
  2406. $this->assertEquals('UTC', $result['config']['site']['timezone']);
  2407. $this->assertEquals(200000, $result['config']['site']['textlimit']);
  2408. $this->assertEquals('false', $result['config']['site']['private']);
  2409. $this->assertEquals('false', $result['config']['site']['ssl']);
  2410. $this->assertEquals(30, $result['config']['site']['shorturllength']);
  2411. }
  2412. /**
  2413. * Test the api_statusnet_version() function.
  2414. * @return void
  2415. */
  2416. public function testApiStatusnetVersion()
  2417. {
  2418. $result = api_statusnet_version('json');
  2419. $this->assertEquals('0.9.7', $result['version']);
  2420. }
  2421. /**
  2422. * Test the api_ff_ids() function.
  2423. * @return void
  2424. */
  2425. public function testApiFfIds()
  2426. {
  2427. $result = api_ff_ids('json');
  2428. $this->assertNull($result);
  2429. }
  2430. /**
  2431. * Test the api_ff_ids() function with a result.
  2432. * @return void
  2433. */
  2434. public function testApiFfIdsWithResult()
  2435. {
  2436. $this->markTestIncomplete();
  2437. }
  2438. /**
  2439. * Test the api_ff_ids() function without an authenticated user.
  2440. * @return void
  2441. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2442. */
  2443. public function testApiFfIdsWithoutAuthenticatedUser()
  2444. {
  2445. $_SESSION['authenticated'] = false;
  2446. api_ff_ids('json');
  2447. }
  2448. /**
  2449. * Test the api_friends_ids() function.
  2450. * @return void
  2451. */
  2452. public function testApiFriendsIds()
  2453. {
  2454. $result = api_friends_ids('json');
  2455. $this->assertNull($result);
  2456. }
  2457. /**
  2458. * Test the api_followers_ids() function.
  2459. * @return void
  2460. */
  2461. public function testApiFollowersIds()
  2462. {
  2463. $result = api_followers_ids('json');
  2464. $this->assertNull($result);
  2465. }
  2466. /**
  2467. * Test the api_direct_messages_new() function.
  2468. * @return void
  2469. */
  2470. public function testApiDirectMessagesNew()
  2471. {
  2472. $result = api_direct_messages_new('json');
  2473. $this->assertNull($result);
  2474. }
  2475. /**
  2476. * Test the api_direct_messages_new() function without an authenticated user.
  2477. * @return void
  2478. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2479. */
  2480. public function testApiDirectMessagesNewWithoutAuthenticatedUser()
  2481. {
  2482. $_SESSION['authenticated'] = false;
  2483. api_direct_messages_new('json');
  2484. }
  2485. /**
  2486. * Test the api_direct_messages_new() function with an user ID.
  2487. * @return void
  2488. */
  2489. public function testApiDirectMessagesNewWithUserId()
  2490. {
  2491. $_POST['text'] = 'message_text';
  2492. $_POST['user_id'] = $this->otherUser['id'];
  2493. $result = api_direct_messages_new('json');
  2494. $this->assertEquals(['direct_message' => ['error' => -1]], $result);
  2495. }
  2496. /**
  2497. * Test the api_direct_messages_new() function with a screen name.
  2498. * @return void
  2499. */
  2500. public function testApiDirectMessagesNewWithScreenName()
  2501. {
  2502. $_POST['text'] = 'message_text';
  2503. $_POST['screen_name'] = $this->otherUser['nick'];
  2504. $result = api_direct_messages_new('json');
  2505. $this->assertEquals(1, $result['direct_message']['id']);
  2506. $this->assertContains('message_text', $result['direct_message']['text']);
  2507. $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
  2508. $this->assertEquals(1, $result['direct_message']['friendica_seen']);
  2509. }
  2510. /**
  2511. * Test the api_direct_messages_new() function with a title.
  2512. * @return void
  2513. */
  2514. public function testApiDirectMessagesNewWithTitle()
  2515. {
  2516. $_POST['text'] = 'message_text';
  2517. $_POST['screen_name'] = $this->otherUser['nick'];
  2518. $_REQUEST['title'] = 'message_title';
  2519. $result = api_direct_messages_new('json');
  2520. $this->assertEquals(1, $result['direct_message']['id']);
  2521. $this->assertContains('message_text', $result['direct_message']['text']);
  2522. $this->assertContains('message_title', $result['direct_message']['text']);
  2523. $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
  2524. $this->assertEquals(1, $result['direct_message']['friendica_seen']);
  2525. }
  2526. /**
  2527. * Test the api_direct_messages_new() function with an RSS result.
  2528. * @return void
  2529. */
  2530. public function testApiDirectMessagesNewWithRss()
  2531. {
  2532. $_POST['text'] = 'message_text';
  2533. $_POST['screen_name'] = $this->otherUser['nick'];
  2534. $result = api_direct_messages_new('rss');
  2535. $this->assertXml($result, 'direct-messages');
  2536. }
  2537. /**
  2538. * Test the api_direct_messages_destroy() function.
  2539. * @return void
  2540. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2541. */
  2542. public function testApiDirectMessagesDestroy()
  2543. {
  2544. api_direct_messages_destroy('json');
  2545. }
  2546. /**
  2547. * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
  2548. * @return void
  2549. */
  2550. public function testApiDirectMessagesDestroyWithVerbose()
  2551. {
  2552. $_GET['friendica_verbose'] = 'true';
  2553. $result = api_direct_messages_destroy('json');
  2554. $this->assertEquals(
  2555. [
  2556. '$result' => [
  2557. 'result' => 'error',
  2558. 'message' => 'message id or parenturi not specified'
  2559. ]
  2560. ],
  2561. $result
  2562. );
  2563. }
  2564. /**
  2565. * Test the api_direct_messages_destroy() function without an authenticated user.
  2566. * @return void
  2567. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2568. */
  2569. public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
  2570. {
  2571. $_SESSION['authenticated'] = false;
  2572. api_direct_messages_destroy('json');
  2573. }
  2574. /**
  2575. * Test the api_direct_messages_destroy() function with a non-zero ID.
  2576. * @return void
  2577. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2578. */
  2579. public function testApiDirectMessagesDestroyWithId()
  2580. {
  2581. $_REQUEST['id'] = 1;
  2582. api_direct_messages_destroy('json');
  2583. }
  2584. /**
  2585. * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
  2586. * @return void
  2587. */
  2588. public function testApiDirectMessagesDestroyWithIdAndVerbose()
  2589. {
  2590. $_REQUEST['id'] = 1;
  2591. $_REQUEST['friendica_parenturi'] = 'parent_uri';
  2592. $_GET['friendica_verbose'] = 'true';
  2593. $result = api_direct_messages_destroy('json');
  2594. $this->assertEquals(
  2595. [
  2596. '$result' => [
  2597. 'result' => 'error',
  2598. 'message' => 'message id not in database'
  2599. ]
  2600. ],
  2601. $result
  2602. );
  2603. }
  2604. /**
  2605. * Test the api_direct_messages_destroy() function with a non-zero ID.
  2606. * @return void
  2607. */
  2608. public function testApiDirectMessagesDestroyWithCorrectId()
  2609. {
  2610. $this->markTestIncomplete('We need to add a dataset for this.');
  2611. }
  2612. /**
  2613. * Test the api_direct_messages_box() function.
  2614. * @return void
  2615. */
  2616. public function testApiDirectMessagesBoxWithSentbox()
  2617. {
  2618. $_REQUEST['page'] = -1;
  2619. $_REQUEST['max_id'] = 10;
  2620. $result = api_direct_messages_box('json', 'sentbox', 'false');
  2621. $this->assertArrayHasKey('direct_message', $result);
  2622. }
  2623. /**
  2624. * Test the api_direct_messages_box() function.
  2625. * @return void
  2626. */
  2627. public function testApiDirectMessagesBoxWithConversation()
  2628. {
  2629. $result = api_direct_messages_box('json', 'conversation', 'false');
  2630. $this->assertArrayHasKey('direct_message', $result);
  2631. }
  2632. /**
  2633. * Test the api_direct_messages_box() function.
  2634. * @return void
  2635. */
  2636. public function testApiDirectMessagesBoxWithAll()
  2637. {
  2638. $result = api_direct_messages_box('json', 'all', 'false');
  2639. $this->assertArrayHasKey('direct_message', $result);
  2640. }
  2641. /**
  2642. * Test the api_direct_messages_box() function.
  2643. * @return void
  2644. */
  2645. public function testApiDirectMessagesBoxWithInbox()
  2646. {
  2647. $result = api_direct_messages_box('json', 'inbox', 'false');
  2648. $this->assertArrayHasKey('direct_message', $result);
  2649. }
  2650. /**
  2651. * Test the api_direct_messages_box() function.
  2652. * @return void
  2653. */
  2654. public function testApiDirectMessagesBoxWithVerbose()
  2655. {
  2656. $result = api_direct_messages_box('json', 'sentbox', 'true');
  2657. $this->assertEquals(
  2658. [
  2659. '$result' => [
  2660. 'result' => 'error',
  2661. 'message' => 'no mails available'
  2662. ]
  2663. ],
  2664. $result
  2665. );
  2666. }
  2667. /**
  2668. * Test the api_direct_messages_box() function with a RSS result.
  2669. * @return void
  2670. */
  2671. public function testApiDirectMessagesBoxWithRss()
  2672. {
  2673. $result = api_direct_messages_box('rss', 'sentbox', 'false');
  2674. $this->assertXml($result, 'direct-messages');
  2675. }
  2676. /**
  2677. * Test the api_direct_messages_box() function without an authenticated user.
  2678. * @return void
  2679. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2680. */
  2681. public function testApiDirectMessagesBoxWithUnallowedUser()
  2682. {
  2683. $_SESSION['allow_api'] = false;
  2684. $_GET['screen_name'] = $this->selfUser['nick'];
  2685. api_direct_messages_box('json', 'sentbox', 'false');
  2686. }
  2687. /**
  2688. * Test the api_direct_messages_sentbox() function.
  2689. * @return void
  2690. */
  2691. public function testApiDirectMessagesSentbox()
  2692. {
  2693. $result = api_direct_messages_sentbox('json');
  2694. $this->assertArrayHasKey('direct_message', $result);
  2695. }
  2696. /**
  2697. * Test the api_direct_messages_inbox() function.
  2698. * @return void
  2699. */
  2700. public function testApiDirectMessagesInbox()
  2701. {
  2702. $result = api_direct_messages_inbox('json');
  2703. $this->assertArrayHasKey('direct_message', $result);
  2704. }
  2705. /**
  2706. * Test the api_direct_messages_all() function.
  2707. * @return void
  2708. */
  2709. public function testApiDirectMessagesAll()
  2710. {
  2711. $result = api_direct_messages_all('json');
  2712. $this->assertArrayHasKey('direct_message', $result);
  2713. }
  2714. /**
  2715. * Test the api_direct_messages_conversation() function.
  2716. * @return void
  2717. */
  2718. public function testApiDirectMessagesConversation()
  2719. {
  2720. $result = api_direct_messages_conversation('json');
  2721. $this->assertArrayHasKey('direct_message', $result);
  2722. }
  2723. /**
  2724. * Test the api_oauth_request_token() function.
  2725. * @return void
  2726. */
  2727. public function testApiOauthRequestToken()
  2728. {
  2729. $this->markTestIncomplete('killme() kills phpunit as well');
  2730. }
  2731. /**
  2732. * Test the api_oauth_access_token() function.
  2733. * @return void
  2734. */
  2735. public function testApiOauthAccessToken()
  2736. {
  2737. $this->markTestIncomplete('killme() kills phpunit as well');
  2738. }
  2739. /**
  2740. * Test the api_fr_photoalbum_delete() function.
  2741. * @return void
  2742. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2743. */
  2744. public function testApiFrPhotoalbumDelete()
  2745. {
  2746. api_fr_photoalbum_delete('json');
  2747. }
  2748. /**
  2749. * Test the api_fr_photoalbum_delete() function with an album name.
  2750. * @return void
  2751. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2752. */
  2753. public function testApiFrPhotoalbumDeleteWithAlbum()
  2754. {
  2755. $_REQUEST['album'] = 'album_name';
  2756. api_fr_photoalbum_delete('json');
  2757. }
  2758. /**
  2759. * Test the api_fr_photoalbum_delete() function with an album name.
  2760. * @return void
  2761. */
  2762. public function testApiFrPhotoalbumDeleteWithValidAlbum()
  2763. {
  2764. $this->markTestIncomplete('We need to add a dataset for this.');
  2765. }
  2766. /**
  2767. * Test the api_fr_photoalbum_delete() function.
  2768. * @return void
  2769. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2770. */
  2771. public function testApiFrPhotoalbumUpdate()
  2772. {
  2773. api_fr_photoalbum_update('json');
  2774. }
  2775. /**
  2776. * Test the api_fr_photoalbum_delete() function with an album name.
  2777. * @return void
  2778. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2779. */
  2780. public function testApiFrPhotoalbumUpdateWithAlbum()
  2781. {
  2782. $_REQUEST['album'] = 'album_name';
  2783. api_fr_photoalbum_update('json');
  2784. }
  2785. /**
  2786. * Test the api_fr_photoalbum_delete() function with an album name.
  2787. * @return void
  2788. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2789. */
  2790. public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
  2791. {
  2792. $_REQUEST['album'] = 'album_name';
  2793. $_REQUEST['album_new'] = 'album_name';
  2794. api_fr_photoalbum_update('json');
  2795. }
  2796. /**
  2797. * Test the api_fr_photoalbum_update() function without an authenticated user.
  2798. * @return void
  2799. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2800. */
  2801. public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
  2802. {
  2803. $_SESSION['authenticated'] = false;
  2804. api_fr_photoalbum_update('json');
  2805. }
  2806. /**
  2807. * Test the api_fr_photoalbum_delete() function with an album name.
  2808. * @return void
  2809. */
  2810. public function testApiFrPhotoalbumUpdateWithValidAlbum()
  2811. {
  2812. $this->markTestIncomplete('We need to add a dataset for this.');
  2813. }
  2814. /**
  2815. * Test the api_fr_photos_list() function.
  2816. * @return void
  2817. */
  2818. public function testApiFrPhotosList()
  2819. {
  2820. $result = api_fr_photos_list('json');
  2821. $this->assertArrayHasKey('photo', $result);
  2822. }
  2823. /**
  2824. * Test the api_fr_photos_list() function without an authenticated user.
  2825. * @return void
  2826. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2827. */
  2828. public function testApiFrPhotosListWithoutAuthenticatedUser()
  2829. {
  2830. $_SESSION['authenticated'] = false;
  2831. api_fr_photos_list('json');
  2832. }
  2833. /**
  2834. * Test the api_fr_photo_create_update() function.
  2835. * @return void
  2836. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2837. */
  2838. public function testApiFrPhotoCreateUpdate()
  2839. {
  2840. api_fr_photo_create_update('json');
  2841. }
  2842. /**
  2843. * Test the api_fr_photo_create_update() function without an authenticated user.
  2844. * @return void
  2845. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2846. */
  2847. public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
  2848. {
  2849. $_SESSION['authenticated'] = false;
  2850. api_fr_photo_create_update('json');
  2851. }
  2852. /**
  2853. * Test the api_fr_photo_create_update() function with an album name.
  2854. * @return void
  2855. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2856. */
  2857. public function testApiFrPhotoCreateUpdateWithAlbum()
  2858. {
  2859. $_REQUEST['album'] = 'album_name';
  2860. api_fr_photo_create_update('json');
  2861. }
  2862. /**
  2863. * Test the api_fr_photo_create_update() function with the update mode.
  2864. * @return void
  2865. */
  2866. public function testApiFrPhotoCreateUpdateWithUpdate()
  2867. {
  2868. $this->markTestIncomplete('We need to create a dataset for this');
  2869. }
  2870. /**
  2871. * Test the api_fr_photo_create_update() function with an uploaded file.
  2872. * @return void
  2873. */
  2874. public function testApiFrPhotoCreateUpdateWithFile()
  2875. {
  2876. $this->markTestIncomplete();
  2877. }
  2878. /**
  2879. * Test the api_fr_photo_delete() function.
  2880. * @return void
  2881. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2882. */
  2883. public function testApiFrPhotoDelete()
  2884. {
  2885. api_fr_photo_delete('json');
  2886. }
  2887. /**
  2888. * Test the api_fr_photo_delete() function without an authenticated user.
  2889. * @return void
  2890. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2891. */
  2892. public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
  2893. {
  2894. $_SESSION['authenticated'] = false;
  2895. api_fr_photo_delete('json');
  2896. }
  2897. /**
  2898. * Test the api_fr_photo_delete() function with a photo ID.
  2899. * @return void
  2900. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2901. */
  2902. public function testApiFrPhotoDeleteWithPhotoId()
  2903. {
  2904. $_REQUEST['photo_id'] = 1;
  2905. api_fr_photo_delete('json');
  2906. }
  2907. /**
  2908. * Test the api_fr_photo_delete() function with a correct photo ID.
  2909. * @return void
  2910. */
  2911. public function testApiFrPhotoDeleteWithCorrectPhotoId()
  2912. {
  2913. $this->markTestIncomplete('We need to create a dataset for this.');
  2914. }
  2915. /**
  2916. * Test the api_fr_photo_detail() function.
  2917. * @return void
  2918. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2919. */
  2920. public function testApiFrPhotoDetail()
  2921. {
  2922. api_fr_photo_detail('json');
  2923. }
  2924. /**
  2925. * Test the api_fr_photo_detail() function without an authenticated user.
  2926. * @return void
  2927. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2928. */
  2929. public function testApiFrPhotoDetailWithoutAuthenticatedUser()
  2930. {
  2931. $_SESSION['authenticated'] = false;
  2932. api_fr_photo_detail('json');
  2933. }
  2934. /**
  2935. * Test the api_fr_photo_detail() function with a photo ID.
  2936. * @return void
  2937. * @expectedException Friendica\Network\HTTPException\NotFoundException
  2938. */
  2939. public function testApiFrPhotoDetailWithPhotoId()
  2940. {
  2941. $_REQUEST['photo_id'] = 1;
  2942. api_fr_photo_detail('json');
  2943. }
  2944. /**
  2945. * Test the api_fr_photo_detail() function with a correct photo ID.
  2946. * @return void
  2947. */
  2948. public function testApiFrPhotoDetailCorrectPhotoId()
  2949. {
  2950. $this->markTestIncomplete('We need to create a dataset for this.');
  2951. }
  2952. /**
  2953. * Test the api_account_update_profile_image() function.
  2954. * @return void
  2955. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2956. */
  2957. public function testApiAccountUpdateProfileImage()
  2958. {
  2959. api_account_update_profile_image('json');
  2960. }
  2961. /**
  2962. * Test the api_account_update_profile_image() function without an authenticated user.
  2963. * @return void
  2964. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  2965. */
  2966. public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
  2967. {
  2968. $_SESSION['authenticated'] = false;
  2969. api_account_update_profile_image('json');
  2970. }
  2971. /**
  2972. * Test the api_account_update_profile_image() function with an uploaded file.
  2973. * @return void
  2974. * @expectedException Friendica\Network\HTTPException\BadRequestException
  2975. */
  2976. public function testApiAccountUpdateProfileImageWithUpload()
  2977. {
  2978. $this->markTestIncomplete();
  2979. }
  2980. /**
  2981. * Test the api_account_update_profile() function.
  2982. * @return void
  2983. */
  2984. public function testApiAccountUpdateProfile()
  2985. {
  2986. $_POST['name'] = 'new_name';
  2987. $_POST['description'] = 'new_description';
  2988. $result = api_account_update_profile('json');
  2989. // We can't use assertSelfUser() here because the user object is missing some properties.
  2990. $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
  2991. $this->assertEquals('Friendica', $result['user']['location']);
  2992. $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
  2993. $this->assertEquals('dfrn', $result['user']['network']);
  2994. $this->assertEquals('new_name', $result['user']['name']);
  2995. $this->assertEquals('new_description', $result['user']['description']);
  2996. }
  2997. /**
  2998. * Test the check_acl_input() function.
  2999. * @return void
  3000. */
  3001. public function testCheckAclInput()
  3002. {
  3003. $result = check_acl_input('<aclstring>');
  3004. // Where does this result come from?
  3005. $this->assertEquals(1, $result);
  3006. }
  3007. /**
  3008. * Test the check_acl_input() function with an empty ACL string.
  3009. * @return void
  3010. */
  3011. public function testCheckAclInputWithEmptyAclString()
  3012. {
  3013. $result = check_acl_input(' ');
  3014. $this->assertFalse($result);
  3015. }
  3016. /**
  3017. * Test the save_media_to_database() function.
  3018. * @return void
  3019. */
  3020. public function testSaveMediaToDatabase()
  3021. {
  3022. $this->markTestIncomplete();
  3023. }
  3024. /**
  3025. * Test the post_photo_item() function.
  3026. * @return void
  3027. */
  3028. public function testPostPhotoItem()
  3029. {
  3030. $this->markTestIncomplete();
  3031. }
  3032. /**
  3033. * Test the prepare_photo_data() function.
  3034. * @return void
  3035. */
  3036. public function testPreparePhotoData()
  3037. {
  3038. $this->markTestIncomplete();
  3039. }
  3040. /**
  3041. * Test the api_friendica_remoteauth() function.
  3042. * @return void
  3043. * @expectedException Friendica\Network\HTTPException\BadRequestException
  3044. */
  3045. public function testApiFriendicaRemoteauth()
  3046. {
  3047. api_friendica_remoteauth();
  3048. }
  3049. /**
  3050. * Test the api_friendica_remoteauth() function with an URL.
  3051. * @return void
  3052. * @expectedException Friendica\Network\HTTPException\BadRequestException
  3053. */
  3054. public function testApiFriendicaRemoteauthWithUrl()
  3055. {
  3056. $_GET['url'] = 'url';
  3057. $_GET['c_url'] = 'url';
  3058. api_friendica_remoteauth();
  3059. }
  3060. /**
  3061. * Test the api_friendica_remoteauth() function with a correct URL.
  3062. * @return void
  3063. */
  3064. public function testApiFriendicaRemoteauthWithCorrectUrl()
  3065. {
  3066. $this->markTestIncomplete("We can't use an assertion here because of goaway().");
  3067. $_GET['url'] = 'url';
  3068. $_GET['c_url'] = $this->selfUser['nurl'];
  3069. api_friendica_remoteauth();
  3070. }
  3071. /**
  3072. * Test the api_share_as_retweet() function.
  3073. * @return void
  3074. */
  3075. public function testApiShareAsRetweet()
  3076. {
  3077. $item = [];
  3078. $result = api_share_as_retweet($item);
  3079. $this->assertFalse($result);
  3080. }
  3081. /**
  3082. * Test the api_share_as_retweet() function with a valid item.
  3083. * @return void
  3084. */
  3085. public function testApiShareAsRetweetWithValidItem()
  3086. {
  3087. $this->markTestIncomplete();
  3088. }
  3089. /**
  3090. * Test the api_get_nick() function.
  3091. * @return void
  3092. */
  3093. public function testApiGetNick()
  3094. {
  3095. $result = api_get_nick($this->otherUser['nurl']);
  3096. $this->assertEquals('othercontact', $result);
  3097. }
  3098. /**
  3099. * Test the api_get_nick() function with a wrong URL.
  3100. * @return void
  3101. */
  3102. public function testApiGetNickWithWrongUrl()
  3103. {
  3104. $result = api_get_nick('wrong_url');
  3105. $this->assertFalse($result);
  3106. }
  3107. /**
  3108. * Test the api_in_reply_to() function.
  3109. * @return void
  3110. */
  3111. public function testApiInReplyTo()
  3112. {
  3113. $result = api_in_reply_to([]);
  3114. $this->assertArrayHasKey('status_id', $result);
  3115. $this->assertArrayHasKey('user_id', $result);
  3116. $this->assertArrayHasKey('status_id_str', $result);
  3117. $this->assertArrayHasKey('user_id_str', $result);
  3118. $this->assertArrayHasKey('screen_name', $result);
  3119. }
  3120. /**
  3121. * Test the api_in_reply_to() function with a valid item.
  3122. * @return void
  3123. */
  3124. public function testApiInReplyToWithValidItem()
  3125. {
  3126. $this->markTestIncomplete();
  3127. }
  3128. /**
  3129. * Test the api_clean_plain_items() function.
  3130. * @return void
  3131. */
  3132. public function testApiCleanPlainItems()
  3133. {
  3134. $_REQUEST['include_entities'] = 'true';
  3135. $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
  3136. $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
  3137. }
  3138. /**
  3139. * Test the api_clean_attachments() function.
  3140. * @return void
  3141. */
  3142. public function testApiCleanAttachments()
  3143. {
  3144. $this->markTestIncomplete();
  3145. }
  3146. /**
  3147. * Test the api_best_nickname() function.
  3148. * @return void
  3149. */
  3150. public function testApiBestNickname()
  3151. {
  3152. $contacts = [];
  3153. $result = api_best_nickname($contacts);
  3154. $this->assertNull($result);
  3155. }
  3156. /**
  3157. * Test the api_best_nickname() function with contacts.
  3158. * @return void
  3159. */
  3160. public function testApiBestNicknameWithContacts()
  3161. {
  3162. $this->markTestIncomplete();
  3163. }
  3164. /**
  3165. * Test the api_friendica_group_show() function.
  3166. * @return void
  3167. */
  3168. public function testApiFriendicaGroupShow()
  3169. {
  3170. $this->markTestIncomplete();
  3171. }
  3172. /**
  3173. * Test the api_friendica_group_delete() function.
  3174. * @return void
  3175. */
  3176. public function testApiFriendicaGroupDelete()
  3177. {
  3178. $this->markTestIncomplete();
  3179. }
  3180. /**
  3181. * Test the api_lists_destroy() function.
  3182. * @return void
  3183. */
  3184. public function testApiListsDestroy()
  3185. {
  3186. $this->markTestIncomplete();
  3187. }
  3188. /**
  3189. * Test the group_create() function.
  3190. * @return void
  3191. */
  3192. public function testGroupCreate()
  3193. {
  3194. $this->markTestIncomplete();
  3195. }
  3196. /**
  3197. * Test the api_friendica_group_create() function.
  3198. * @return void
  3199. */
  3200. public function testApiFriendicaGroupCreate()
  3201. {
  3202. $this->markTestIncomplete();
  3203. }
  3204. /**
  3205. * Test the api_lists_create() function.
  3206. * @return void
  3207. */
  3208. public function testApiListsCreate()
  3209. {
  3210. $this->markTestIncomplete();
  3211. }
  3212. /**
  3213. * Test the api_friendica_group_update() function.
  3214. * @return void
  3215. */
  3216. public function testApiFriendicaGroupUpdate()
  3217. {
  3218. $this->markTestIncomplete();
  3219. }
  3220. /**
  3221. * Test the api_lists_update() function.
  3222. * @return void
  3223. */
  3224. public function testApiListsUpdate()
  3225. {
  3226. $this->markTestIncomplete();
  3227. }
  3228. /**
  3229. * Test the api_friendica_activity() function.
  3230. * @return void
  3231. */
  3232. public function testApiFriendicaActivity()
  3233. {
  3234. $this->markTestIncomplete();
  3235. }
  3236. /**
  3237. * Test the api_friendica_notification() function.
  3238. * @return void
  3239. * @expectedException Friendica\Network\HTTPException\BadRequestException
  3240. */
  3241. public function testApiFriendicaNotification()
  3242. {
  3243. api_friendica_notification('json');
  3244. }
  3245. /**
  3246. * Test the api_friendica_notification() function without an authenticated user.
  3247. * @return void
  3248. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  3249. */
  3250. public function testApiFriendicaNotificationWithoutAuthenticatedUser()
  3251. {
  3252. $_SESSION['authenticated'] = false;
  3253. api_friendica_notification('json');
  3254. }
  3255. /**
  3256. * Test the api_friendica_notification() function with an argument count.
  3257. * @return void
  3258. */
  3259. public function testApiFriendicaNotificationWithArgumentCount()
  3260. {
  3261. $this->app->argc = 3;
  3262. $result = api_friendica_notification('json');
  3263. $this->assertEquals(['note' => false], $result);
  3264. }
  3265. /**
  3266. * Test the api_friendica_notification() function with an XML result.
  3267. * @return void
  3268. */
  3269. public function testApiFriendicaNotificationWithXmlResult()
  3270. {
  3271. $this->markTestIncomplete('Fails with "Invalid argument supplied for foreach()".');
  3272. $this->app->argc = 3;
  3273. $result = api_friendica_notification('xml');
  3274. $this->assertXml($result, 'notes');
  3275. }
  3276. /**
  3277. * Test the api_friendica_notification_seen() function.
  3278. * @return void
  3279. */
  3280. public function testApiFriendicaNotificationSeen()
  3281. {
  3282. $this->markTestIncomplete();
  3283. }
  3284. /**
  3285. * Test the api_friendica_direct_messages_setseen() function.
  3286. * @return void
  3287. */
  3288. public function testApiFriendicaDirectMessagesSetseen()
  3289. {
  3290. $this->markTestIncomplete();
  3291. }
  3292. /**
  3293. * Test the api_friendica_direct_messages_search() function.
  3294. * @return void
  3295. */
  3296. public function testApiFriendicaDirectMessagesSearch()
  3297. {
  3298. $this->markTestIncomplete();
  3299. }
  3300. /**
  3301. * Test the api_friendica_profile_show() function.
  3302. * @return void
  3303. */
  3304. public function testApiFriendicaProfileShow()
  3305. {
  3306. $result = api_friendica_profile_show('json');
  3307. // We can't use assertSelfUser() here because the user object is missing some properties.
  3308. $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']);
  3309. $this->assertEquals('Friendica', $result['$result']['friendica_owner']['location']);
  3310. $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']);
  3311. $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']);
  3312. $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']);
  3313. $this->assertTrue($result['$result']['friendica_owner']['verified']);
  3314. $this->assertFalse($result['$result']['multi_profiles']);
  3315. }
  3316. /**
  3317. * Test the api_friendica_profile_show() function with a profile ID.
  3318. * @return void
  3319. */
  3320. public function testApiFriendicaProfileShowWithProfileId()
  3321. {
  3322. $this->markTestIncomplete('We need to add a dataset for this.');
  3323. }
  3324. /**
  3325. * Test the api_friendica_profile_show() function with a wrong profile ID.
  3326. * @return void
  3327. * @expectedException Friendica\Network\HTTPException\BadRequestException
  3328. */
  3329. public function testApiFriendicaProfileShowWithWrongProfileId()
  3330. {
  3331. $_REQUEST['profile_id'] = 666;
  3332. api_friendica_profile_show('json');
  3333. }
  3334. /**
  3335. * Test the api_friendica_profile_show() function without an authenticated user.
  3336. * @return void
  3337. * @expectedException Friendica\Network\HTTPException\ForbiddenException
  3338. */
  3339. public function testApiFriendicaProfileShowWithoutAuthenticatedUser()
  3340. {
  3341. $_SESSION['authenticated'] = false;
  3342. api_friendica_profile_show('json');
  3343. }
  3344. /**
  3345. * Test the api_saved_searches_list() function.
  3346. * @return void
  3347. */
  3348. public function testApiSavedSearchesList()
  3349. {
  3350. $result = api_saved_searches_list('json');
  3351. $this->assertEquals(1, $result['terms'][0]['id']);
  3352. $this->assertEquals(1, $result['terms'][0]['id_str']);
  3353. $this->assertEquals('Saved search', $result['terms'][0]['name']);
  3354. $this->assertEquals('Saved search', $result['terms'][0]['query']);
  3355. }
  3356. }