AuthTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.cake_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. /**
  20. * AuthTest::testId()
  21. *
  22. * @return void
  23. */
  24. public function testId() {
  25. $id = Auth::id();
  26. $this->assertNull($id);
  27. CakeSession::write('Auth.User.id', 1);
  28. $id = Auth::id();
  29. $this->assertEquals(1, $id);
  30. }
  31. /**
  32. * AuthTest::testHasRole()
  33. *
  34. * @return void
  35. */
  36. public function testHasRole() {
  37. $res = Auth::hasRole(1, array(2, 3, 6));
  38. $this->assertFalse($res);
  39. $res = Auth::hasRole(3, array(2, 3, 6));
  40. $this->assertTrue($res);
  41. $res = Auth::hasRole(3, 1);
  42. $this->assertFalse($res);
  43. $res = Auth::hasRole(3, '3');
  44. $this->assertTrue($res);
  45. $res = Auth::hasRole(3, '');
  46. $this->assertFalse($res);
  47. }
  48. /**
  49. * AuthTest::testHasRoleWithSession()
  50. *
  51. * @return void
  52. */
  53. public function testHasRoleWithSession() {
  54. if (!defined('USER_ROLE_KEY')) {
  55. define('USER_ROLE_KEY', 'Role');
  56. }
  57. CakeSession::write('Auth.User.id', 1);
  58. $roles = array(
  59. array('id' => '1', 'name' => 'User', 'alias' => 'user'),
  60. array('id' => '2', 'name' => 'Moderator', 'alias' => 'moderator'),
  61. array('id' => '3', 'name' => 'Admin', 'alias' => 'admin'),
  62. );
  63. CakeSession::write('Auth.User.' . USER_ROLE_KEY, $roles);
  64. $res = Auth::hasRole(4);
  65. $this->assertFalse($res);
  66. $res = Auth::hasRole(3);
  67. $this->assertTrue($res);
  68. }
  69. /**
  70. * AuthTest::testHasRoles()
  71. *
  72. * @return void
  73. */
  74. public function testHasRoles() {
  75. $res = Auth::hasRoles(array(1, 3), true, array(2, 3, 6));
  76. $this->assertTrue($res);
  77. $res = Auth::hasRoles(array(3), true, array(2, 3, 6));
  78. $this->assertTrue($res);
  79. $res = Auth::hasRoles(3, true, array(2, 3, 6));
  80. $this->assertTrue($res);
  81. $res = Auth::hasRoles(array(), true, array(2, 3, 6));
  82. $this->assertFalse($res);
  83. $res = Auth::hasRoles(null, true, array(2, 3, 6));
  84. $this->assertFalse($res);
  85. $res = Auth::hasRoles(array(2, 7), false, array(2, 3, 6));
  86. $this->assertFalse($res);
  87. $res = Auth::hasRoles(array(2, 6), false, array(2, 3, 6));
  88. $this->assertTrue($res);
  89. $res = Auth::hasRoles(array(2, 6), true, array(2, 3, 6));
  90. $this->assertTrue($res);
  91. $res = Auth::hasRoles(array(9, 11), true, array());
  92. $this->assertFalse($res);
  93. $res = Auth::hasRoles(array(9, 11), true, '');
  94. $this->assertFalse($res);
  95. $res = Auth::hasRoles(array(2, 7), false, array());
  96. $this->assertFalse($res);
  97. $res = Auth::hasRoles(array(2, 7), false);
  98. $this->assertFalse($res);
  99. }
  100. }