From 08b39500b3b27181a7a1a0e4ee51dabce45673c2 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Thu, 16 Feb 2023 17:57:34 +0000 Subject: [PATCH] Fixed gallery images not visible until draft publish For #4028 --- app/Auth/Permissions/PermissionApplicator.php | 5 +++++ tests/Helpers/EntityProvider.php | 13 +++++++++++++ tests/Uploads/ImageTest.php | 11 +++++++++++ 3 files changed, 29 insertions(+) diff --git a/app/Auth/Permissions/PermissionApplicator.php b/app/Auth/Permissions/PermissionApplicator.php index 20cc87e48..84ac27f87 100644 --- a/app/Auth/Permissions/PermissionApplicator.php +++ b/app/Auth/Permissions/PermissionApplicator.php @@ -158,6 +158,11 @@ class PermissionApplicator $query->select('id')->from('pages') ->whereColumn('pages.id', '=', $fullPageIdColumn) ->where('pages.draft', '=', false); + })->orWhereExists(function (QueryBuilder $query) use ($fullPageIdColumn) { + $query->select('id')->from('pages') + ->whereColumn('pages.id', '=', $fullPageIdColumn) + ->where('pages.draft', '=', true) + ->where('pages.created_by', '=', $this->currentUser()->id); }); }); } diff --git a/tests/Helpers/EntityProvider.php b/tests/Helpers/EntityProvider.php index d79015f75..8b045db54 100644 --- a/tests/Helpers/EntityProvider.php +++ b/tests/Helpers/EntityProvider.php @@ -184,6 +184,19 @@ class EntityProvider return $pageRepo->publishDraft($draftPage, $input); } + /** + * Create and return a new test draft page. + */ + public function newDraftPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page + { + $book = $this->book(); + $pageRepo = app(PageRepo::class); + $draftPage = $pageRepo->getNewDraftPage($book); + $pageRepo->updatePageDraft($draftPage, $input); + $this->addToCache($draftPage); + return $draftPage; + } + /** * @param Entity|Entity[] $entities */ diff --git a/tests/Uploads/ImageTest.php b/tests/Uploads/ImageTest.php index fb98565fc..53040ea08 100644 --- a/tests/Uploads/ImageTest.php +++ b/tests/Uploads/ImageTest.php @@ -120,6 +120,17 @@ class ImageTest extends TestCase $this->withHtml($searchFailRequest)->assertElementNotExists('div'); } + public function test_image_gallery_lists_for_draft_page() + { + $this->actingAs($this->users->editor()); + $draft = $this->entities->newDraftPage(); + $this->files->uploadGalleryImageToPage($this, $draft); + $image = Image::query()->where('uploaded_to', '=', $draft->id)->firstOrFail(); + + $resp = $this->get("/images/gallery?page=1&uploaded_to={$draft->id}"); + $resp->assertSee($image->getThumb(150, 150)); + } + public function test_image_usage() { $page = $this->entities->page();