adminUser = User::factory()->create(['role' => Role::ADMIN]); $this->managerUser = User::factory()->create(['role' => Role::MANAGER]); } // ==================== Authentication ==================== public function test_guest_cannot_access_maf_orders_index(): void { $response = $this->get(route('maf_order.index')); $response->assertRedirect(route('login')); } public function test_authenticated_admin_can_access_maf_orders_index(): void { $response = $this->actingAs($this->adminUser) ->get(route('maf_order.index')); $response->assertStatus(200); $response->assertViewIs('maf_orders.index'); } public function test_manager_cannot_access_maf_orders_index(): void { $response = $this->actingAs($this->managerUser) ->get(route('maf_order.index')); $response->assertStatus(403); } // ==================== Index ==================== public function test_maf_orders_index_displays_orders(): void { MafOrder::factory()->create(); $response = $this->actingAs($this->adminUser) ->get(route('maf_order.index')); $response->assertStatus(200); } // ==================== Store (create) ==================== public function test_can_create_maf_order(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->adminUser) ->post(route('maf_order.store'), [ 'product_id' => $product->id, 'quantity' => 5, 'order_number' => 'MO-TEST-001', ]); $response->assertRedirect(); $this->assertDatabaseHas('maf_orders', [ 'product_id' => $product->id, 'quantity' => 5, 'order_number' => 'MO-TEST-001', 'in_stock' => 0, ]); } public function test_store_maf_order_requires_product_id(): void { $response = $this->actingAs($this->adminUser) ->post(route('maf_order.store'), [ 'quantity' => 5, ]); $response->assertSessionHasErrors('product_id'); } public function test_store_maf_order_requires_quantity(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->adminUser) ->post(route('maf_order.store'), [ 'product_id' => $product->id, ]); $response->assertSessionHasErrors('quantity'); } public function test_guest_cannot_create_maf_order(): void { $product = Product::factory()->create(); $response = $this->post(route('maf_order.store'), [ 'product_id' => $product->id, 'quantity' => 3, ]); $response->assertRedirect(route('login')); } // ==================== Show ==================== public function test_can_view_maf_order_details(): void { $mafOrder = MafOrder::factory()->create(); $response = $this->actingAs($this->adminUser) ->get(route('maf_order.show', $mafOrder)); $response->assertStatus(200); $response->assertViewIs('maf_orders.edit'); } public function test_guest_cannot_view_maf_order_details(): void { $mafOrder = MafOrder::factory()->create(); $response = $this->get(route('maf_order.show', $mafOrder)); $response->assertRedirect(route('login')); } // ==================== Update ==================== public function test_can_update_maf_order(): void { $product = Product::factory()->create(); $mafOrder = MafOrder::factory()->create([ 'product_id' => $product->id, 'quantity' => 3, ]); $response = $this->actingAs($this->adminUser) ->post(route('maf_order.update', $mafOrder), [ 'product_id' => $product->id, 'quantity' => 10, 'order_number' => 'MO-UPDATED', ]); $response->assertRedirect(); $this->assertDatabaseHas('maf_orders', [ 'id' => $mafOrder->id, 'quantity' => 10, ]); } public function test_guest_cannot_update_maf_order(): void { $product = Product::factory()->create(); $mafOrder = MafOrder::factory()->create(['product_id' => $product->id]); $response = $this->post(route('maf_order.update', $mafOrder), [ 'product_id' => $product->id, 'quantity' => 10, ]); $response->assertRedirect(route('login')); } // ==================== Destroy ==================== public function test_can_delete_maf_order(): void { $mafOrder = MafOrder::factory()->create(); $mafOrderId = $mafOrder->id; $response = $this->actingAs($this->adminUser) ->delete(route('maf_order.delete', $mafOrder)); $response->assertRedirect(); $this->assertSoftDeleted('maf_orders', ['id' => $mafOrderId]); } public function test_guest_cannot_delete_maf_order(): void { $mafOrder = MafOrder::factory()->create(); $response = $this->delete(route('maf_order.delete', $mafOrder)); $response->assertRedirect(route('login')); } // ==================== SetInStock ==================== public function test_set_in_stock_updates_stock_and_status(): void { $mafOrder = MafOrder::factory()->create([ 'quantity' => 8, 'in_stock' => 0, 'status' => 'active', ]); $response = $this->actingAs($this->adminUser) ->post(route('maf_order.set_in_stock', $mafOrder)); $response->assertRedirect(route('maf_order.show', $mafOrder)); $this->assertDatabaseHas('maf_orders', [ 'id' => $mafOrder->id, 'in_stock' => 8, 'status' => 'на складе', ]); } public function test_guest_cannot_set_in_stock(): void { $mafOrder = MafOrder::factory()->create(['quantity' => 5]); $response = $this->post(route('maf_order.set_in_stock', $mafOrder)); $response->assertRedirect(route('login')); } }