| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- declare(strict_types=1);
- namespace Tests\Feature;
- use App\Jobs\Export\ExportCommonCatalogJob;
- use App\Jobs\Import\ImportJob;
- use App\Models\CommonCatalogItem;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Bus;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class CommonCatalogControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected bool $seed = true;
- private User $admin;
- private User $manager;
- protected function setUp(): void
- {
- parent::setUp();
- $this->admin = User::factory()->create(['role' => Role::ADMIN]);
- $this->manager = User::factory()->create(['role' => Role::MANAGER]);
- }
- public function test_guest_cannot_access_common_catalog(): void
- {
- $this->get(route('common-catalog.index'))->assertRedirect(route('login'));
- }
- public function test_standard_authenticated_roles_can_view_year_independent_catalog(): void
- {
- $item = CommonCatalogItem::factory()->create([
- 'article' => '0254',
- 'calculator_name' => 'Оборудование для благоустройства',
- ]);
- $this->actingAs($this->manager)
- ->withSession(['year' => 2021])
- ->get(route('common-catalog.index'))
- ->assertOk()
- ->assertViewIs('common_catalog.index')
- ->assertSee($item->article)
- ->assertSee($item->calculator_name);
- }
- public function test_manager_cannot_mutate_common_catalog(): void
- {
- $this->actingAs($this->manager)
- ->post(route('common-catalog.store'), $this->validData())
- ->assertForbidden();
- }
- public function test_admin_can_create_update_and_delete_item(): void
- {
- $this->actingAs($this->admin)
- ->post(route('common-catalog.store'), $this->validData())
- ->assertRedirect();
- $item = CommonCatalogItem::query()->where('article', '0007')->firstOrFail();
- $updated = $this->validData();
- $updated['calculator_name'] = 'Обновлённое наименование';
- $this->actingAs($this->admin)
- ->put(route('common-catalog.update', $item), $updated)
- ->assertRedirect();
- $this->assertDatabaseHas('common_catalog_items', [
- 'id' => $item->id,
- 'article' => '0007',
- 'calculator_name' => 'Обновлённое наименование',
- ]);
- $this->actingAs($this->admin)
- ->delete(route('common-catalog.destroy', $item))
- ->assertRedirect(route('common-catalog.index'));
- $this->assertSoftDeleted('common_catalog_items', ['id' => $item->id]);
- }
- public function test_article_and_calculator_name_are_required_and_article_is_unique(): void
- {
- CommonCatalogItem::factory()->create(['article' => '0007']);
- $this->actingAs($this->admin)
- ->post(route('common-catalog.store'), [
- 'article' => '0007',
- 'calculator_name' => '',
- ])
- ->assertSessionHasErrors(['article', 'calculator_name']);
- }
- public function test_item_card_contains_technical_description_and_calculation_placeholders(): void
- {
- $item = CommonCatalogItem::factory()->create();
- $this->actingAs($this->admin)
- ->get(route('common-catalog.show', $item))
- ->assertOk()
- ->assertSeeText('Техническое описание')
- ->assertSeeText('Калькуляция');
- }
- public function test_admin_can_upload_and_delete_image(): void
- {
- Storage::fake('public');
- $item = CommonCatalogItem::factory()->create();
- $this->actingAs($this->admin)
- ->post(route('common-catalog.image.upload', $item), [
- 'image' => UploadedFile::fake()->image('item.png', 200, 200),
- ])
- ->assertRedirect();
- $item->refresh();
- $this->assertNotNull($item->imageFile);
- Storage::disk('public')->assertExists($item->imageFile->path);
- $fileId = $item->imageFile->id;
- $this->actingAs($this->admin)
- ->delete(route('common-catalog.image.delete', $item))
- ->assertRedirect();
- $this->assertDatabaseMissing('files', ['id' => $fileId]);
- $this->assertNull($item->refresh()->image_file_id);
- }
- public function test_admin_can_upload_and_delete_multiple_documents(): void
- {
- Storage::fake('public');
- $item = CommonCatalogItem::factory()->create();
- foreach (['certificate.pdf', 'manual.pdf'] as $filename) {
- $this->actingAs($this->admin)
- ->post(route('common-catalog.documents.upload', $item), [
- 'document' => UploadedFile::fake()->create($filename, 20, 'application/pdf'),
- ])
- ->assertRedirect();
- }
- $this->assertCount(2, $item->refresh()->documents);
- $document = $item->documents->first();
- $this->actingAs($this->admin)
- ->delete(route('common-catalog.documents.delete', [$item, $document]))
- ->assertRedirect();
- $this->assertCount(1, $item->refresh()->documents);
- $this->assertDatabaseMissing('files', ['id' => $document->id]);
- }
- public function test_import_and_export_are_queued(): void
- {
- Storage::fake('upload');
- Bus::fake([ImportJob::class, ExportCommonCatalogJob::class]);
- $this->actingAs($this->admin)
- ->post(route('common-catalog.import'), [
- 'import_file' => UploadedFile::fake()->create(
- 'common-catalog.xlsx',
- 20,
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- ),
- ])
- ->assertRedirect(route('common-catalog.index'));
- $this->assertDatabaseHas('imports', [
- 'type' => 'common_catalog',
- 'year' => null,
- 'user_id' => $this->admin->id,
- ]);
- Bus::assertDispatched(ImportJob::class);
- $this->actingAs($this->admin)
- ->post(route('common-catalog.export'))
- ->assertRedirect(route('common-catalog.index'));
- Bus::assertDispatched(ExportCommonCatalogJob::class);
- }
- private function validData(): array
- {
- return [
- 'article' => '0007',
- 'calculator_name' => "0007\nИгровой комплекс",
- 'kind' => 'Игровое оборудование',
- 'dimensions' => '2 × 3 × 1,5 м',
- 'fall_height' => 1.2,
- 'dimension_unit' => 'м',
- 'weight' => 350.5,
- 'volume' => 4.25,
- 'places' => 3,
- 'age_group' => '3+',
- 'max_users' => 8,
- 'unit' => 'шт.',
- 'series' => 'Двор',
- 'trademark' => 'Наш Двор',
- ];
- }
- }
|