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
|
<?php
use PHPUnit\Framework\TestCase;
final class PaginationHandlerTest extends ApiTestCase
{
// ── Helper methods ───────────────────────────────────────────────────────
/** @return array{bookid: int, total_pages: int, pagination: string}|null */
private function getPagination(int $bookid): ?array
{
$pdo = \Db::pdo();
$sth = $pdo->prepare("SELECT bookid, total_pages, pagination FROM epube_pagination WHERE bookid = ?");
$sth->execute([$bookid]);
return $sth->fetch() ?: null;
}
private function deletePagination(int $bookid): void
{
$pdo = \Db::pdo();
$sth = $pdo->prepare("DELETE FROM epube_pagination WHERE bookid = ?");
$sth->execute([$bookid]);
}
// ── Authentication ───────────────────────────────────────────────────────
public function test_getpagination_requires_authentication(): void
{
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getpagination',
'id' => 2,
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
public function test_storepagination_requires_authentication(): void
{
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storepagination',
'id' => 2,
'payload' => '{"page":1}',
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
public function test_deletepagination_requires_authentication(): void
{
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'deletepagination',
'id' => 2,
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
// ── storepagination ──────────────────────────────────────────────────────
public function test_storepagination_creates_new_record(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99998;
$this->deletePagination($bookid);
$payload = '{"page":12,"cfi":"epubcfi(/6/14[]/4:123)"}';
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storepagination',
'id' => $bookid,
'payload' => $payload,
]);
$this->assertEquals(200, $status);
// Verify DB record was created
$record = $this->getPagination($bookid);
$this->assertNotNull($record);
$this->assertEquals($bookid, $record['bookid']);
$this->assertEquals($payload, $record['pagination']);
$this->assertEquals(100, (int)$record['total_pages']);
}
public function test_storepagination_updates_existing_record(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99997;
$this->deletePagination($bookid);
// Create initial record
$pdo = \Db::pdo();
$sth = $pdo->prepare("INSERT INTO epube_pagination (bookid, pagination, total_pages) VALUES (?, ?, ?)");
$sth->execute([$bookid, '{"page":5}', 50]);
$newPayload = '{"page":25,"cfi":"epubcfi(/6/20[]/4:456)"}';
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storepagination',
'id' => $bookid,
'payload' => $newPayload,
]);
$this->assertEquals(200, $status);
// Verify DB was updated (total_pages stays 100)
$record = $this->getPagination($bookid);
$this->assertNotNull($record);
$this->assertEquals($newPayload, $record['pagination']);
$this->assertEquals(100, (int)$record['total_pages']);
}
public function test_storepagination_with_zero_id(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storepagination',
'id' => 0,
'payload' => '{"page":1}',
]);
$this->assertEquals(200, $status);
// With id=0, the if ($bookid) check fails → no-op
}
public function test_storepagination_with_empty_payload(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99996;
$this->deletePagination($bookid);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storepagination',
'id' => $bookid,
'payload' => '',
]);
$this->assertEquals(200, $status);
// With empty payload, the if ($payload) check fails → no-op
// Verify no record was created
$record = $this->getPagination($bookid);
$this->assertNull($record);
}
// ── getpagination ────────────────────────────────────────────────────────
public function test_getpagination_returns_stored_data(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99995;
$this->deletePagination($bookid);
$payload = '{"page":42,"cfi":"epubcfi(/6/30[]/4:789)"}';
$this->post($this->apiUrl, [
'op' => 'storepagination',
'id' => $bookid,
'payload' => $payload,
]);
[$status, $body, $headers] = $this->post($this->apiUrl, [
'op' => 'getpagination',
'id' => $bookid,
], false);
$this->assertEquals(200, $status);
$this->assertEquals($payload, $body);
}
public function test_getpagination_with_no_record(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$this->deletePagination(99999);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getpagination',
'id' => 99999,
]);
$this->assertEquals(404, $status);
$this->assertEquals('File not found.', $body['error']);
}
public function test_getpagination_with_zero_id(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getpagination',
'id' => 0,
]);
$this->assertEquals(404, $status);
$this->assertEquals('File not found.', $body['error']);
}
public function test_getpagination_with_negative_id(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getpagination',
'id' => -1,
]);
$this->assertEquals(404, $status);
$this->assertEquals('File not found.', $body['error']);
}
// ── deletepagination ─────────────────────────────────────────────────────
public function test_deletepagination_removes_record(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99994;
$this->deletePagination($bookid);
// Create record first
$pdo = \Db::pdo();
$sth = $pdo->prepare("INSERT INTO epube_pagination (bookid, pagination, total_pages) VALUES (?, ?, ?)");
$sth->execute([$bookid, '{"page":1}', 100]);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'deletepagination',
'id' => $bookid,
]);
$this->assertEquals(200, $status);
// Verify record was deleted
$record = $this->getPagination($bookid);
$this->assertNull($record);
}
public function test_deletepagination_with_no_record(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$this->deletePagination(99999);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'deletepagination',
'id' => 99999,
]);
$this->assertEquals(404, $status);
$this->assertEquals('Not found.', $body['error']);
}
public function test_deletepagination_with_zero_id(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'deletepagination',
'id' => 0,
]);
$this->assertEquals(404, $status);
$this->assertEquals('Not found.', $body['error']);
}
}
|