2 use PHPUnit\Framework\TestCase
;
4 set_include_path(dirname(__DIR__
) ."/include" . PATH_SEPARATOR
.
5 dirname(__DIR__
) . PATH_SEPARATOR
.
8 require_once "autoload.php";
10 final class ApiTest
extends TestCase
{
12 public function __construct() {
14 initialize_user_prefs(1);
15 set_pref('ENABLE_API_ACCESS', true, 1);
17 parent
::__construct();
20 public function apiCall($args, $method) {
23 $api = new API($args);
26 $rv = json_decode(ob_get_contents(), true);
29 $this->assertEquals(API
::STATUS_OK
, $rv['status']);
34 public function testBasicAuth() {
35 $this->assertEquals(true,
36 authenticate_user("admin", "password"));
39 public function testVersion() {
41 $ret = $this->apiCall([], "getVersion");
43 $this->assertStringStartsWith(
45 $ret['content']['version']);
48 public function testLogin() {
50 $ret = $this->apiCall(["op" => "login",
52 "password" => "password"], "login");
54 $this->assertNotEmpty($ret['content']['session_id']);
57 public function testGetUnread() {
59 $ret = $this->apiCall([],"getUnread");
61 $this->assertNotEmpty($ret['content']['unread']);
64 public function testGetFeeds() {
66 $ret = $this->apiCall([], "getFeeds");
68 $this->assertInternalType('array', $ret['content']);
70 $this->assertEquals("http://tt-rss.org/forum/rss.php",
71 $ret['content'][0]['feed_url']);
75 public function testGetCategories() {
77 $ret = $this->apiCall([], "getCategories");
79 $this->assertInternalType('array', $ret['content']);
81 $this->assertEquals(2, sizeof($ret['content']));
83 foreach ($ret['content'] as $cat) {
85 $this->assertNotEmpty($cat['title']);
86 $this->assertNotNull($cat['id']);
87 $this->assertGreaterThanOrEqual(0, $cat['unread']);
91 public function testGetHeadlines() {
93 $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
95 $this->assertInternalType('array', $ret['content']);
97 foreach ($ret['content'] as $hl) {
98 $this->assertInternalType('array', $hl);
100 $this->assertNotEmpty($hl['guid']);
101 $this->assertNotEmpty($hl['title']);
102 $this->assertNotEmpty($hl['link']);
105 $ret = $this->apiCall(['feed_id' => 1, 'view_mode' => 'all_articles'], "getHeadlines");
107 $this->assertInternalType('array', $ret['content']);
109 foreach ($ret['content'] as $hl) {
110 $this->assertInternalType('array', $hl);
112 $this->assertNotEmpty($hl['guid']);
113 $this->assertNotEmpty($hl['title']);
114 $this->assertNotEmpty($hl['link']);
118 public function testArticle() {
121 $ret = $this->apiCall(['feed_id' => -4], "getHeadlines");
123 $this->assertInternalType('array', $ret['content'][0]);
124 $id = $ret['content'][0]['id'];
125 $title = $ret['content'][0]['title'];
127 $ret = $this->apiCall(['article_id' => $id], "getArticle");
129 $this->assertInternalType('array', $ret['content']);
130 $this->assertNotEmpty($ret['content'][0]['content']);
131 $this->assertEquals($title, $ret['content'][0]['title']);
135 public function testCounters() {
138 $ret = $this->apiCall(['output_mode' => 'flc'], "getCounters");
140 $this->assertInternalType('array', $ret['content']);
142 foreach ($ret['content'] as $ctr) {
143 $this->assertInternalType('array', $ctr);
145 $this->assertNotNull($ctr['id']);
146 $this->assertGreaterThanOrEqual(0, $ctr['counter']);
150 public function testGetConfig() {
153 $ret = $this->apiCall([], "getConfig");
155 $this->assertInternalType('array', $ret['content']);
157 foreach ($ret['content'] as $k => $v) {
158 $this->assertInternalType('string', $k);
159 $this->assertNotEmpty($k);
163 public function testBasicPrefs() {
166 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
167 $this->assertEquals(1, $ret['content']['value']);
169 set_pref('ENABLE_API_ACCESS', false, 1);
171 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
172 $this->assertEquals(0, $ret['content']['value']);
174 set_pref('ENABLE_API_ACCESS', true, 1);
176 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
177 $this->assertEquals(1, $ret['content']['value']);
180 public function testFeedTree() {
183 $ret = $this->apiCall([], "getFeedTree");
184 $this->assertInternalType('array', $ret['content']);
187 foreach ($ret['content'] as $tr) {
188 $this->assertInternalType('array', $tr);
190 $this->assertInternalType('array', $tr['items']);
193 foreach ($tr['items'] as $cr) {
194 $this->assertInternalType('array', $cr['items']);
196 $this->assertNotEmpty($cr['id']);
197 $this->assertNotEmpty($cr['name']);
200 foreach ($cr['items'] as $fr) {
201 $this->assertNotEmpty($fr['id']);
202 $this->assertNotEmpty($fr['name']);
209 public function testLabels() {
210 label_create('Test', '', '', 1);
213 $ret = $this->apiCall([], "getLabels");
214 $this->assertInternalType('array', $ret['content']);
216 $this->assertEquals('Test', $ret['content'][0]['caption']);
217 $label_id = feed_to_label_id($ret['content'][0]['id']);
219 $this->assertGreaterThan(0, $label_id);
221 // TODO: assign label to article
223 label_remove($label_id, 1);
225 $ret = $this->apiCall([], "getLabels");
226 $this->assertEmpty($ret['content']);