]> git.wh0rd.org Git - tt-rss.git/blob - tests/ApiTest.php
feeds: remove sql_bool_to_bool()
[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->assertGreaterThanOrEqual(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                         $this->assertContains($cat['title'],
90                                 ['Special', 'Labels', 'Uncategorized']);
91                 }
92         }
93
94         public function testGetHeadlines() {
95                 $this->testLogin();
96                 $ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");
97
98                 $this->assertInternalType('array', $ret['content']);
99
100                 foreach ($ret['content'] as $hl) {
101                         $this->assertInternalType('array', $hl);
102
103                         $this->assertNotEmpty($hl['guid']);
104                         $this->assertNotEmpty($hl['title']);
105                         $this->assertNotEmpty($hl['link']);
106                 }
107
108                 $ret = $this->apiCall(['feed_id' => 1, 'view_mode' => 'all_articles'], "getHeadlines");
109
110                 $this->assertInternalType('array', $ret['content']);
111
112                 foreach ($ret['content'] as $hl) {
113                         $this->assertInternalType('array', $hl);
114
115                         $this->assertNotEmpty($hl['guid']);
116                         $this->assertNotEmpty($hl['title']);
117                         $this->assertNotEmpty($hl['link']);
118                 }
119         }
120
121         public function testArticle() {
122
123                 $this->testLogin();
124                 $ret = $this->apiCall(['feed_id' => -4], "getHeadlines");
125
126                 $this->assertInternalType('array', $ret['content'][0]);
127                 $article_id = $ret['content'][0]['id'];
128                 $title = $ret['content'][0]['title'];
129
130                 $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
131
132                 $this->assertInternalType('array', $ret['content']);
133                 $this->assertNotEmpty($ret['content'][0]['content']);
134                 $this->assertEquals($title, $ret['content'][0]['title']);
135         }
136
137         public function testCounters() {
138
139                 $this->testLogin();
140                 $ret = $this->apiCall(['output_mode' => 'flc'], "getCounters");
141
142                 $this->assertInternalType('array', $ret['content']);
143
144                 foreach ($ret['content'] as $ctr) {
145                         $this->assertInternalType('array', $ctr);
146
147                         $this->assertNotNull($ctr['id']);
148                         $this->assertGreaterThanOrEqual(0, $ctr['counter']);
149                 }
150         }
151
152         public function testGetConfig() {
153
154                 $this->testLogin();
155                 $ret = $this->apiCall([], "getConfig");
156
157                 $this->assertInternalType('array', $ret['content']);
158
159                 foreach ($ret['content'] as $k => $v) {
160                         $this->assertInternalType('string', $k);
161                         $this->assertNotEmpty($k);
162                 }
163         }
164
165         public function testBasicPrefs() {
166
167                 $this->testLogin();
168                 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
169                 $this->assertEquals(1, $ret['content']['value']);
170
171                 set_pref('ENABLE_API_ACCESS', false, 1);
172
173                 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
174                 $this->assertEquals(0, $ret['content']['value']);
175
176                 set_pref('ENABLE_API_ACCESS', true, 1);
177
178                 $ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
179                 $this->assertEquals(1, $ret['content']['value']);
180         }
181
182         public function testFeedTree() {
183
184                 $this->testLogin();
185                 $ret = $this->apiCall([], "getFeedTree");
186                 $this->assertInternalType('array', $ret['content']);
187
188                 // root
189                 foreach ($ret['content'] as $tr) {
190                         $this->assertInternalType('array', $tr);
191
192                         $this->assertInternalType('array', $tr['items']);
193
194                         // cats
195                         foreach ($tr['items'] as $cr) {
196                                 $this->assertInternalType('array', $cr['items']);
197
198                                 $this->assertNotEmpty($cr['id']);
199                                 $this->assertNotEmpty($cr['name']);
200
201                                 // feeds
202                                 foreach ($cr['items'] as $fr) {
203                                         $this->assertNotEmpty($fr['id']);
204                                         $this->assertNotEmpty($fr['name']);
205                                 }
206                         }
207                 }
208         }
209
210
211         public function testLabels() {
212                 // create label
213
214                 Labels::create('Test', '', '', 1);
215
216                 $this->testLogin();
217                 $ret = $this->apiCall([], "getLabels");
218                 $this->assertInternalType('array', $ret['content']);
219
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);
223
224                 $this->assertLessThan(0, $label_feed_id);
225                 $this->assertGreaterThan(0, $label_id);
226
227                 // assign/remove label to article
228
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'];
232
233                 $ret = $this->apiCall(['article_ids' => $article_id,
234                         'label_id' => $label_feed_id, "assign" => "true"],
235                         "setArticleLabel");
236
237                 $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
238                 $this->assertContains($label_feed_id, $ret['content'][0]['labels'][0]);
239
240                 $ret = $this->apiCall(['article_ids' => $article_id,
241                         'label_id' => $label_feed_id, "assign" => "false"],
242                         "setArticleLabel");
243
244                 $ret = $this->apiCall(['article_id' => $article_id], "getArticle");
245                 $this->assertEmpty($ret['content'][0]['labels']);
246
247                 // clean up and check
248
249                 Labels::remove($label_id, 1);
250
251                 $ret = $this->apiCall([], "getLabels");
252                 $this->assertEmpty($ret['content']);
253         }
254
255
256 }