ProductionOrderFileAndChatControllerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Feature;
  4. use App\Models\ChatMessage;
  5. use App\Models\CommonCatalogItem;
  6. use App\Models\File;
  7. use App\Models\ProductionOrder;
  8. use App\Models\ProductionOrderItem;
  9. use App\Models\User;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Http\UploadedFile;
  12. use Illuminate\Support\Facades\Storage;
  13. use Tests\TestCase;
  14. class ProductionOrderFileAndChatControllerTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. protected bool $seed = true;
  18. private User $admin;
  19. private User $assistant;
  20. private User $manager;
  21. private ProductionOrder $order;
  22. private ProductionOrderItem $item;
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. Storage::fake('public');
  27. $this->admin = User::factory()->admin()->create();
  28. $this->assistant = User::factory()->assistantHead()->create();
  29. $this->manager = User::factory()->manager()->create();
  30. $this->order = ProductionOrder::factory()->create(['manager_id' => $this->manager->id]);
  31. $this->item = ProductionOrderItem::factory()->create([
  32. 'production_order_id' => $this->order->id,
  33. 'common_catalog_item_id' => CommonCatalogItem::factory(),
  34. ]);
  35. }
  36. public function test_assistant_can_upload_documents_photos_and_item_passport(): void
  37. {
  38. $this->actingAs($this->assistant)
  39. ->post(route('schedule.orders.documents.store', $this->order), [
  40. 'documents' => [UploadedFile::fake()->create('instruction.pdf', 10, 'application/pdf')],
  41. ])
  42. ->assertRedirect();
  43. $this->actingAs($this->assistant)
  44. ->post(route('schedule.orders.photos.store', $this->order), [
  45. 'photos' => [UploadedFile::fake()->image('object.jpg', 100, 100)],
  46. ])
  47. ->assertRedirect();
  48. $this->actingAs($this->assistant)
  49. ->post(route('schedule.orders.items.passport.store', [$this->order, $this->item]), [
  50. 'passport' => UploadedFile::fake()->create('passport.pdf', 10, 'application/pdf'),
  51. ])
  52. ->assertRedirect();
  53. $this->assertSame(1, $this->order->documents()->count());
  54. $this->assertSame(1, $this->order->photos()->count());
  55. $this->assertNotNull($this->item->fresh()->passport_file_id);
  56. foreach (File::query()->get() as $file) {
  57. Storage::disk('public')->assertExists($file->path);
  58. }
  59. }
  60. public function test_manager_can_chat_but_cannot_manage_order_files(): void
  61. {
  62. $this->actingAs($this->manager)
  63. ->post(route('schedule.orders.chat-messages.store', $this->order), [
  64. 'message' => 'Заказ принят в работу',
  65. 'attachments' => [UploadedFile::fake()->create('note.txt', 1, 'text/plain')],
  66. ])
  67. ->assertRedirect()
  68. ->assertSessionHas('success');
  69. $message = ChatMessage::query()->where('production_order_id', $this->order->id)->firstOrFail();
  70. $this->assertSame('Заказ принят в работу', $message->message);
  71. $this->assertSame(1, $message->files()->count());
  72. $this->actingAs($this->manager)
  73. ->post(route('schedule.orders.documents.store', $this->order), [
  74. 'documents' => [UploadedFile::fake()->create('forbidden.pdf', 1, 'application/pdf')],
  75. ])
  76. ->assertForbidden();
  77. $this->actingAs($this->manager)
  78. ->post(route('schedule.orders.items.passport.store', [$this->order, $this->item]), [
  79. 'passport' => UploadedFile::fake()->create('forbidden.pdf', 1, 'application/pdf'),
  80. ])
  81. ->assertForbidden();
  82. }
  83. public function test_order_card_shows_mime_icons_image_previews_and_chat(): void
  84. {
  85. $pdf = File::factory()->pdf()->create(['user_id' => $this->admin->id]);
  86. $image = File::factory()->image()->create(['user_id' => $this->admin->id]);
  87. $passport = File::factory()->image()->create(['user_id' => $this->admin->id]);
  88. $this->order->documents()->attach([$pdf->id, $image->id]);
  89. $this->order->photos()->attach($image->id);
  90. $this->item->update(['passport_file_id' => $passport->id]);
  91. ChatMessage::query()->create([
  92. 'production_order_id' => $this->order->id,
  93. 'user_id' => $this->manager->id,
  94. 'message' => 'Сообщение в чате заказа',
  95. ]);
  96. $this->actingAs($this->manager)
  97. ->get(route('schedule.orders.show', $this->order))
  98. ->assertOk()
  99. ->assertSee('bi-file-earmark-pdf', false)
  100. ->assertSee('bi-file-earmark-image', false)
  101. ->assertSee('data-toggle="lightbox"', false)
  102. ->assertSee('Сообщение в чате заказа')
  103. ->assertSee(route('schedule.orders.chat-messages.store', $this->order), false)
  104. ->assertDontSee(route('schedule.orders.documents.store', $this->order), false);
  105. }
  106. public function test_file_and_chat_routes_reject_resources_from_another_order(): void
  107. {
  108. $otherOrder = ProductionOrder::factory()->create(['manager_id' => $this->manager->id]);
  109. $otherItem = ProductionOrderItem::factory()->create([
  110. 'production_order_id' => $otherOrder->id,
  111. 'common_catalog_item_id' => CommonCatalogItem::factory(),
  112. ]);
  113. $message = ChatMessage::query()->create([
  114. 'production_order_id' => $otherOrder->id,
  115. 'user_id' => $this->admin->id,
  116. 'message' => 'Чужое сообщение',
  117. ]);
  118. $this->actingAs($this->admin)
  119. ->post(route('schedule.orders.items.passport.store', [$this->order, $otherItem]), [
  120. 'passport' => UploadedFile::fake()->create('passport.pdf', 1, 'application/pdf'),
  121. ])
  122. ->assertNotFound();
  123. $this->actingAs($this->admin)
  124. ->delete(route('schedule.orders.chat-messages.destroy', [$this->order, $message]))
  125. ->assertNotFound();
  126. }
  127. }