2022-04-07 04:57:18 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Api;
|
|
|
|
|
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Deletion;
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class RecycleBinApiTest extends TestCase
|
|
|
|
{
|
|
|
|
use TestsApi;
|
|
|
|
|
2022-04-26 00:54:59 +08:00
|
|
|
protected string $baseEndpoint = '/api/recycle-bin';
|
2022-04-07 04:57:18 +08:00
|
|
|
|
|
|
|
protected array $endpointMap = [
|
2022-04-26 00:54:59 +08:00
|
|
|
['get', '/api/recycle-bin'],
|
|
|
|
['put', '/api/recycle-bin/1'],
|
|
|
|
['delete', '/api/recycle-bin/1'],
|
2022-04-07 04:57:18 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
public function test_settings_manage_permission_needed_for_all_endpoints()
|
|
|
|
{
|
|
|
|
$editor = $this->getEditor();
|
|
|
|
$this->giveUserPermissions($editor, ['settings-manage']);
|
|
|
|
$this->actingAs($editor);
|
|
|
|
|
|
|
|
foreach ($this->endpointMap as [$method, $uri]) {
|
|
|
|
$resp = $this->json($method, $uri);
|
|
|
|
$resp->assertStatus(403);
|
|
|
|
$resp->assertJson($this->permissionErrorResponse());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:16:45 +08:00
|
|
|
public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
|
2022-04-07 04:57:18 +08:00
|
|
|
{
|
|
|
|
$editor = $this->getEditor();
|
|
|
|
$this->giveUserPermissions($editor, ['restrictions-manage-all']);
|
|
|
|
$this->actingAs($editor);
|
2022-04-24 16:16:45 +08:00
|
|
|
|
2022-04-07 04:57:18 +08:00
|
|
|
foreach ($this->endpointMap as [$method, $uri]) {
|
|
|
|
$resp = $this->json($method, $uri);
|
|
|
|
$resp->assertStatus(403);
|
|
|
|
$resp->assertJson($this->permissionErrorResponse());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_index_endpoint_returns_expected_page()
|
|
|
|
{
|
2022-04-26 00:54:59 +08:00
|
|
|
$admin = $this->getAdmin();
|
2022-04-21 04:58:16 +08:00
|
|
|
|
2022-04-07 04:57:18 +08:00
|
|
|
$page = Page::query()->first();
|
2022-04-26 00:54:59 +08:00
|
|
|
$book = Book::query()->first();
|
|
|
|
$this->actingAs($admin)->delete($page->getUrl());
|
|
|
|
$this->delete($book->getUrl());
|
2022-04-07 04:57:18 +08:00
|
|
|
|
|
|
|
$deletions = Deletion::query()->orderBy('id')->get();
|
|
|
|
|
|
|
|
$resp = $this->getJson($this->baseEndpoint);
|
|
|
|
|
|
|
|
$expectedData = $deletions
|
|
|
|
->zip([$page, $book])
|
2022-04-26 00:54:59 +08:00
|
|
|
->map(function (Collection $data) use ($admin) {
|
2022-04-07 04:57:18 +08:00
|
|
|
return [
|
|
|
|
'id' => $data[0]->id,
|
2022-04-26 00:54:59 +08:00
|
|
|
'deleted_by' => $admin->id,
|
2022-04-07 04:57:18 +08:00
|
|
|
'created_at' => $data[0]->created_at->toJson(),
|
|
|
|
'updated_at' => $data[0]->updated_at->toJson(),
|
|
|
|
'deletable_type' => $data[1]->getMorphClass(),
|
2022-04-26 00:54:59 +08:00
|
|
|
'deletable_id' => $data[1]->id,
|
|
|
|
'deletable' => [
|
|
|
|
'name' => $data[1]->name,
|
|
|
|
],
|
2022-04-07 04:57:18 +08:00
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
$resp->assertJson([
|
2022-04-24 16:16:45 +08:00
|
|
|
'data' => $expectedData->values()->all(),
|
2022-04-21 04:58:16 +08:00
|
|
|
'total' => 2,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-04-26 00:54:59 +08:00
|
|
|
public function test_index_endpoint_returns_children_count()
|
2022-04-21 04:58:16 +08:00
|
|
|
{
|
2022-04-26 00:54:59 +08:00
|
|
|
$admin = $this->getAdmin();
|
2022-04-24 16:16:45 +08:00
|
|
|
|
2022-04-21 04:58:16 +08:00
|
|
|
$book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
|
2022-04-26 00:54:59 +08:00
|
|
|
$this->actingAs($admin)->delete($book->getUrl());
|
2022-04-21 04:58:16 +08:00
|
|
|
|
|
|
|
$deletion = Deletion::query()->orderBy('id')->first();
|
|
|
|
|
|
|
|
$resp = $this->getJson($this->baseEndpoint);
|
|
|
|
|
|
|
|
$expectedData = [
|
|
|
|
[
|
2022-04-26 00:54:59 +08:00
|
|
|
'id' => $deletion->id,
|
|
|
|
'deletable' => [
|
|
|
|
'pages_count' => $book->pages_count,
|
|
|
|
'chapters_count' => $book->chapters_count,
|
2022-04-21 04:58:16 +08:00
|
|
|
],
|
2022-04-24 16:16:45 +08:00
|
|
|
],
|
2022-04-21 04:58:16 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$resp->assertJson([
|
2022-04-24 16:16:45 +08:00
|
|
|
'data' => $expectedData,
|
2022-04-21 04:58:16 +08:00
|
|
|
'total' => 1,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_index_endpoint_returns_parent()
|
|
|
|
{
|
2022-04-26 00:54:59 +08:00
|
|
|
$admin = $this->getAdmin();
|
2022-04-21 04:58:16 +08:00
|
|
|
$page = Page::query()->whereHas('chapter')->with('chapter')->first();
|
|
|
|
|
2022-04-26 00:54:59 +08:00
|
|
|
$this->actingAs($admin)->delete($page->getUrl());
|
2022-04-21 04:58:16 +08:00
|
|
|
$deletion = Deletion::query()->orderBy('id')->first();
|
|
|
|
|
|
|
|
$resp = $this->getJson($this->baseEndpoint);
|
|
|
|
|
|
|
|
$expectedData = [
|
|
|
|
[
|
2022-04-26 00:54:59 +08:00
|
|
|
'id' => $deletion->id,
|
|
|
|
'deletable' => [
|
|
|
|
'parent' => [
|
|
|
|
'id' => $page->chapter->id,
|
|
|
|
'name' => $page->chapter->name,
|
2022-04-26 01:42:31 +08:00
|
|
|
'type' => 'chapter',
|
|
|
|
],
|
|
|
|
],
|
2022-04-24 16:16:45 +08:00
|
|
|
],
|
2022-04-21 04:58:16 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$resp->assertJson([
|
2022-04-24 16:16:45 +08:00
|
|
|
'data' => $expectedData,
|
|
|
|
'total' => 1,
|
2022-04-07 04:57:18 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_restore_endpoint()
|
|
|
|
{
|
|
|
|
$page = Page::query()->first();
|
2022-04-26 00:54:59 +08:00
|
|
|
$this->asAdmin()->delete($page->getUrl());
|
2022-04-07 04:57:18 +08:00
|
|
|
$page->refresh();
|
|
|
|
|
|
|
|
$deletion = Deletion::query()->orderBy('id')->first();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('pages', [
|
2022-04-26 00:54:59 +08:00
|
|
|
'id' => $page->id,
|
2022-04-24 16:16:45 +08:00
|
|
|
'deleted_at' => $page->deleted_at,
|
2022-04-07 04:57:18 +08:00
|
|
|
]);
|
|
|
|
|
2022-04-26 00:54:59 +08:00
|
|
|
$resp = $this->putJson($this->baseEndpoint . '/' . $deletion->id);
|
|
|
|
$resp->assertJson([
|
2022-04-26 01:42:31 +08:00
|
|
|
'restore_count' => 1,
|
2022-04-26 00:54:59 +08:00
|
|
|
]);
|
2022-04-07 04:57:18 +08:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('pages', [
|
2022-04-26 00:54:59 +08:00
|
|
|
'id' => $page->id,
|
2022-04-24 16:16:45 +08:00
|
|
|
'deleted_at' => null,
|
2022-04-07 04:57:18 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_destroy_endpoint()
|
|
|
|
{
|
|
|
|
$page = Page::query()->first();
|
2022-04-26 00:54:59 +08:00
|
|
|
$this->asAdmin()->delete($page->getUrl());
|
2022-04-07 04:57:18 +08:00
|
|
|
$page->refresh();
|
|
|
|
|
|
|
|
$deletion = Deletion::query()->orderBy('id')->first();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('pages', [
|
2022-04-26 00:54:59 +08:00
|
|
|
'id' => $page->id,
|
2022-04-24 16:16:45 +08:00
|
|
|
'deleted_at' => $page->deleted_at,
|
2022-04-07 04:57:18 +08:00
|
|
|
]);
|
|
|
|
|
2022-04-26 00:54:59 +08:00
|
|
|
$resp = $this->deleteJson($this->baseEndpoint . '/' . $deletion->id);
|
|
|
|
$resp->assertJson([
|
2022-04-26 01:42:31 +08:00
|
|
|
'delete_count' => 1,
|
2022-04-26 00:54:59 +08:00
|
|
|
]);
|
2022-04-07 04:57:18 +08:00
|
|
|
|
2022-04-26 00:54:59 +08:00
|
|
|
$this->assertDatabaseMissing('pages', ['id' => $page->id]);
|
2022-04-07 04:57:18 +08:00
|
|
|
}
|
|
|
|
}
|