]> BookStack Code Mirror - bookstack/commitdiff
API: Added zip export tests, reorganised tests
authorDan Brown <redacted>
Fri, 18 Jul 2025 09:58:10 +0000 (10:58 +0100)
committerDan Brown <redacted>
Fri, 18 Jul 2025 09:58:10 +0000 (10:58 +0100)
Extracted an extra method into helper for reuse.

tests/Api/BooksApiTest.php
tests/Api/ChaptersApiTest.php
tests/Api/ExportsApiTest.php [new file with mode: 0644]
tests/Api/PagesApiTest.php
tests/Exports/ZipExportTest.php
tests/Exports/ZipTestHelper.php

index 084cb59bd5c633a11a512aa2814b2a9a95f8b996..22ccfb482c9c29a450c2dc27c286bfc580b3f9a6 100644 (file)
@@ -287,62 +287,4 @@ class BooksApiTest extends TestCase
         $resp->assertStatus(204);
         $this->assertActivityExists('book_delete');
     }
         $resp->assertStatus(204);
         $this->assertActivityExists('book_delete');
     }
-
-    public function test_export_html_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $book = $this->entities->book();
-
-        $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
-        $resp->assertStatus(200);
-        $resp->assertSee($book->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
-    }
-
-    public function test_export_plain_text_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $book = $this->entities->book();
-
-        $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
-        $resp->assertStatus(200);
-        $resp->assertSee($book->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
-    }
-
-    public function test_export_pdf_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $book = $this->entities->book();
-
-        $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
-        $resp->assertStatus(200);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
-    }
-
-    public function test_export_markdown_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $book = Book::visible()->has('pages')->has('chapters')->first();
-
-        $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
-        $resp->assertStatus(200);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
-        $resp->assertSee('# ' . $book->name);
-        $resp->assertSee('# ' . $book->pages()->first()->name);
-        $resp->assertSee('# ' . $book->chapters()->first()->name);
-    }
-
-    public function test_cant_export_when_not_have_permission()
-    {
-        $types = ['html', 'plaintext', 'pdf', 'markdown'];
-        $this->actingAsApiEditor();
-        $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
-
-        $book = $this->entities->book();
-        foreach ($types as $type) {
-            $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
-            $this->assertPermissionError($resp);
-        }
-    }
 }
 }
index 9698d4dd9c254dea3ba5d3e1a6390bd61033b84d..5d7b0530891a3ea6415b31473e130f60c226d7cc 100644 (file)
@@ -269,61 +269,4 @@ class ChaptersApiTest extends TestCase
         $resp->assertStatus(204);
         $this->assertActivityExists('chapter_delete');
     }
         $resp->assertStatus(204);
         $this->assertActivityExists('chapter_delete');
     }
-
-    public function test_export_html_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $chapter = $this->entities->chapter();
-
-        $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
-        $resp->assertStatus(200);
-        $resp->assertSee($chapter->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
-    }
-
-    public function test_export_plain_text_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $chapter = $this->entities->chapter();
-
-        $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
-        $resp->assertStatus(200);
-        $resp->assertSee($chapter->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
-    }
-
-    public function test_export_pdf_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $chapter = $this->entities->chapter();
-
-        $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
-        $resp->assertStatus(200);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
-    }
-
-    public function test_export_markdown_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $chapter = Chapter::visible()->has('pages')->first();
-
-        $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
-        $resp->assertStatus(200);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
-        $resp->assertSee('# ' . $chapter->name);
-        $resp->assertSee('# ' . $chapter->pages()->first()->name);
-    }
-
-    public function test_cant_export_when_not_have_permission()
-    {
-        $types = ['html', 'plaintext', 'pdf', 'markdown'];
-        $this->actingAsApiEditor();
-        $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
-
-        $chapter = Chapter::visible()->has('pages')->first();
-        foreach ($types as $type) {
-            $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
-            $this->assertPermissionError($resp);
-        }
-    }
 }
 }
