ImportControllerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Jobs\Import\ImportJob;
  4. use App\Models\Import;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Http\UploadedFile;
  9. use Illuminate\Support\Facades\Bus;
  10. use Illuminate\Support\Facades\Storage;
  11. use Tests\TestCase;
  12. class ImportControllerTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. protected $seed = true;
  16. private User $adminUser;
  17. private User $managerUser;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  22. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  23. }
  24. // ==================== Authentication ====================
  25. public function test_guest_cannot_access_import_index(): void
  26. {
  27. $response = $this->get(route('import.index'));
  28. $response->assertRedirect(route('login'));
  29. }
  30. public function test_guest_cannot_access_import_show(): void
  31. {
  32. $import = Import::factory()->create();
  33. $response = $this->get(route('import.show', $import));
  34. $response->assertRedirect(route('login'));
  35. }
  36. public function test_guest_cannot_create_import(): void
  37. {
  38. $response = $this->post(route('import.create'), []);
  39. $response->assertRedirect(route('login'));
  40. }
  41. // ==================== Authorization ====================
  42. public function test_manager_cannot_access_import_index(): void
  43. {
  44. $response = $this->actingAs($this->managerUser)
  45. ->get(route('import.index'));
  46. $response->assertStatus(403);
  47. }
  48. // ==================== Index ====================
  49. public function test_admin_can_access_import_index(): void
  50. {
  51. $response = $this->actingAs($this->adminUser)
  52. ->get(route('import.index'));
  53. $response->assertStatus(200);
  54. $response->assertViewIs('import.index');
  55. }
  56. // ==================== Show ====================
  57. public function test_admin_can_view_import(): void
  58. {
  59. $import = Import::factory()->create();
  60. $response = $this->actingAs($this->adminUser)
  61. ->get(route('import.show', $import));
  62. $response->assertStatus(200);
  63. $response->assertViewIs('import.show');
  64. }
  65. // ==================== Store (upload file + dispatch job) ====================
  66. public function test_admin_can_upload_valid_xlsx_and_dispatches_job(): void
  67. {
  68. Bus::fake();
  69. Storage::fake('upload');
  70. $file = UploadedFile::fake()->create('orders_import.xlsx', 50, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  71. $response = $this->actingAs($this->adminUser)
  72. ->post(route('import.create'), [
  73. 'type' => 'orders',
  74. 'import_file' => $file,
  75. ]);
  76. $response->assertRedirect(route('import.index'));
  77. $response->assertSessionHas('success');
  78. Bus::assertDispatched(ImportJob::class);
  79. $this->assertDatabaseHas('imports', [
  80. 'type' => 'orders',
  81. 'status' => 'new',
  82. ]);
  83. }
  84. public function test_import_store_requires_type(): void
  85. {
  86. Bus::fake();
  87. Storage::fake('upload');
  88. $file = UploadedFile::fake()->create('test.xlsx', 50);
  89. $response = $this->actingAs($this->adminUser)
  90. ->post(route('import.create'), [
  91. 'import_file' => $file,
  92. ]);
  93. $response->assertSessionHasErrors('type');
  94. Bus::assertNotDispatched(ImportJob::class);
  95. }
  96. public function test_import_store_requires_valid_type(): void
  97. {
  98. Bus::fake();
  99. Storage::fake('upload');
  100. $file = UploadedFile::fake()->create('test.xlsx', 50);
  101. $response = $this->actingAs($this->adminUser)
  102. ->post(route('import.create'), [
  103. 'type' => 'invalid_type',
  104. 'import_file' => $file,
  105. ]);
  106. $response->assertSessionHasErrors('type');
  107. Bus::assertNotDispatched(ImportJob::class);
  108. }
  109. public function test_import_store_requires_file(): void
  110. {
  111. Bus::fake();
  112. $response = $this->actingAs($this->adminUser)
  113. ->post(route('import.create'), [
  114. 'type' => 'orders',
  115. ]);
  116. $response->assertSessionHasErrors('import_file');
  117. Bus::assertNotDispatched(ImportJob::class);
  118. }
  119. public function test_admin_can_import_reclamations_type(): void
  120. {
  121. Bus::fake();
  122. Storage::fake('upload');
  123. $file = UploadedFile::fake()->create('reclamations.xlsx', 50);
  124. $response = $this->actingAs($this->adminUser)
  125. ->post(route('import.create'), [
  126. 'type' => 'reclamations',
  127. 'import_file' => $file,
  128. ]);
  129. $response->assertRedirect(route('import.index'));
  130. Bus::assertDispatched(ImportJob::class);
  131. $this->assertDatabaseHas('imports', ['type' => 'reclamations', 'status' => 'new']);
  132. }
  133. public function test_admin_can_import_mafs_type(): void
  134. {
  135. Bus::fake();
  136. Storage::fake('upload');
  137. $file = UploadedFile::fake()->create('mafs.xlsx', 50);
  138. $response = $this->actingAs($this->adminUser)
  139. ->post(route('import.create'), [
  140. 'type' => 'mafs',
  141. 'import_file' => $file,
  142. ]);
  143. $response->assertRedirect(route('import.index'));
  144. Bus::assertDispatched(ImportJob::class);
  145. }
  146. public function test_admin_can_import_catalog_type(): void
  147. {
  148. Bus::fake();
  149. Storage::fake('upload');
  150. $file = UploadedFile::fake()->create('catalog.xlsx', 50);
  151. $response = $this->actingAs($this->adminUser)
  152. ->post(route('import.create'), [
  153. 'type' => 'catalog',
  154. 'import_file' => $file,
  155. ]);
  156. $response->assertRedirect(route('import.index'));
  157. Bus::assertDispatched(ImportJob::class);
  158. }
  159. }