diff options
Diffstat (limited to 'tests_integration/ListTest.php')
| -rw-r--r-- | tests_integration/ListTest.php | 462 |
1 files changed, 231 insertions, 231 deletions
diff --git a/tests_integration/ListTest.php b/tests_integration/ListTest.php index 95985fc..4c4c607 100644 --- a/tests_integration/ListTest.php +++ b/tests_integration/ListTest.php @@ -3,235 +3,235 @@ use PHPUnit\Framework\TestCase; final class ListTest 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); - 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<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 ?? []]; - } - - // ── 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('read_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']); - } + 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); + 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<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 ?? []]; + } + + // ── 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('read_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']); + } } |
