AuthTest.php 2.8 KB

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