use BookStack\Entities\Tools\PageContent;
use BookStack\Uploads\Attachment;
use BookStack\Uploads\Image;
+use FilesystemIterator;
use Illuminate\Support\Carbon;
use Illuminate\Testing\TestResponse;
use Tests\TestCase;
$this->assertEquals($instanceId, $zipInstanceId);
}
+ public function test_export_leaves_no_temp_files()
+ {
+ $tempDir = sys_get_temp_dir();
+ $startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
+
+ $page = $this->entities->pageWithinChapter();
+ $this->asEditor();
+ $pageResp = $this->get($page->getUrl("/export/zip"));
+ $pageResp->streamedContent();
+ $pageResp->assertOk();
+ $this->get($page->chapter->getUrl("/export/zip"))->assertOk();
+ $this->get($page->book->getUrl("/export/zip"))->assertOk();
+
+ $afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
+
+ $this->assertEquals($startTempFileCount, $afterTempFileCount);
+ }
+
public function test_page_export()
{
$page = $this->entities->page();
public function test_book_export()
{
- $book = $this->entities->book();
+ $book = $this->entities->bookHasChaptersAndPages();
$book->tags()->saveMany(Tag::factory()->count(2)->make());
$zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
$this->assertCount($chapter->pages()->count(), $chapterData['pages']);
}
+ public function test_draft_pages_are_not_included()
+ {
+ $editor = $this->users->editor();
+ $entities = $this->entities->createChainBelongingToUser($editor);
+ $book = $entities['book'];
+ $page = $entities['page'];
+ $chapter = $entities['chapter'];
+ $book->tags()->saveMany(Tag::factory()->count(2)->make());
+
+ $page->created_by = $editor->id;
+ $page->draft = true;
+ $page->save();
+
+ $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
+ $zip = $this->extractZipResponse($zipResp);
+ $this->assertCount(0, $zip->data['book']['chapters'][0]['pages'] ?? ['cat']);
+
+ $zipResp = $this->actingAs($editor)->get($chapter->getUrl("/export/zip"));
+ $zip = $this->extractZipResponse($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);
+ $this->assertCount(0, $zip->data['book']['pages'] ?? ['cat']);
+ }
+
public function test_cross_reference_links_are_converted()
{
$this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
}
+ public function test_exports_rate_limited_low_for_guest_viewers()
+ {
+ $this->setSettings(['app-public' => 'true']);
+
+ $page = $this->entities->page();
+ for ($i = 0; $i < 4; $i++) {
+ $this->get($page->getUrl("/export/zip"))->assertOk();
+ }
+ $this->get($page->getUrl("/export/zip"))->assertStatus(429);
+ }
+
+ public function test_exports_rate_limited_higher_for_logged_in_viewers()
+ {
+ $this->asAdmin();
+
+ $page = $this->entities->page();
+ for ($i = 0; $i < 10; $i++) {
+ $this->get($page->getUrl("/export/zip"))->assertOk();
+ }
+ $this->get($page->getUrl("/export/zip"))->assertStatus(429);
+ }
+
protected function extractZipResponse(TestResponse $response): ZipResultData
{
$zipData = $response->streamedContent();