diff --git a/tests/Api/ExportsApiTest.php b/tests/Api/ExportsApiTest.php
new file mode 100644 (file)
index 0000000..d427c1a
--- /dev/null
@@ -0,0 +1,210 @@
+<?php
+
+namespace Api;
+
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
+use Tests\Api\TestsApi;
+use Tests\Exports\ZipTestHelper;
+use Tests\TestCase;
+
+class ExportsApiTest extends TestCase
+{
+    use TestsApi;
+
+    public function test_book_html_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $book = $this->entities->book();
+
+        $resp = $this->get("/api/books/{$book->id}/export/html");
+        $resp->assertStatus(200);
+        $resp->assertSee($book->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
+    }
+
+    public function test_book_plain_text_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $book = $this->entities->book();
+
+        $resp = $this->get("/api/books/{$book->id}/export/plaintext");
+        $resp->assertStatus(200);
+        $resp->assertSee($book->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
+    }
+
+    public function test_book_pdf_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $book = $this->entities->book();
+
+        $resp = $this->get("/api/books/{$book->id}/export/pdf");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
+    }
+
+    public function test_book_markdown_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $book = Book::visible()->has('pages')->has('chapters')->first();
+
+        $resp = $this->get("/api/books/{$book->id}/export/markdown");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
+        $resp->assertSee('# ' . $book->name);
+        $resp->assertSee('# ' . $book->pages()->first()->name);
+        $resp->assertSee('# ' . $book->chapters()->first()->name);
+    }
+
+    public function test_book_zip_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $book = Book::visible()->has('pages')->has('chapters')->first();
+
+        $resp = $this->get("/api/books/{$book->id}/export/zip");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.zip"');
+
+        $zip = ZipTestHelper::extractFromZipResponse($resp);
+        $this->assertArrayHasKey('book', $zip->data);
+    }
+
+    public function test_chapter_html_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $chapter = $this->entities->chapter();
+
+        $resp = $this->get("/api/chapters/{$chapter->id}/export/html");
+        $resp->assertStatus(200);
+        $resp->assertSee($chapter->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
+    }
+
+    public function test_chapter_plain_text_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $chapter = $this->entities->chapter();
+
+        $resp = $this->get("/api/chapters/{$chapter->id}/export/plaintext");
+        $resp->assertStatus(200);
+        $resp->assertSee($chapter->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
+    }
+
+    public function test_chapter_pdf_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $chapter = $this->entities->chapter();
+
+        $resp = $this->get("/api/chapters/{$chapter->id}/export/pdf");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
+    }
+
+    public function test_chapter_markdown_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $chapter = Chapter::visible()->has('pages')->first();
+
+        $resp = $this->get("/api/chapters/{$chapter->id}/export/markdown");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
+        $resp->assertSee('# ' . $chapter->name);
+        $resp->assertSee('# ' . $chapter->pages()->first()->name);
+    }
+
+    public function test_chapter_zip_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $chapter = Chapter::visible()->has('pages')->first();
+
+        $resp = $this->get("/api/chapters/{$chapter->id}/export/zip");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.zip"');
+
+        $zip = ZipTestHelper::extractFromZipResponse($resp);
+        $this->assertArrayHasKey('chapter', $zip->data);
+    }
+
+    public function test_page_html_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $page = $this->entities->page();
+
+        $resp = $this->get("/api/pages/{$page->id}/export/html");
+        $resp->assertStatus(200);
+        $resp->assertSee($page->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
+    }
+
+    public function test_page_plain_text_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $page = $this->entities->page();
+
+        $resp = $this->get("/api/pages/{$page->id}/export/plaintext");
+        $resp->assertStatus(200);
+        $resp->assertSee($page->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
+    }
+
+    public function test_page_pdf_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $page = $this->entities->page();
+
+        $resp = $this->get("/api/pages/{$page->id}/export/pdf");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
+    }
+
+    public function test_page_markdown_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $page = $this->entities->page();
+
+        $resp = $this->get("/api/pages/{$page->id}/export/markdown");
+        $resp->assertStatus(200);
+        $resp->assertSee('# ' . $page->name);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
+    }
+
+    public function test_page_zip_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $page = $this->entities->page();
+
+        $resp = $this->get("/api/pages/{$page->id}/export/zip");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.zip"');
+
+        $zip = ZipTestHelper::extractFromZipResponse($resp);
+        $this->assertArrayHasKey('page', $zip->data);
+    }
+
+    public function test_cant_export_when_not_have_permission()
+    {
+        $types = ['html', 'plaintext', 'pdf', 'markdown', 'zip'];
+        $this->actingAsApiEditor();
+        $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
+
+        $book = $this->entities->book();
+        foreach ($types as $type) {
+            $resp = $this->get("/api/books/{$book->id}/export/{$type}");
+            $this->assertPermissionError($resp);
+        }
+
+        $chapter = Chapter::visible()->has('pages')->first();
+        foreach ($types as $type) {
+            $resp = $this->get("/api/chapters/{$chapter->id}/export/{$type}");
+            $this->assertPermissionError($resp);
+        }
+
+        $page = $this->entities->page();
+        foreach ($types as $type) {
+            $resp = $this->get("/api/pages/{$page->id}/export/{$type}");
+            $this->assertPermissionError($resp);
+        }
+    }
+}
index 22659d5bb726bf011403ce14cc728d810bde861a..ced8954eb1149490c9a88ac01be2a2d3c664cf45 100644 (file)
@@ -308,60 +308,4 @@ class PagesApiTest extends TestCase
         $resp->assertStatus(204);
         $this->assertActivityExists('page_delete', $page);
     }
         $resp->assertStatus(204);
         $this->assertActivityExists('page_delete', $page);
     }
