about summary refs log tree commit diff stats
path: root/tests_integration/TogglefavHandlerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests_integration/TogglefavHandlerTest.php')
-rw-r--r--tests_integration/TogglefavHandlerTest.php237
1 files changed, 237 insertions, 0 deletions
diff --git a/tests_integration/TogglefavHandlerTest.php b/tests_integration/TogglefavHandlerTest.php
new file mode 100644
index 0000000..c55d3e1
--- /dev/null
+++ b/tests_integration/TogglefavHandlerTest.php
@@ -0,0 +1,237 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+final class TogglefavHandlerTest 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);
+		$errno   = curl_errno($ch);
+		$error   = curl_error($ch);
+		curl_close($ch);
+
+		if ($errno) {
+			$this->fail("cURL error ($errno): $error");
+		}
+
+		$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 ?? []];
+	}
+
+	/**
+	 * Query the favorites table via PDO to check if a favorite exists.
+	 */
+	private function favoriteExists(int $bookId): bool
+	{
+		$pdo = Db::pdo();
+		$sth = $pdo->prepare("SELECT 1 FROM epube_favorites WHERE bookid = ? AND owner = 'test'");
+		$sth->execute([$bookId]);
+		return (bool) $sth->fetch();
+	}
+
+	/**
+	 * Remove any existing favorite for a book to ensure clean state.
+	 */
+	private function removeFavorite(int $bookId): void
+	{
+		$pdo = Db::pdo();
+		$sth = $pdo->prepare("DELETE FROM epube_favorites WHERE bookid = ? AND owner = 'test'");
+		$sth->execute([$bookId]);
+	}
+
+	// ── Unauthenticated access ──────────────────────────────────────────────
+
+	public function test_togglefav_requires_authentication(): void
+	{
+		$ch = curl_init($this->apiUrl);
+		curl_setopt_array($ch, [
+			CURLOPT_RETURNTRANSFER => true,
+			CURLOPT_POST           => true,
+			CURLOPT_POSTFIELDS     => http_build_query([
+				'op'   => 'togglefav',
+				'id'   => 2,
+			]),
+			CURLOPT_TIMEOUT        => 10,
+			CURLOPT_CONNECTTIMEOUT => 5,
+		]);
+		$body    = curl_exec($ch);
+		$status  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+		curl_close($ch);
+
+		$this->assertEquals(401, $status);
+	}
+
+	// ── Toggle on ────────────────────────────────────────────────────────────
+
+	public function test_togglefav_adds_favorite(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 2; // Adventures of Huckleberry Finn
+
+		// Ensure clean state
+		$this->removeFavorite($bookId);
+
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals($bookId, $body['id'] ?? null);
+		$this->assertEquals(1, $body['status'] ?? null);
+		$this->assertTrue($this->favoriteExists($bookId));
+	}
+
+	// ── Toggle off ───────────────────────────────────────────────────────────
+
+	public function test_togglefav_removes_favorite(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 3; // The Adventures of Sherlock Holmes
+
+		// Ensure favorite exists
+		$this->removeFavorite($bookId);
+		$this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		// Toggle off
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals($bookId, $body['id'] ?? null);
+		$this->assertEquals(0, $body['status'] ?? null);
+		$this->assertFalse($this->favoriteExists($bookId));
+	}
+
+	// ── Toggle again (re-add) ────────────────────────────────────────────────
+
+	public function test_togglefav_re_adds_after_removal(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 4; // Metamorphosis
+
+		// Ensure favorite exists, then remove it
+		$this->removeFavorite($bookId);
+		$this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+		$this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		// Toggle back on
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		$this->assertEquals(200, $status);
+		$this->assertEquals($bookId, $body['id'] ?? null);
+		$this->assertEquals(1, $body['status'] ?? null);
+		$this->assertTrue($this->favoriteExists($bookId));
+	}
+
+	// ── Invalid book ID ──────────────────────────────────────────────────────
+
+	public function test_togglefav_with_zero_id(): void
+	{
+		$this->loginAs('test', 'test');
+		$bookId = 0;
+
+		// Ensure clean state
+		$this->removeFavorite($bookId);
+
+		[$status, $body] = $this->authenticatedPost($this->apiUrl, [
+			'op' => 'togglefav',
+			'id' => $bookId,
+		]);
+
+		// Handler casts to int, so id=0 is valid — creates a favorite for bookid=0
+		$this->assertEquals(200, $status);
+		$this->assertEquals(0, $body['id'] ?? null);
+		$this->assertEquals(1, $body['status'] ?? null);
+		$this->assertTrue($this->favoriteExists($bookId));
+
+		// Clean up
+		$this->removeFavorite($bookId);
+	}
+}