Skip to content

Commit 8e99fc6

Browse files
committed
Books: On delete, redirect to shelf if in context
For #6029 Added tests to cover
1 parent 5e12b67 commit 8e99fc6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

app/Entities/Controllers/BookController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,14 @@ public function destroy(string $bookSlug)
224224
{
225225
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
226226
$this->checkOwnablePermission(Permission::BookDelete, $book);
227+
$contextShelf = $this->shelfContext->getContextualShelfForBook($book);
227228

228229
$this->bookRepo->destroy($book);
229230

231+
if ($contextShelf) {
232+
return redirect($contextShelf->getUrl());
233+
}
234+
230235
return redirect('/books');
231236
}
232237

tests/Entity/BookTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ public function test_delete()
154154
$this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
155155
}
156156

157+
public function test_delete_with_shelf_context_returns_to_shelf_view_after_delete()
158+
{
159+
$shelf = $this->entities->shelfHasBooks();
160+
/** @var Book $book */
161+
$book = $shelf->books()->first();
162+
163+
$this->asEditor()->get($shelf->getUrl());
164+
$this->get($book->getUrl());
165+
$this->get($book->getUrl('/delete'));
166+
$resp = $this->delete($book->getUrl());
167+
168+
$resp->assertRedirect($shelf->getUrl());
169+
}
170+
157171
public function test_cancel_on_create_page_leads_back_to_books_listing()
158172
{
159173
$resp = $this->asEditor()->get('/create-book');

tests/Helpers/EntityProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ public function shelf(callable|null $queryFilter = null): Bookshelf
110110
return $shelf;
111111
}
112112

113+
/**
114+
* Get a shelf that has books assigned.
115+
*/
116+
public function shelfHasBooks(): Bookshelf
117+
{
118+
return $this->shelf(fn(Builder $query) => $query->whereHas('books'));
119+
}
120+
113121
/**
114122
* Get all entity types from the system.
115123
* @return array{page: Page, chapter: Chapter, book: Book, bookshelf: Bookshelf}

0 commit comments

Comments
 (0)