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->assertGreaterThanOrEqual(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']);
89 $this->assertContains($cat['title'],
90 ['Special', 'Labels', 'Uncategorized']);
94 public function testGetHeadlines() {
96 $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
98 $this->assertInternalType('array', $ret['content']);
100 foreach ($ret['content'] as $hl) {
101 $this->assertInternalType('array', $hl);
103 $this->assertNotEmpty($hl['guid']);
104 $this->assertNotEmpty($hl['title']);
105 $this->assertNotEmpty($hl['link']);
108 $ret = $this->apiCall(['feed_id' => 1, 'view_mode' => 'all_articles'], "getHeadlines");
110 $this->assertInternalType('array', $ret['content']);
112 foreach ($ret['content'] as $hl) {
113 $this->assertInternalType('array', $hl);
115 $this->assertNotEmpty($hl['guid']);
116 $this->assertNotEmpty($hl['title']);
117 $this->assertNotEmpty($hl['link']);
121 public function testArticle() {
124 $ret = $this->apiCall(['feed_id' => -4], "getHeadlines");
126 $this->assertInternalType('array', $ret['content'][0]);
127 $article_id = $ret['content'][0]['id'];
128 $title = $ret['content'][0]['title'];
130 $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
132 $this->assertInternalType('array', $ret['content']);
133 $this->assertNotEmpty($ret['content'][0]['content']);
134 $this->assertEquals($title, $ret['content'][0]['title']);
137 public function testCounters() {
140 $ret = $this->apiCall(['output_mode' => 'flc'], "getCounters");
142 $this->assertInternalType('array', $ret['content']);
144 foreach ($ret['content'] as $ctr) {
145 $this->assertInternalType('array', $ctr);
147 $this->assertNotNull($ctr['id']);
148 $this->assertGreaterThanOrEqual(0, $ctr['counter']);
152 public function testGetConfig() {
155 $ret = $this->apiCall([], "getConfig");
157 $this->assertInternalType('array', $ret['content']);
159 foreach ($ret['content'] as $k => $v) {
160 $this->assertInternalType('string', $k);
161 $this->assertNotEmpty($k);
165 public function testBasicPrefs() {
168 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
169 $this->assertEquals(1, $ret['content']['value']);
171 set_pref('ENABLE_API_ACCESS', false, 1);
173 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
174 $this->assertEquals(0, $ret['content']['value']);
176 set_pref('ENABLE_API_ACCESS', true, 1);
178 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
179 $this->assertEquals(1, $ret['content']['value']);
182 public function testFeedTree() {
185 $ret = $this->apiCall([], "getFeedTree");
186 $this->assertInternalType('array', $ret['content']);
189 foreach ($ret['content'] as $tr) {
190 $this->assertInternalType('array', $tr);
192 $this->assertInternalType('array', $tr['items']);
195 foreach ($tr['items'] as $cr) {
196 $this->assertInternalType('array', $cr['items']);
198 $this->assertNotEmpty($cr['id']);
199 $this->assertNotEmpty($cr['name']);
202 foreach ($cr['items'] as $fr) {
203 $this->assertNotEmpty($fr['id']);
204 $this->assertNotEmpty($fr['name']);
211 public function testLabels() {
214 Labels::create('Test', '', '', 1);
217 $ret = $this->apiCall([], "getLabels");
218 $this->assertInternalType('array', $ret['content']);
220 $this->assertEquals('Test', $ret['content'][0]['caption']);
221 $label_feed_id = $ret['content'][0]['id'];
222 $label_id = Labels::feed_to_label_id($label_feed_id);
224 $this->assertLessThan(0, $label_feed_id);
225 $this->assertGreaterThan(0, $label_id);
227 // assign/remove label to article
229 $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
230 $this->assertInternalType('array', $ret['content'][0]);
231 $article_id = $ret['content'][0]['id'];
233 $ret = $this->apiCall(['article_ids' => $article_id,
234 'label_id' => $label_feed_id, "assign" => "true"],
237 $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
238 $this->assertContains($label_feed_id, $ret['content'][0]['labels'][0]);
240 $ret = $this->apiCall(['article_ids' => $article_id,
241 'label_id' => $label_feed_id, "assign" => "false"],
244 $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
245 $this->assertEmpty($ret['content'][0]['labels']);
247 // clean up and check
249 Labels::remove($label_id, 1);
251 $ret = $this->apiCall([], "getLabels");
252 $this->assertEmpty($ret['content']);