mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-28 11:00:39 +00:00
test refactoring
This commit is contained in:
@@ -7,7 +7,6 @@ use Domain\Files\Models\File;
|
|||||||
use Domain\Folders\Models\Folder;
|
use Domain\Folders\Models\Folder;
|
||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
use Domain\Traffic\Models\Traffic;
|
use Domain\Traffic\Models\Traffic;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Database\Factories\UserFactory;
|
use Database\Factories\UserFactory;
|
||||||
use Domain\Settings\Models\Setting;
|
use Domain\Settings\Models\Setting;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Users\Resources;
|
namespace App\Users\Resources;
|
||||||
|
|
||||||
use ByteUnits\Metric;
|
use ByteUnits\Metric;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Domain\Admin\Controllers\Dashboard;
|
namespace Domain\Admin\Controllers\Dashboard;
|
||||||
|
|
||||||
use ByteUnits\Metric;
|
use ByteUnits\Metric;
|
||||||
|
|||||||
@@ -1,22 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Domain\Files\Actions;
|
namespace Domain\Files\Actions;
|
||||||
|
|
||||||
use Domain\Folders\Models\Folder;
|
use Domain\Folders\Models\Folder;
|
||||||
|
|
||||||
|
|
||||||
class CreateFolderStructureAction
|
class CreateFolderStructureAction
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Create a new action instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
// Prepare the action for execution, leveraging constructor injection.
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the action.
|
* Execute the action.
|
||||||
*
|
*
|
||||||
@@ -24,7 +12,6 @@ class CreateFolderStructureAction
|
|||||||
*/
|
*/
|
||||||
public function __invoke($path, $parent, $user_id)
|
public function __invoke($path, $parent, $user_id)
|
||||||
{
|
{
|
||||||
|
|
||||||
$folders = array_slice(explode('/', $path), 1, -1);
|
$folders = array_slice(explode('/', $path), 1, -1);
|
||||||
|
|
||||||
$parent_id = $parent;
|
$parent_id = $parent;
|
||||||
@@ -36,30 +23,22 @@ class CreateFolderStructureAction
|
|||||||
|
|
||||||
// If file have some parent folders
|
// If file have some parent folders
|
||||||
if (count($folders) > 0) {
|
if (count($folders) > 0) {
|
||||||
|
|
||||||
// If uploading structure has same lenght as a already existed structure
|
// If uploading structure has same lenght as a already existed structure
|
||||||
if (count($folders) === count($structure)) {
|
if (count($folders) === count($structure)) {
|
||||||
|
|
||||||
// Get correct file parent from the already craeted structure
|
// Get correct file parent from the already craeted structure
|
||||||
$last_folder = $this->get_file_parent($structure, $folders);
|
$last_folder = $this->get_file_parent($structure, $folders);
|
||||||
|
|
||||||
} elseif (count($folders) !== count($structure)) {
|
} elseif (count($folders) !== count($structure)) {
|
||||||
|
|
||||||
|
|
||||||
if (count($structure) > 0) {
|
if (count($structure) > 0) {
|
||||||
|
|
||||||
// Check what folders are missed in structure and return missed folder with last created folder in structure
|
// Check what folders are missed in structure and return missed folder with last created folder in structure
|
||||||
$data = $this->check_exist_folders($structure, $folders);
|
$data = $this->check_exist_folders($structure, $folders);
|
||||||
|
|
||||||
$folders = $data[0];
|
$folders = $data[0];
|
||||||
|
|
||||||
$parent_id = $data[1];
|
$parent_id = $data[1];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create folders
|
// Create folders
|
||||||
foreach ($folders as $folder) {
|
foreach ($folders as $folder) {
|
||||||
|
|
||||||
$new_folder = Folder::create([
|
$new_folder = Folder::create([
|
||||||
'name' => $folder,
|
'name' => $folder,
|
||||||
'parent_id' => $parent_id,
|
'parent_id' => $parent_id,
|
||||||
@@ -69,7 +48,7 @@ class CreateFolderStructureAction
|
|||||||
$parent_id = $new_folder->id;
|
$parent_id = $new_folder->id;
|
||||||
|
|
||||||
$last_folder = $new_folder->id;
|
$last_folder = $new_folder->id;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,37 +63,32 @@ class CreateFolderStructureAction
|
|||||||
$parent_name = '';
|
$parent_name = '';
|
||||||
|
|
||||||
foreach (array_reverse($folders) as $folder) {
|
foreach (array_reverse($folders) as $folder) {
|
||||||
|
|
||||||
$item = $structure->where('name', $folder);
|
$item = $structure->where('name', $folder);
|
||||||
|
|
||||||
$parent = $item->pluck('parent')->pluck('name')[0];
|
$parent = $item->pluck('parent')->pluck('name')[0];
|
||||||
|
|
||||||
// Check if folder have valid parent name
|
// Check if folder have valid parent name
|
||||||
if ($parent && $folder === $parent_name || $parent_name == '') {
|
if ($parent && $folder === $parent_name || $parent_name == '') {
|
||||||
|
|
||||||
$parent_name = $parent;
|
$parent_name = $parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $structure->where('name', $folders[array_key_last($folders)])->first()->id;
|
return $structure->where('name', $folders[array_key_last($folders)])->first()->id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the folders that is need to create in already created structure and last created parent
|
* Return the folders that is need to create in already created structure and last created parent
|
||||||
*/
|
*/
|
||||||
private function check_exist_folders($structure, $folders)
|
private function check_exist_folders($structure, $folders): array
|
||||||
{
|
{
|
||||||
|
|
||||||
$create_folders = [];
|
$create_folders = [];
|
||||||
$last_parent = '';
|
$last_parent = '';
|
||||||
|
|
||||||
foreach ($folders as $folder) {
|
foreach ($folders as $folder) {
|
||||||
|
|
||||||
// Filter folders that is need to create
|
// Filter folders that is need to create
|
||||||
if (! $structure->where('name', $folder)->first()) {
|
if (! $structure->where('name', $folder)->first()) {
|
||||||
|
|
||||||
array_push($create_folders, $folder);
|
array_push($create_folders, $folder);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Find last created folder
|
// Find last created folder
|
||||||
$last_parent = $structure->where('name', $folder)->first()->id;
|
$last_parent = $structure->where('name', $folder)->first()->id;
|
||||||
}
|
}
|
||||||
@@ -123,4 +97,3 @@ class CreateFolderStructureAction
|
|||||||
return [$create_folders, $last_parent];
|
return [$create_folders, $last_parent];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use Domain\Files\Requests\UploadRequest;
|
|||||||
use Domain\Files\Models\File as UserFile;
|
use Domain\Files\Models\File as UserFile;
|
||||||
use Domain\Traffic\Actions\RecordUploadAction;
|
use Domain\Traffic\Actions\RecordUploadAction;
|
||||||
use App\Users\Exceptions\InvalidUserActionException;
|
use App\Users\Exceptions\InvalidUserActionException;
|
||||||
use Domain\Files\Actions\CreateFolderStructureAction;
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||||
|
|
||||||
class UploadFileAction
|
class UploadFileAction
|
||||||
{
|
{
|
||||||
@@ -25,7 +25,7 @@ class UploadFileAction
|
|||||||
/**
|
/**
|
||||||
* Upload new file
|
* Upload new file
|
||||||
*
|
*
|
||||||
* @throws InvalidUserActionException
|
* @throws InvalidUserActionException|FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public function __invoke(
|
public function __invoke(
|
||||||
UploadRequest $request,
|
UploadRequest $request,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests\Domain\Folders;
|
namespace Tests\Domain\Folders;
|
||||||
|
|
||||||
use Storage;
|
use Storage;
|
||||||
@@ -413,58 +414,4 @@ class FolderTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function it_upload_folders_structure_with_files()
|
|
||||||
{
|
|
||||||
$file_1 = UploadedFile::fake()
|
|
||||||
->create('fake-file_1.pdf', 12000000, 'application/pdf');
|
|
||||||
|
|
||||||
$file_2 = UploadedFile::fake()
|
|
||||||
->create('fake-file_2.pdf', 12000000, 'application/pdf');
|
|
||||||
|
|
||||||
$user = User::factory(User::class)
|
|
||||||
->create();
|
|
||||||
|
|
||||||
$uploaded_file_1 = $this
|
|
||||||
->actingAs($user)
|
|
||||||
->postJson('/api/upload', [
|
|
||||||
'filename' => $file_2->name,
|
|
||||||
'file' => $file_2,
|
|
||||||
'path' => '/Folder_1/' . $file_2->name,
|
|
||||||
'folder_id' => null,
|
|
||||||
'is_last' => 'true',
|
|
||||||
])->assertStatus(201);
|
|
||||||
|
|
||||||
$uploaded_file_2 = $this
|
|
||||||
->actingAs($user)
|
|
||||||
->postJson('/api/upload', [
|
|
||||||
'filename' => $file_1->name,
|
|
||||||
'file' => $file_1,
|
|
||||||
'path' => '/Folder_1/Folder_2/' . $file_1->name,
|
|
||||||
'folder_id' => null,
|
|
||||||
'is_last' => 'true',
|
|
||||||
])->assertStatus(201);
|
|
||||||
|
|
||||||
|
|
||||||
$file_1_parent = Folder::whereName('Folder_1')->first();
|
|
||||||
|
|
||||||
$file_2_parent = Folder::whereName('Folder_2')->first();
|
|
||||||
|
|
||||||
$this->assertDatabaseHas('folders', [
|
|
||||||
'id' => $uploaded_file_1['folder_id'],
|
|
||||||
'parent_id' => null,
|
|
||||||
'id' => $uploaded_file_2['folder_id'],
|
|
||||||
'parent_id' => $file_1_parent->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertDatabaseHas('files', [
|
|
||||||
'id' => $uploaded_file_1['id'],
|
|
||||||
'folder_id' => $file_1_parent->id,
|
|
||||||
'id' => $uploaded_file_2['id'],
|
|
||||||
'folder_id' => $file_2_parent->id,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Domain\Folders;
|
||||||
|
|
||||||
|
use App\Users\Models\User;
|
||||||
|
use Domain\Files\Models\File;
|
||||||
|
use Domain\Folders\Models\Folder;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class FolderUploadTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function check_folder_tree_structure_creation_after_folder_upload()
|
||||||
|
{
|
||||||
|
$user = User::factory()
|
||||||
|
->hasSettings()
|
||||||
|
->create();
|
||||||
|
|
||||||
|
$file = UploadedFile::fake()
|
||||||
|
->create('fake-file_1.pdf', 120000, 'application/pdf');
|
||||||
|
|
||||||
|
$this
|
||||||
|
->actingAs($user)
|
||||||
|
->postJson('/api/upload', [
|
||||||
|
'filename' => $file->name,
|
||||||
|
'file' => $file,
|
||||||
|
'path' => "/level_1/level_2/level_3/$file->name",
|
||||||
|
'parent_id' => null,
|
||||||
|
'is_last' => 'true',
|
||||||
|
])->assertStatus(201);
|
||||||
|
|
||||||
|
$file = File::first();
|
||||||
|
|
||||||
|
$level_1 = Folder::where('name', 'level_1')->first();
|
||||||
|
$level_2 = Folder::where('name', 'level_2')->first();
|
||||||
|
$level_3 = Folder::where('name', 'level_3')->first();
|
||||||
|
|
||||||
|
$this->assertEquals(null, $level_1->parent_id);
|
||||||
|
$this->assertEquals($level_2->parent_id, $level_1->id);
|
||||||
|
$this->assertEquals($level_3->parent_id, $level_2->id);
|
||||||
|
|
||||||
|
$this->assertEquals($level_3->id, $file->parent_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function check_children_tree_structure_creation_after_folder_upload()
|
||||||
|
{
|
||||||
|
$user = User::factory()
|
||||||
|
->hasSettings()
|
||||||
|
->create();
|
||||||
|
|
||||||
|
$brother = UploadedFile::fake()
|
||||||
|
->create('brother.pdf', 120000, 'application/pdf');
|
||||||
|
|
||||||
|
$sister = UploadedFile::fake()
|
||||||
|
->create('sister.pdf', 120000, 'application/pdf');
|
||||||
|
|
||||||
|
$this
|
||||||
|
->actingAs($user)
|
||||||
|
->postJson('/api/upload', [
|
||||||
|
'filename' => $brother->name,
|
||||||
|
'file' => $brother,
|
||||||
|
'path' => "/Folder/Brother/$brother->name",
|
||||||
|
'parent_id' => null,
|
||||||
|
'is_last' => 'true',
|
||||||
|
])->assertStatus(201);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->actingAs($user)
|
||||||
|
->postJson('/api/upload', [
|
||||||
|
'filename' => $sister->name,
|
||||||
|
'file' => $sister,
|
||||||
|
'path' => "/Folder/Sister/$sister->name",
|
||||||
|
'parent_id' => null,
|
||||||
|
'is_last' => 'true',
|
||||||
|
])->assertStatus(201);
|
||||||
|
|
||||||
|
$brotherFile = File::where('name', 'brother.pdf')->first();
|
||||||
|
$sisterFile = File::where('name', 'sister.pdf')->first();
|
||||||
|
|
||||||
|
$brotherFolder = Folder::where('name', 'Brother')->first();
|
||||||
|
$sisterFolder = Folder::where('name', 'Sister')->first();
|
||||||
|
|
||||||
|
// Check correctness of siblings file appended to the folder
|
||||||
|
$this->assertEquals($brotherFolder->id, $brotherFile->parent_id);
|
||||||
|
$this->assertEquals($sisterFolder->id, $sisterFile->parent_id);
|
||||||
|
|
||||||
|
$home = Folder::where('name', 'Folder')->first();
|
||||||
|
|
||||||
|
// Check correctness of siblings folders appended to the home folder
|
||||||
|
$this->assertEquals($home->id, $brotherFolder->parent_id);
|
||||||
|
$this->assertEquals($home->id, $sisterFolder->parent_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user