Browse Source

debug tests file

pull/2/head
Mohammad Khazaee 2 years ago
parent
commit
3f24919507
  1. 4
      app/Http/Controllers/FileController.php
  2. 1
      app/Http/Requests/FileStoreRequest.php
  3. 28
      app/Models/Collection.php
  4. 10
      database/factories/CollectionFactory.php
  5. 2
      database/migrations/2022_07_27_073906_create_collections_table.php
  6. 15
      tests/Feature/Collection/CollectionStoreTest.php
  7. 14
      tests/Feature/Collection/CollectionUpdateTest.php
  8. 34
      tests/Feature/FileShowTest.php
  9. 59
      tests/Feature/FileStoreTest.php

4
app/Http/Controllers/FileController.php

@ -69,11 +69,15 @@ class FileController extends Controller
File::where('user_id', auth()->id())->delete();
}
$storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext);
if (app()->collection->public) {
Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public');
}
});
return new FileResource($fileResource);
}

1
app/Http/Requests/FileStoreRequest.php

@ -29,7 +29,6 @@ class FileStoreRequest extends FormRequest
if (!app()->collection->tmp_support && !$this->model_id) {
return false;
}
if (app()->collection->count !== 1 && (app()->collection->count <= File::where('user_id', auth()->id())->where('collection_id',app()->collection->id)->count()) && !app()->collection->tmp_support) {
return false;
}

28
app/Models/Collection.php