-
-    public function test_export_html_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $page = $this->entities->page();
-
-        $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/html");
-        $resp->assertStatus(200);
-        $resp->assertSee($page->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
-    }
-
-    public function test_export_plain_text_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $page = $this->entities->page();
-
-        $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/plaintext");
-        $resp->assertStatus(200);
-        $resp->assertSee($page->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
-    }
-
-    public function test_export_pdf_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $page = $this->entities->page();
-
-        $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/pdf");
-        $resp->assertStatus(200);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
-    }
-
-    public function test_export_markdown_endpoint()
-    {
-        $this->actingAsApiEditor();
-        $page = $this->entities->page();
-
-        $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/markdown");
-        $resp->assertStatus(200);
-        $resp->assertSee('# ' . $page->name);
-        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
-    }
-
-    public function test_cant_export_when_not_have_permission()
-    {
-        $types = ['html', 'plaintext', 'pdf', 'markdown'];
-        $this->actingAsApiEditor();
-        $this->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
-
-        $page = $this->entities->page();
-        foreach ($types as $type) {
-            $resp = $this->get($this->baseEndpoint . "/{$page->id}/export/{$type}");
-            $this->assertPermissionError($resp);
-        }
-    }
 }
 }
index 1434c013f7310116029318cb0d082492d935d7e7..1310dcc24566b72bddd9bdc96d7027b665ae6ab9 100644 (file)
@@ -41,7 +41,7 @@ class ZipExportTest extends TestCase
     {
         $page = $this->entities->page();
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
     {
         $page = $this->entities->page();
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
         $this->assertArrayNotHasKey('book', $zip->data);
 
         $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
         $this->assertArrayNotHasKey('book', $zip->data);
@@ -83,7 +83,7 @@ class ZipExportTest extends TestCase
     {
         $page = $this->entities->page();
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
     {
         $page = $this->entities->page();
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $pageData = $zip->data['page'];
         $this->assertEquals([
 
         $pageData = $zip->data['page'];
         $this->assertEquals([
@@ -105,7 +105,7 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $pageData = $zip->data['page'];
         $this->assertEquals($markdown, $pageData['markdown']);
 
         $pageData = $zip->data['page'];
         $this->assertEquals($markdown, $pageData['markdown']);
@@ -121,7 +121,7 @@ class ZipExportTest extends TestCase
         ]);
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
         ]);
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $pageData = $zip->data['page'];
         $this->assertEquals([
 
         $pageData = $zip->data['page'];
         $this->assertEquals([
@@ -147,7 +147,7 @@ class ZipExportTest extends TestCase
         $image = Image::findOrFail($result['response']->id);
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
         $image = Image::findOrFail($result['response']->id);
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $pageData = $zip->data['page'];
 
         $this->assertCount(1, $pageData['images']);
         $pageData = $zip->data['page'];
 
         $this->assertCount(1, $pageData['images']);
@@ -173,7 +173,7 @@ class ZipExportTest extends TestCase
         $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
 
         $zipResp = $this->get($page->getUrl("/export/zip"));
         $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
 
         $zipResp = $this->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $pageData = $zip->data['page'];
         $this->assertCount(1, $pageData['attachments']);
 
         $pageData = $zip->data['page'];
         $this->assertCount(1, $pageData['attachments']);
@@ -203,7 +203,7 @@ class ZipExportTest extends TestCase
         ]);
 
         $zipResp = $this->get($page->getUrl("/export/zip"));
         ]);
 
         $zipResp = $this->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $pageData = $zip->data['page'];
         $this->assertCount(1, $pageData['attachments']);
 
         $pageData = $zip->data['page'];
         $this->assertCount(1, $pageData['attachments']);
@@ -221,7 +221,7 @@ class ZipExportTest extends TestCase
         $book->tags()->saveMany(Tag::factory()->count(2)->make());
 
         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
         $book->tags()->saveMany(Tag::factory()->count(2)->make());
 
         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $this->assertArrayHasKey('book', $zip->data);
 
         $bookData = $zip->data['book'];
         $this->assertArrayHasKey('book', $zip->data);
 
         $bookData = $zip->data['book'];
@@ -243,7 +243,7 @@ class ZipExportTest extends TestCase
         $coverImage = $book->cover()->first();
 
         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
         $coverImage = $book->cover()->first();
 
         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
 
         $this->assertArrayHasKey('cover', $zip->data['book']);
         $coverRef = $zip->data['book']['cover'];
 
         $this->assertArrayHasKey('cover', $zip->data['book']);
         $coverRef = $zip->data['book']['cover'];
@@ -258,7 +258,7 @@ class ZipExportTest extends TestCase
         $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
 
         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
         $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
 
         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $this->assertArrayHasKey('chapter', $zip->data);
 
         $chapterData = $zip->data['chapter'];
         $this->assertArrayHasKey('chapter', $zip->data);
 
         $chapterData = $zip->data['chapter'];
@@ -284,18 +284,18 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $this->assertCount(0, $zip->data['book']['chapters'][0]['pages'] ?? ['cat']);
 
         $zipResp = $this->actingAs($editor)->get($chapter->getUrl("/export/zip"));
         $this->assertCount(0, $zip->data['book']['chapters'][0]['pages'] ?? ['cat']);
 
         $zipResp = $this->actingAs($editor)->get($chapter->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $this->assertCount(0, $zip->data['chapter']['pages'] ?? ['cat']);
 
         $page->chapter_id = 0;
         $page->save();
 
         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
         $this->assertCount(0, $zip->data['chapter']['pages'] ?? ['cat']);
 
         $page->chapter_id = 0;
         $page->save();
 
         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $this->assertCount(0, $zip->data['book']['pages'] ?? ['cat']);
     }
 
         $this->assertCount(0, $zip->data['book']['pages'] ?? ['cat']);
     }
 
@@ -314,7 +314,7 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $bookData = $zip->data['book'];
         $chapterData = $bookData['chapters'][0];
         $pageData = $chapterData['pages'][0];
         $bookData = $zip->data['book'];
         $chapterData = $bookData['chapters'][0];
         $pageData = $chapterData['pages'][0];
@@ -342,7 +342,7 @@ class ZipExportTest extends TestCase
         $chapter->save();
 
         $zipResp = $this->get($book->getUrl("/export/zip"));
         $chapter->save();
 
         $zipResp = $this->get($book->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $bookData = $zip->data['book'];
         $chapterData = $bookData['chapters'][0];
 
         $bookData = $zip->data['book'];
         $chapterData = $bookData['chapters'][0];
 
@@ -367,7 +367,7 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->get($page->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $pageData = $zip->data['page'];
 
         $ref = '[[bsexport:image:' . $image->id . ']]';
         $pageData = $zip->data['page'];
 
         $ref = '[[bsexport:image:' . $image->id . ']]';
@@ -381,7 +381,7 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $pageData = $zip->data['page'];
 
         $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
         $pageData = $zip->data['page'];
 
         $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
@@ -402,7 +402,7 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $pageData = $zip->data['page'];
 
         $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
         $pageData = $zip->data['page'];
 
         $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
@@ -417,7 +417,7 @@ class ZipExportTest extends TestCase
         $page->save();
 
         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
         $page->save();
 
         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
-        $zip = $this->extractZipResponse($zipResp);
+        $zip = ZipTestHelper::extractFromZipResponse($zipResp);
         $pageData = $zip->data['chapter']['pages'][0];
 
         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
         $pageData = $zip->data['chapter']['pages'][0];
 
         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
@@ -444,30 +444,4 @@ class ZipExportTest extends TestCase
         }
         $this->get($page->getUrl("/export/zip"))->assertStatus(429);
     }
         }
         $this->get($page->getUrl("/export/zip"))->assertStatus(429);
     }
-
-    protected function extractZipResponse(TestResponse $response): ZipResultData
-    {
-        $zipData = $response->streamedContent();
-        $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
-
-        file_put_contents($zipFile, $zipData);
-        $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
-        if (file_exists($extractDir)) {
-            unlink($extractDir);
-        }
-        mkdir($extractDir);
-
-        $zip = new ZipArchive();
-        $zip->open($zipFile, ZipArchive::RDONLY);
-        $zip->extractTo($extractDir);
-
-        $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
-        $data = json_decode($dataJson, true);
-
-        return new ZipResultData(
-            $zipFile,
-            $extractDir,
-            $data,
-        );
-    }
 }
 }
