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']); } }