auth.test.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. use Laravel\Str;
  3. use Laravel\Auth;
  4. use Laravel\Cookie;
  5. use Laravel\Session;
  6. use Laravel\Crypter;
  7. use Laravel\Session\Payload;
  8. class AuthTest extends PHPUnit_Framework_TestCase {
  9. /**
  10. * Setup teh test environment.
  11. */
  12. public function setUp()
  13. {
  14. $_SERVER['auth.login.stub'] = null;
  15. Cookie::$jar = array();
  16. Config::$items = array();
  17. Auth::$user = null;
  18. Session::$instance = null;
  19. Config::set('database.default', 'sqlite');
  20. }
  21. /**
  22. * Tear down the test environment.
  23. */
  24. public function tearDown()
  25. {
  26. $_SERVER['auth.login.stub'] = null;
  27. Cookie::$jar = array();
  28. Config::$items = array();
  29. Auth::$user = null;
  30. Session::$instance = null;
  31. Config::set('database.default', 'mysql');
  32. }
  33. /**
  34. * Test the Auth::user method.
  35. *
  36. * @group laravel
  37. */
  38. public function testUserMethodReturnsCurrentUser()
  39. {
  40. Auth::$user = 'Taylor';
  41. $this->assertEquals('Taylor', Auth::user());
  42. }
  43. /**
  44. * Test the Auth::check method.
  45. *
  46. * @group laravel
  47. */
  48. public function testCheckMethodReturnsTrueWhenUserIsSet()
  49. {
  50. $this->assertTrue(AuthUserReturnsDummy::check());
  51. }
  52. /**
  53. * Test the Auth::check method.
  54. *
  55. * @group laravel
  56. */
  57. public function testCheckMethodReturnsFalseWhenNoUserIsSet()
  58. {
  59. $this->assertFalse(AuthUserReturnsNull::check());
  60. }
  61. /**
  62. * Test the Auth::guest method.
  63. *
  64. * @group laravel
  65. */
  66. public function testGuestReturnsTrueWhenNoUserIsSet()
  67. {
  68. $this->assertTrue(AuthUserReturnsNull::guest());
  69. }
  70. /**
  71. * Test the Auth::guest method.
  72. *
  73. * @group laravel
  74. */
  75. public function testGuestReturnsFalseWhenUserIsSet()
  76. {
  77. $this->assertFalse(AuthUserReturnsDummy::guest());
  78. }
  79. /**
  80. * Test the Auth::user method.
  81. *
  82. * @group laravel
  83. */
  84. public function testUserMethodReturnsNullWhenNoUserExistsAndNoRecallerExists()
  85. {
  86. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  87. $this->assertNull(Auth::user());
  88. }
  89. /**
  90. * Test the Auth::user method.
  91. *
  92. * @group laravel
  93. */
  94. public function testUserReturnsUserByID()
  95. {
  96. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  97. Session::$instance->session['data'][Auth::user_key] = 1;
  98. $this->assertEquals('Taylor Otwell', Auth::user()->name);
  99. }
  100. /**
  101. * Test the Auth::user method.
  102. *
  103. * @group laravel
  104. */
  105. public function testNullReturnedWhenUserIDNotValidInteger()
  106. {
  107. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  108. Session::$instance->session['data'][Auth::user_key] = 'asdlkasd';
  109. $this->assertNull(Auth::user());
  110. }
  111. /**
  112. * Test the Auth::recall method.
  113. *
  114. * @group laravel
  115. */
  116. public function testUserCanBeRecalledViaCookie()
  117. {
  118. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  119. $cookie = Crypter::encrypt('1|'.Str::random(40));
  120. Cookie::forever(Config::get('auth.cookie'), $cookie);
  121. $this->assertEquals('Taylor Otwell', AuthLoginStub::user()->name);
  122. $this->assertTrue(AuthLoginStub::user() === $_SERVER['auth.login.stub']['user']);
  123. }
  124. /**
  125. * Test the Auth::attempt method.
  126. *
  127. * @group laravel
  128. */
  129. public function testAttemptMethodReturnsFalseWhenCredentialsAreInvalid()
  130. {
  131. $this->assertFalse(Auth::attempt('foo', 'foo'));
  132. $this->assertFalse(Auth::attempt('foo', null));
  133. $this->assertFalse(Auth::attempt(null, null));
  134. $this->assertFalse(Auth::attempt('taylor', 'password'));
  135. $this->assertFalse(Auth::attempt('taylor', 232));
  136. }
  137. /**
  138. * Test the Auth::attempt method.
  139. *
  140. * @group laravel
  141. */
  142. public function testAttemptReturnsTrueWhenCredentialsAreCorrect()
  143. {
  144. $this->assertTrue(AuthLoginStub::attempt('taylor', 'password1'));
  145. $this->assertEquals('Taylor Otwell', $_SERVER['auth.login.stub']['user']->name);
  146. $this->assertFalse($_SERVER['auth.login.stub']['remember']);
  147. $this->assertTrue(AuthLoginStub::attempt('taylor', 'password1', true));
  148. $this->assertEquals('Taylor Otwell', $_SERVER['auth.login.stub']['user']->name);
  149. $this->assertTrue($_SERVER['auth.login.stub']['remember']);
  150. }
  151. /**
  152. * Test Auth::login method.
  153. *
  154. * @group laravel
  155. */
  156. public function testLoginMethodStoresUserKeyInSession()
  157. {
  158. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  159. $user = new StdClass;
  160. $user->id = 10;
  161. Auth::login($user);
  162. $this->assertEquals(10, Session::$instance->session['data'][Auth::user_key]);
  163. Auth::login(5);
  164. $this->assertEquals(5, Session::$instance->session['data'][Auth::user_key]);
  165. }
  166. /**
  167. * Test the Auth::login method.
  168. *
  169. * @group laravel
  170. */
  171. public function testLoginStoresRememberCookieWhenNeeded()
  172. {
  173. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  174. // Set the session vars to make sure remember cookie uses them
  175. Config::set('session.path', 'foo');
  176. Config::set('session.domain', 'bar');
  177. Config::set('session.secure', true);
  178. Auth::login(10, true);
  179. $this->assertTrue(isset(Cookie::$jar[Config::get('auth.cookie')]));
  180. $cookie = Cookie::$jar[Config::get('auth.cookie')]['value'];
  181. $cookie = explode('|', Crypter::decrypt($cookie));
  182. $this->assertEquals(10, $cookie[0]);
  183. $this->assertEquals('foo', Cookie::$jar[Config::get('auth.cookie')]['path']);
  184. $this->assertEquals('bar', Cookie::$jar[Config::get('auth.cookie')]['domain']);
  185. $this->assertTrue(Cookie::$jar[Config::get('auth.cookie')]['secure']);
  186. }
  187. /**
  188. * Test the Auth::logout method.
  189. *
  190. * @group laravel
  191. */
  192. public function testLogoutMethodLogsOutUser()
  193. {
  194. Session::$instance = new Payload($this->getMock('Laravel\\Session\\Drivers\\Driver'));
  195. Session::$instance->session['data'][Auth::user_key] = 10;
  196. Config::set('auth.logout', function($user) { $_SERVER['auth.logout.stub'] = $user; });
  197. Auth::$user = 'Taylor';
  198. Auth::logout();
  199. $this->assertEquals('Taylor', $_SERVER['auth.logout.stub']);
  200. $this->assertNull(Auth::$user);
  201. $this->assertFalse(isset(Session::$instance->session['data'][Auth::user_key]));
  202. $this->assertTrue(Cookie::$jar[Config::get('auth.cookie')]['minutes'] < 0);
  203. }
  204. }
  205. class AuthUserReturnsNull extends Laravel\Auth {
  206. public static function user() {}
  207. }
  208. class AuthUserReturnsDummy extends Laravel\Auth {
  209. public static function user() { return 'Taylor'; }
  210. }
  211. class AuthLoginStub extends Laravel\Auth {
  212. public static function login($user, $remember = false)
  213. {
  214. $_SERVER['auth.login.stub'] = compact('user', 'remember');
  215. }
  216. }