RoleTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. use Webkul\User\Models\Role;
  3. use function Pest\Laravel\deleteJson;
  4. use function Pest\Laravel\get;
  5. use function Pest\Laravel\postJson;
  6. use function Pest\Laravel\putJson;
  7. it('should returns the role index page', function () {
  8. // Act and Assert.
  9. $this->loginAsAdmin();
  10. get(route('admin.settings.roles.index'))
  11. ->assertOk()
  12. ->assertSeeText(trans('admin::app.settings.roles.index.title'))
  13. ->assertSeeText(trans('admin::app.settings.roles.index.create-btn'));
  14. });
  15. it('should returns the create page of role', function () {
  16. // Act and Assert.
  17. $this->loginAsAdmin();
  18. get(route('admin.settings.roles.create'))
  19. ->assertOk()
  20. ->assertSeeText(trans('admin::app.settings.roles.create.title'))
  21. ->assertSeeText(trans('admin::app.settings.roles.create.save-btn'));
  22. });
  23. it('should fail the validation with errors when certain field not provided when store the roles', function () {
  24. // Act and Assert.
  25. $this->loginAsAdmin();
  26. postJson(route('admin.settings.roles.store'))
  27. ->assertJsonValidationErrorFor('name')
  28. ->assertJsonValidationErrorFor('description')
  29. ->assertJsonValidationErrorFor('permission_type')
  30. ->assertUnprocessable();
  31. });
  32. it('should store the newly created roles', function () {
  33. // Act and Assert.
  34. $this->loginAsAdmin();
  35. postJson(route('admin.settings.roles.store'), $data = [
  36. 'name' => fake()->name(),
  37. 'permission_type' => fake()->randomElement(['custom', 'all']),
  38. 'description' => fake()->sentence(),
  39. 'permissions' => [
  40. 0 => acl()->getRoles()->random(),
  41. ],
  42. ])
  43. ->assertRedirect(route('admin.settings.roles.index'))
  44. ->isRedirection();
  45. $this->assertModelWise([
  46. Role::class => [
  47. [
  48. 'name' => $data['name'],
  49. 'permission_type' => $data['permission_type'],
  50. 'description' => $data['description'],
  51. ],
  52. ],
  53. ]);
  54. });
  55. it('should returns the edit page of roles', function () {
  56. // Arrange.
  57. $role = Role::factory()->create();
  58. // Act and Assert.
  59. $this->loginAsAdmin();
  60. putJson(route('admin.settings.roles.edit', $role->id), $data = [
  61. 'name' => fake()->name(),
  62. 'permission_type' => fake()->randomElement(['custom', 'all']),
  63. 'description' => fake()->sentence(),
  64. ])
  65. ->assertRedirect(route('admin.settings.roles.index'))
  66. ->isRedirection();
  67. $this->assertModelWise([
  68. Role::class => [
  69. [
  70. 'name' => $data['name'],
  71. 'permission_type' => $data['permission_type'],
  72. 'description' => $data['description'],
  73. ],
  74. ],
  75. ]);
  76. });
  77. it('should fail the validation with errors when certain field not provided when update the roles', function () {
  78. // Arrange.
  79. $role = Role::factory()->create();
  80. // Act and Assert.
  81. $this->loginAsAdmin();
  82. putJson(route('admin.settings.roles.update', $role->id))
  83. ->assertJsonValidationErrorFor('name')
  84. ->assertJsonValidationErrorFor('description')
  85. ->assertJsonValidationErrorFor('permission_type')
  86. ->assertUnprocessable();
  87. });
  88. it('should update the existing role', function () {
  89. // Arrange.
  90. $role = Role::factory()->create();
  91. // Act and Assert.
  92. $this->loginAsAdmin();
  93. putJson(route('admin.settings.roles.update', $role->id), $data = [
  94. 'name' => fake()->name(),
  95. 'permission_type' => fake()->randomElement(['custom', 'all']),
  96. 'description' => fake()->sentence(),
  97. ])
  98. ->assertRedirect(route('admin.settings.roles.index'))
  99. ->isRedirection();
  100. $this->assertModelWise([
  101. Role::class => [
  102. [
  103. 'name' => $data['name'],
  104. 'permission_type' => $data['permission_type'],
  105. 'description' => $data['description'],
  106. ],
  107. ],
  108. ]);
  109. });
  110. it('should delete the existing role', function () {
  111. // Arrange.
  112. $role = Role::factory()->create();
  113. // Act and Assert.
  114. $this->loginAsAdmin();
  115. deleteJson(route('admin.settings.roles.delete', $role->id))
  116. ->assertOk()
  117. ->assertJsonPath('message', trans('admin::app.settings.roles.delete-success'));
  118. $this->assertDatabaseMissing('roles', [
  119. 'id' => $role->id,
  120. ]);
  121. });