Skip to content

NOW MANAGED ON CODEBERG

Books: On delete, redirect to shelf if in context

Dan Brown8e99fc678352
diff --git a/app/Entities/Controllers/BookController.php b/app/Entities/Controllers/BookController.php
index c94057fa9..fca530f8a 100644
--- a/app/Entities/Controllers/BookController.php
+++ b/app/Entities/Controllers/BookController.php
@@ -224,9 +224,14 @@ class BookController extends Controller
{
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission(Permission::BookDelete, $book);
+ $contextShelf = $this->shelfContext->getContextualShelfForBook($book);
$this->bookRepo->destroy($book);
+ if ($contextShelf) {
+ return redirect($contextShelf->getUrl());
+ }
+
return redirect('/books');
}
diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php
index a7142f037..5f0aabc38 100644
--- a/tests/Entity/BookTest.php
+++ b/tests/Entity/BookTest.php
@@ -154,6 +154,20 @@ class BookTest extends TestCase
$this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
}
+ public function test_delete_with_shelf_context_returns_to_shelf_view_after_delete()
+ {
+ $shelf = $this->entities->shelfHasBooks();
+ /** @var Book $book */
+ $book = $shelf->books()->first();
+
+ $this->asEditor()->get($shelf->getUrl());
+ $this->get($book->getUrl());
+ $this->get($book->getUrl('/delete'));
+ $resp = $this->delete($book->getUrl());
+
+ $resp->assertRedirect($shelf->getUrl());
+ }
+
public function test_cancel_on_create_page_leads_back_to_books_listing()
{
$resp = $this->asEditor()->get('/create-book');
diff --git a/tests/Helpers/EntityProvider.php b/tests/Helpers/EntityProvider.php
index 5163cef14..d4cdce512 100644
--- a/tests/Helpers/EntityProvider.php
+++ b/tests/Helpers/EntityProvider.php
@@ -110,6 +110,14 @@ class EntityProvider
return $shelf;
}
+ /**
+ * Get a shelf that has books assigned.
+ */
+ public function shelfHasBooks(): Bookshelf
+ {
+ return $this->shelf(fn(Builder $query) => $query->whereHas('books'));
+ }
+
/**
* Get all entity types from the system.
* @return array{page: Page, chapter: Chapter, book: Book, bookshelf: Bookshelf}