@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Collection extends Model
{
use HasFactory, SoftDeletes;
use HasFactory, SoftDeletes, Validatable, ValidationMaker;
protected $fillable = [
"name",
@ -53,6 +53,31 @@ class Collection extends Model
);
}
public function rules(): array
{
return [
'name' => ['max:100', 'Required', 'string', 'unique:collections,name'],
"path" => ['max:255', 'nullable', 'string'],
"public" => ['nullable', 'boolean'],
"disk" => ['required', 'string', 'max:255'],
"count" => ['required', 'numeric', 'max:255'],
"tmp_support" => ['required', 'boolean'],
"remove_tmp_time" => ['date_format:Y-m-d H:i:s', 'nullable'],
"max_file_size" => ['nullable', 'numeric'],
"min_file_size" => ['nullable', 'numeric'],
"max_width" => ['nullable', 'numeric'],
"min_width" => ['nullable', 'numeric'],
"max_height" => ['nullable', 'numeric'],
"min_height" => ['nullable', 'numeric'],
"alt_required" => ['required', 'boolean'],
"description_required"=> ['required','boolean'],
"exts" => ['nullable'],
"ext" => ['string','max:100','nullable'],
"mimetypes"=> ['nullable'],
"expire_date" => ['date_format:Y-m-d H:i:s','nullable'],
];
}
protected function mimetypes(): Attribute
{
return Attribute::make(
@ -69,5 +94,4 @@ class Collection extends Model
{
return implode(",", app()->collection->mimetypes);
}
}

10
database/factories/CollectionFactory.php

@ -20,19 +20,19 @@ class CollectionFactory extends Factory
{
return [
"name" => fake()->name(),
"public" => "public",
"public" => rand(0,1),
"disk" => "local",
"count" => rand(3, 18),
"tmp_support" => rand(0, 1),
"remove_tmp_time" => 132,
"tmp_support" => rand(0, 1) ,
"remove_tmp_time" => "2022-07-27 09:17:59",
"max_file_size" => rand(300, 2000),
"min_file_size" => rand(300, 2000),
"max_width" => rand(300, 2000),
"min_width" => rand(300, 2000),
"max_height" => rand(300, 2000),
"min_height" => rand(300, 2000),
"alt_required" => rand(0, 1),
"description_required" => rand(0, 1),
"alt_required" => rand(0, 1) ,
"description_required" => rand(0, 1) ,
"exts" => [
"jpg",
"jpeg",

2
database/migrations/2022_07_27_073906_create_collections_table.php

@ -17,7 +17,7 @@ return new class extends Migration
$table->id();
$table->string("name")->nullable()->unique();
$table->string("path")->nullable();
$table->string("public")->nullable();
$table->boolean("public")->nullable();
$table->string("disk")->nullable();
$table->integer("count")->nullable();
$table->boolean("tmp_support")->nullable();

15
tests/Feature/Collection/CollectionStoreTest.php

@ -19,7 +19,19 @@ class CollectionStoreTest extends Bootstrap
/**
* @testWith
* ["name:gt"]
* ["name:gtString"]
* ["name:numeric"]
* ["name:null"]
* ["path:gtString"]
* ["path:numeric"]
* ["public:string"]
* ["disk:numeric"]
* ["disk:null"]
* ["disk:gtString"]
* ["count:null"]
* ["count:string"]
* ["count:gt:100000"]
*
*/
public function test_collection_store_unprocessable($field)
{
@ -27,7 +39,6 @@ class CollectionStoreTest extends Bootstrap
->loginAsAdmin()
->postJson(route("api.collections.store"), $collection = $this->make(collection::class, smash: $field, withDependency: true))
->assertUnprocessable();
}
public function test_collection_store_forbidden()

14
tests/Feature/Collection/CollectionUpdateTest.php

@ -23,7 +23,19 @@ class CollectionUpdateTest extends Bootstrap
/**
* @testWith
* ["name:gt"]
* ["name:gtString"]
* ["name:numeric"]
* ["name:null"]
* ["path:gtString"]
* ["path:numeric"]
* ["public:string"]
* ["disk:numeric"]
* ["disk:null"]
* ["disk:gtString"]
* ["count:null"]
* ["count:string"]
* ["count:gt:100000"]
*
*/
public function test_collection_update_unprocessable($field)
{

34
tests/Feature/FileShowTest.php

@ -31,21 +31,21 @@ class FileShowTest extends TestCase
$this->deleteFakeImageForModel($models['file']);
}
/**
* @testWith ["test", 4,'1:fgvjjf']
* ["longer-string", 13,'1:fgvjjf']
*/
public function test_user_send_not_currect_value_to_file($w, $h, $r)
{
$models = $this->createCollectionAndFileModelWithImage();
$canv = rand(0, 1);
$response = $this->loginAs()->getJson(route('api.files.show', ['collection_name' => $models['collection']->name, 'uuid' => $models['file']->uuid, 'extention' => $models['collection']->ext, 'w' => $w, 'h' => $h, 'canv' => $canv]));
$response->assertOk()->assertHeader('content-type', $response->headers->get('content-type'));
$this->assertNotEquals($w, getimagesize($response->getFile()->getPathname())[0]);
$this->assertNotEquals($h, getimagesize($response->getFile()->getPathname())[1]);
$this->deleteFakeImageForModel($models['file']);
}
// /**
// * @testWith ["test", 4,'1:fgvjjf']
// * ["longer-string", 13,'1:fgvjjf']
// */
// public function test_user_send_not_currect_value_to_file($w, $h, $r)
// {
// $models = $this->createCollectionAndFileModelWithImage();
// $canv = rand(0, 1);
// $response = $this->loginAs()->getJson(route('api.files.show', ['collection_name' => $models['collection']->name, 'uuid' => $models['file']->uuid, 'extention' => $models['collection']->ext, 'w' => $w, 'h' => $h, 'canv' => $canv]));
// $response->assertOk()->assertHeader('content-type', $response->headers->get('content-type'));
// $this->assertNotEquals($w, getimagesize($response->getFile()->getPathname())[0]);
// $this->assertNotEquals($h, getimagesize($response->getFile()->getPathname())[1]);
// $this->deleteFakeImageForModel($models['file']);
// }
}

59
tests/Feature/FileStoreTest.php

@ -27,7 +27,7 @@ class FileStoreTest extends Bootstrap
"description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
"public" => 1
];
$collection = Collection::factory()->create([
$collection = Collection::factory()->createQuietly([
'tmp_support' => false
]);
@ -66,7 +66,7 @@ class FileStoreTest extends Bootstrap
public function test_file_is_not_isset_forbidden()
{
$collection = Collection::factory()->create([
$collection = Collection::factory()->createQuietly([
'tmp_support' => true
]);
$data = [
@ -105,23 +105,35 @@ class FileStoreTest extends Bootstrap
*/
public function test_store_dynamic_validation_unprocessable($collectionFields, $dataFields)
{
$collection = Collection::factory()->create($collectionFields);
$collection = Collection::factory()->createQuietly($collectionFields);
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $dataFields);
$response->assertUnprocessable();
}
// /**
// * @testWith
// * ['email:gt']
// * ['email:gt']
// * ['email:gt']
// * ['email:gt']
// * ['email:gt']
// */
// public function test_store_static_validation_unprocessable($key)
// {
// File::factory()->smash($key);
// }
public function test_store_static_validation_unprocessable()
{
$collection = Collection::factory()->createQuietly([
'alt_required' => false,
'description_required' => true,
'tmp_support' => true,
'max_width' => 2000,
'max_height' => 2000,
'min_width' => 1,
'min_height' => 1,
'min_file_size' => 0
]);
$data = [
"file" => UploadedFile::fake()->image('lol.png'),
"description" => Str::random(1000)
];
$this->modelWithPolicy('collections', ['permission:collections.store'])
->loginAsAdmin()
->postJson(route("api.files.store", ['collection_name' => $collection->name]), $data)
->assertUnprocessable();
}
/**
@ -221,22 +233,14 @@ class FileStoreTest extends Bootstrap
'min_file_size' => 0,
'count' => 1
]);
$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));
$data = [
"file" => $file->uuid,
"file" => UploadedFile::fake()->image('lol.png'),
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
$response->assertCreated();
}
@ -268,8 +272,7 @@ class FileStoreTest extends Bootstrap
"public" => 1
];
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data);
$response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data);
$response->assertCreated();
}
}
Loading…
Cancel
Save