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