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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
<?php
use PHPUnit\Framework\TestCase;
final class ListTest extends TestCase
{
private string $apiUrl;
private ?string $cookieFile = null;
protected function setUp(): void
{
$this->apiUrl = getenv('API_URL') ?: 'http://localhost:9080/backend.php';
$this->cookieFile = tempnam(sys_get_temp_dir(), 'epube_cookie_');
}
protected function tearDown(): void
{
if ($this->cookieFile && file_exists($this->cookieFile)) {
unlink($this->cookieFile);
}
}
/**
* Login and return a cookie file path ready for authenticated requests.
* @return string Path to the cookie jar file.
*/
private function loginAs(string $username, string $password): string
{
$ch = curl_init($this->apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query([
'op' => 'perform-login',
'user' => $username,
'password' => $password,
]),
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 5,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->assertEquals(200, $status, "Login failed: $body");
$decoded = json_decode($body, true);
$this->assertArrayHasKey('result', $decoded);
$this->assertEquals('ok', $decoded['result']);
return $this->cookieFile;
}
/**
* Make an authenticated HTTP POST request and return [status_code, decoded_body].
* @param array<string, string|int> $data
* @return array<int, mixed>
*/
private function authenticatedPost(string $endpoint, array $data): array
{
$ch = curl_init($endpoint);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 5,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) {
$this->fail("cURL error ($errno): $error");
}
/** @var array<string, mixed>|null $decoded */
$decoded = json_decode($body, true);
return [$status, $decoded ?? []];
}
// ── Unauthenticated access ──────────────────────────────────────────────
public function test_list_requires_authentication(): void
{
$ch = curl_init($this->apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(['op' => 'list']),
CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 5,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->assertEquals(401, $status);
}
// ── Basic list (authenticated) ──────────────────────────────────────────
public function test_list_returns_books(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('books', $body);
$this->assertArrayHasKey('total', $body);
$this->assertIsArray($body['books']);
$this->assertGreaterThan(0, $body['total']);
}
public function test_list_book_fields(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'limit' => 1,
]);
$this->assertEquals(200, $status);
$this->assertCount(1, $body['books']);
$book = $body['books'][0];
$this->assertArrayHasKey('id', $book);
$this->assertArrayHasKey('title', $book);
$this->assertArrayHasKey('author_sort', $book);
$this->assertArrayHasKey('series_name', $book);
$this->assertArrayHasKey('series_index', $book);
$this->assertArrayHasKey('cover_link', $book);
$this->assertArrayHasKey('is_fav', $book);
$this->assertArrayHasKey('download_formats', $book);
}
// ── Pagination ──────────────────────────────────────────────────────────
public function test_list_pagination(): void
{
$this->loginAs('test', 'test');
[$status1, $body1] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'limit' => 2,
'offset' => 0,
]);
[$status2, $body2] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'limit' => 2,
'offset' => 2,
]);
$this->assertEquals(200, $status1);
$this->assertEquals(200, $status2);
$this->assertCount(2, $body1['books']);
$this->assertCount(2, $body2['books']);
$this->assertNotEquals($body1['books'][0]['id'], $body2['books'][0]['id']);
}
// ── Search / query ──────────────────────────────────────────────────────
public function test_list_search_by_title(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'query' => 'Huckleberry',
]);
$this->assertEquals(200, $status);
$this->assertGreaterThan(0, $body['total']);
foreach ($body['books'] as $book) {
$this->assertStringContainsString('Huckleberry', $book['title']);
}
}
public function test_list_search_by_author(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'query' => 'Kafka',
]);
$this->assertEquals(200, $status);
$this->assertGreaterThan(0, $body['total']);
foreach ($body['books'] as $book) {
$this->assertStringContainsString('Kafka', $book['author_sort']);
}
}
public function test_list_search_no_match(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'query' => 'xyznonexistent',
]);
$this->assertEquals(200, $status);
$this->assertEquals(0, $body['total']);
$this->assertCount(0, $body['books']);
}
// ── Favorites mode ──────────────────────────────────────────────────────
public function test_list_favorites_empty(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'mode' => 'favorites',
]);
$this->assertEquals(200, $status);
$this->assertEquals(0, $body['total']);
$this->assertCount(0, $body['books']);
}
// ── Selecting books by ids ──────────────────────────────────────────────
/**
* Fetch book IDs from a full list so we can reference real IDs in subsequent tests.
*/
private function getAllBookIds(): array
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'limit' => 100,
]);
$this->assertEquals(200, $status);
return array_map(fn($b) => (int) $b['id'], $body['books']);
}
public function test_list_by_ids_single(): void
{
$ids = $this->getAllBookIds();
$this->assertGreaterThan(0, count($ids), 'Test data should contain at least one book');
$singleId = $ids[0];
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => (string) $singleId,
]);
$this->assertEquals(200, $status);
$this->assertEquals(1, $body['total']);
$this->assertCount(1, $body['books']);
$this->assertEquals($singleId, (int) $body['books'][0]['id']);
}
public function test_list_by_ids_multiple(): void
{
$ids = $this->getAllBookIds();
$this->assertGreaterThanOrEqual(3, count($ids), 'Test data should contain at least 3 books');
$selectedIds = [$ids[0], $ids[1], $ids[2]];
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => implode(',', $selectedIds),
]);
$this->assertEquals(200, $status);
$expectedTotal = count($selectedIds);
$this->assertEquals($expectedTotal, $body['total']);
$this->assertCount($expectedTotal, $body['books']);
$foundIds = array_map(fn($b) => (int) $b['id'], $body['books']);
sort($foundIds);
$sortedSelected = $selectedIds;
sort($sortedSelected);
$this->assertEquals($sortedSelected, $foundIds);
}
public function test_list_by_ids_nonexistent(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => '999999',
]);
$this->assertEquals(200, $status);
$this->assertEquals(0, $body['total']);
$this->assertCount(0, $body['books']);
}
public function test_list_by_ids_mixed_existing_and_nonexistent(): void
{
$ids = $this->getAllBookIds();
$this->assertGreaterThan(0, count($ids));
$selectedIds = [$ids[0], 999999, $ids[1]];
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => implode(',', $selectedIds),
]);
$this->assertEquals(200, $status);
$this->assertEquals(2, $body['total']);
$this->assertCount(2, $body['books']);
$foundIds = array_map(fn($b) => (int) $b['id'], $body['books']);
$this->assertContains((int) $ids[0], $foundIds);
$this->assertContains((int) $ids[1], $foundIds);
}
public function test_list_by_ids_overrides_query(): void
{
$ids = $this->getAllBookIds();
$this->assertGreaterThan(0, count($ids));
// Use a query that would match many books, but restrict to a single id
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => (string) $ids[0],
'query' => 'the',
]);
$this->assertEquals(200, $status);
$this->assertEquals(1, $body['total']);
$this->assertCount(1, $body['books']);
$foundIds = array_map(fn($b) => (int) $b['id'], $body['books']);
$this->assertContains((int) $ids[0], $foundIds);
}
public function test_list_by_ids_empty_string_returns_all(): void
{
$this->loginAs('test', 'test');
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => '',
]);
$this->assertEquals(200, $status);
$this->assertGreaterThan(0, $body['total']);
$this->assertIsArray($body['books']);
}
public function test_list_by_ids_with_invalid_values(): void
{
$ids = $this->getAllBookIds();
$this->assertGreaterThan(0, count($ids));
// Mix of valid and invalid (non-numeric) values; intval should filter them out
[$status, $body] = $this->authenticatedPost($this->apiUrl, [
'op' => 'list',
'ids' => (string) $ids[0] . ',abc,0,',
]);
$this->assertEquals(200, $status);
$this->assertEquals(1, $body['total']);
$this->assertCount(1, $body['books']);
$foundIds = array_map(fn($b) => (int) $b['id'], $body['books']);
$this->assertContains((int) $ids[0], $foundIds);
}
}
|