AuthTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. App::uses('Auth', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. /**
  5. * 2010-06-29 ms
  6. */
  7. class AuthTest extends MyCakeTestCase {
  8. public $fixtures = array('core.session');
  9. public function setUp() {
  10. ClassRegistry::init('Session');
  11. }
  12. public function tearDown() {
  13. ClassRegistry::flush();
  14. CakeSession::delete('Auth');
  15. }
  16. public function testId() {
  17. $id = Auth::id();
  18. $this->assertNull($id);
  19. CakeSession::write('Auth.User.id', 1);
  20. $id = Auth::id();
  21. $this->assertEquals(1, $id);
  22. }
  23. public function testHasRole() {
  24. $res = Auth::hasRole(1, array(2, 3, 6));
  25. $this->assertFalse($res);
  26. $res = Auth::hasRole(3, array(2, 3, 6));
  27. $this->assertTrue($res);
  28. $res = Auth::hasRole(3, 1);
  29. $this->assertFalse($res);
  30. $res = Auth::hasRole(3, '3');
  31. $this->assertTrue($res);
  32. $res = Auth::hasRole(3, '');
  33. $this->assertFalse($res);
  34. }
  35. public function testHasRoles() {
  36. $res = Auth::hasRoles(array(1, 3), true, array(2, 3, 6));
  37. $this->assertTrue($res);
  38. $res = Auth::hasRoles(array(3), true, array(2, 3, 6));
  39. $this->assertTrue($res);
  40. $res = Auth::hasRoles(3, true, array(2, 3, 6));
  41. $this->assertTrue($res);
  42. $res = Auth::hasRoles(array(), true, array(2, 3, 6));
  43. $this->assertFalse($res);
  44. $res = Auth::hasRoles(null, true, array(2, 3, 6));
  45. $this->assertFalse($res);
  46. $res = Auth::hasRoles(array(2, 7), false, array(2, 3, 6));
  47. $this->assertFalse($res);
  48. $res = Auth::hasRoles(array(2, 6), false, array(2, 3, 6));
  49. $this->assertTrue($res);
  50. $res = Auth::hasRoles(array(2, 6), true, array(2, 3, 6));
  51. $this->assertTrue($res);
  52. $res = Auth::hasRoles(array(9, 11), true, array());
  53. $this->assertFalse($res);
  54. $res = Auth::hasRoles(array(9, 11), true, '');
  55. $this->assertFalse($res);
  56. $res = Auth::hasRoles(array(2, 7), false, array());
  57. $this->assertFalse($res);
  58. $res = Auth::hasRoles(array(2, 7), false);
  59. $this->assertFalse($res);
  60. }
  61. }