ProductControllerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. 'nav' => $nav,
  132. ]));
  133. }
  134. // --- Store (3) ---
  135. public function test_admin_can_create_product(): void
  136. {
  137. $data = $this->validProductData();
  138. $response = $this->actingAs($this->adminUser)
  139. ->post(route('catalog.store'), $data);
  140. $response->assertRedirect();
  141. $this->assertDatabaseHas('products', ['article' => $data['article']]);
  142. }
  143. public function test_store_product_requires_article(): void
  144. {
  145. $data = $this->validProductData();
  146. unset($data['article']);
  147. $response = $this->actingAs($this->adminUser)
  148. ->post(route('catalog.store'), $data);
  149. $response->assertSessionHasErrors('article');
  150. }
  151. public function test_store_product_requires_name_tz(): void
  152. {
  153. $data = $this->validProductData();
  154. unset($data['name_tz']);
  155. $response = $this->actingAs($this->adminUser)
  156. ->post(route('catalog.store'), $data);
  157. $response->assertSessionHasErrors('name_tz');
  158. }
  159. // --- Update (2) ---
  160. public function test_admin_can_update_product(): void
  161. {
  162. $product = Product::factory()->create();
  163. $data = $this->validProductData();
  164. $data['article'] = 'UPDATED-001';
  165. $data['name_tz'] = 'Обновлённая горка';
  166. $response = $this->actingAs($this->adminUser)
  167. ->post(route('catalog.update', $product), $data);
  168. $response->assertRedirect();
  169. $this->assertDatabaseHas('products', [
  170. 'id' => $product->id,
  171. 'article' => 'UPDATED-001',
  172. 'name_tz' => 'Обновлённая горка',
  173. ]);
  174. }
  175. public function test_manager_cannot_update_product(): void
  176. {
  177. $product = Product::factory()->create();
  178. $response = $this->actingAs($this->managerUser)
  179. ->post(route('catalog.update', $product), $this->validProductData());
  180. $response->assertForbidden();
  181. }
  182. public function test_update_product_redirects_to_parent_url_from_nav_context(): void
  183. {
  184. $product = Product::factory()->create();
  185. $reclamation = \App\Models\Reclamation::factory()->create();
  186. $nav = 'catalog-nav-token';
  187. $data = $this->validProductData();
  188. $data['article'] = 'UPDATED-NAV';
  189. $data['nav'] = $nav;
  190. $response = $this->actingAs($this->adminUser)
  191. ->withSession([
  192. 'navigation' => [
  193. $nav => [
  194. 'updated_at' => time(),
  195. 'stack' => [
  196. route('reclamations.index'),
  197. route('reclamations.show', $reclamation),
  198. route('catalog.show', $product),
  199. ],
  200. ],
  201. ],
  202. ])
  203. ->post(route('catalog.update', $product), $data);
  204. $response->assertRedirect(route('reclamations.show', [
  205. 'reclamation' => $reclamation,
  206. 'nav' => 'catalog-nav-token',
  207. ]));
  208. }
  209. // --- Delete (1) ---
  210. public function test_admin_can_delete_product(): void
  211. {
  212. $product = Product::factory()->create();
  213. $response = $this->actingAs($this->adminUser)
  214. ->delete(route('catalog.delete', $product));
  215. $response->assertRedirect();
  216. $this->assertSoftDeleted('products', ['id' => $product->id]);
  217. }
  218. }