| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- declare(strict_types=1);
- namespace Tests\Feature;
- use App\Enums\ProductionOrderExecutionType;
- use App\Enums\ProductionOrderNotificationEvent;
- use App\Enums\ProductionOrderStatus;
- use App\Jobs\SendUserNotificationChannelJob;
- use App\Models\CommonCatalogItem;
- use App\Models\ProductionOrder;
- use App\Models\User;
- use App\Models\UserNotification;
- use App\Models\UserNotificationSetting;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Queue;
- use Tests\TestCase;
- class ProductionOrderNotificationTest extends TestCase
- {
- use RefreshDatabase;
- protected bool $seed = true;
- protected function setUp(): void
- {
- parent::setUp();
- Queue::fake();
- }
- public function test_order_creation_notifies_manager_when_event_channel_is_enabled(): void
- {
- $admin = User::factory()->admin()->create();
- $manager = User::factory()->manager()->create();
- $catalogItem = CommonCatalogItem::factory()->create();
- $this->enableEvent($manager, ProductionOrderNotificationEvent::Created);
- $this->actingAs($admin)
- ->post(route('schedule.orders.store'), [
- 'order_number' => 'УВ-001',
- 'order_year' => 2026,
- 'customer_name' => 'Заказчик',
- 'object_address' => 'ул. Тестовая, 1',
- 'invoice_number' => 'СЧ-1',
- 'payment_date' => '2026-09-01',
- 'supply_working_days' => 5,
- 'status' => ProductionOrderStatus::Placed->value,
- 'execution_type' => ProductionOrderExecutionType::Delivery->value,
- 'manager_id' => $manager->id,
- 'items' => [[
- 'common_catalog_item_id' => $catalogItem->id,
- 'order_item_number' => 'МАФ-1',
- ]],
- ])
- ->assertRedirect();
- $this->assertDatabaseHas('user_notifications', [
- 'user_id' => $manager->id,
- 'type' => UserNotification::TYPE_PRODUCTION_ORDER,
- 'event' => ProductionOrderNotificationEvent::Created->value,
- ]);
- Queue::assertPushed(SendUserNotificationChannelJob::class);
- }
- public function test_delivery_and_installation_notify_bound_driver_and_brigadier(): void
- {
- $admin = User::factory()->admin()->create();
- $manager = User::factory()->manager()->create();
- $driver = User::factory()->driver()->create();
- $brigadier = User::factory()->brigadier()->create();
- $order = ProductionOrder::factory()->create(['manager_id' => $manager->id]);
- $this->enableEvent($driver, ProductionOrderNotificationEvent::DeliveryAdded);
- $this->enableEvent($brigadier, ProductionOrderNotificationEvent::InstallationAdded);
- $this->actingAs($admin)
- ->post(route('schedule.orders.deliveries.store', $order), [
- 'delivery_date' => '2026-09-10',
- 'driver_id' => $driver->id,
- 'has_passports_and_certificates' => true,
- ])
- ->assertRedirect();
- $this->actingAs($admin)
- ->post(route('schedule.orders.installations.store', $order), [
- 'installation_date' => '2026-09-12',
- 'installation_days' => 2,
- 'brigadier_id' => $brigadier->id,
- ])
- ->assertRedirect();
- $this->assertDatabaseHas('user_notifications', [
- 'user_id' => $driver->id,
- 'event' => ProductionOrderNotificationEvent::DeliveryAdded->value,
- ]);
- $this->assertDatabaseHas('user_notifications', [
- 'user_id' => $brigadier->id,
- 'event' => ProductionOrderNotificationEvent::InstallationAdded->value,
- ]);
- $this->assertDatabaseMissing('user_notifications', [
- 'user_id' => $manager->id,
- 'type' => UserNotification::TYPE_PRODUCTION_ORDER,
- ]);
- }
- public function test_admin_can_configure_production_order_events_for_user(): void
- {
- $admin = User::factory()->admin()->create();
- $manager = User::factory()->manager()->create();
- $this->actingAs($admin)
- ->get(route('user.show', $manager))
- ->assertOk()
- ->assertSee('График заказов')
- ->assertSee('Добавлена доставка');
- $this->actingAs($admin)
- ->post(route('user.store'), [
- 'id' => $manager->id,
- 'name' => $manager->name,
- 'email' => $manager->email,
- 'role_id' => $manager->role_id,
- 'notification_settings' => [
- 'production_orders' => [
- ProductionOrderNotificationEvent::StatusChanged->value => ['browser' => '1'],
- ],
- ],
- ])
- ->assertRedirect(route('user.index'));
- $settings = UserNotificationSetting::query()->where('user_id', $manager->id)->firstOrFail();
- $this->assertTrue($settings->production_order_settings['status_changed']['browser']);
- $this->assertFalse($settings->production_order_settings['created']['browser']);
- }
- private function enableEvent(User $user, ProductionOrderNotificationEvent $event): void
- {
- UserNotificationSetting::query()->create([
- 'user_id' => $user->id,
- 'production_order_settings' => [
- $event->value => ['browser' => true, 'push' => false, 'email' => false],
- ],
- ]);
- }
- }
|