ProductionOrderNotificationTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Feature;
  4. use App\Enums\ProductionOrderExecutionType;
  5. use App\Enums\ProductionOrderNotificationEvent;
  6. use App\Enums\ProductionOrderStatus;
  7. use App\Jobs\SendUserNotificationChannelJob;
  8. use App\Models\CommonCatalogItem;
  9. use App\Models\ProductionOrder;
  10. use App\Models\User;
  11. use App\Models\UserNotification;
  12. use App\Models\UserNotificationSetting;
  13. use Illuminate\Foundation\Testing\RefreshDatabase;
  14. use Illuminate\Support\Facades\Queue;
  15. use Tests\TestCase;
  16. class ProductionOrderNotificationTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. protected bool $seed = true;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. Queue::fake();
  24. }
  25. public function test_order_creation_notifies_manager_when_event_channel_is_enabled(): void
  26. {
  27. $admin = User::factory()->admin()->create();
  28. $manager = User::factory()->manager()->create();
  29. $catalogItem = CommonCatalogItem::factory()->create();
  30. $this->enableEvent($manager, ProductionOrderNotificationEvent::Created);
  31. $this->actingAs($admin)
  32. ->post(route('schedule.orders.store'), [
  33. 'order_number' => 'УВ-001',
  34. 'order_year' => 2026,
  35. 'customer_name' => 'Заказчик',
  36. 'object_address' => 'ул. Тестовая, 1',
  37. 'invoice_number' => 'СЧ-1',
  38. 'payment_date' => '2026-09-01',
  39. 'supply_working_days' => 5,
  40. 'status' => ProductionOrderStatus::Placed->value,
  41. 'execution_type' => ProductionOrderExecutionType::Delivery->value,
  42. 'manager_id' => $manager->id,
  43. 'items' => [[
  44. 'common_catalog_item_id' => $catalogItem->id,
  45. 'order_item_number' => 'МАФ-1',
  46. ]],
  47. ])
  48. ->assertRedirect();
  49. $this->assertDatabaseHas('user_notifications', [
  50. 'user_id' => $manager->id,
  51. 'type' => UserNotification::TYPE_PRODUCTION_ORDER,
  52. 'event' => ProductionOrderNotificationEvent::Created->value,
  53. ]);
  54. Queue::assertPushed(SendUserNotificationChannelJob::class);
  55. }
  56. public function test_delivery_and_installation_notify_bound_driver_and_brigadier(): void
  57. {
  58. $admin = User::factory()->admin()->create();
  59. $manager = User::factory()->manager()->create();
  60. $driver = User::factory()->driver()->create();
  61. $brigadier = User::factory()->brigadier()->create();
  62. $order = ProductionOrder::factory()->create(['manager_id' => $manager->id]);
  63. $this->enableEvent($driver, ProductionOrderNotificationEvent::DeliveryAdded);
  64. $this->enableEvent($brigadier, ProductionOrderNotificationEvent::InstallationAdded);
  65. $this->actingAs($admin)
  66. ->post(route('schedule.orders.deliveries.store', $order), [
  67. 'delivery_date' => '2026-09-10',
  68. 'driver_id' => $driver->id,
  69. 'has_passports_and_certificates' => true,
  70. ])
  71. ->assertRedirect();
  72. $this->actingAs($admin)
  73. ->post(route('schedule.orders.installations.store', $order), [
  74. 'installation_date' => '2026-09-12',
  75. 'installation_days' => 2,
  76. 'brigadier_id' => $brigadier->id,
  77. ])
  78. ->assertRedirect();
  79. $this->assertDatabaseHas('user_notifications', [
  80. 'user_id' => $driver->id,
  81. 'event' => ProductionOrderNotificationEvent::DeliveryAdded->value,
  82. ]);
  83. $this->assertDatabaseHas('user_notifications', [
  84. 'user_id' => $brigadier->id,
  85. 'event' => ProductionOrderNotificationEvent::InstallationAdded->value,
  86. ]);
  87. $this->assertDatabaseMissing('user_notifications', [
  88. 'user_id' => $manager->id,
  89. 'type' => UserNotification::TYPE_PRODUCTION_ORDER,
  90. ]);
  91. }
  92. public function test_admin_can_configure_production_order_events_for_user(): void
  93. {
  94. $admin = User::factory()->admin()->create();
  95. $manager = User::factory()->manager()->create();
  96. $this->actingAs($admin)
  97. ->get(route('user.show', $manager))
  98. ->assertOk()
  99. ->assertSee('График заказов')
  100. ->assertSee('Добавлена доставка');
  101. $this->actingAs($admin)
  102. ->post(route('user.store'), [
  103. 'id' => $manager->id,
  104. 'name' => $manager->name,
  105. 'email' => $manager->email,
  106. 'role_id' => $manager->role_id,
  107. 'notification_settings' => [
  108. 'production_orders' => [
  109. ProductionOrderNotificationEvent::StatusChanged->value => ['browser' => '1'],
  110. ],
  111. ],
  112. ])
  113. ->assertRedirect(route('user.index'));
  114. $settings = UserNotificationSetting::query()->where('user_id', $manager->id)->firstOrFail();
  115. $this->assertTrue($settings->production_order_settings['status_changed']['browser']);
  116. $this->assertFalse($settings->production_order_settings['created']['browser']);
  117. }
  118. private function enableEvent(User $user, ProductionOrderNotificationEvent $event): void
  119. {
  120. UserNotificationSetting::query()->create([
  121. 'user_id' => $user->id,
  122. 'production_order_settings' => [
  123. $event->value => ['browser' => true, 'push' => false, 'email' => false],
  124. ],
  125. ]);
  126. }
  127. }