summaryrefslogtreecommitdiff
path: root/tests/ApiTest.php
blob: f8d34b7c7f14c4f6ae25682cd2263da0285d113c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
use PHPUnit\Framework\TestCase;

set_include_path(dirname(__DIR__) ."/include" . PATH_SEPARATOR .
	dirname(__DIR__) . PATH_SEPARATOR .
	get_include_path());

require_once "autoload.php";

final class ApiTest extends TestCase {

	public function __construct() {
		init_plugins();
		initialize_user_prefs(1);
		set_pref('ENABLE_API_ACCESS', true, 1);

		parent::__construct();
	}

	public function apiCall($args, $method) {
		$_REQUEST = $args;

		$api = new API($args);
		ob_start();
		$api->$method();
		$rv = json_decode(ob_get_contents(), true);
		ob_end_clean();

		$this->assertEquals(API::STATUS_OK, $rv['status']);

		return $rv;
	}

	public function testBasicAuth() {
		$this->assertEquals(true,
			authenticate_user("admin", "password"));
	}

	public function testVersion() {

		$ret = $this->apiCall([], "getVersion");

		$this->assertStringStartsWith(
			VERSION_STATIC,
			$ret['content']['version']);
	}

	public function testLogin() {

		$ret = $this->apiCall(["op" => "login",
			"user" => "admin",
			"password" => "password"], "login");

		$this->assertNotEmpty($ret['content']['session_id']);
	}

	public function testGetUnread() {
		$this->testLogin();
		$ret = $this->apiCall([],"getUnread");

		$this->assertNotEmpty($ret['content']['unread']);
	}

	public function testGetFeeds() {
		$this->testLogin();
		$ret = $this->apiCall([], "getFeeds");

		$this->assertInternalType('array', $ret['content']);

		$this->assertEquals("http://tt-rss.org/forum/rss.php",
			$ret['content'][0]['feed_url']);

	}

	public function testGetCategories() {
		$this->testLogin();
		$ret = $this->apiCall([], "getCategories");

		$this->assertInternalType('array', $ret['content']);

		$this->assertEquals(2, sizeof($ret['content']));

		foreach ($ret['content'] as $cat) {

			$this->assertNotEmpty($cat['title']);
			$this->assertNotNull($cat['id']);
			$this->assertGreaterThanOrEqual(0, $cat['unread']);
		}
	}

	public function testGetHeadlines() {
		$this->testLogin();
		$ret = $this->apiCall(['feed_id' => -4, 'view_mode' => 'adaptive'], "getHeadlines");

		$this->assertInternalType('array', $ret['content']);

		foreach ($ret['content'] as $hl) {
			$this->assertInternalType('array', $hl);

			$this->assertNotEmpty($hl['guid']);
			$this->assertNotEmpty($hl['title']);
			$this->assertNotEmpty($hl['link']);
		}

		$ret = $this->apiCall(['feed_id' => 1, 'view_mode' => 'all_articles'], "getHeadlines");

		$this->assertInternalType('array', $ret['content']);

		foreach ($ret['content'] as $hl) {
			$this->assertInternalType('array', $hl);

			$this->assertNotEmpty($hl['guid']);
			$this->assertNotEmpty($hl['title']);
			$this->assertNotEmpty($hl['link']);
		}
	}

	public function testArticle() {

		$this->testLogin();
		$ret = $this->apiCall(['feed_id' => -4], "getHeadlines");

		$this->assertInternalType('array', $ret['content'][0]);
		$id = $ret['content'][0]['id'];
		$title = $ret['content'][0]['title'];

		$ret = $this->apiCall(['article_id' => $id], "getArticle");

		$this->assertInternalType('array', $ret['content']);
		$this->assertNotEmpty($ret['content'][0]['content']);
		$this->assertEquals($title, $ret['content'][0]['title']);

	}

	public function testCounters() {

		$this->testLogin();
		$ret = $this->apiCall(['output_mode' => 'flc'], "getCounters");

		$this->assertInternalType('array', $ret['content']);

		foreach ($ret['content'] as $ctr) {
			$this->assertInternalType('array', $ctr);

			$this->assertNotNull($ctr['id']);
			$this->assertGreaterThanOrEqual(0, $ctr['counter']);
		}
	}

	public function testGetConfig() {

		$this->testLogin();
		$ret = $this->apiCall([], "getConfig");

		$this->assertInternalType('array', $ret['content']);

		foreach ($ret['content'] as $k => $v) {
			$this->assertInternalType('string', $k);
			$this->assertNotEmpty($k);
		}
	}

	public function testBasicPrefs() {

		$this->testLogin();
		$ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
		$this->assertEquals(1, $ret['content']['value']);

		set_pref('ENABLE_API_ACCESS', false, 1);

		$ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
		$this->assertEquals(0, $ret['content']['value']);

		set_pref('ENABLE_API_ACCESS', true, 1);

		$ret = $this->apiCall(['pref_name' => 'ENABLE_API_ACCESS'], "getPref");
		$this->assertEquals(1, $ret['content']['value']);
	}

	public function testFeedTree() {

		$this->testLogin();
		$ret = $this->apiCall([], "getFeedTree");
		$this->assertInternalType('array', $ret['content']);

		// root
		foreach ($ret['content'] as $tr) {
			$this->assertInternalType('array', $tr);

			$this->assertInternalType('array', $tr['items']);

			// cats
			foreach ($tr['items'] as $cr) {
				$this->assertInternalType('array', $cr['items']);

				$this->assertNotEmpty($cr['id']);
				$this->assertNotEmpty($cr['name']);

				// feeds
				foreach ($cr['items'] as $fr) {
					$this->assertNotEmpty($fr['id']);
					$this->assertNotEmpty($fr['name']);
				}
			}
		}
	}


	public function testLabels() {
		label_create('Test', '', '', 1);

		$this->testLogin();
		$ret = $this->apiCall([], "getLabels");
		$this->assertInternalType('array', $ret['content']);

		$this->assertEquals('Test', $ret['content'][0]['caption']);
		$label_id = feed_to_label_id($ret['content'][0]['id']);

		$this->assertGreaterThan(0, $label_id);

		// TODO: assign label to article

		label_remove($label_id, 1);

		$ret = $this->apiCall([], "getLabels");
		$this->assertEmpty($ret['content']);
	}


}