index d830d8eb6bf9ea925f06f8e45fb1da084030ced2..50517a87d02906babd2c4b0bc9c483b8d222d045 100644 (file)
@@ -4,6 +4,7 @@ namespace Tests\Exports;
 
 use BookStack\Exports\Import;
 use Illuminate\Http\UploadedFile;
 
 use BookStack\Exports\Import;
 use Illuminate\Http\UploadedFile;
+use Illuminate\Testing\TestResponse;
 use ZipArchive;
 
 class ZipTestHelper
 use ZipArchive;
 
 class ZipTestHelper
@@ -56,4 +57,30 @@ class ZipTestHelper
 
         return new UploadedFile($zipFile, 'upload.zip', 'application/zip', null, true);
     }
 
         return new UploadedFile($zipFile, 'upload.zip', 'application/zip', null, true);
     }
+
+    public static function extractFromZipResponse(TestResponse $response): ZipResultData
+    {
+        $zipData = $response->streamedContent();
+        $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
+
+        file_put_contents($zipFile, $zipData);
+        $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
+        if (file_exists($extractDir)) {
+            unlink($extractDir);
+        }
+        mkdir($extractDir);
+
+        $zip = new ZipArchive();
+        $zip->open($zipFile, ZipArchive::RDONLY);
+        $zip->extractTo($extractDir);
+
+        $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
+        $data = json_decode($dataJson, true);
+
+        return new ZipResultData(
+            $zipFile,
+            $extractDir,
+            $data,
+        );
+    }
 }
 }