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