| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace Tests\Feature;
- use App\Models\Product;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class ProductControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected bool $seed = true;
- protected User $adminUser;
- protected User $managerUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->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]);
- }
- }
|