mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-19 16:32:15 +00:00
controller refactoring part 17
This commit is contained in:
189
tests/Domain/Traffic/TrafficTest.php
Normal file
189
tests/Domain/Traffic/TrafficTest.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
namespace Tests\Domain\Traffic;
|
||||
|
||||
use Storage;
|
||||
use Tests\TestCase;
|
||||
use App\Users\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Domain\Files\Models\File;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
|
||||
class TrafficTest extends TestCase
|
||||
{
|
||||
public UploadedFile $file;
|
||||
public User $user;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->file = UploadedFile::fake()
|
||||
->image('fake-file.jpg', 1200);
|
||||
|
||||
$this->user = User::factory()
|
||||
->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_record_user_file_upload()
|
||||
{
|
||||
$this
|
||||
->actingAs($this->user)
|
||||
->postJson('/api/upload', [
|
||||
'filename' => $this->file->name,
|
||||
'file' => $this->file,
|
||||
'folder_id' => null,
|
||||
'is_last' => 'true',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $this->user->id,
|
||||
'upload' => 991,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_record_current_month_and_go_to_the_next_month_and_record_it()
|
||||
{
|
||||
$this
|
||||
->actingAs($this->user)
|
||||
->postJson('/api/upload', [
|
||||
'filename' => $this->file->name,
|
||||
'file' => $this->file,
|
||||
'folder_id' => null,
|
||||
'is_last' => 'true',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $this->user->id,
|
||||
'upload' => 991,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$this->travel(1)->months();
|
||||
|
||||
$secondFile = UploadedFile::fake()
|
||||
->image('fake-file-2.jpg', 1200);
|
||||
|
||||
$this
|
||||
->actingAs($this->user)
|
||||
->postJson('/api/upload', [
|
||||
'filename' => $secondFile->name,
|
||||
'file' => $secondFile,
|
||||
'folder_id' => null,
|
||||
'is_last' => 'true',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $this->user->id,
|
||||
'upload' => 991,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$this->assertDatabaseCount('traffic', 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function editor_upload_file_into_shared_folder()
|
||||
{
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'user_id' => $this->user->id,
|
||||
'author' => 'user',
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $folder->id,
|
||||
'user_id' => $this->user->id,
|
||||
'type' => 'folder',
|
||||
'is_protected' => false,
|
||||
'permission' => 'editor',
|
||||
]);
|
||||
|
||||
// Check public shared item
|
||||
$this->postJson("/api/editor/upload/$share->token", [
|
||||
'filename' => $this->file->name,
|
||||
'file' => $this->file,
|
||||
'folder_id' => $folder->id,
|
||||
'is_last' => 'true',
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $this->user->id,
|
||||
'upload' => 991,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function user_download_file()
|
||||
{
|
||||
$document = UploadedFile::fake()
|
||||
->create(Str::random() . '-fake-file.pdf', 1200, 'application/pdf');
|
||||
|
||||
Storage::putFileAs("files/{$this->user->id}", $document, $document->name);
|
||||
|
||||
File::factory(File::class)
|
||||
->create([
|
||||
'filesize' => $document->getSize(),
|
||||
'user_id' => $this->user->id,
|
||||
'basename' => $document->name,
|
||||
'name' => $document->name,
|
||||
]);
|
||||
|
||||
$this
|
||||
->actingAs($this->user)
|
||||
->get("/file/$document->name")
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $this->user->id,
|
||||
'download' => $document->getSize(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function visitor_download_file()
|
||||
{
|
||||
$document = UploadedFile::fake()
|
||||
->create(Str::random() . '-fake-file.pdf', 1200, 'application/pdf');
|
||||
|
||||
Storage::putFileAs("files/{$this->user->id}", $document, $document->name);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'filesize' => $document->getSize(),
|
||||
'user_id' => $this->user->id,
|
||||
'basename' => $document->name,
|
||||
'name' => $document->name,
|
||||
]);
|
||||
|
||||
$share = Share::factory(Share::class)
|
||||
->create([
|
||||
'item_id' => $file->id,
|
||||
'user_id' => $this->user->id,
|
||||
'type' => 'file',
|
||||
'is_protected' => false,
|
||||
]);
|
||||
|
||||
$this->get("/file/$document->name/$share->token")
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseHas('traffic', [
|
||||
'user_id' => $this->user->id,
|
||||
'download' => $document->getSize(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user