added it_rename_file, it_move_file_to_another_folder, it_delete_multiple_files_softly

This commit is contained in:
Peter Papp
2021-02-27 11:17:04 +01:00
parent 97ade6c238
commit 3328fdebb0
4 changed files with 141 additions and 67 deletions
@@ -188,13 +188,13 @@ class EditItemsController extends Controller
return Demo::response_204(); return Demo::response_204();
} }
foreach ($request->input('items') as $file) { foreach ($request->input('items') as $item) {
// Check permission to delete item for authenticated editor // Check permission to delete item for authenticated editor
if ($request->user()->tokenCan('editor')) { if ($request->user()->tokenCan('editor')) {
// Prevent force delete for non-master users // Prevent force delete for non-master users
if ($file['force_delete']) abort('401'); if ($item['force_delete']) abort('401');
// check if shared_token cookie exist // check if shared_token cookie exist
if (!$request->hasCookie('shared_token')) abort('401'); if (!$request->hasCookie('shared_token')) abort('401');
@@ -203,10 +203,10 @@ class EditItemsController extends Controller
$shared = get_shared($request->cookie('shared_token')); $shared = get_shared($request->cookie('shared_token'));
// Get file|folder item // Get file|folder item
$item = get_item($file['type'], $file['id']); $item = get_item($item['type'], $item['id']);
// Check access to requested directory // Check access to requested directory
if ($file['type'] === 'folder') { if ($item['type'] === 'folder') {
Guardian::check_item_access($item->id, $shared); Guardian::check_item_access($item->id, $shared);
} else { } else {
Guardian::check_item_access($item->folder_id, $shared); Guardian::check_item_access($item->folder_id, $shared);
@@ -214,7 +214,7 @@ class EditItemsController extends Controller
} }
// Delete item // Delete item
Editor::delete_item($file, $file['id']); Editor::delete_item($item, $item['id']);
} }
return response(null, 204); return response(null, 204);
+7 -10
View File
@@ -337,18 +337,15 @@ class Editor
} }
// Delete item // Delete item
if ($item['type'] !== 'folder') { if ($item['type'] === 'file') {
// Get file // Get file
$item = File::withTrashed() $item = UserFile::withTrashed()
->where('user_id', $user->id) ->find($id);
->where('unique_id', $unique_id)
->first();
// Get folder shared record // Get folder shared record
$shared = Share::where('user_id', $user->id) $shared = Share::where('type', 'file')
->where('type', '=', 'file') ->where('item_id', $id)
->where('item_id', $unique_id)
->first(); ->first();
// Delete file shared record // Delete file shared record
@@ -357,7 +354,7 @@ class Editor
} }
// Force delete file // Force delete file
if ($file['force_delete']) { if ($item['force_delete']) {
// Delete file // Delete file
Storage::delete('/file-manager/' . $item->basename); Storage::delete('/file-manager/' . $item->basename);
@@ -370,7 +367,7 @@ class Editor
} }
// Soft delete file // Soft delete file
if (!$file['force_delete']) { if (!$item['force_delete']) {
// Soft delete file // Soft delete file
$item->delete(); $item->delete();
+97 -20
View File
@@ -3,7 +3,10 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Models\File; use App\Models\File;
use App\Models\Folder;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase; use Tests\TestCase;
class FileTest extends TestCase class FileTest extends TestCase
@@ -28,12 +31,106 @@ class FileTest extends TestCase
} }
/**
* @test
*/
public function it_rename_file() public function it_rename_file()
{ {
$file = File::factory(File::class)
->create();
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->patchJson("/api/rename/{$file->id}", [
'name' => 'Renamed Item',
'type' => 'file',
])
->assertStatus(200)
->assertJsonFragment([
'name' => 'Renamed Item',
]);
$this->assertDatabaseHas('files', [
'name' => 'Renamed Item'
]);
} }
/**
* @test
*/
public function it_move_file_to_another_folder() public function it_move_file_to_another_folder()
{
$folder = Folder::factory(Folder::class)
->create();
$file = File::factory(File::class)
->create();
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson("/api/move", [
'to_id' => $folder->id,
'items' => [
[
'type' => 'file',
'id' => $file->id,
]
],
])->assertStatus(204);
$this->assertDatabaseHas('files', [
'id' => $file->id,
'folder_id' => $folder->id,
]);
}
/**
* @test
*/
public function it_delete_multiple_files_softly()
{
$user = User::factory(User::class)
->create();
$file_1 = File::factory(File::class)
->create();
$file_2 = File::factory(File::class)
->create();
Sanctum::actingAs($user);
$this->postJson("/api/remove", [
'items' => [
[
'id' => $file_1->id,
'type' => 'file',
'force_delete' => false,
],
[
'id' => $file_2->id,
'type' => 'file',
'force_delete' => false,
],
],
])->assertStatus(204);
$this->assertSoftDeleted('files', [
'id' => $file_1->id,
]);
$this->assertSoftDeleted('files', [
'id' => $file_2->id,
]);
}
public function it_delete_multiple_files_hardly()
{ {
} }
@@ -42,24 +139,4 @@ class FileTest extends TestCase
{ {
} }
public function it_delete_single_file()
{
}
public function it_delete_multiple_files()
{
}
public function it_delete_file_softly()
{
}
public function it_delete_file_hardly()
{
}
} }
+32 -32
View File
@@ -81,7 +81,7 @@ class FolderTest extends TestCase
/** /**
* @test * @test
*/ */
public function it_set_folder_icon() public function it_set_folder_emoji()
{ {
$folder = Folder::factory(Folder::class) $folder = Folder::factory(Folder::class)
->create(); ->create();
@@ -145,37 +145,6 @@ class FolderTest extends TestCase
]); ]);
} }
/**
* @test
*/
public function it_move_folder_to_another_folder()
{
$root = Folder::factory(Folder::class)
->create();
$children = Folder::factory(Folder::class)
->create();
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson("/api/move", [
'to_id' => $root->id,
'items' => [
[
'type' => 'folder',
'id' => $children->id,
]
],
])->assertStatus(204);
$this->assertEquals(
$root->id, Folder::find($children->id)->parent_id
);
}
/** /**
* @test * @test
*/ */
@@ -227,6 +196,37 @@ class FolderTest extends TestCase
]); ]);
} }
/**
* @test
*/
public function it_move_folder_to_another_folder()
{
$root = Folder::factory(Folder::class)
->create();
$children = Folder::factory(Folder::class)
->create();
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson("/api/move", [
'to_id' => $root->id,
'items' => [
[
'type' => 'folder',
'id' => $children->id,
]
],
])->assertStatus(204);
$this->assertEquals(
$root->id, Folder::find($children->id)->parent_id
);
}
/** /**
* @test * @test
*/ */