AuthTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. App::uses('Auth', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. /**
  5. */
  6. class AuthTest extends MyCakeTestCase {
  7. public $fixtures = array('core.cake_session');
  8. public function setUp() {
  9. parent::setUp();
  10. ClassRegistry::init(array('table' => 'cake_sessions', 'class' => 'Session', 'alias' => 'Session'));
  11. }
  12. public function tearDown() {
  13. parent::tearDown();
  14. ClassRegistry::flush();
  15. CakeSession::delete('Auth');
  16. }
  17. /**
  18. * AuthTest::testId()
  19. *
  20. * @return void
  21. */
  22. public function testId() {
  23. $id = Auth::id();
  24. $this->assertNull($id);
  25. CakeSession::write('Auth.User.id', 1);
  26. $id = Auth::id();
  27. $this->assertEquals(1, $id);
  28. }
  29. /**
  30. * AuthTest::testHasRole()
  31. *
  32. * @return void
  33. */
  34. public function testHasRole() {
  35. $res = Auth::hasRole(1, array(2, 3, 6));
  36. $this->assertFalse($res);
  37. $res = Auth::hasRole(3, array(2, 3, 6));
  38. $this->assertTrue($res);
  39. $res = Auth::hasRole(3, 1);
  40. $this->assertFalse($res);
  41. $res = Auth::hasRole(3, '3');
  42. $this->assertTrue($res);
  43. $res = Auth::hasRole(3, '');
  44. $this->assertFalse($res);
  45. }
  46. /**
  47. * AuthTest::testHasRoleWithSession()
  48. *
  49. * @return void
  50. */
  51. public function testHasRoleWithSession() {
  52. if (!defined('USER_ROLE_KEY')) {
  53. define('USER_ROLE_KEY', 'Role');
  54. }
  55. CakeSession::write('Auth.User.id', 1);
  56. $roles = array(
  57. array('id' => '1', 'name' => 'User', 'alias' => 'user'),
  58. array('id' => '2', 'name' => 'Moderator', 'alias' => 'moderator'),
  59. array('id' => '3', 'name' => 'Admin', 'alias' => 'admin'),
  60. );
  61. CakeSession::write('Auth.User.' . USER_ROLE_KEY, $roles);
  62. $res = Auth::hasRole(4);
  63. $this->assertFalse($res);
  64. $res = Auth::hasRole(3);
  65. $this->assertTrue($res);
  66. }
  67. /**
  68. * AuthTest::testHasRoles()
  69. *
  70. * @return void
  71. */
  72. public function testHasRoles() {
  73. $res = Auth::hasRoles(array(1, 3), true, array(2, 3, 6));
  74. $this->assertTrue($res);
  75. $res = Auth::hasRoles(array(3), true, array(2, 3, 6));
  76. $this->assertTrue($res);
  77. $res = Auth::hasRoles(3, true, array(2, 3, 6));
  78. $this->assertTrue($res);
  79. $res = Auth::hasRoles(array(), true, array(2, 3, 6));
  80. $this->assertFalse($res);
  81. $res = Auth::hasRoles(null, true, array(2, 3, 6));
  82. $this->assertFalse($res);
  83. $res = Auth::hasRoles(array(2, 7), false, array(2, 3, 6));
  84. $this->assertFalse($res);
  85. $res = Auth::hasRoles(array(2, 6), false, array(2, 3, 6));
  86. $this->assertTrue($res);
  87. $res = Auth::hasRoles(array(2, 6), true, array(2, 3, 6));
  88. $this->assertTrue($res);
  89. $res = Auth::hasRoles(array(9, 11), true, array());
  90. $this->assertFalse($res);
  91. $res = Auth::hasRoles(array(9, 11), true, '');
  92. $this->assertFalse($res);
  93. $res = Auth::hasRoles(array(2, 7), false, array());
  94. $this->assertFalse($res);
  95. $res = Auth::hasRoles(array(2, 7), false);
  96. $this->assertFalse($res);
  97. }
  98. }