mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-10-29 16:09:29 +03:00
API: Added zip export tests, reorganised tests
Extracted an extra method into helper for reuse.
This commit is contained in:
@@ -287,62 +287,4 @@ class BooksApiTest extends TestCase
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,61 +269,4 @@ class ChaptersApiTest extends TestCase
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
210
tests/Api/ExportsApiTest.php
Normal file
210
tests/Api/ExportsApiTest.php
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -308,60 +308,4 @@ class PagesApiTest extends TestCase
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ class ZipExportTest extends TestCase
|
||||
{
|
||||
$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);
|
||||
@@ -83,7 +83,7 @@ class ZipExportTest extends TestCase
|
||||
{
|
||||
$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([
|
||||
@@ -105,7 +105,7 @@ class ZipExportTest extends TestCase
|
||||
$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']);
|
||||
@@ -121,7 +121,7 @@ class ZipExportTest extends TestCase
|
||||
]);
|
||||
|
||||
$zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
|
||||
$pageData = $zip->data['page'];
|
||||
$this->assertCount(1, $pageData['attachments']);
|
||||
@@ -203,7 +203,7 @@ class ZipExportTest extends TestCase
|
||||
]);
|
||||
|
||||
$zipResp = $this->get($page->getUrl("/export/zip"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$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"));
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$zip = $this->extractZipResponse($zipResp);
|
||||
$zip = ZipTestHelper::extractFromZipResponse($zipResp);
|
||||
$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"));
|
||||
$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']);
|
||||
@@ -444,30 +444,4 @@ class ZipExportTest extends TestCase
|
||||
}
|
||||
$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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Exports;
|
||||
|
||||
use BookStack\Exports\Import;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Testing\TestResponse;
|
||||
use ZipArchive;
|
||||
|
||||
class ZipTestHelper
|
||||
@@ -56,4 +57,30 @@ class ZipTestHelper
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user