UsersTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. use Encore\Admin\Auth\Database\Administrator;
  3. class UsersTest extends TestCase
  4. {
  5. protected $user;
  6. public function setUp()
  7. {
  8. parent::setUp();
  9. $this->user = Administrator::first();
  10. $this->be($this->user, 'admin');
  11. }
  12. public function testUsersIndexPage()
  13. {
  14. $this->visit('admin/auth/users')
  15. ->see('Administrator');
  16. }
  17. public function testCreateUser()
  18. {
  19. $user = [
  20. 'username' => 'Test',
  21. 'name' => 'Name',
  22. 'password' => '123456',
  23. 'password_confirmation' => '123456',
  24. ];
  25. // create user
  26. $this->visit('admin/auth/users/create')
  27. ->see('Create')
  28. ->submitForm('Submit', $user)
  29. ->seePageIs('admin/auth/users')
  30. ->seeInDatabase(config('admin.database.users_table'), ['username' => 'Test']);
  31. // assign role to user
  32. $this->visit('admin/auth/users/2/edit')
  33. ->see('Edit')
  34. ->submitForm('Submit', ['roles' => [1]])
  35. ->seePageIs('admin/auth/users')
  36. ->seeInDatabase(config('admin.database.role_users_table'), ['user_id' => 2, 'role_id' => 1]);
  37. $this->visit('admin/auth/logout')
  38. ->dontSeeIsAuthenticated('admin')
  39. ->seePageIs('admin/auth/login')
  40. ->submitForm('Login', ['username' => $user['username'], 'password' => $user['password']])
  41. ->see('dashboard')
  42. ->seeIsAuthenticated('admin')
  43. ->seePageIs('admin');
  44. $this->assertTrue($this->app['auth']->guard('admin')->getUser()->isAdministrator());
  45. $this->see('<span>Users</span>')
  46. ->see('<span>Roles</span>')
  47. ->see('<span>Permission</span>')
  48. ->see('<span>Operation log</span>')
  49. ->see('<span>Menu</span>');
  50. }
  51. public function testUpdateUser()
  52. {
  53. $this->visit('admin/auth/users/'.$this->user->id.'/edit')
  54. ->see('Create')
  55. ->submitForm('Submit', ['name' => 'test', 'roles' => [1]])
  56. ->seePageIs('admin/auth/users')
  57. ->seeInDatabase(config('admin.database.users_table'), ['name' => 'test']);
  58. }
  59. public function testResetPassword()
  60. {
  61. $password = 'odjwyufkglte';
  62. $data = [
  63. 'password' => $password,
  64. 'password_confirmation' => $password,
  65. 'roles' => [1],
  66. ];
  67. $this->visit('admin/auth/users/'.$this->user->id.'/edit')
  68. ->see('Create')
  69. ->submitForm('Submit', $data)
  70. ->seePageIs('admin/auth/users')
  71. ->visit('admin/auth/logout')
  72. ->dontSeeIsAuthenticated('admin')
  73. ->seePageIs('admin/auth/login')
  74. ->submitForm('Login', ['username' => $this->user->username, 'password' => $password])
  75. ->see('dashboard')
  76. ->seeIsAuthenticated('admin')
  77. ->seePageIs('admin');
  78. }
  79. }