AuthTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. parent::setUp();
  11. ClassRegistry::init('Session');
  12. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  13. }
  14. public function tearDown() {
  15. parent::tearDown();
  16. ClassRegistry::flush();
  17. CakeSession::delete('Auth');
  18. }
  19. public function testId() {
  20. $id = Auth::id();
  21. $this->assertNull($id);
  22. CakeSession::write('Auth.User.id', 1);
  23. $id = Auth::id();
  24. $this->assertEquals(1, $id);
  25. }
  26. public function testHasRole() {
  27. $res = Auth::hasRole(1, array(2, 3, 6));
  28. $this->assertFalse($res);
  29. $res = Auth::hasRole(3, array(2, 3, 6));
  30. $this->assertTrue($res);
  31. $res = Auth::hasRole(3, 1);
  32. $this->assertFalse($res);
  33. $res = Auth::hasRole(3, '3');
  34. $this->assertTrue($res);
  35. $res = Auth::hasRole(3, '');
  36. $this->assertFalse($res);
  37. }
  38. public function testHasRoles() {
  39. $res = Auth::hasRoles(array(1, 3), true, array(2, 3, 6));
  40. $this->assertTrue($res);
  41. $res = Auth::hasRoles(array(3), true, array(2, 3, 6));
  42. $this->assertTrue($res);
  43. $res = Auth::hasRoles(3, true, array(2, 3, 6));
  44. $this->assertTrue($res);
  45. $res = Auth::hasRoles(array(), true, array(2, 3, 6));
  46. $this->assertFalse($res);
  47. $res = Auth::hasRoles(null, true, array(2, 3, 6));
  48. $this->assertFalse($res);
  49. $res = Auth::hasRoles(array(2, 7), false, array(2, 3, 6));
  50. $this->assertFalse($res);
  51. $res = Auth::hasRoles(array(2, 6), false, array(2, 3, 6));
  52. $this->assertTrue($res);
  53. $res = Auth::hasRoles(array(2, 6), true, array(2, 3, 6));
  54. $this->assertTrue($res);
  55. $res = Auth::hasRoles(array(9, 11), true, array());
  56. $this->assertFalse($res);
  57. $res = Auth::hasRoles(array(9, 11), true, '');
  58. $this->assertFalse($res);
  59. $res = Auth::hasRoles(array(2, 7), false, array());
  60. $this->assertFalse($res);
  61. $res = Auth::hasRoles(array(2, 7), false);
  62. $this->assertFalse($res);
  63. }
  64. }