Browse Source

complete restore file

pull/2/head
Mohammad Khazaee 2 years ago
parent
commit
d72622e858
  1. 2
      app/Http/Middleware/BindFileModelMiddleware.php
  2. 2
      tests/Feature/FileDeleteTest.php
  3. 47
      tests/Feature/FileRestoreTest.php

2
app/Http/Middleware/BindFileModelMiddleware.php

@ -57,7 +57,7 @@ class BindFileModelMiddleware
}
if ($request->route()->action['as'] == 'api.files.destroy') {
$file = File::findOrFail($request->route('uuid'));
$file = File::withTrashed()->findOrFail($request->route('uuid'));
$Collection = Collection::withTrashed()->where('name', $request->route('collection_name'))->firstOrFail();
if (Storage::disk($Collection->disk)->exists($file->server_path . $file->uuid . '.' . $Collection->ext)) {
app()->bind('file', function () use ($file) {

2
tests/Feature/FileDeleteTest.php

@ -41,6 +41,6 @@ class FileDeleteTest extends TestCase
$imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
$response = $this->loginAs()->deleteJson(route('api.files.destroy', ['collection_name' => $collection->name, 'uuid' => $file->uuid, 'extention' => $collection->ext]));
$response->assertok();
$response->assertok();
}
}

47
tests/Feature/FileRestoreTest.php

@ -0,0 +1,47 @@
<?php
namespace Tests\Feature;
use App\Image\ImageProcessor;
use App\Models\Collection;
use App\Models\File;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class FileRestoreTest extends TestCase
{
public function test_user_with_permission_can_not_restore_file()
{
$this->assertFalse("it's not mohammad's fault, I'm waiting for dynamic policy");
}
public function test_user_with_permission_can_restore_file()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => false,
'tmp_support' => true,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0
]);
$uuid = app()->uuid;
$file = File::factory()->createQuietly([
'uuid' => $uuid,
'server_path' => '/' . date('y') . '/' . date('m') . '/',
'user_id' => auth()->id(),
'collection_id' => $collection->id
]);
$imageProcessor = new ImageProcessor;
$imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext));
$file->delete();
$response = $this->loginAs()->deleteJson(route('api.files.destroy', ['collection_name' => $collection->name, 'uuid' => $file->uuid, 'extention' => $collection->ext]));
$response->assertok();
}
}
Loading…
Cancel
Save