adminUser = User::factory()->create(['role' => Role::ADMIN]); $this->managerUser = User::factory()->create(['role' => Role::MANAGER]); } private function validProductData(): array { return [ 'article' => 'TEST-001', 'name_tz' => 'Тестовая горка', 'type_tz' => 'Горка', 'nomenclature_number' => '123456', 'sizes' => '1000x2000x1500', 'manufacturer' => 'ООО Тест', 'manufacturer_name' => 'ООО Тест Производитель', 'unit' => 'шт', 'type' => 'standard', 'product_price' => 100000, 'installation_price' => 20000, 'total_price' => 120000, 'note' => 'тестовое примечание', 'year' => date('Y'), ]; } // --- Guest redirects (3) --- public function test_guest_cannot_access_catalog_index(): void { $response = $this->get(route('catalog.index')); $response->assertRedirect(route('login')); } public function test_guest_cannot_access_catalog_create(): void { $response = $this->get(route('catalog.create')); $response->assertRedirect(route('login')); } public function test_guest_cannot_store_product(): void { $response = $this->post(route('catalog.store'), $this->validProductData()); $response->assertRedirect(route('login')); } // --- Authorization (2) --- public function test_manager_cannot_create_product(): void { $response = $this->actingAs($this->managerUser) ->post(route('catalog.store'), $this->validProductData()); $response->assertForbidden(); } public function test_manager_cannot_delete_product(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->managerUser) ->delete(route('catalog.delete', $product)); $response->assertForbidden(); } // --- Index (2) --- public function test_admin_can_access_catalog_index(): void { $response = $this->actingAs($this->adminUser) ->get(route('catalog.index')); $response->assertOk(); $response->assertViewIs('catalog.index'); } public function test_manager_can_access_catalog_index(): void { $response = $this->actingAs($this->managerUser) ->get(route('catalog.index')); $response->assertOk(); $response->assertViewIs('catalog.index'); } // --- Create form (1) --- public function test_admin_can_access_catalog_create(): void { $response = $this->actingAs($this->adminUser) ->get(route('catalog.create')); $response->assertOk(); $response->assertViewIs('catalog.edit'); } // --- Show (2) --- public function test_admin_can_view_product(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->adminUser) ->get(route('catalog.show', $product)); $response->assertOk(); $response->assertViewIs('catalog.edit'); } public function test_manager_can_view_product(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->managerUser) ->get(route('catalog.show', $product)); $response->assertOk(); $response->assertViewIs('catalog.edit'); } // --- Store (3) --- public function test_admin_can_create_product(): void { $data = $this->validProductData(); $response = $this->actingAs($this->adminUser) ->post(route('catalog.store'), $data); $response->assertRedirect(); $this->assertDatabaseHas('products', ['article' => $data['article']]); } public function test_store_product_requires_article(): void { $data = $this->validProductData(); unset($data['article']); $response = $this->actingAs($this->adminUser) ->post(route('catalog.store'), $data); $response->assertSessionHasErrors('article'); } public function test_store_product_requires_name_tz(): void { $data = $this->validProductData(); unset($data['name_tz']); $response = $this->actingAs($this->adminUser) ->post(route('catalog.store'), $data); $response->assertSessionHasErrors('name_tz'); } // --- Update (2) --- public function test_admin_can_update_product(): void { $product = Product::factory()->create(); $data = $this->validProductData(); $data['article'] = 'UPDATED-001'; $data['name_tz'] = 'Обновлённая горка'; $response = $this->actingAs($this->adminUser) ->post(route('catalog.update', $product), $data); $response->assertRedirect(); $this->assertDatabaseHas('products', [ 'id' => $product->id, 'article' => 'UPDATED-001', 'name_tz' => 'Обновлённая горка', ]); } public function test_manager_cannot_update_product(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->managerUser) ->post(route('catalog.update', $product), $this->validProductData()); $response->assertForbidden(); } // --- Delete (1) --- public function test_admin_can_delete_product(): void { $product = Product::factory()->create(); $response = $this->actingAs($this->adminUser) ->delete(route('catalog.delete', $product)); $response->assertRedirect(); $this->assertSoftDeleted('products', ['id' => $product->id]); } }