adminUser = User::factory()->create(['role' => Role::ADMIN]); $this->managerUser = User::factory()->create(['role' => Role::MANAGER]); $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]); } // ==================== Guest redirects ==================== public function test_guest_cannot_access_product_sku_index(): void { $response = $this->get(route('product_sku.index')); $response->assertRedirect(route('login')); } public function test_guest_cannot_access_product_sku_show(): void { $sku = ProductSKU::factory()->create(); $response = $this->get(route('product_sku.show', $sku)); $response->assertRedirect(route('login')); } public function test_guest_cannot_update_product_sku(): void { $sku = ProductSKU::factory()->create(); $response = $this->post(route('product_sku.update', $sku), []); $response->assertRedirect(route('login')); } // ==================== Authorization ==================== public function test_brigadier_cannot_access_product_sku_index(): void { $response = $this->actingAs($this->brigadierUser) ->get(route('product_sku.index')); $response->assertStatus(403); } public function test_manager_cannot_export_mafs(): void { $response = $this->actingAs($this->managerUser) ->post(route('mafs.export')); $response->assertStatus(403); } // ==================== Index ==================== public function test_admin_can_access_product_sku_index(): void { $response = $this->actingAs($this->adminUser) ->get(route('product_sku.index')); $response->assertStatus(200); $response->assertViewIs('products_sku.index'); } public function test_manager_can_access_product_sku_index(): void { $response = $this->actingAs($this->managerUser) ->get(route('product_sku.index')); $response->assertStatus(200); $response->assertViewIs('products_sku.index'); } // ==================== Show ==================== public function test_admin_can_view_product_sku(): void { $sku = ProductSKU::factory()->create(); $response = $this->actingAs($this->adminUser) ->get(route('product_sku.show', $sku)); $response->assertStatus(200); $response->assertViewIs('products_sku.edit'); } public function test_manager_can_view_product_sku(): void { $sku = ProductSKU::factory()->create(); $response = $this->actingAs($this->managerUser) ->get(route('product_sku.show', $sku)); $response->assertStatus(200); $response->assertViewIs('products_sku.edit'); } // ==================== Update ==================== public function test_admin_can_update_product_sku(): void { $product = Product::factory()->create(); $order = Order::factory()->create(); $sku = ProductSKU::factory()->create([ 'product_id' => $product->id, 'order_id' => $order->id, ]); $response = $this->actingAs($this->adminUser) ->post(route('product_sku.update', $sku), [ 'product_id' => $product->id, 'order_id' => $order->id, 'status' => 'shipped', 'factory_number' => 'FN-999999', 'comment' => 'Updated by admin', ]); $response->assertRedirect(); $this->assertDatabaseHas('products_sku', [ 'id' => $sku->id, 'factory_number' => 'FN-999999', 'comment' => 'Updated by admin', ]); } public function test_manager_can_update_product_sku(): void { $product = Product::factory()->create(); $order = Order::factory()->create(); $sku = ProductSKU::factory()->create([ 'product_id' => $product->id, 'order_id' => $order->id, ]); $response = $this->actingAs($this->managerUser) ->post(route('product_sku.update', $sku), [ 'product_id' => $product->id, 'order_id' => $order->id, 'status' => 'needs', 'factory_number' => 'FN-777777', ]); $response->assertRedirect(); } // ==================== Export ==================== public function test_admin_can_export_mafs(): void { Bus::fake(); $response = $this->actingAs($this->adminUser) ->post(route('mafs.export')); $response->assertRedirect(route('product_sku.index')); $response->assertSessionHas('success'); Bus::assertDispatched(ExportMafJob::class); } }