EmployeeFeatureTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Tests\Feature\Admin\Employees;
  3. use App\Shop\Employees\Employee;
  4. use App\Shop\Employees\Repositories\EmployeeRepository;
  5. use Illuminate\Auth\Events\Lockout;
  6. use Tests\TestCase;
  7. class EmployeeFeatureTest extends TestCase
  8. {
  9. /** @test */
  10. public function it_should_go_directly_to_dashboard_when_employee_is_already_logged_in()
  11. {
  12. $this
  13. ->actingAs($this->employee, 'employee')
  14. ->get(route('admin.login'))
  15. ->assertStatus(302)
  16. ->assertRedirect(route('admin.dashboard'));
  17. }
  18. /** @test */
  19. public function it_can_show_the_admin_login_form()
  20. {
  21. $this->get(route('admin.login'))
  22. ->assertStatus(200)
  23. ->assertSee('Sign in to start your session')
  24. ->assertSee('I forgot my password')
  25. ->assertSee('Register a new membership');
  26. }
  27. /** @test */
  28. public function it_redirect_back_outside_if_customer_accessing_admin_page()
  29. {
  30. $this->actingAs($this->customer)
  31. ->get(route('admin.dashboard'))
  32. ->assertStatus(302)
  33. ->assertRedirect(route('admin.login'))
  34. ->assertSessionHas('error', 'You must be an employee to see this page');
  35. }
  36. /** @test */
  37. public function it_throws_the_too_many_login_attempts_event()
  38. {
  39. $this->expectsEvents(Lockout::class);
  40. $employee = factory(Employee::class)->create();
  41. for ($i=0; $i <= 5; $i++) {
  42. $data = [
  43. 'email' => $employee->email,
  44. 'password' => 'unknown'
  45. ];
  46. $this->post(route('admin.login'), $data);
  47. }
  48. }
  49. /** @test */
  50. public function it_redirects_back_to_login_page_when_credentials_are_wrong()
  51. {
  52. $employee = factory(Employee::class)->create();
  53. $data = [
  54. 'email' => $employee->email,
  55. 'password' => 'unknown'
  56. ];
  57. $this->post(route('admin.login'), $data)
  58. ->assertStatus(302)
  59. ->assertRedirect(route('home'))
  60. ->assertSessionHasErrors();
  61. }
  62. /** @test */
  63. public function it_can_login_to_the_dashboard()
  64. {
  65. $employee = factory(Employee::class)->create();
  66. $data = [
  67. 'email' => $employee->email,
  68. 'password' => 'secret'
  69. ];
  70. $this->post(route('admin.login'), $data)
  71. ->assertStatus(302)
  72. ->assertRedirect(route('admin.dashboard'));
  73. }
  74. /** @test */
  75. public function it_can_show_the_create_and_edit_employee_page()
  76. {
  77. $this->actingAs($this->employee, 'employee')
  78. ->get(route('admin.employees.create'))
  79. ->assertStatus(200);
  80. $this->actingAs($this->employee, 'employee')
  81. ->get(route('admin.employees.edit', $this->employee->id))
  82. ->assertStatus(200)
  83. ->assertSee(htmlentities($this->employee->name, ENT_QUOTES));
  84. }
  85. /** @test */
  86. public function it_can_update_the_profile()
  87. {
  88. $data = ['name' => 'King Kong', 'email' => 'test@test.com'];
  89. $this->actingAs($this->employee, 'employee')
  90. ->put(route('admin.employee.profile.update', $this->employee->id), $data)
  91. ->assertStatus(302)
  92. ->assertSessionHas(['message' => 'Update successful']);
  93. }
  94. /** @test */
  95. public function it_can_show_the_profile_of_the_logged_user()
  96. {
  97. $employee = factory(Employee::class)->create();
  98. $this->actingAs($this->employee, 'employee')
  99. ->get(route('admin.employee.profile', $employee->id))
  100. ->assertStatus(200)
  101. ->assertSee(htmlentities($employee->name, ENT_QUOTES));
  102. }
  103. /** @test */
  104. public function it_errors_when_editing_an_employee_that_is_not_found()
  105. {
  106. $this->actingAs($this->employee, 'employee')
  107. ->get(route('admin.employees.edit', 999))
  108. ->assertStatus(404);
  109. }
  110. /** @test */
  111. public function it_errors_when_looking_for_an_employee_that_is_not_found()
  112. {
  113. $this->actingAs($this->employee, 'employee')
  114. ->get(route('admin.employees.show', 999))
  115. ->assertStatus(404);
  116. }
  117. /** @test */
  118. public function it_can_list_all_the_employees()
  119. {
  120. $employee = factory(Employee::class)->create();
  121. $this->actingAs($this->employee, 'employee')
  122. ->get(route('admin.employees.index'))
  123. ->assertStatus(200)
  124. ->assertSee(htmlentities($employee->name, ENT_QUOTES));
  125. }
  126. /** @test */
  127. public function it_errors_when_the_email_is_already_taken()
  128. {
  129. $data = [
  130. 'name' => $this->faker->name,
  131. 'email' => $this->employee->email,
  132. 'password' => 'secret'
  133. ];
  134. $this->actingAs($this->employee, 'employee')
  135. ->post(route('admin.employees.store'), $data)
  136. ->assertStatus(302)
  137. ->assertSessionHas(['errors']);
  138. }
  139. /** @test */
  140. public function it_errors_if_the_password_is_less_than_eight_characters()
  141. {
  142. $data = [
  143. 'name' => $this->faker->name,
  144. 'email' => $this->faker->email,
  145. 'password' => 'secret'
  146. ];
  147. $this->actingAs($this->employee, 'employee')
  148. ->post(route('admin.employees.store'), $data)
  149. ->assertStatus(302)
  150. ->assertSessionHas(['errors']);
  151. }
  152. /** @test */
  153. public function it_can_only_soft_delete_an_employee()
  154. {
  155. $employee = factory(Employee::class)->create();
  156. $this->actingAs($this->employee, 'employee')
  157. ->delete(route('admin.employees.destroy', $employee->id))
  158. ->assertStatus(302)
  159. ->assertRedirect(route('admin.employees.index'));
  160. $this->assertDatabaseHas('employees', $employee->toArray());
  161. }
  162. /** @test */
  163. public function it_should_update_the_employee_password()
  164. {
  165. $employee = factory(Employee::class)->create();
  166. $update = [
  167. 'name' => $employee->name,
  168. 'email' => $employee->email,
  169. 'password' => 'secret!!'
  170. ];
  171. $this->actingAs($this->employee, 'employee')
  172. ->put(route('admin.employees.update', $employee->id), $update)
  173. ->assertStatus(302)
  174. ->assertRedirect(route('admin.employees.edit', $employee->id));
  175. $collection = collect($update)->except('password');
  176. $this->assertDatabaseHas('employees', $collection->all());
  177. }
  178. /** @test */
  179. public function it_can_update_the_employee()
  180. {
  181. $update = [
  182. 'name' => $this->faker->name,
  183. 'email' => $this->faker->unique()->email,
  184. 'status' => 0
  185. ];
  186. $this->actingAs($this->employee, 'employee')
  187. ->put(route('admin.employees.update', $this->employee->id), $update)
  188. ->assertStatus(302)
  189. ->assertRedirect(route('admin.employees.edit', $this->employee->id));
  190. $this->assertDatabaseHas('employees', $update);
  191. }
  192. /** @test */
  193. public function it_can_show_the_employee()
  194. {
  195. $this->actingAs($this->employee, 'employee')
  196. ->get(route('admin.employees.show', $this->employee->id))
  197. ->assertStatus(200)
  198. ->assertViewHas('employee');
  199. }
  200. /** @test */
  201. public function it_can_create_an_employee()
  202. {
  203. $data = [
  204. 'name' => $this->faker->name,
  205. 'email' => $this->faker->email,
  206. 'password' => 'secret!!',
  207. 'role' => $this->role->id
  208. ];
  209. $this->actingAs($this->employee, 'employee')
  210. ->post(route('admin.employees.store'), $data)
  211. ->assertStatus(302)
  212. ->assertRedirect(route('admin.employees.index'));
  213. $created = collect($data)->except('password', 'role');
  214. $this->assertDatabaseHas('employees', $created->all());
  215. }
  216. }