mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-19 19:55:02 +00:00
added shareEditor test
This commit is contained in:
@@ -0,0 +1,489 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Share;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BrowseTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations, Queueable;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_navigator_tree()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder_level_1 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 1',
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_2 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 2',
|
||||
'parent_id' => $folder_level_1->id,
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_3 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 3',
|
||||
'parent_id' => $folder_level_2->id,
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_2_sibling = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 2 Sibling',
|
||||
'parent_id' => $folder_level_1->id,
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/navigation")
|
||||
->assertStatus(200)
|
||||
->assertExactJson([
|
||||
[
|
||||
"name" => "Home",
|
||||
"location" => "base",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_1->id,
|
||||
"parent_id" => null,
|
||||
"name" => "level 1",
|
||||
"items" => 2,
|
||||
"trashed_items" => 2,
|
||||
"type" => "folder",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_2->id,
|
||||
"parent_id" => $folder_level_1->id,
|
||||
"name" => "level 2",
|
||||
"items" => 1,
|
||||
"trashed_items" => 1,
|
||||
"type" => "folder",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_3->id,
|
||||
"user_id" => $user->id,
|
||||
"parent_id" => $folder_level_2->id,
|
||||
"name" => "level 3",
|
||||
"color" => null,
|
||||
"emoji" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $folder_level_3->created_at,
|
||||
"updated_at" => $folder_level_3->updated_at->toJson(),
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"folders" => [],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
"id" => $folder_level_2_sibling->id,
|
||||
"parent_id" => $folder_level_1->id,
|
||||
"name" => "level 2 Sibling",
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"folders" => []
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_folder_content()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$root = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'root',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'parent_id' => $root->id,
|
||||
'name' => 'Documents',
|
||||
"user_scope" => "master",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $root->id,
|
||||
'name' => 'Document',
|
||||
'basename' => 'document.pdf',
|
||||
"mimetype" => "application/pdf",
|
||||
"user_scope" => "master",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/folders/$root->id")
|
||||
->assertStatus(200)
|
||||
->assertExactJson([
|
||||
[
|
||||
"id" => $folder->id,
|
||||
"user_id" => $user->id,
|
||||
"parent_id" => $root->id,
|
||||
"name" => "Documents",
|
||||
"color" => null,
|
||||
"emoji" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $folder->created_at,
|
||||
"updated_at" => $folder->updated_at->toJson(),
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"parent" => [
|
||||
"id" => $root->id,
|
||||
"name" => "root",
|
||||
"items" => 2,
|
||||
"trashed_items" => 2,
|
||||
"type" => "folder",
|
||||
],
|
||||
"shared" => null,
|
||||
],
|
||||
[
|
||||
"id" => $file->id,
|
||||
"user_id" => $user->id,
|
||||
"folder_id" => $root->id,
|
||||
"thumbnail" => null,
|
||||
"name" => "Document",
|
||||
"basename" => "document.pdf",
|
||||
"mimetype" => "application/pdf",
|
||||
"filesize" => $file->filesize,
|
||||
"type" => "file",
|
||||
"metadata" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $file->created_at,
|
||||
"updated_at" => $file->updated_at->toJson(),
|
||||
"file_url" => "http://localhost/file/document.pdf",
|
||||
"parent" => [
|
||||
"id" => $root->id,
|
||||
"name" => "root",
|
||||
"items" => 2,
|
||||
"trashed_items" => 2,
|
||||
"type" => "folder",
|
||||
],
|
||||
"shared" => null,
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_recent_files()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$root = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'root',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$file_1 = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $root->id,
|
||||
'name' => 'Document 1',
|
||||
'basename' => 'document-1.pdf',
|
||||
"mimetype" => "application/pdf",
|
||||
"user_scope" => "master",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
$this->travel(5)->minutes();
|
||||
|
||||
$file_2 = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $root->id,
|
||||
'name' => 'Document 2',
|
||||
'basename' => 'document-2.pdf',
|
||||
"mimetype" => "application/pdf",
|
||||
"user_scope" => "master",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/latest")
|
||||
->assertStatus(200)
|
||||
->assertExactJson([
|
||||
[
|
||||
"id" => $file_2->id,
|
||||
"user_id" => $user->id,
|
||||
"folder_id" => $root->id,
|
||||
"thumbnail" => null,
|
||||
"name" => "Document 2",
|
||||
"basename" => "document-2.pdf",
|
||||
"mimetype" => "application/pdf",
|
||||
"filesize" => $file_2->filesize,
|
||||
"type" => "file",
|
||||
"metadata" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $file_2->created_at,
|
||||
"updated_at" => $file_2->updated_at->toJson(),
|
||||
"file_url" => "http://localhost/file/document-2.pdf",
|
||||
"parent" => [
|
||||
"id" => $root->id,
|
||||
"name" => "root",
|
||||
"items" => 2,
|
||||
"trashed_items" => 2,
|
||||
"type" => "folder",
|
||||
],
|
||||
],
|
||||
[
|
||||
"id" => $file_1->id,
|
||||
"user_id" => $user->id,
|
||||
"folder_id" => $root->id,
|
||||
"thumbnail" => null,
|
||||
"name" => "Document 1",
|
||||
"basename" => "document-1.pdf",
|
||||
"mimetype" => "application/pdf",
|
||||
"filesize" => $file_1->filesize,
|
||||
"type" => "file",
|
||||
"metadata" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $file_1->created_at,
|
||||
"updated_at" => $file_1->updated_at->toJson(),
|
||||
"file_url" => "http://localhost/file/document-1.pdf",
|
||||
"parent" => [
|
||||
"id" => $root->id,
|
||||
"name" => "root",
|
||||
"items" => 2,
|
||||
"trashed_items" => 2,
|
||||
"type" => "folder",
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_participant_uploads()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
"user_scope" => "editor",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/participants")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => $file->id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_trash_root()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'parent_id' => null,
|
||||
'name' => 'root',
|
||||
'user_id' => $user->id,
|
||||
'deleted_at' => Carbon::now(),
|
||||
"user_scope" => "master",
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => null,
|
||||
'name' => 'Document',
|
||||
'basename' => 'document.pdf',
|
||||
"mimetype" => "application/pdf",
|
||||
"user_scope" => "master",
|
||||
"type" => "file",
|
||||
'user_id' => $user->id,
|
||||
'deleted_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $folder->id,
|
||||
'user_id' => $user->id,
|
||||
'deleted_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/trash")
|
||||
->assertStatus(200)
|
||||
->assertExactJson([
|
||||
[
|
||||
"id" => $folder->id,
|
||||
"user_id" => $user->id,
|
||||
"parent_id" => null,
|
||||
"name" => "root",
|
||||
"color" => null,
|
||||
"emoji" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => $folder->deleted_at,
|
||||
"created_at" => $folder->created_at,
|
||||
"updated_at" => $folder->updated_at->toJson(),
|
||||
"items" => 0,
|
||||
"trashed_items" => 1,
|
||||
"type" => "folder",
|
||||
"parent" => null,
|
||||
],
|
||||
[
|
||||
"id" => $file->id,
|
||||
"user_id" => $user->id,
|
||||
"folder_id" => null,
|
||||
"thumbnail" => null,
|
||||
"name" => "Document",
|
||||
"basename" => "document.pdf",
|
||||
"mimetype" => "application/pdf",
|
||||
"filesize" => $file->filesize,
|
||||
"type" => "file",
|
||||
"metadata" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => $file->deleted_at,
|
||||
"created_at" => $file->created_at,
|
||||
"updated_at" => $file->updated_at->toJson(),
|
||||
"file_url" => "http://localhost/file/document.pdf",
|
||||
"parent" => null
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_shared_items()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
collect([$folder, $file])
|
||||
->each(function ($item) use ($user) {
|
||||
Share::factory(Share::class)
|
||||
->create([
|
||||
"type" => $item->type === 'folder' ? 'folder' : 'file',
|
||||
"item_id" => $item->id,
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
});
|
||||
|
||||
collect([$folder, $file])
|
||||
->each(function ($item) use ($user) {
|
||||
$this->getJson("/api/browse/shared")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => $item->id
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_searched_file()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'name' => 'Document',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/search?query=doc")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => $file->id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_searched_folder()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'Documents',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/search?query=doc")
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'id' => $folder->id
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\User;
|
||||
use App\Models\Zip;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use App\Services\SetupService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ContentAccessTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_public_user_avatar()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$avatar = UploadedFile::fake()
|
||||
->image('fake-avatar.jpg');
|
||||
|
||||
Storage::putFileAs('avatars', $avatar, 'fake-avatar.jpg');
|
||||
|
||||
$this->get('avatars/fake-avatar.jpg')
|
||||
->assertStatus(200);
|
||||
|
||||
Storage::assertExists('avatars/fake-avatar.jpg');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_public_system_image()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$system = UploadedFile::fake()
|
||||
->image('fake-logo.jpg');
|
||||
|
||||
Storage::putFileAs('system', $system, 'fake-logo.jpg');
|
||||
|
||||
$this->get('system/fake-logo.jpg')
|
||||
->assertStatus(200);
|
||||
|
||||
Storage::assertExists('system/fake-logo.jpg');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_private_user_file()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create(Str::random() . '-fake-file.pdf', 1200, 'application/pdf');
|
||||
|
||||
Storage::putFileAs("files/$user->id", $file, $file->name);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'basename' => $file->name,
|
||||
'name' => 'fake-file.pdf',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->get("file/$file->name")
|
||||
->assertOk();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_private_user_image_thumbnail()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$thumbnail = UploadedFile::fake()
|
||||
->image(Str::random() . '-fake-thumbnail.jpg');
|
||||
|
||||
Storage::putFileAs("files/$user->id", $thumbnail, $thumbnail->name);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'thumbnail' => $thumbnail->name,
|
||||
'name' => 'fake-thumbnail.jpg',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->get("thumbnail/$thumbnail->name")
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_private_user_zip()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create('archive.zip', 2000, 'application/zip');
|
||||
|
||||
Storage::putFileAs('zip', $file, 'EHWKcuvKzA4Gv29v-archive.zip');
|
||||
|
||||
$zip = Zip::factory(Zip::class)->create([
|
||||
'basename' => 'EHWKcuvKzA4Gv29v-archive.zip',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->get("zip/$zip->id")
|
||||
->assertOk();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function logged_user_try_to_get_another_private_user_image_thumbnail()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$users = User::factory(User::class)
|
||||
->count(2)
|
||||
->create();
|
||||
|
||||
$thumbnail = UploadedFile::fake()
|
||||
->image(Str::random() . '-fake-thumbnail.jpg');
|
||||
|
||||
Storage::putFileAs("files/{$users[0]->id}", $thumbnail, $thumbnail->name);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $users[0]->id,
|
||||
'thumbnail' => $thumbnail->name,
|
||||
'name' => 'fake-thumbnail.jpg',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($users[1]);
|
||||
|
||||
$this->get("thumbnail/$thumbnail->name")
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function logged_user_try_to_get_another_private_user_file()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$users = User::factory(User::class)
|
||||
->count(2)
|
||||
->create();
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create(Str::random() . '-fake-file.pdf', 1200, 'application/pdf');
|
||||
|
||||
Storage::putFileAs("files/{$users[0]->id}", $file, $file->name);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'user_id' => $users[0]->id,
|
||||
'basename' => $file->name,
|
||||
'name' => 'fake-file.pdf',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($users[1]);
|
||||
|
||||
$this->get("file/$file->name")
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function logged_user_try_to_get_another_private_user_zip()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create('archive.zip', 2000, 'application/zip');
|
||||
|
||||
Storage::putFileAs('zip', $file, 'EHWKcuvKzA4Gv29v-archive.zip');
|
||||
|
||||
$zip = Zip::factory(Zip::class)->create([
|
||||
'basename' => 'EHWKcuvKzA4Gv29v-archive.zip',
|
||||
]);
|
||||
|
||||
$this->get("zip/$zip->id")
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function guest_try_to_get_private_user_file()
|
||||
{
|
||||
$this->get("file/fake-file.pdf")
|
||||
->assertRedirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function guest_try_to_get_private_user_zip()
|
||||
{
|
||||
$this->get("zip/EHWKcuvKzA4Gv29v-archive.zip")
|
||||
->assertRedirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function guest_try_to_get_private_user_image_thumbnail()
|
||||
{
|
||||
$this->get("thumbnail/fake-thumbnail.jpg")
|
||||
->assertRedirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function guest_try_to_get_private_user_folder()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$this->getJson("/api/browse/folders/$folder->id")
|
||||
->assertStatus(401);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\User;
|
||||
use App\Models\Zip;
|
||||
use App\Services\SetupService;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FileTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_test_file_factory()
|
||||
{
|
||||
$file = File::factory(File::class)
|
||||
->create();
|
||||
|
||||
$this->assertDatabaseHas('files', [
|
||||
'id' => $file->id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_upload_image_file_and_create_thumbnail()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->image('fake-image.jpg');
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $file,
|
||||
'folder_id' => null,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$disk->assertMissing(
|
||||
"chunks/fake-image.jpg"
|
||||
);
|
||||
|
||||
$disk->assertExists(
|
||||
"files/$user->id/fake-image.jpg"
|
||||
);
|
||||
|
||||
$disk->assertExists(
|
||||
"files/$user->id/thumbnail-fake-image.jpg"
|
||||
);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_upload_new_file()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create('fake-file.pdf', 1200, 'application/pdf');
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $file,
|
||||
'folder_id' => null,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$disk->assertMissing(
|
||||
"chunks/fake-file.pdf"
|
||||
);
|
||||
|
||||
$disk->assertExists(
|
||||
"files/$user->id/fake-file.pdf"
|
||||
);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
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()
|
||||
{
|
||||
$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();
|
||||
|
||||
$files = File::factory(File::class)
|
||||
->count(2)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $files[0]->id,
|
||||
'type' => 'file',
|
||||
'force_delete' => false,
|
||||
],
|
||||
[
|
||||
'id' => $files[1]->id,
|
||||
'type' => 'file',
|
||||
'force_delete' => false,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$files
|
||||
->each(function ($file) {
|
||||
$this->assertSoftDeleted('files', [
|
||||
'id' => $file->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_delete_multiple_files_hardly()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
collect([0, 1])
|
||||
->each(function ($index) {
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create("fake-file-$index.pdf", 1200, 'application/pdf');
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $file,
|
||||
'folder_id' => null,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
$file_ids = File::all()->pluck('id');
|
||||
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $file_ids->first(),
|
||||
'type' => 'file',
|
||||
'force_delete' => true,
|
||||
],
|
||||
[
|
||||
'id' => $file_ids->last(),
|
||||
'type' => 'file',
|
||||
'force_delete' => true,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$file_ids
|
||||
->each(function ($id, $index) use ($user) {
|
||||
|
||||
$this->assertDatabaseMissing('files', [
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertMissing(
|
||||
"files/$user->id/fake-file-$index.pdf"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_zip_multiple_files_and_download_it()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
collect([0, 1])
|
||||
->each(function ($index) {
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create("fake-file-$index.pdf", 1200, 'application/pdf');
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $file,
|
||||
'folder_id' => null,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
$file_ids = File::all()->pluck('id');
|
||||
|
||||
$this->postJson("/api/zip/files", [
|
||||
'items' => $file_ids,
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('zips', [
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertExists(
|
||||
'zip/' . Zip::first()->basename
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,504 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\User;
|
||||
use App\Models\Zip;
|
||||
use App\Services\SetupService;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
// TODO: pridat foldre do api skupiny
|
||||
|
||||
class FolderTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_test_folder_factory()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'id' => $folder->id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_create_new_folder()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson('/api/create-folder', [
|
||||
'name' => 'New Folder',
|
||||
'parent_id' => null,
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertJsonFragment([
|
||||
'name' => 'New Folder',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'name' => 'New Folder'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_rename_folder()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->patchJson("/api/rename/{$folder->id}", [
|
||||
'name' => 'Renamed Folder',
|
||||
'type' => 'folder',
|
||||
])
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Renamed Folder',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'name' => 'Renamed Folder'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_set_folder_emoji()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$emoji_fragment = [
|
||||
'category' => 'Smileys & Emotion (face-smiling)',
|
||||
'char' => '😁',
|
||||
'name' => 'beaming face with smiling eyes',
|
||||
];
|
||||
|
||||
$this->patchJson("/api/rename/{$folder->id}", [
|
||||
'name' => 'Renamed Folder',
|
||||
'type' => 'folder',
|
||||
'emoji' => $emoji_fragment
|
||||
])
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Renamed Folder',
|
||||
'emoji' => $emoji_fragment
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'color' => null,
|
||||
'emoji' => json_encode($emoji_fragment)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_set_folder_color()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->patchJson("/api/rename/{$folder->id}", [
|
||||
'name' => 'Folder Name',
|
||||
'type' => 'folder',
|
||||
'color' => '#AD6FFE'
|
||||
])
|
||||
->assertStatus(200)
|
||||
->assertJsonFragment([
|
||||
'name' => 'Folder Name',
|
||||
'emoji' => null,
|
||||
'color' => '#AD6FFE',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'color' => '#AD6FFE',
|
||||
'emoji' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_add_folder_to_favourites()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/folders/favourites", [
|
||||
'folders' => [
|
||||
$folder->id
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$this->assertDatabaseHas('favourite_folder', [
|
||||
'user_id' => $user->id,
|
||||
'folder_id' => $folder->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_remove_folder_from_favourites()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$user
|
||||
->favouriteFolders()
|
||||
->attach($folder->id);
|
||||
|
||||
$this->deleteJson("/api/folders/favourites/$folder->id")
|
||||
->assertStatus(204);
|
||||
|
||||
$this->assertDatabaseMissing('favourite_folder', [
|
||||
'user_id' => $user->id,
|
||||
'folder_id' => $folder->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
public function it_delete_multiple_folder_softly()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder_1 = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$folder_2 = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$user->favouriteFolders()->attach($folder_1->id);
|
||||
$user->favouriteFolders()->attach($folder_2->id);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $folder_1->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => false,
|
||||
],
|
||||
[
|
||||
'id' => $folder_2->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => false,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
collect([$folder_1, $folder_2])
|
||||
->each(function ($folder) {
|
||||
|
||||
$this->assertSoftDeleted('folders', [
|
||||
'id' => $folder->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('favourite_folder', [
|
||||
'folder_id' => $folder->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_delete_multiple_folder_hardly()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
$folder_1 = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$folder_2 = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $folder_1->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => true,
|
||||
],
|
||||
[
|
||||
'id' => $folder_2->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => true,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$this->assertDatabaseMissing('folders', [
|
||||
'id' => $folder_1->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('folders', [
|
||||
'id' => $folder_2->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_delete_folder_with_their_content_within_softly()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder_root = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
$folder_children = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $folder_root->id,
|
||||
]);
|
||||
|
||||
$file_1 = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $folder_root->id,
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$file_2 = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $folder_children->id,
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $folder_root->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => false,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
collect([$file_1, $file_2])
|
||||
->each(function ($file) {
|
||||
$this->assertSoftDeleted('files', [
|
||||
'id' => $file->id,
|
||||
]);
|
||||
});
|
||||
|
||||
collect([$folder_root, $folder_children])
|
||||
->each(function ($file) {
|
||||
$this->assertSoftDeleted('folders', [
|
||||
'id' => $file->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_delete_folder_with_their_content_within_hardly()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder_root = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
$folder_children = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $folder_root->id,
|
||||
]);
|
||||
|
||||
collect([$folder_root, $folder_children])
|
||||
->each(function ($folder, $index) {
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create("fake-file-$index.pdf", 1200, 'application/pdf');
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $file,
|
||||
'folder_id' => $folder->id,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
$uploaded_files = File::all();
|
||||
|
||||
collect([0, 1])
|
||||
->each(function ($index) use ($folder_root) {
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $folder_root->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => $index,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
});
|
||||
|
||||
$uploaded_files
|
||||
->each(function ($file, $index) use ($user) {
|
||||
|
||||
$this->assertDatabaseMissing('files', [
|
||||
'id' => $file->id,
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertMissing(
|
||||
"files/$user->id/fake-file-$index.pdf"
|
||||
);
|
||||
});
|
||||
|
||||
collect([$folder_root, $folder_children])
|
||||
->each(function ($id) {
|
||||
$this->assertDatabaseMissing('folders', [
|
||||
'id' => $id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_zip_folder_with_content_within_and_download()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
collect([0, 1])
|
||||
->each(function ($index) use ($folder) {
|
||||
|
||||
$file = UploadedFile::fake()
|
||||
->create("fake-file-$index.pdf", 1200, 'application/pdf');
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $file,
|
||||
'folder_id' => $folder->id,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
});
|
||||
|
||||
$this->getJson("/api/zip/folder/$folder->id")
|
||||
->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('zips', [
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertExists(
|
||||
'zip/' . Zip::first()->basename
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\User;
|
||||
use App\Services\SetupService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TrashTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->setup = app()->make(SetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_restore_items_from_trash()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$attributes = [
|
||||
'user_id' => $user->id,
|
||||
'deleted_at' => Carbon::now(),
|
||||
];
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create($attributes);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create($attributes);
|
||||
|
||||
$this->postJson("/api/trash/restore", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $file->id,
|
||||
'type' => 'file',
|
||||
],
|
||||
[
|
||||
'id' => $folder->id,
|
||||
'type' => 'folder',
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$this->assertDatabaseHas('files', [
|
||||
'deleted_at' => null
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('folders', [
|
||||
'deleted_at' => null
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_dump_trash()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$this->setup->create_directories();
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $user->id
|
||||
]);
|
||||
|
||||
$image = UploadedFile::fake()
|
||||
->image('fake-image.jpg');
|
||||
|
||||
$this->postJson('/api/upload', [
|
||||
'file' => $image,
|
||||
'folder_id' => null,
|
||||
'is_last' => true,
|
||||
])->assertStatus(201);
|
||||
|
||||
$file = File::first();
|
||||
|
||||
$this->postJson("/api/remove", [
|
||||
'items' => [
|
||||
[
|
||||
'id' => $file->id,
|
||||
'type' => 'file',
|
||||
'force_delete' => false,
|
||||
],
|
||||
[
|
||||
'id' => $folder->id,
|
||||
'type' => 'folder',
|
||||
'force_delete' => false,
|
||||
],
|
||||
],
|
||||
])->assertStatus(204);
|
||||
|
||||
$this->deleteJson("/api/trash/dump")
|
||||
->assertStatus(204);
|
||||
|
||||
$this->assertDatabaseMissing('files', [
|
||||
'id' => $file->id
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('folders', [
|
||||
'id' => $folder->id
|
||||
]);
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/fake-image.jpg"
|
||||
);
|
||||
|
||||
$disk->assertMissing(
|
||||
"files/$user->id/thumbnail-fake-image.jpg"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user