ApiTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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->assertEquals(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. }
  62. }
  63. public function testGetHeadlines() {
  64. $this->testLogin();
  65. $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
  66. $this->assertInternalType('array', $ret['content']);
  67. foreach ($ret['content'] as $hl) {
  68. $this->assertInternalType('array', $hl);
  69. $this->assertNotEmpty($hl['guid']);
  70. $this->assertNotEmpty($hl['title']);
  71. $this->assertNotEmpty($hl['link']);
  72. }
  73. $ret = $this->apiCall(['feed_id' => 1, 'view_mode' => 'all_articles'], "getHeadlines");
  74. $this->assertInternalType('array', $ret['content']);
  75. foreach ($ret['content'] as $hl) {
  76. $this->assertInternalType('array', $hl);
  77. $this->assertNotEmpty($hl['guid']);
  78. $this->assertNotEmpty($hl['title']);
  79. $this->assertNotEmpty($hl['link']);
  80. }
  81. }
  82. public function testArticle() {
  83. $this->testLogin();
  84. $ret = $this->apiCall(['feed_id' => -4], "getHeadlines");
  85. $this->assertInternalType('array', $ret['content'][0]);
  86. $id = $ret['content'][0]['id'];
  87. $title = $ret['content'][0]['title'];
  88. $ret = $this->apiCall(['article_id' => $id], "getArticle");
  89. $this->assertInternalType('array', $ret['content']);
  90. $this->assertNotEmpty($ret['content'][0]['content']);
  91. $this->assertEquals($title, $ret['content'][0]['title']);
  92. }
  93. public function testCounters() {
  94. $this->testLogin();
  95. $ret = $this->apiCall(['output_mode' => 'flc'], "getCounters");
  96. $this->assertInternalType('array', $ret['content']);
  97. foreach ($ret['content'] as $ctr) {
  98. $this->assertInternalType('array', $ctr);
  99. $this->assertNotNull($ctr['id']);
  100. $this->assertGreaterThanOrEqual(0, $ctr['counter']);
  101. }
  102. }
  103. public function testGetConfig() {
  104. $this->testLogin();
  105. $ret = $this->apiCall([], "getConfig");
  106. $this->assertInternalType('array', $ret['content']);
  107. foreach ($ret['content'] as $k => $v) {
  108. $this->assertInternalType('string', $k);
  109. $this->assertNotEmpty($k);
  110. }
  111. }
  112. public function testBasicPrefs() {
  113. $this->testLogin();
  114. $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
  115. $this->assertEquals(1, $ret['content']['value']);
  116. set_pref('ENABLE_API_ACCESS', false, 1);
  117. $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
  118. $this->assertEquals(0, $ret['content']['value']);
  119. set_pref('ENABLE_API_ACCESS', true, 1);
  120. $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
  121. $this->assertEquals(1, $ret['content']['value']);
  122. }
  123. public function testFeedTree() {
  124. $this->testLogin();
  125. $ret = $this->apiCall([], "getFeedTree");
  126. $this->assertInternalType('array', $ret['content']);
  127. // root
  128. foreach ($ret['content'] as $tr) {
  129. $this->assertInternalType('array', $tr);
  130. $this->assertInternalType('array', $tr['items']);
  131. // cats
  132. foreach ($tr['items'] as $cr) {
  133. $this->assertInternalType('array', $cr['items']);
  134. $this->assertNotEmpty($cr['id']);
  135. $this->assertNotEmpty($cr['name']);
  136. // feeds
  137. foreach ($cr['items'] as $fr) {
  138. $this->assertNotEmpty($fr['id']);
  139. $this->assertNotEmpty($fr['name']);
  140. }
  141. }
  142. }
  143. }
  144. public function testLabels() {
  145. label_create('Test', '', '', 1);
  146. $this->testLogin();
  147. $ret = $this->apiCall([], "getLabels");
  148. $this->assertInternalType('array', $ret['content']);
  149. $this->assertEquals('Test', $ret['content'][0]['caption']);
  150. $label_id = feed_to_label_id($ret['content'][0]['id']);
  151. $this->assertGreaterThan(0, $label_id);
  152. // TODO: assign label to article
  153. label_remove($label_id, 1);
  154. $ret = $this->apiCall([], "getLabels");
  155. $this->assertEmpty($ret['content']);
  156. }
  157. }