AuthTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // can also be false if session cannot be started
  19. $this->assertTrue($id === null || $id === false);
  20. CakeSession::write('Auth.User.id', 1);
  21. $id = Auth::id();
  22. $this->assertEquals(1, $id);
  23. }
  24. public function testHasRole() {
  25. $res = Auth::hasRole(1, array(2, 3, 6));
  26. $this->assertFalse($res);
  27. $res = Auth::hasRole(3, array(2, 3, 6));
  28. $this->assertTrue($res);
  29. $res = Auth::hasRole(3, 1);
  30. $this->assertFalse($res);
  31. $res = Auth::hasRole(3, '3');
  32. $this->assertTrue($res);
  33. $res = Auth::hasRole(3, '');
  34. $this->assertFalse($res);
  35. }
  36. public function testHasRoles() {
  37. $res = Auth::hasRoles(array(1, 3), true, array(2, 3, 6));
  38. $this->assertTrue($res);
  39. $res = Auth::hasRoles(array(3), true, array(2, 3, 6));
  40. $this->assertTrue($res);
  41. $res = Auth::hasRoles(3, true, array(2, 3, 6));
  42. $this->assertTrue($res);
  43. $res = Auth::hasRoles(array(), true, array(2, 3, 6));
  44. $this->assertFalse($res);
  45. $res = Auth::hasRoles(null, true, array(2, 3, 6));
  46. $this->assertFalse($res);
  47. $res = Auth::hasRoles(array(2, 7), false, array(2, 3, 6));
  48. $this->assertFalse($res);
  49. $res = Auth::hasRoles(array(2, 6), false, array(2, 3, 6));
  50. $this->assertTrue($res);
  51. $res = Auth::hasRoles(array(2, 6), true, array(2, 3, 6));
  52. $this->assertTrue($res);
  53. $res = Auth::hasRoles(array(9, 11), true, array());
  54. $this->assertFalse($res);
  55. $res = Auth::hasRoles(array(9, 11), true, '');
  56. $this->assertFalse($res);
  57. $res = Auth::hasRoles(array(2, 7), false, array());
  58. $this->assertFalse($res);
  59. $res = Auth::hasRoles(array(2, 7), false);
  60. $this->assertFalse($res);
  61. }
  62. }