AdminAreaControllerTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Dictionary\Area;
  4. use App\Models\Dictionary\District;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Http\UploadedFile;
  9. use Illuminate\Support\Facades\Storage;
  10. use Tests\TestCase;
  11. class AdminAreaControllerTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. protected $seed = true;
  15. private User $adminUser;
  16. private User $managerUser;
  17. private District $district;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  22. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  23. $this->district = District::factory()->create(['shortname' => 'ЦАО', 'name' => 'Центральный']);
  24. }
  25. // ==================== Authentication & Authorization ====================
  26. public function test_guest_cannot_access_areas_index(): void
  27. {
  28. $response = $this->get(route('admin.area.index'));
  29. $response->assertRedirect(route('login'));
  30. }
  31. public function test_manager_cannot_access_areas_index(): void
  32. {
  33. $response = $this->actingAs($this->managerUser)
  34. ->get(route('admin.area.index'));
  35. $response->assertStatus(403);
  36. }
  37. public function test_admin_can_access_areas_index(): void
  38. {
  39. $response = $this->actingAs($this->adminUser)
  40. ->get(route('admin.area.index'));
  41. $response->assertStatus(200);
  42. $response->assertViewIs('admin.areas.index');
  43. }
  44. // ==================== Index ====================
  45. public function test_areas_index_displays_areas(): void
  46. {
  47. $area = Area::factory()->create([
  48. 'name' => 'Тестовый район',
  49. 'district_id' => $this->district->id,
  50. ]);
  51. $response = $this->actingAs($this->adminUser)
  52. ->get(route('admin.area.index'));
  53. $response->assertStatus(200);
  54. $response->assertSee('Тестовый район');
  55. }
  56. public function test_areas_index_can_filter_by_district(): void
  57. {
  58. $area1 = Area::factory()->create([
  59. 'name' => 'Район ЦАО',
  60. 'district_id' => $this->district->id,
  61. ]);
  62. $otherDistrict = District::factory()->create(['shortname' => 'САО']);
  63. $area2 = Area::factory()->create([
  64. 'name' => 'Район САО',
  65. 'district_id' => $otherDistrict->id,
  66. ]);
  67. $response = $this->actingAs($this->adminUser)
  68. ->get(route('admin.area.index', ['district_id' => $this->district->id]));
  69. $response->assertStatus(200);
  70. $response->assertSee('Район ЦАО');
  71. $response->assertDontSee('Район САО');
  72. }
  73. // ==================== Show ====================
  74. public function test_admin_can_view_area_edit_form(): void
  75. {
  76. $area = Area::factory()->create(['district_id' => $this->district->id]);
  77. $response = $this->actingAs($this->adminUser)
  78. ->get(route('admin.area.show', $area->id));
  79. $response->assertStatus(200);
  80. $response->assertViewIs('admin.areas.edit');
  81. $response->assertSee($area->name);
  82. }
  83. public function test_manager_cannot_view_area_edit_form(): void
  84. {
  85. $area = Area::factory()->create(['district_id' => $this->district->id]);
  86. $response = $this->actingAs($this->managerUser)
  87. ->get(route('admin.area.show', $area->id));
  88. $response->assertStatus(403);
  89. }
  90. // ==================== Store ====================
  91. public function test_admin_can_create_new_area(): void
  92. {
  93. $response = $this->actingAs($this->adminUser)
  94. ->post(route('admin.area.store'), [
  95. 'name' => 'Тестовый район',
  96. 'district_id' => $this->district->id,
  97. ]);
  98. $response->assertRedirect(route('admin.area.index'));
  99. $this->assertDatabaseHas('areas', [
  100. 'name' => 'Тестовый район',
  101. 'district_id' => $this->district->id,
  102. ]);
  103. }
  104. public function test_admin_can_update_existing_area(): void
  105. {
  106. $area = Area::factory()->create([
  107. 'name' => 'Старое название',
  108. 'district_id' => $this->district->id,
  109. ]);
  110. $newDistrict = District::factory()->create();
  111. $response = $this->actingAs($this->adminUser)
  112. ->post(route('admin.area.store'), [
  113. 'id' => $area->id,
  114. 'name' => 'Новое название',
  115. 'district_id' => $newDistrict->id,
  116. ]);
  117. $response->assertRedirect(route('admin.area.index'));
  118. $this->assertDatabaseHas('areas', [
  119. 'id' => $area->id,
  120. 'name' => 'Новое название',
  121. 'district_id' => $newDistrict->id,
  122. ]);
  123. }
  124. public function test_store_area_requires_name(): void
  125. {
  126. $response = $this->actingAs($this->adminUser)
  127. ->post(route('admin.area.store'), [
  128. 'district_id' => $this->district->id,
  129. ]);
  130. $response->assertSessionHasErrors('name');
  131. }
  132. public function test_store_area_requires_district_id(): void
  133. {
  134. $response = $this->actingAs($this->adminUser)
  135. ->post(route('admin.area.store'), [
  136. 'name' => 'Тестовый район',
  137. ]);
  138. $response->assertSessionHasErrors('district_id');
  139. }
  140. public function test_store_area_validates_district_exists(): void
  141. {
  142. $response = $this->actingAs($this->adminUser)
  143. ->post(route('admin.area.store'), [
  144. 'name' => 'Тестовый район',
  145. 'district_id' => 99999,
  146. ]);
  147. $response->assertSessionHasErrors('district_id');
  148. }
  149. // ==================== Destroy ====================
  150. public function test_admin_can_delete_area(): void
  151. {
  152. $area = Area::factory()->create(['district_id' => $this->district->id]);
  153. $response = $this->actingAs($this->adminUser)
  154. ->delete(route('admin.area.destroy', $area->id));
  155. $response->assertRedirect(route('admin.area.index'));
  156. $this->assertSoftDeleted('areas', ['id' => $area->id]);
  157. }
  158. // ==================== Undelete ====================
  159. public function test_admin_can_restore_deleted_area(): void
  160. {
  161. $area = Area::factory()->create(['district_id' => $this->district->id]);
  162. $area->delete();
  163. $response = $this->actingAs($this->adminUser)
  164. ->post(route('admin.area.undelete', $area->id));
  165. $response->assertRedirect(route('admin.area.index'));
  166. $this->assertDatabaseHas('areas', [
  167. 'id' => $area->id,
  168. 'deleted_at' => null,
  169. ]);
  170. }
  171. // ==================== Export ====================
  172. public function test_admin_can_export_areas(): void
  173. {
  174. Area::factory()->count(3)->create(['district_id' => $this->district->id]);
  175. $response = $this->actingAs($this->adminUser)
  176. ->post(route('admin.area.export'));
  177. $response->assertRedirect();
  178. }
  179. // ==================== Import ====================
  180. public function test_admin_can_import_areas(): void
  181. {
  182. Storage::fake('upload');
  183. $file = $this->createTestAreasXlsx();
  184. $response = $this->actingAs($this->adminUser)
  185. ->post(route('admin.area.import'), [
  186. 'import_file' => $file,
  187. ]);
  188. $response->assertRedirect(route('admin.area.index'));
  189. }
  190. public function test_import_requires_file(): void
  191. {
  192. $response = $this->actingAs($this->adminUser)
  193. ->post(route('admin.area.import'), []);
  194. $response->assertSessionHasErrors('import_file');
  195. }
  196. public function test_import_requires_xlsx_file(): void
  197. {
  198. $file = UploadedFile::fake()->create('test.txt', 100, 'text/plain');
  199. $response = $this->actingAs($this->adminUser)
  200. ->post(route('admin.area.import'), [
  201. 'import_file' => $file,
  202. ]);
  203. $response->assertSessionHasErrors('import_file');
  204. }
  205. // ==================== Helper Methods ====================
  206. private function createTestAreasXlsx(): UploadedFile
  207. {
  208. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  209. $sheet = $spreadsheet->getActiveSheet();
  210. $sheet->setCellValue('A1', 'ID');
  211. $sheet->setCellValue('B1', 'Название');
  212. $sheet->setCellValue('C1', 'Округ (сокращение)');
  213. $sheet->setCellValue('D1', 'ID округа');
  214. $sheet->setCellValue('A2', '');
  215. $sheet->setCellValue('B2', 'Тестовый район');
  216. $sheet->setCellValue('C2', 'ЦАО');
  217. $sheet->setCellValue('D2', $this->district->id);
  218. $tempPath = sys_get_temp_dir() . '/test_areas_' . uniqid() . '.xlsx';
  219. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  220. $writer->save($tempPath);
  221. return new UploadedFile(
  222. $tempPath,
  223. 'test_areas.xlsx',
  224. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  225. null,
  226. true
  227. );
  228. }
  229. }