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;
$version = trim(file_get_contents(base_path('version')));
$this->assertEquals($version, $zip->data['instance']['version']);
- $instanceId = decrypt($zip->data['instance']['id_ciphertext']);
- $this->assertEquals('bookstack', $instanceId);
+ $zipInstanceId = $zip->data['instance']['id'];
+ $instanceId = setting('instance-id');
+ $this->assertNotEmpty($instanceId);
+ $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()
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('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
}
+ public function test_image_links_are_handled_when_using_external_storage_url()
+ {
+ $page = $this->entities->page();
+
+ $this->asEditor();
+ $this->files->uploadGalleryImageToPage($this, $page);
+ /** @var Image $image */
+ $image = Image::query()->where('type', '=', 'gallery')
+ ->where('uploaded_to', '=', $page->id)->first();
+
+ config()->set('filesystems.url', 'https://i.example.com/content');
+
+ $storageUrl = 'https://i.example.com/content/' . ltrim($image->path, '/');
+ $page->html = '<p><a href="' . $image->url . '">Original URL</a><a href="' . $storageUrl . '">Storage URL</a></p>';
+ $page->save();
+
+ $zipResp = $this->get($page->getUrl("/export/zip"));
+ $zip = $this->extractZipResponse($zipResp);
+ $pageData = $zip->data['page'];
+
+ $ref = '[[bsexport:image:' . $image->id . ']]';
+ $this->assertStringContainsString("<a href=\"{$ref}\">Original URL</a><a href=\"{$ref}\">Storage URL</a>", $pageData['html']);
+ }
+
public function test_cross_reference_links_external_to_export_are_not_converted()
{
$page = $this->entities->page();
$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();