ApiTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. set_include_path(dirname(__DIR__) ."/include" . PATH_SEPARATOR .
  4. dirname(__DIR__) . PATH_SEPARATOR .
  5. get_include_path());
  6. require_once "autoload.php";
  7. final class ApiTest extends TestCase {
  8. public function __construct() {
  9. init_plugins();
  10. initialize_user_prefs(1);
  11. set_pref('ENABLE_API_ACCESS', true, 1);
  12. parent::__construct();
  13. }
  14. public function apiCall($args, $method) {
  15. $_REQUEST = $args;
  16. $api = new API($args);
  17. ob_start();
  18. $api->$method();
  19. $rv = json_decode(ob_get_contents(), true);
  20. ob_end_clean();
  21. $this->assertEquals(API::STATUS_OK, $rv['status']);
  22. return $rv;
  23. }
  24. public function testBasicAuth() {
  25. $this->assertEquals(true,
  26. authenticate_user("admin", "password"));
  27. }
  28. public function testVersion() {
  29. $ret = $this->apiCall([], "getVersion");
  30. $this->assertStringStartsWith(
  31. VERSION_STATIC,
  32. $ret['content']['version']);
  33. }
  34. public function testLogin() {
  35. $ret = $this->apiCall(["op" => "login",
  36. "user" => "admin",
  37. "password" => "password"], "login");
  38. $this->assertNotEmpty($ret['content']['session_id']);
  39. }
  40. public function testGetUnread() {
  41. $this->testLogin();
  42. $ret = $this->apiCall([],"getUnread");
  43. $this->assertNotEmpty($ret['content']['unread']);
  44. }
  45. public function testGetFeeds() {
  46. $this->testLogin();
  47. $ret = $this->apiCall([], "getFeeds");
  48. $this->assertInternalType('array', $ret['content']);
  49. $this->assertEquals("http://tt-rss.org/forum/rss.php",
  50. $ret['content'][0]['feed_url']);
  51. }
  52. public function testGetCategories() {
  53. $this->testLogin();
  54. $ret = $this->apiCall([], "getCategories");
  55. $this->assertInternalType('array', $ret['content']);
  56. $this->assertGreaterThanOrEqual(2, sizeof($ret['content']));
  57. foreach ($ret['content'] as $cat) {
  58. $this->assertNotEmpty($cat['title']);
  59. $this->assertNotNull($cat['id']);
  60. $this->assertGreaterThanOrEqual(0, $cat['unread']);
  61. $this->assertContains($cat['title'],
  62. ['Special', 'Labels', 'Uncategorized']);
  63. }
  64. }
  65. public function testGetHeadlines() {
  66. $this->testLogin();
  67. $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
  68. $this->assertInternalType('array', $ret['content']);
  69. foreach ($ret['content'] as $hl) {
  70. $this->assertInternalType('array', $hl);
  71. $this->assertNotEmpty($hl['guid']);
  72. $this->assertNotEmpty($hl['title']);
  73. $this->assertNotEmpty($hl['link']);
  74. }
  75. $ret = $this->apiCall(['feed_id' => 1, 'view_mode' => 'all_articles'], "getHeadlines");
  76. $this->assertInternalType('array', $ret['content']);
  77. foreach ($ret['content'] as $hl) {
  78. $this->assertInternalType('array', $hl);
  79. $this->assertNotEmpty($hl['guid']);
  80. $this->assertNotEmpty($hl['title']);
  81. $this->assertNotEmpty($hl['link']);
  82. }
  83. }
  84. public function testArticle() {
  85. $this->testLogin();
  86. $ret = $this->apiCall(['feed_id' => -4], "getHeadlines");
  87. $this->assertInternalType('array', $ret['content'][0]);
  88. $article_id = $ret['content'][0]['id'];
  89. $title = $ret['content'][0]['title'];
  90. $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
  91. $this->assertInternalType('array', $ret['content']);
  92. $this->assertNotEmpty($ret['content'][0]['content']);
  93. $this->assertEquals($title, $ret['content'][0]['title']);
  94. }
  95. public function testCounters() {
  96. $this->testLogin();
  97. $ret = $this->apiCall(['output_mode' => 'flc'], "getCounters");
  98. $this->assertInternalType('array', $ret['content']);
  99. foreach ($ret['content'] as $ctr) {
  100. $this->assertInternalType('array', $ctr);
  101. $this->assertNotNull($ctr['id']);
  102. $this->assertGreaterThanOrEqual(0, $ctr['counter']);
  103. }
  104. }
  105. public function testGetConfig() {
  106. $this->testLogin();
  107. $ret = $this->apiCall([], "getConfig");
  108. $this->assertInternalType('array', $ret['content']);
  109. foreach ($ret['content'] as $k => $v) {
  110. $this->assertInternalType('string', $k);
  111. $this->assertNotEmpty($k);
  112. }
  113. }
  114. public function testBasicPrefs() {
  115. $this->testLogin();
  116. $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
  117. $this->assertEquals(1, $ret['content']['value']);
  118. set_pref('ENABLE_API_ACCESS', false, 1);
  119. $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
  120. $this->assertEquals(0, $ret['content']['value']);
  121. set_pref('ENABLE_API_ACCESS', true, 1);
  122. $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
  123. $this->assertEquals(1, $ret['content']['value']);
  124. }
  125. public function testFeedTree() {
  126. $this->testLogin();
  127. $ret = $this->apiCall([], "getFeedTree");
  128. $this->assertInternalType('array', $ret['content']);
  129. // root
  130. foreach ($ret['content'] as $tr) {
  131. $this->assertInternalType('array', $tr);
  132. $this->assertInternalType('array', $tr['items']);
  133. // cats
  134. foreach ($tr['items'] as $cr) {
  135. $this->assertInternalType('array', $cr['items']);
  136. $this->assertNotEmpty($cr['id']);
  137. $this->assertNotEmpty($cr['name']);
  138. // feeds
  139. foreach ($cr['items'] as $fr) {
  140. $this->assertNotEmpty($fr['id']);
  141. $this->assertNotEmpty($fr['name']);
  142. }
  143. }
  144. }
  145. }
  146. public function testLabels() {
  147. // create label
  148. Labels::create('Test', '', '', 1);
  149. $this->testLogin();
  150. $ret = $this->apiCall([], "getLabels");
  151. $this->assertInternalType('array', $ret['content']);
  152. $this->assertEquals('Test', $ret['content'][0]['caption']);
  153. $label_feed_id = $ret['content'][0]['id'];
  154. $label_id = Labels::feed_to_label_id($label_feed_id);
  155. $this->assertLessThan(0, $label_feed_id);
  156. $this->assertGreaterThan(0, $label_id);
  157. // assign/remove label to article
  158. $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
  159. $this->assertInternalType('array', $ret['content'][0]);
  160. $article_id = $ret['content'][0]['id'];
  161. $ret = $this->apiCall(['article_ids' => $article_id,
  162. 'label_id' => $label_feed_id, "assign" => "true"],
  163. "setArticleLabel");
  164. $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
  165. $this->assertContains($label_feed_id, $ret['content'][0]['labels'][0]);
  166. $ret = $this->apiCall(['article_ids' => $article_id,
  167. 'label_id' => $label_feed_id, "assign" => "false"],
  168. "setArticleLabel");
  169. $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
  170. $this->assertEmpty($ret['content'][0]['labels']);
  171. // clean up and check
  172. Labels::remove($label_id, 1);
  173. $ret = $this->apiCall([], "getLabels");
  174. $this->assertEmpty($ret['content']);
  175. }
  176. }