ChatMessageControllerTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\ChatMessage;
  4. use App\Models\Order;
  5. use App\Models\Reclamation;
  6. use App\Models\Role;
  7. use App\Models\User;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Tests\TestCase;
  10. class ChatMessageControllerTest extends TestCase
  11. {
  12. use RefreshDatabase;
  13. protected $seed = true;
  14. public function test_brigadier_can_send_order_chat_message_without_manual_recipient_selection(): void
  15. {
  16. $admin = User::factory()->create(['role' => Role::ADMIN]);
  17. $manager = User::factory()->create(['role' => Role::MANAGER]);
  18. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  19. $order = Order::factory()->create([
  20. 'user_id' => $manager->id,
  21. 'brigadier_id' => $brigadier->id,
  22. 'order_status_id' => Order::STATUS_IN_MOUNT,
  23. ]);
  24. $response = $this->actingAs($brigadier)
  25. ->post(route('order.chat-messages.store', $order), [
  26. 'message' => 'Сообщение бригадира по площадке',
  27. 'notification_type' => ChatMessage::NOTIFICATION_RESPONSIBLES,
  28. ]);
  29. $response->assertRedirect();
  30. $response->assertSessionHas('success');
  31. $message = ChatMessage::query()->where('order_id', $order->id)->first();
  32. $this->assertNotNull($message);
  33. $this->assertSame(ChatMessage::NOTIFICATION_RESPONSIBLES, $message->notification_type);
  34. $recipientIds = $message->notifiedUsers->pluck('id')->all();
  35. $this->assertContains($admin->id, $recipientIds);
  36. $this->assertContains($manager->id, $recipientIds);
  37. $this->assertNotContains($brigadier->id, $recipientIds);
  38. }
  39. public function test_brigadier_can_send_reclamation_chat_message_without_manual_recipient_selection(): void
  40. {
  41. $admin = User::factory()->create(['role' => Role::ADMIN]);
  42. $manager = User::factory()->create(['role' => Role::MANAGER]);
  43. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  44. $reclamation = Reclamation::factory()->create([
  45. 'user_id' => $manager->id,
  46. 'brigadier_id' => $brigadier->id,
  47. 'status_id' => Reclamation::STATUS_IN_WORK,
  48. ]);
  49. $response = $this->actingAs($brigadier)
  50. ->post(route('reclamations.chat-messages.store', $reclamation), [
  51. 'message' => 'Сообщение бригадира по рекламации',
  52. 'notification_type' => ChatMessage::NOTIFICATION_RESPONSIBLES,
  53. ]);
  54. $response->assertRedirect();
  55. $response->assertSessionHas('success');
  56. $message = ChatMessage::query()->where('reclamation_id', $reclamation->id)->first();
  57. $this->assertNotNull($message);
  58. $this->assertSame(ChatMessage::NOTIFICATION_RESPONSIBLES, $message->notification_type);
  59. $recipientIds = $message->notifiedUsers->pluck('id')->all();
  60. $this->assertContains($admin->id, $recipientIds);
  61. $this->assertContains($manager->id, $recipientIds);
  62. $this->assertNotContains($brigadier->id, $recipientIds);
  63. }
  64. }