post($this->apiUrl, [ 'op' => 'download', 'id' => 6, ]); $this->assertEquals(401, $status); $this->assertArrayHasKey('error', $body); } // ── Valid downloads ────────────────────────────────────────────────────── public function test_download_returns_200_for_existing_file(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); // data.id 6 = Huckleberry Finn EPUB [$status, $body, $headers] = $this->post($this->apiUrl, [ 'op' => 'download', 'id' => 6, ], false); $this->assertEquals(200, $status); $this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? ''); $this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? ''); $this->assertStringContainsString('epub', $headers['Content-Disposition'] ?? ''); $this->assertNotEmpty($body); } public function test_download_original_epub_format(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); // data.id 5 = Huckleberry Finn ORIGINAL_EPUB [$status, $body, $headers] = $this->post($this->apiUrl, [ 'op' => 'download', 'id' => 5, ], false); $this->assertEquals(200, $status); $this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? ''); $this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? ''); $this->assertNotEmpty($body); } public function test_download_different_book(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); // data.id 9 = Sherlock Holmes EPUB [$status, $body, $headers] = $this->post($this->apiUrl, [ 'op' => 'download', 'id' => 9, ], false); $this->assertEquals(200, $status); $this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? ''); $this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? ''); $this->assertNotEmpty($body); } // ── Missing/invalid ID ─────────────────────────────────────────────────── public function test_download_without_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' => 'download', ]); $this->assertEquals(404, $status); $this->assertEquals('Not found.', $body['error']); } public function test_download_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' => 'download', 'id' => 0, ]); $this->assertEquals(404, $status); $this->assertEquals('Not found.', $body['error']); } public function test_download_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' => 'download', 'id' => -1, ]); $this->assertEquals(404, $status); $this->assertEquals('Not found.', $body['error']); } public function test_download_with_nonexistent_data_id(): void { // Login [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'perform-login', 'user' => 'test', 'password' => 'test', ]); $this->assertEquals(200, $status); // data.id 99999 doesn't exist in the DB [$status, $body] = $this->post($this->apiUrl, [ 'op' => 'download', 'id' => 99999, ]); $this->assertEquals(404, $status); $this->assertEquals('Not found.', $body['error']); } }