| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace Tests\Feature;
- use App\Models\MafOrder;
- use App\Models\Product;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class MafOrderControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private User $adminUser;
- private User $managerUser;
- private User $assistantHeadUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
- $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
- $this->assistantHeadUser = User::factory()->create(['role' => Role::ASSISTANT_HEAD]);
- }
- // ==================== 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);
- }
- public function test_assistant_head_can_access_maf_orders_index(): void
- {
- $response = $this->actingAs($this->assistantHeadUser)
- ->get(route('maf_order.index'));
- $response->assertStatus(200);
- $response->assertViewIs('maf_orders.index');
- }
- // ==================== 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'));
- }
- public function test_set_order_in_stock_updates_all_rows_by_order_number(): void
- {
- $first = MafOrder::factory()->create([
- 'order_number' => 'MO-BULK-001',
- 'quantity' => 3,
- 'in_stock' => 0,
- 'status' => 'заказан',
- ]);
- $second = MafOrder::factory()->create([
- 'order_number' => 'MO-BULK-001',
- 'quantity' => 7,
- 'in_stock' => 1,
- 'status' => 'заказан',
- ]);
- $other = MafOrder::factory()->create([
- 'order_number' => 'MO-BULK-002',
- 'quantity' => 9,
- 'in_stock' => 2,
- 'status' => 'заказан',
- ]);
- $response = $this->actingAs($this->adminUser)
- ->post(route('maf_order.set_order_in_stock'), [
- 'bulk_order_number' => 'MO-BULK-001',
- ]);
- $response->assertRedirect(route('maf_order.index'));
- $this->assertDatabaseHas('maf_orders', [
- 'id' => $first->id,
- 'in_stock' => 3,
- 'status' => 'на складе',
- ]);
- $this->assertDatabaseHas('maf_orders', [
- 'id' => $second->id,
- 'in_stock' => 7,
- 'status' => 'на складе',
- ]);
- $this->assertDatabaseHas('maf_orders', [
- 'id' => $other->id,
- 'in_stock' => 2,
- 'status' => 'заказан',
- ]);
- }
- public function test_guest_cannot_set_order_in_stock(): void
- {
- $response = $this->post(route('maf_order.set_order_in_stock'), [
- 'bulk_order_number' => 'MO-BULK-001',
- ]);
- $response->assertRedirect(route('login'));
- }
- }
|