1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-10-22 07:52:19 +03:00
Files
bookstack/tests/Api/RecycleBinApiTest.php
Dan Brown 4c7d6420ee DB: Aligned entity structure to a common table
As per PR #5800

* DB: Planned out new entity table format via migrations

* DB: Created entity migration logic

Made some other tweaks/fixes while testing.

* DB: Added change of entity relation columns to suit new entities table

* DB: Got most view queries working for new structure

* Entities: Started logic change to new structure

Updated base entity class, and worked through BaseRepo.
Need to go through other repos next.

Removed a couple of redundant interfaces as part of this since we can
move the logic onto the shared ContainerData model as needed.

* Entities: Been through repos to update for new format

* Entities: Updated repos to act on refreshed clones

Changes to core entity models are now done on clones to ensure clean
state before save, and those clones are returned back if changes are
needed after that action.

* Entities: Updated model classes & relations for changes

* Entities: Changed from *Data to a common "contents" system

Added smart loading from builder instances which should hydrate with
"contents()" loaded via join, while keeping the core model original.

* Entities: Moved entity description/covers to own non-model classes

Added back some interfaces.

* Entities: Removed use of contents system for data access

* Entities: Got most queries back to working order

* Entities: Reverted back to data from contents, fixed various issues

* Entities: Started addressing issues from tests

* Entities: Addressed further tests/issues

* Entities: Been through tests to get all passing in dev

Fixed issues and needed test changes along the way.

* Entities: Addressed phpstan errors

* Entities: Reviewed TODO notes

* Entities: Ensured book/shelf relation data removed on destroy

* Entities: Been through API responses & adjusted field visibility

* Entities: Added type index to massively improve query speed
2025-10-18 13:14:30 +01:00

184 lines
5.5 KiB
PHP

<?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;
protected string $baseEndpoint = '/api/recycle-bin';
protected array $endpointMap = [
['get', '/api/recycle-bin'],
['put', '/api/recycle-bin/1'],
['delete', '/api/recycle-bin/1'],
];
public function test_settings_manage_permission_needed_for_all_endpoints()
{
$editor = $this->users->editor();
$this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
$this->actingAs($editor);
foreach ($this->endpointMap as [$method, $uri]) {
$resp = $this->json($method, $uri);
$resp->assertStatus(403);
$resp->assertJson($this->permissionErrorResponse());
}
}
public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
{
$editor = $this->users->editor();
$this->permissions->grantUserRolePermissions($editor, ['restrictions-manage-all']);
$this->actingAs($editor);
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()
{
$admin = $this->users->admin();
$page = $this->entities->page();
$book = $this->entities->book();
$this->actingAs($admin)->delete($page->getUrl());
$this->delete($book->getUrl());
$deletions = Deletion::query()->orderBy('id')->get();
$resp = $this->getJson($this->baseEndpoint);
$expectedData = $deletions
->zip([$page, $book])
->map(function (Collection $data) use ($admin) {
return [
'id' => $data[0]->id,
'deleted_by' => $admin->id,
'created_at' => $data[0]->created_at->toJson(),
'updated_at' => $data[0]->updated_at->toJson(),
'deletable_type' => $data[1]->getMorphClass(),
'deletable_id' => $data[1]->id,
'deletable' => [
'name' => $data[1]->name,
],
];
});
$resp->assertJson([
'data' => $expectedData->values()->all(),
'total' => 2,
]);
}
public function test_index_endpoint_returns_children_count()
{
$admin = $this->users->admin();
$book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
$this->actingAs($admin)->delete($book->getUrl());
$deletion = Deletion::query()->orderBy('id')->first();
$resp = $this->getJson($this->baseEndpoint);
$expectedData = [
[
'id' => $deletion->id,
'deletable' => [
'pages_count' => $book->pages_count,
'chapters_count' => $book->chapters_count,
],
],
];
$resp->assertJson([
'data' => $expectedData,
'total' => 1,
]);
}
public function test_index_endpoint_returns_parent()
{
$admin = $this->users->admin();
$page = $this->entities->pageWithinChapter();
$this->actingAs($admin)->delete($page->getUrl());
$deletion = Deletion::query()->orderBy('id')->first();
$resp = $this->getJson($this->baseEndpoint);
$expectedData = [
[
'id' => $deletion->id,
'deletable' => [
'parent' => [
'id' => $page->chapter->id,
'name' => $page->chapter->name,
'type' => 'chapter',
],
],
],
];
$resp->assertJson([
'data' => $expectedData,
'total' => 1,
]);
}
public function test_restore_endpoint()
{
$page = $this->entities->page();
$this->asAdmin()->delete($page->getUrl());
$page->refresh();
$deletion = Deletion::query()->orderBy('id')->first();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id,
'deleted_at' => $page->deleted_at,
]);
$resp = $this->putJson($this->baseEndpoint . '/' . $deletion->id);
$resp->assertJson([
'restore_count' => 1,
]);
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id,
'deleted_at' => null,
]);
}
public function test_destroy_endpoint()
{
$page = $this->entities->page();
$this->asAdmin()->delete($page->getUrl());
$page->refresh();
$deletion = Deletion::query()->orderBy('id')->first();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id,
'deleted_at' => $page->deleted_at,
]);
$resp = $this->deleteJson($this->baseEndpoint . '/' . $deletion->id);
$resp->assertJson([
'delete_count' => 1,
]);
$this->assertDatabaseMissing('entities', ['id' => $page->id, 'type' => 'page']);
}
}