ProductControllerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Product;
  4. use App\Models\Role;
  5. use App\Models\User;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Tests\TestCase;
  8. class ProductControllerTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. protected bool $seed = true;
  12. protected User $adminUser;
  13. protected User $managerUser;
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  18. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  19. }
  20. private function validProductData(): array
  21. {
  22. return [
  23. 'article' => 'TEST-001',
  24. 'name_tz' => 'Тестовая горка',
  25. 'type_tz' => 'Горка',
  26. 'nomenclature_number' => '123456',
  27. 'sizes' => '1000x2000x1500',
  28. 'manufacturer' => 'ООО Тест',
  29. 'manufacturer_name' => 'ООО Тест Производитель',
  30. 'unit' => 'шт',
  31. 'type' => 'standard',
  32. 'product_price' => 100000,
  33. 'installation_price' => 20000,
  34. 'total_price' => 120000,
  35. 'note' => 'тестовое примечание',
  36. 'year' => date('Y'),
  37. ];
  38. }
  39. // --- Guest redirects (3) ---
  40. public function test_guest_cannot_access_catalog_index(): void
  41. {
  42. $response = $this->get(route('catalog.index'));
  43. $response->assertRedirect(route('login'));
  44. }
  45. public function test_guest_cannot_access_catalog_create(): void
  46. {
  47. $response = $this->get(route('catalog.create'));
  48. $response->assertRedirect(route('login'));
  49. }
  50. public function test_guest_cannot_store_product(): void
  51. {
  52. $response = $this->post(route('catalog.store'), $this->validProductData());
  53. $response->assertRedirect(route('login'));
  54. }
  55. // --- Authorization (2) ---
  56. public function test_manager_cannot_create_product(): void
  57. {
  58. $response = $this->actingAs($this->managerUser)
  59. ->post(route('catalog.store'), $this->validProductData());
  60. $response->assertForbidden();
  61. }
  62. public function test_manager_cannot_delete_product(): void
  63. {
  64. $product = Product::factory()->create();
  65. $response = $this->actingAs($this->managerUser)
  66. ->delete(route('catalog.delete', $product));
  67. $response->assertForbidden();
  68. }
  69. // --- Index (2) ---
  70. public function test_admin_can_access_catalog_index(): void
  71. {
  72. $response = $this->actingAs($this->adminUser)
  73. ->get(route('catalog.index'));
  74. $response->assertOk();
  75. $response->assertViewIs('catalog.index');
  76. }
  77. public function test_manager_can_access_catalog_index(): void
  78. {
  79. $response = $this->actingAs($this->managerUser)
  80. ->get(route('catalog.index'));
  81. $response->assertOk();
  82. $response->assertViewIs('catalog.index');
  83. }
  84. // --- Create form (1) ---
  85. public function test_admin_can_access_catalog_create(): void
  86. {
  87. $response = $this->actingAs($this->adminUser)
  88. ->get(route('catalog.create'));
  89. $response->assertOk();
  90. $response->assertViewIs('catalog.edit');
  91. }
  92. // --- Show (2) ---
  93. public function test_admin_can_view_product(): void
  94. {
  95. $product = Product::factory()->create();
  96. $response = $this->actingAs($this->adminUser)
  97. ->get(route('catalog.show', $product));
  98. $response->assertOk();
  99. $response->assertViewIs('catalog.edit');
  100. }
  101. public function test_manager_can_view_product(): void
  102. {
  103. $product = Product::factory()->create();
  104. $response = $this->actingAs($this->managerUser)
  105. ->get(route('catalog.show', $product));
  106. $response->assertOk();
  107. $response->assertViewIs('catalog.edit');
  108. }
  109. public function test_product_show_uses_nav_context_back_url(): void
  110. {
  111. $product = Product::factory()->create();
  112. $reclamation = \App\Models\Reclamation::factory()->create();
  113. $indexResponse = $this->actingAs($this->adminUser)
  114. ->get(route('reclamations.index'));
  115. $nav = $indexResponse->viewData('nav');
  116. $this->actingAs($this->adminUser)
  117. ->get(route('reclamations.show', [
  118. 'reclamation' => $reclamation,
  119. 'nav' => $nav,
  120. ]))
  121. ->assertOk();
  122. $response = $this->actingAs($this->adminUser)
  123. ->get(route('catalog.show', [
  124. 'product' => $product,
  125. 'nav' => $nav,
  126. ]));
  127. $response->assertOk();
  128. $response->assertViewHas('nav', $nav);
  129. $response->assertViewHas('back_url', route('reclamations.show', [
  130. 'reclamation' => $reclamation,
  131. ]));
  132. }
  133. // --- Store (3) ---
  134. public function test_admin_can_create_product(): void
  135. {
  136. $data = $this->validProductData();
  137. $response = $this->actingAs($this->adminUser)
  138. ->post(route('catalog.store'), $data);
  139. $response->assertRedirect();
  140. $this->assertDatabaseHas('products', ['article' => $data['article']]);
  141. }
  142. public function test_store_product_requires_article(): void
  143. {
  144. $data = $this->validProductData();
  145. unset($data['article']);
  146. $response = $this->actingAs($this->adminUser)
  147. ->post(route('catalog.store'), $data);
  148. $response->assertSessionHasErrors('article');
  149. }
  150. public function test_store_product_requires_name_tz(): void
  151. {
  152. $data = $this->validProductData();
  153. unset($data['name_tz']);
  154. $response = $this->actingAs($this->adminUser)
  155. ->post(route('catalog.store'), $data);
  156. $response->assertSessionHasErrors('name_tz');
  157. }
  158. // --- Update (2) ---
  159. public function test_admin_can_update_product(): void
  160. {
  161. $product = Product::factory()->create();
  162. $data = $this->validProductData();
  163. $data['article'] = 'UPDATED-001';
  164. $data['name_tz'] = 'Обновлённая горка';
  165. $response = $this->actingAs($this->adminUser)
  166. ->post(route('catalog.update', $product), $data);
  167. $response->assertRedirect();
  168. $this->assertDatabaseHas('products', [
  169. 'id' => $product->id,
  170. 'article' => 'UPDATED-001',
  171. 'name_tz' => 'Обновлённая горка',
  172. ]);
  173. }
  174. public function test_manager_cannot_update_product(): void
  175. {
  176. $product = Product::factory()->create();
  177. $response = $this->actingAs($this->managerUser)
  178. ->post(route('catalog.update', $product), $this->validProductData());
  179. $response->assertForbidden();
  180. }
  181. public function test_update_product_redirects_to_parent_url_from_nav_context(): void
  182. {
  183. $product = Product::factory()->create();
  184. $reclamation = \App\Models\Reclamation::factory()->create();
  185. $nav = 'catalog-nav-token';
  186. $data = $this->validProductData();
  187. $data['article'] = 'UPDATED-NAV';
  188. $data['nav'] = $nav;
  189. $response = $this->actingAs($this->adminUser)
  190. ->withSession([
  191. 'navigation' => [
  192. $nav => [
  193. 'updated_at' => time(),
  194. 'stack' => [
  195. route('reclamations.index'),
  196. route('reclamations.show', $reclamation),
  197. route('catalog.show', $product),
  198. ],
  199. ],
  200. ],
  201. ])
  202. ->post(route('catalog.update', $product), $data);
  203. $response->assertRedirect(route('reclamations.show', [
  204. 'reclamation' => $reclamation,
  205. ]));
  206. }
  207. // --- Delete (1) ---
  208. public function test_admin_can_delete_product(): void
  209. {
  210. $product = Product::factory()->create();
  211. $response = $this->actingAs($this->adminUser)
  212. ->delete(route('catalog.delete', $product));
  213. $response->assertRedirect();
  214. $this->assertSoftDeleted('products', ['id' => $product->id]);
  215. }
  216. }