You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.3 KiB

2 years ago
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Http\Controllers\Traits\FileTrait;
  4. use App\Models\Collection;
  5. use App\Models\File;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Illuminate\Foundation\Testing\WithFaker;
  8. use Illuminate\Http\UploadedFile;
  9. use Tests\Bootstrap;
  10. use Tests\TestCase;
  11. use Illuminate\Support\Str;
  12. use Tests\Feature\Traits\FileTraits;
  13. class FileStoreTest extends Bootstrap
  14. {
  15. use FileTraits;
  16. public function test_tmp_and_model_id_forbidden()
  17. {
  18. $data = [
  19. "file" => UploadedFile::fake()->image('test.png'),
  20. "alts" => ['1', '2', '3'],
  21. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  22. "public" => 1
  23. ];
  24. $collection = Collection::factory()->create([
  25. 'tmp_support' => false
  26. ]);
  27. $response = $this->loginAs('dick')->postJson(route('api.file.store', ['collection_name' => $collection->name]), $data);
  28. $response->assertForbidden();
  29. }
  30. public function test_tmp_false_and_collection_is_full_forbidden()
  31. {
  32. $randomCount = rand(2, 10);
  33. $collection = Collection::factory()->createQuietly([
  34. 'tmp_support' => false,
  35. 'count' => $randomCount
  36. ]);
  37. for($i=$randomCount; $i > 0; $i--) {
  38. $uuid = app()->uuid;
  39. $this->one(File::class, [
  40. 'uuid' => $uuid,
  41. 'user_id' => auth()->id(),
  42. 'collection_id' => $collection->id,
  43. 'server_path' => '/' . date('y') . '/' . date('m') . '/' . $uuid . '.' . $collection->ext,
  44. ]);
  45. }
  46. $data = [
  47. "file" => UploadedFile::fake()->image('test.png'),
  48. "alts" => ['1', '2', '3'],
  49. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  50. "public" => 1
  51. ];
  52. $response = $this->loginAs('dick')->postJson(route('api.file.store', ['collection_name' => $collection->name]), $data);
  53. $response->assertForbidden();
  54. }
  55. public function test_file_is_not_isset_forbidden()
  56. {
  57. $collection = Collection::factory()->create([
  58. 'tmp_support' => true
  59. ]);
  60. $data = [
  61. "file" => app()->uuid,
  62. "alts" => ['1', '2', '3'],
  63. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  64. "public" => 1
  65. ];
  66. $response = $this->loginAs('dick')->postJson(route('api.file.store', ['collection_name' => $collection->name]), $data);
  67. $response->assertForbidden();
  68. }
  69. /**
  70. * @dataProvider storeValidationTestProvider
  71. */
  72. public function test_store_dynamic_validation_unprocessable($collectionFields, $dataFields)
  73. {
  74. $collection = Collection::factory()->create($collectionFields);
  75. $response = $this->loginAs('dick')->postJson(route('api.file.store', ['collection_name' => $collection->name]), $dataFields);
  76. $response->assertUnprocessable();
  77. }
  78. // /**
  79. // * @testWith
  80. // * ['email:gt']
  81. // * ['email:gt']
  82. // * ['email:gt']
  83. // * ['email:gt']
  84. // * ['email:gt']
  85. // */
  86. // public function test_store_static_validation_unprocessable($key)
  87. // {
  88. // File::factory()->smash($key);
  89. // }
  90. public function test_store_dynamic_validation_stored_file_unprocessable()
  91. {
  92. $collection = Collection::factory()->create([
  93. 'alt_required' => false,
  94. 'description_required' => false,
  95. 'tmp_support' => true,
  96. 'max_width' => 2000,
  97. 'max_height' => 2000,
  98. 'min_width' => 1,
  99. 'min_height' => 1,
  100. 'min_file_size' => 0
  101. ]);
  102. $uuid = app()->uuid;
  103. $file = File::factory()->createImage(storage_path('app/' . date('y') . '/' . date('m') . '/' . $uuid . '.' . $collection->ext))->create([
  104. 'uuid' => $uuid,
  105. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  106. 'collection_id' => $collection->id
  107. ]);
  108. $data = [
  109. "file" => $file->uuid,
  110. "alts" => ['1', '2', '3'],
  111. "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf',
  112. "public" => 1
  113. ];
  114. $response = $this->loginAs('dick')->postJson(route('api.file.store', ['collection_name' => $collection->name]), $data);
  115. $response->assertUnprocessable();
  116. }
  117. }