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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
<?php
use PHPUnit\Framework\TestCase;
final class LastreadHandlerTest extends ApiTestCase
{
// ── Helper methods ───────────────────────────────────────────────────────
/** @return array{bookid: int, lastread: int, lastcfi: string, lastts: int}|null */
private function getBookLastread(int $bookid): ?array
{
$pdo = \Db::pdo();
$sth = $pdo->prepare("SELECT bookid, lastread, lastcfi, lastts FROM epube_books WHERE bookid = ? AND owner = 'test'");
$sth->execute([$bookid]);
return $sth->fetch() ?: null;
}
private function deleteBookLastread(int $bookid): void
{
$pdo = \Db::pdo();
$sth = $pdo->prepare("DELETE FROM epube_books WHERE bookid = ? AND owner = 'test'");
$sth->execute([$bookid]);
}
// ── Authentication ───────────────────────────────────────────────────────
public function test_getlastread_requires_authentication(): void
{
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getlastread',
'id' => 2,
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
public function test_storelastread_requires_authentication(): void
{
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => 2,
'page' => 10,
'cfi' => 'epubcfi(/6/10[])',
'timestamp' => time(),
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
// ── getlastread ──────────────────────────────────────────────────────────
public function test_getlastread_with_no_existing_record(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
// Ensure no record exists
$this->deleteBookLastread(99999);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getlastread',
'id' => 99999,
]);
$this->assertEquals(404, $status);
$this->assertEquals('Not found.', $body['error']);
}
public function test_getlastread_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' => 'getlastread',
'id' => 0,
]);
$this->assertEquals(404, $status);
$this->assertEquals('Not found.', $body['error']);
}
public function test_getlastread_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' => 'getlastread',
'id' => -1,
]);
$this->assertEquals(404, $status);
$this->assertEquals('Not found.', $body['error']);
}
// ── storelastread — create new record ────────────────────────────────────
public function test_storelastread_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->deleteBookLastread($bookid);
$page = 42;
$cfi = 'epubcfi(/6/10[])';
$timestamp = time();
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => $bookid,
'page' => $page,
'cfi' => $cfi,
'timestamp' => $timestamp,
]);
$this->assertEquals(200, $status);
$this->assertEquals($page, $body['page']);
$this->assertEquals($cfi, $body['cfi']);
// Verify DB record was created
$record = $this->getBookLastread($bookid);
$this->assertNotNull($record);
$this->assertEquals($bookid, $record['bookid']);
$this->assertEquals($page, (int)$record['lastread']);
$this->assertEquals($cfi, $record['lastcfi']);
$this->assertEquals($timestamp, (int)$record['lastts']);
}
// ── storelastread — update existing record ───────────────────────────────
public function test_storelastread_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->deleteBookLastread($bookid);
// Create initial record
$pdo = \Db::pdo();
$sth = $pdo->prepare("INSERT INTO epube_books (bookid, owner, lastread, lastcfi, lastts) VALUES (?, 'test', 10, 'epubcfi(/1/2[])', 1000)");
$sth->execute([$bookid]);
// Store new progress with newer timestamp
$newPage = 25;
$newCfi = 'epubcfi(/6/14[])';
$newTimestamp = 2000;
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => $bookid,
'page' => $newPage,
'cfi' => $newCfi,
'timestamp' => $newTimestamp,
]);
$this->assertEquals(200, $status);
$this->assertEquals($newPage, $body['page']);
$this->assertEquals($newCfi, $body['cfi']);
// Verify DB was updated
$record = $this->getBookLastread($bookid);
$this->assertNotNull($record);
$this->assertEquals($newPage, (int)$record['lastread']);
$this->assertEquals($newCfi, $record['lastcfi']);
$this->assertEquals($newTimestamp, (int)$record['lastts']);
}
public function test_storelastread_ignores_older_timestamp(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99996;
$this->deleteBookLastread($bookid);
// Create initial record with higher timestamp
$pdo = \Db::pdo();
$sth = $pdo->prepare("INSERT INTO epube_books (bookid, owner, lastread, lastcfi, lastts) VALUES (?, 'test', 50, 'epubcfi(/6/20[])', 5000)");
$sth->execute([$bookid]);
// Try to store with older timestamp — should be ignored
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => $bookid,
'page' => 10,
'cfi' => 'epubcfi(/6/10[])',
'timestamp' => 1000,
]);
$this->assertEquals(200, $status);
$this->assertEquals(10, $body['page']);
// Verify DB was NOT updated (still has old values)
$record = $this->getBookLastread($bookid);
$this->assertNotNull($record);
$this->assertEquals(50, (int)$record['lastread']);
$this->assertEquals('epubcfi(/6/20[])', $record['lastcfi']);
$this->assertEquals(5000, (int)$record['lastts']);
}
public function test_storelastread_rejects_older_timestamp_even_with_newer_page(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99995;
$this->deleteBookLastread($bookid);
// Create initial record
$pdo = \Db::pdo();
$sth = $pdo->prepare("INSERT INTO epube_books (bookid, owner, lastread, lastcfi, lastts) VALUES (?, 'test', 10, 'epubcfi(/6/10[])', 5000)");
$sth->execute([$bookid]);
// Try to store with newer page but older timestamp — should be rejected
// Condition: (timestamp >= lastts) AND (page >= lastread OR page == -1)
// timestamp check fails, so no update
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => $bookid,
'page' => 20, // newer page
'cfi' => 'epubcfi(/6/12[])',
'timestamp' => 1000, // older timestamp
]);
$this->assertEquals(200, $status);
// Verify DB was NOT updated
$record = $this->getBookLastread($bookid);
$this->assertNotNull($record);
$this->assertEquals(10, (int)$record['lastread']);
$this->assertEquals('epubcfi(/6/10[])', $record['lastcfi']);
$this->assertEquals(5000, (int)$record['lastts']);
}
// ── storelastread — reset progress ───────────────────────────────────────
public function test_storelastread_resets_with_page_minus_one(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99994;
$this->deleteBookLastread($bookid);
// Create initial record with high page
$pdo = \Db::pdo();
$sth = $pdo->prepare("INSERT INTO epube_books (bookid, owner, lastread, lastcfi, lastts) VALUES (?, 'test', 99, 'epubcfi(/6/99[])', 1000)");
$sth->execute([$bookid]);
// Reset with page=-1 (PAGE_RESET_PROGRESS) — allowed when timestamp >= lastts
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => $bookid,
'page' => -1,
'cfi' => '',
'timestamp' => 2000, // newer timestamp
]);
$this->assertEquals(200, $status);
$this->assertEquals(-1, $body['page']);
// Verify DB was reset
$record = $this->getBookLastread($bookid);
$this->assertNotNull($record);
$this->assertEquals(-1, (int)$record['lastread']);
$this->assertEquals('', $record['lastcfi']);
$this->assertEquals(2000, (int)$record['lastts']);
}
// ── getlastread — after store ────────────────────────────────────────────
public function test_getlastread_returns_stored_data(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$bookid = 99993;
$this->deleteBookLastread($bookid);
// Store lastread
$page = 77;
$cfi = 'epubcfi(/6/50[])';
$timestamp = 3000;
$this->post($this->apiUrl, [
'op' => 'storelastread',
'id' => $bookid,
'page' => $page,
'cfi' => $cfi,
'timestamp' => $timestamp,
]);
// Retrieve it
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'getlastread',
'id' => $bookid,
]);
$this->assertEquals(200, $status);
$this->assertEquals($page, $body['page']);
$this->assertEquals($cfi, $body['cfi']);
$this->assertEquals(100, $body['total']);
$this->assertEquals($timestamp, $body['timestamp']);
}
// ── Missing/invalid parameters ───────────────────────────────────────────
public function test_storelastread_with_zero_id_returns_page_as_is(): 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' => 'storelastread',
'id' => 0,
'page' => 10,
'cfi' => 'epubcfi()',
'timestamp' => time(),
]);
$this->assertEquals(200, $status);
// With id=0, $bookid=0, the if ($bookid) check fails → no-op, but returns the page as-is
$this->assertEquals(10, $body['page']);
}
public function test_storelastread_with_negative_id_returns_page_as_is(): 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' => 'storelastread',
'id' => -1,
'page' => 10,
'cfi' => 'epubcfi()',
'timestamp' => time(),
]);
$this->assertEquals(200, $status);
// With id=-1, $bookid=0 (cast), the if ($bookid) check fails → no-op, but returns the page as-is
$this->assertEquals(10, $body['page']);
}
}
|