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); curl_close($ch); $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 $data * @return array */ 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|null $decoded */ $decoded = json_decode($body, true); return [$status, $decoded ?? []]; } // ── Unauthenticated access ────────────────────────────────────────────── public function test_list_requires_authentication(): void { $ch = curl_init($this->apiUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query(['op' => 'list']), CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 5, ]); $body = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $this->assertEquals(401, $status); } // ── Basic list (authenticated) ────────────────────────────────────────── public function test_list_returns_books(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', ]); $this->assertEquals(200, $status); $this->assertArrayHasKey('books', $body); $this->assertArrayHasKey('total', $body); $this->assertIsArray($body['books']); $this->assertGreaterThan(0, $body['total']); } public function test_list_book_fields(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'limit' => 1, ]); $this->assertEquals(200, $status); $this->assertCount(1, $body['books']); $book = $body['books'][0]; $this->assertArrayHasKey('id', $book); $this->assertArrayHasKey('title', $book); $this->assertArrayHasKey('author_sort', $book); $this->assertArrayHasKey('series_name', $book); $this->assertArrayHasKey('series_index', $book); $this->assertArrayHasKey('cover_link', $book); $this->assertArrayHasKey('is_fav', $book); $this->assertArrayHasKey('download_formats', $book); } // ── Pagination ────────────────────────────────────────────────────────── public function test_list_pagination(): void { $this->loginAs('test', 'test'); [$status1, $body1] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'limit' => 2, 'offset' => 0, ]); [$status2, $body2] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'limit' => 2, 'offset' => 2, ]); $this->assertEquals(200, $status1); $this->assertEquals(200, $status2); $this->assertCount(2, $body1['books']); $this->assertCount(2, $body2['books']); $this->assertNotEquals($body1['books'][0]['id'], $body2['books'][0]['id']); } // ── Search / query ────────────────────────────────────────────────────── public function test_list_search_by_title(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'query' => 'Huckleberry', ]); $this->assertEquals(200, $status); $this->assertGreaterThan(0, $body['total']); foreach ($body['books'] as $book) { $this->assertStringContainsString('Huckleberry', $book['title']); } } public function test_list_search_by_author(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'query' => 'Kafka', ]); $this->assertEquals(200, $status); $this->assertGreaterThan(0, $body['total']); foreach ($body['books'] as $book) { $this->assertStringContainsString('Kafka', $book['author_sort']); } } public function test_list_search_no_match(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'query' => 'xyznonexistent', ]); $this->assertEquals(200, $status); $this->assertEquals(0, $body['total']); $this->assertCount(0, $body['books']); } // ── Favorites mode ────────────────────────────────────────────────────── public function test_list_favorites_empty(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'mode' => 'favorites', ]); $this->assertEquals(200, $status); $this->assertEquals(0, $body['total']); $this->assertCount(0, $body['books']); } // ── Selecting books by ids ────────────────────────────────────────────── /** * Fetch book IDs from a full list so we can reference real IDs in subsequent tests. */ private function getAllBookIds(): array { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'limit' => 100, ]); $this->assertEquals(200, $status); return array_map(fn($b) => (int) $b['id'], $body['books']); } public function test_list_by_ids_single(): void { $ids = $this->getAllBookIds(); $this->assertGreaterThan(0, count($ids), 'Test data should contain at least one book'); $singleId = $ids[0]; [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => (string) $singleId, ]); $this->assertEquals(200, $status); $this->assertEquals(1, $body['total']); $this->assertCount(1, $body['books']); $this->assertEquals($singleId, (int) $body['books'][0]['id']); } public function test_list_by_ids_multiple(): void { $ids = $this->getAllBookIds(); $this->assertGreaterThanOrEqual(3, count($ids), 'Test data should contain at least 3 books'); $selectedIds = [$ids[0], $ids[1], $ids[2]]; [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => implode(',', $selectedIds), ]); $this->assertEquals(200, $status); $expectedTotal = count($selectedIds); $this->assertEquals($expectedTotal, $body['total']); $this->assertCount($expectedTotal, $body['books']); $foundIds = array_map(fn($b) => (int) $b['id'], $body['books']); sort($foundIds); $sortedSelected = $selectedIds; sort($sortedSelected); $this->assertEquals($sortedSelected, $foundIds); } public function test_list_by_ids_nonexistent(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => '999999', ]); $this->assertEquals(200, $status); $this->assertEquals(0, $body['total']); $this->assertCount(0, $body['books']); } public function test_list_by_ids_mixed_existing_and_nonexistent(): void { $ids = $this->getAllBookIds(); $this->assertGreaterThan(0, count($ids)); $selectedIds = [$ids[0], 999999, $ids[1]]; [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => implode(',', $selectedIds), ]); $this->assertEquals(200, $status); $this->assertEquals(2, $body['total']); $this->assertCount(2, $body['books']); $foundIds = array_map(fn($b) => (int) $b['id'], $body['books']); $this->assertContains((int) $ids[0], $foundIds); $this->assertContains((int) $ids[1], $foundIds); } public function test_list_by_ids_overrides_query(): void { $ids = $this->getAllBookIds(); $this->assertGreaterThan(0, count($ids)); // Use a query that would match many books, but restrict to a single id [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => (string) $ids[0], 'query' => 'the', ]); $this->assertEquals(200, $status); $this->assertEquals(1, $body['total']); $this->assertCount(1, $body['books']); $foundIds = array_map(fn($b) => (int) $b['id'], $body['books']); $this->assertContains((int) $ids[0], $foundIds); } public function test_list_by_ids_empty_string_returns_all(): void { $this->loginAs('test', 'test'); [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => '', ]); $this->assertEquals(200, $status); $this->assertGreaterThan(0, $body['total']); $this->assertIsArray($body['books']); } public function test_list_by_ids_with_invalid_values(): void { $ids = $this->getAllBookIds(); $this->assertGreaterThan(0, count($ids)); // Mix of valid and invalid (non-numeric) values; intval should filter them out [$status, $body] = $this->authenticatedPost($this->apiUrl, [ 'op' => 'list', 'ids' => (string) $ids[0] . ',abc,0,', ]); $this->assertEquals(200, $status); $this->assertEquals(1, $body['total']); $this->assertCount(1, $body['books']); $foundIds = array_map(fn($b) => (int) $b['id'], $body['books']); $this->assertContains((int) $ids[0], $foundIds); } }