about summary refs log tree commit diff stats
path: root/tests_integration/LastreadHandlerTest.php
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-28 07:37:29 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-28 07:37:29 +0300
commitb8c711336ec996855aa9605c990723262edfcd6f (patch)
treef63777529c316862a089fbca1a6f2b0be1254e1b /tests_integration/LastreadHandlerTest.php
parent8b453f2c6741e5a0ac18f73c0470f00ed6e91ed6 (diff)
downloadthe-epube-b8c711336ec996855aa9605c990723262edfcd6f.tar.gz
the-epube-b8c711336ec996855aa9605c990723262edfcd6f.tar.xz
add lastread tests
Diffstat (limited to 'tests_integration/LastreadHandlerTest.php')
-rw-r--r--tests_integration/LastreadHandlerTest.php403
1 files changed, 403 insertions, 0 deletions
diff --git a/tests_integration/LastreadHandlerTest.php b/tests_integration/LastreadHandlerTest.php
new file mode 100644
index 0000000..a4e4da8
--- /dev/null
+++ b/tests_integration/LastreadHandlerTest.php
@@ -0,0 +1,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']);
+	}
+}