AdminRoleControllerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\AdminPermission;
  4. use App\Models\AdminRole;
  5. use Illuminate\Support\Arr;
  6. use Tests\AdminTestCase;
  7. use Illuminate\Foundation\Testing\WithFaker;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Tests\Traits\RequestActions;
  10. class AdminRoleControllerTest extends AdminTestCase
  11. {
  12. use RefreshDatabase;
  13. use RequestActions;
  14. use WithFaker;
  15. protected $resourceName = 'admin-roles';
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->login();
  20. }
  21. public function testStoreValidation()
  22. {
  23. // name, slug required
  24. // permissions array
  25. $res = $this->storeResource([
  26. 'name' => '',
  27. 'slug' => '',
  28. 'permissions' => 'not array',
  29. ]);
  30. $res->assertJsonValidationErrors(['name', 'slug', 'permissions']);
  31. factory(AdminRole::class)->create([
  32. 'name' => 'name',
  33. 'slug' => 'slug',
  34. ]);
  35. factory(AdminPermission::class)->create();
  36. // name, slug unique
  37. // permissions.* exists
  38. $res = $this->storeResource([
  39. 'name' => 'name',
  40. 'slug' => 'slug',
  41. 'permissions' => [99],
  42. ]);
  43. $res->assertJsonValidationErrors(['name', 'slug', 'permissions']);
  44. }
  45. public function testStore()
  46. {
  47. $inputs = [
  48. 'name' => 'name',
  49. 'slug' => 'slug',
  50. ];
  51. $res = $this->storeResource($inputs);
  52. $res->assertStatus(201);
  53. $this->assertDatabaseHas('admin_roles', $inputs);
  54. $inputs = [
  55. 'name' => 'name1',
  56. 'slug' => 'slug2',
  57. ];
  58. $permissionId = factory(AdminPermission::class)->create()->id;
  59. $res = $this->storeResource($inputs + [
  60. 'permissions' => [$permissionId],
  61. ]);
  62. $res->assertStatus(201);
  63. $this->assertDatabaseHas('admin_role_permission', [
  64. 'role_id' => $this->getLastInsertId('admin_roles'),
  65. 'permission_id' => $permissionId,
  66. ]);
  67. }
  68. /**
  69. * 生成一个角色,并关联一个权限,然后返回角色和权限的 id
  70. *
  71. * @return array [$roleId, $permissionId]
  72. */
  73. protected function createRole()
  74. {
  75. $role = factory(AdminRole::class)->create();
  76. $permissionId = factory(AdminPermission::class)->create()->id;
  77. $role->permissions()->attach($permissionId);
  78. return [$role->id, $permissionId];
  79. }
  80. public function testEdit()
  81. {
  82. [$roleId, $_] = $this->createRole();
  83. $res = $this->editResource($roleId);
  84. $res->assertStatus(200)
  85. ->assertJson(AdminRole::first()->toArray())
  86. ->assertJsonCount(1, 'permissions');
  87. }
  88. public function testUpdate()
  89. {
  90. [$roleId1, $permissionId1] = $this->createRole();
  91. // 清空关联权限
  92. // 不更新任何字段
  93. $inputs = AdminRole::first()->toArray();
  94. $res = $this->updateResource($roleId1, $inputs + ['permissions' => []]);
  95. $res->assertStatus(201);
  96. $this->assertDatabaseHas('admin_roles', $inputs);
  97. $this->assertDatabaseMissing('admin_role_permission', [
  98. 'role_id' => $roleId1,
  99. 'permission_id' => $permissionId1,
  100. ]);
  101. [$roleId2, $permissionId2] = $this->createRole();
  102. // 更新字段和权限
  103. $inputs = [
  104. 'name' => 'new name',
  105. 'slug' => 'new slug',
  106. 'permissions' => [$permissionId1], // 取消原来的权限 2,更换为权限 1
  107. ];
  108. $res = $this->updateResource($roleId2, $inputs);
  109. $res->assertStatus(201);
  110. $this->assertDatabaseHas('admin_roles', Arr::except($inputs, 'permissions'));
  111. $this->assertDatabaseHas('admin_role_permission', [
  112. 'role_id' => $roleId2,
  113. 'permission_id' => $permissionId1,
  114. ]);
  115. $this->assertDatabaseMissing('admin_role_permission', [
  116. 'role_id' => $roleId2,
  117. 'permission_id' => $permissionId2,
  118. ]);
  119. // 移除全部权限
  120. $res = $this->updateResource($roleId2, ['permissions' => []]);
  121. $res->assertStatus(201);
  122. $this->assertDatabaseMissing('admin_role_permission', [
  123. 'role_id' => $roleId2,
  124. ]);
  125. }
  126. public function testDestroy()
  127. {
  128. [$roleId, $permissionId] = $this->createRole();
  129. $res = $this->destroyResource($roleId);
  130. $res->assertStatus(204);
  131. $this->assertDatabaseMissing('admin_roles', ['id' => $roleId]);
  132. $this->assertDatabaseMissing('admin_role_permission', [
  133. 'role_id' => $roleId,
  134. 'permission_id' => $permissionId,
  135. ]);
  136. }
  137. public function testIndex()
  138. {
  139. factory(AdminRole::class, 20);
  140. factory(AdminPermission::class, 5);
  141. app(\AdminRolePermissionTableSeeder::class)->run();
  142. $res = $this->getResources();
  143. $res->assertStatus(200)
  144. ->assertJsonCount(15, 'data')
  145. // 角色对应的权限数
  146. ->assertJsonCount(AdminRole::orderByDesc('id')->first()->permissions->count(), 'data.0.permissions');
  147. // 测试筛选
  148. factory(AdminRole::class)
  149. ->create([
  150. 'name' => 'role name query',
  151. 'slug' => 'role slug query',
  152. ])
  153. ->permissions()
  154. ->create(factory(AdminPermission::class)->create(['name' => 'perm name query'])->toArray());
  155. $res = $this->getResources([
  156. 'id' => $this->getLastInsertId('admin_roles'),
  157. 'name' => 'role name',
  158. 'slug' => 'role slug',
  159. ]);
  160. $res->assertJsonCount(1, 'data');
  161. // 权限名称筛选
  162. $res = $this->getResources(['permission_name' => 'perm name']);
  163. $res->assertJsonCount(1, 'data');
  164. $res = $this->getResources(['permission_name' => 'nothing']);
  165. $res->assertJsonCount(0, 'data');
  166. }
  167. }