-<?php namespace Tests;
+<?php
+
+namespace Tests;
use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Deletion;
+use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
-use DB;
use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\DB;
class RecycleBinTest extends TestCase
{
"DELETE:/settings/recycle-bin/{$deletion->id}",
];
- foreach($routes as $route) {
+ foreach ($routes as $route) {
[$method, $url] = explode(':', $route);
$resp = $this->call($method, $url);
$this->assertPermissionError($resp);
$this->giveUserPermissions($editor, ['restrictions-manage-all']);
- foreach($routes as $route) {
+ foreach ($routes as $route) {
[$method, $url] = explode(':', $route);
$resp = $this->call($method, $url);
$this->assertPermissionError($resp);
$this->giveUserPermissions($editor, ['settings-manage']);
- foreach($routes as $route) {
+ foreach ($routes as $route) {
DB::beginTransaction();
[$method, $url] = explode(':', $route);
$resp = $this->call($method, $url);
$this->assertNotPermissionError($resp);
DB::rollBack();
}
-
}
public function test_recycle_bin_view()
public function test_recycle_bin_empty()
{
$page = Page::query()->first();
- $book = Book::query()->where('id' , '!=', $page->book_id)->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
+ $book = Book::query()->where('id', '!=', $page->book_id)->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
$editor = $this->getEditor();
$this->actingAs($editor)->delete($page->getUrl());
$this->actingAs($editor)->delete($book->getUrl());
$itemCount = 2 + $book->pages->count() + $book->chapters->count();
$redirectReq = $this->get('/settings/recycle-bin');
- $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
+ $redirectReq->assertNotificationContains('Deleted ' . $itemCount . ' total items from the recycle bin');
}
public function test_entity_restore()
$itemCount = 1 + $book->pages->count() + $book->chapters->count();
$redirectReq = $this->get('/settings/recycle-bin');
- $redirectReq->assertNotificationContains('Restored '.$itemCount.' total items from the recycle bin');
+ $redirectReq->assertNotificationContains('Restored ' . $itemCount . ' total items from the recycle bin');
}
public function test_permanent_delete()
$itemCount = 1 + $book->pages->count() + $book->chapters->count();
$redirectReq = $this->get('/settings/recycle-bin');
- $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
+ $redirectReq->assertNotificationContains('Deleted ' . $itemCount . ' total items from the recycle bin');
+ }
+
+ public function test_permanent_delete_for_each_type()
+ {
+ /** @var Entity $entity */
+ foreach ([new Bookshelf(), new Book(), new Chapter(), new Page()] as $entity) {
+ $entity = $entity->newQuery()->first();
+ $this->asEditor()->delete($entity->getUrl());
+ $deletion = Deletion::query()->orderBy('id', 'desc')->firstOrFail();
+
+ $deleteReq = $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
+ $deleteReq->assertRedirect('/settings/recycle-bin');
+ $this->assertDatabaseMissing('deletions', ['id' => $deletion->id]);
+ $this->assertDatabaseMissing($entity->getTable(), ['id' => $entity->id]);
+ }
}
public function test_permanent_entity_delete_updates_existing_activity_with_entity_name()
$deletion = $page->deletions()->firstOrFail();
$this->assertDatabaseHas('activities', [
- 'type' => 'page_delete',
- 'entity_id' => $page->id,
+ 'type' => 'page_delete',
+ 'entity_id' => $page->id,
'entity_type' => $page->getMorphClass(),
]);
$this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
$this->assertDatabaseMissing('activities', [
- 'type' => 'page_delete',
- 'entity_id' => $page->id,
+ 'type' => 'page_delete',
+ 'entity_id' => $page->id,
'entity_type' => $page->getMorphClass(),
]);
$this->assertDatabaseHas('activities', [
- 'type' => 'page_delete',
- 'entity_id' => null,
+ 'type' => 'page_delete',
+ 'entity_id' => null,
'entity_type' => null,
- 'detail' => $page->name,
+ 'detail' => $page->name,
]);
}
$chapterRestoreView->assertSeeText($chapter->name);
$chapterRestore = $this->post("/settings/recycle-bin/{$chapterDeletion->id}/restore");
- $chapterRestore->assertRedirect("/settings/recycle-bin");
- $this->assertDatabaseMissing("deletions", ["id" => $chapterDeletion->id]);
+ $chapterRestore->assertRedirect('/settings/recycle-bin');
+ $this->assertDatabaseMissing('deletions', ['id' => $chapterDeletion->id]);
$chapter->refresh();
$this->assertNotNull($chapter->deleted_at);
$chapter->refresh();
$this->assertNull($chapter->deleted_at);
}
-}
\ No newline at end of file
+
+ public function test_restore_page_shows_link_to_parent_restore_if_parent_also_deleted()
+ {
+ /** @var Book $book */
+ $book = Book::query()->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
+ $chapter = $book->chapters->first();
+ /** @var Page $page */
+ $page = $chapter->pages->first();
+ $this->asEditor()->delete($page->getUrl());
+ $this->asEditor()->delete($book->getUrl());
+
+ $bookDeletion = $book->deletions()->first();
+ $pageDeletion = $page->deletions()->first();
+
+ $pageRestoreView = $this->asAdmin()->get("/settings/recycle-bin/{$pageDeletion->id}/restore");
+ $pageRestoreView->assertSee('The parent of this item has also been deleted.');
+ $pageRestoreView->assertElementContains('a[href$="/settings/recycle-bin/' . $bookDeletion->id . '/restore"]', 'Restore Parent');
+ }
+}