| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace Tests\Feature;
- use App\Jobs\Import\ImportJob;
- use App\Models\Import;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Bus;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class ImportControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private User $adminUser;
- private User $managerUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
- $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
- }
- // ==================== Authentication ====================
- public function test_guest_cannot_access_import_index(): void
- {
- $response = $this->get(route('import.index'));
- $response->assertRedirect(route('login'));
- }
- public function test_guest_cannot_access_import_show(): void
- {
- $import = Import::factory()->create();
- $response = $this->get(route('import.show', $import));
- $response->assertRedirect(route('login'));
- }
- public function test_guest_cannot_create_import(): void
- {
- $response = $this->post(route('import.create'), []);
- $response->assertRedirect(route('login'));
- }
- // ==================== Authorization ====================
- public function test_manager_cannot_access_import_index(): void
- {
- $response = $this->actingAs($this->managerUser)
- ->get(route('import.index'));
- $response->assertStatus(403);
- }
- // ==================== Index ====================
- public function test_admin_can_access_import_index(): void
- {
- $response = $this->actingAs($this->adminUser)
- ->get(route('import.index'));
- $response->assertStatus(200);
- $response->assertViewIs('import.index');
- }
- // ==================== Show ====================
- public function test_admin_can_view_import(): void
- {
- $import = Import::factory()->create();
- $response = $this->actingAs($this->adminUser)
- ->get(route('import.show', $import));
- $response->assertStatus(200);
- $response->assertViewIs('import.show');
- }
- // ==================== Store (upload file + dispatch job) ====================
- public function test_admin_can_upload_valid_xlsx_and_dispatches_job(): void
- {
- Bus::fake();
- Storage::fake('upload');
- $file = UploadedFile::fake()->create('orders_import.xlsx', 50, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'type' => 'orders',
- 'import_file' => $file,
- ]);
- $response->assertRedirect(route('import.index'));
- $response->assertSessionHas('success');
- Bus::assertDispatched(ImportJob::class);
- $this->assertDatabaseHas('imports', [
- 'type' => 'orders',
- 'status' => 'new',
- ]);
- }
- public function test_import_store_requires_type(): void
- {
- Bus::fake();
- Storage::fake('upload');
- $file = UploadedFile::fake()->create('test.xlsx', 50);
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'import_file' => $file,
- ]);
- $response->assertSessionHasErrors('type');
- Bus::assertNotDispatched(ImportJob::class);
- }
- public function test_import_store_requires_valid_type(): void
- {
- Bus::fake();
- Storage::fake('upload');
- $file = UploadedFile::fake()->create('test.xlsx', 50);
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'type' => 'invalid_type',
- 'import_file' => $file,
- ]);
- $response->assertSessionHasErrors('type');
- Bus::assertNotDispatched(ImportJob::class);
- }
- public function test_import_store_requires_file(): void
- {
- Bus::fake();
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'type' => 'orders',
- ]);
- $response->assertSessionHasErrors('import_file');
- Bus::assertNotDispatched(ImportJob::class);
- }
- public function test_admin_can_import_reclamations_type(): void
- {
- Bus::fake();
- Storage::fake('upload');
- $file = UploadedFile::fake()->create('reclamations.xlsx', 50);
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'type' => 'reclamations',
- 'import_file' => $file,
- ]);
- $response->assertRedirect(route('import.index'));
- Bus::assertDispatched(ImportJob::class);
- $this->assertDatabaseHas('imports', ['type' => 'reclamations', 'status' => 'new']);
- }
- public function test_admin_can_import_mafs_type(): void
- {
- Bus::fake();
- Storage::fake('upload');
- $file = UploadedFile::fake()->create('mafs.xlsx', 50);
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'type' => 'mafs',
- 'import_file' => $file,
- ]);
- $response->assertRedirect(route('import.index'));
- Bus::assertDispatched(ImportJob::class);
- }
- public function test_admin_can_import_catalog_type(): void
- {
- Bus::fake();
- Storage::fake('upload');
- $file = UploadedFile::fake()->create('catalog.xlsx', 50);
- $response = $this->actingAs($this->adminUser)
- ->post(route('import.create'), [
- 'type' => 'catalog',
- 'import_file' => $file,
- ]);
- $response->assertRedirect(route('import.index'));
- Bus::assertDispatched(ImportJob::class);
- }
- }
|