| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); |
|---|
| 12 |
|
|---|
| 13 |
$t = new lime_test(47); |
|---|
| 14 |
|
|---|
| 15 |
class MySessionStorage extends sfSessionTestStorage |
|---|
| 16 |
{ |
|---|
| 17 |
public function regenerate($destroy = false) |
|---|
| 18 |
{ |
|---|
| 19 |
$this->sessionId = rand(1, 9999); |
|---|
| 20 |
|
|---|
| 21 |
return true; |
|---|
| 22 |
} |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
$dispatcher = new sfEventDispatcher(); |
|---|
| 26 |
$sessionPath = sys_get_temp_dir().'/sessions_'.rand(11111, 99999); |
|---|
| 27 |
$storage = new MySessionStorage(array('session_path' => $sessionPath)); |
|---|
| 28 |
|
|---|
| 29 |
$user = new sfBasicSecurityUser($dispatcher, $storage); |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
$t->diag('->initialize()'); |
|---|
| 33 |
$t->todo('->initialize() times out the user if no request made for a long time'); |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
$t->diag('->getCredentials()'); |
|---|
| 37 |
$user->clearCredentials(); |
|---|
| 38 |
$user->addCredential('user'); |
|---|
| 39 |
$t->is($user->getCredentials(), array('user'), '->getCredentials() returns user credentials as an array'); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
$t->diag('->setAuthenticated() ->isAuthenticated()'); |
|---|
| 43 |
$t->is($user->isAuthenticated(), false, '->isAuthenticated() returns false by default'); |
|---|
| 44 |
$user->setAuthenticated(true); |
|---|
| 45 |
$t->is($user->isAuthenticated(), true, '->isAuthenticated() returns true if the user is authenticated'); |
|---|
| 46 |
$user->setAuthenticated(false); |
|---|
| 47 |
$t->is($user->isAuthenticated(), false, '->setAuthenticated() accepts a boolean as its first parameter'); |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
$user->setAuthenticated(false); |
|---|
| 51 |
$id = $storage->getSessionId(); |
|---|
| 52 |
$user->setAuthenticated(true); |
|---|
| 53 |
$t->isnt($id, $id = $storage->getSessionId(), '->setAuthenticated() regenerates the session id if the authentication changes'); |
|---|
| 54 |
$user->setAuthenticated(true); |
|---|
| 55 |
$t->is($storage->getSessionId(), $id, '->setAuthenticated() does not regenerate the session id if the authentication does not change'); |
|---|
| 56 |
$user->addCredential('foo'); |
|---|
| 57 |
$t->isnt($id, $id = $storage->getSessionId(), '->addCredential() regenerates the session id if a new credential is added'); |
|---|
| 58 |
$t->is($id, $storage->getSessionId(), '->addCredential() does not regenerate the session id if the credential already exists'); |
|---|
| 59 |
$user->removeCredential('foo'); |
|---|
| 60 |
$t->isnt($id, $id = $storage->getSessionId(), '->removeCredential() regenerates the session id if a credential is removed'); |
|---|
| 61 |
$t->is($id, $storage->getSessionId(), '->removeCredential() does not regenerate the session id if the credential does not exist'); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
$user = new sfBasicSecurityUser($dispatcher, $storage); |
|---|
| 65 |
$t->diag('->setTimedOut() ->isTimedOut()'); |
|---|
| 66 |
$t->is($user->isTimedOut(), false, '->isTimedOut() returns false if the session is not timed out'); |
|---|
| 67 |
$user->setTimedOut(); |
|---|
| 68 |
$t->is($user->isTimedOut(), true, '->isTimedOut() returns true if the session is timed out'); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
$t->diag('->hasCredential()'); |
|---|
| 72 |
$user->clearCredentials(); |
|---|
| 73 |
$t->is($user->hasCredential('admin'), false, '->hasCredential() returns false if user has not the credential'); |
|---|
| 74 |
|
|---|
| 75 |
$user->addCredential('admin'); |
|---|
| 76 |
$t->is($user->hasCredential('admin'), true, '->addCredential() takes a credential as its first argument'); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$t->is($user->hasCredential(array('admin', 'user')), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
$t->is($user->hasCredential(array(array('admin', 'user'))), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
$t->is($user->hasCredential(array(array('admin', 'user'), 'owner')), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 86 |
$user->addCredential('owner'); |
|---|
| 87 |
$t->is($user->hasCredential(array(array('admin', 'user'), 'owner')), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
$user->clearCredentials(); |
|---|
| 92 |
$credential = array(array('root', 'admin', 'editor', array('supplier', 'owner'), array('supplier', 'group'), 'accounts')); |
|---|
| 93 |
$t->is($user->hasCredential($credential), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 94 |
$user->addCredential('admin'); |
|---|
| 95 |
$t->is($user->hasCredential($credential), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 96 |
$user->clearCredentials(); |
|---|
| 97 |
$user->addCredential('supplier'); |
|---|
| 98 |
$t->is($user->hasCredential($credential), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 99 |
$user->addCredential('owner'); |
|---|
| 100 |
$t->is($user->hasCredential($credential), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
$user->clearCredentials(); |
|---|
| 105 |
$credential = array(array('root', array('supplier', array('owner', 'quasiowner')), 'accounts')); |
|---|
| 106 |
$t->is($user->hasCredential($credential), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 107 |
$user->addCredential('root'); |
|---|
| 108 |
$t->is($user->hasCredential($credential), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 109 |
$user->clearCredentials(); |
|---|
| 110 |
$user->addCredential('supplier'); |
|---|
| 111 |
$t->is($user->hasCredential($credential), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 112 |
$user->addCredential('owner'); |
|---|
| 113 |
$t->is($user->hasCredential($credential), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 114 |
$user->addCredential('quasiowner'); |
|---|
| 115 |
$t->is($user->hasCredential($credential), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 116 |
$user->removeCredential('owner'); |
|---|
| 117 |
$t->is($user->hasCredential($credential), true, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 118 |
$user->removeCredential('supplier'); |
|---|
| 119 |
$t->is($user->hasCredential($credential), false, '->hasCredential() can takes an array of credential as a parameter'); |
|---|
| 120 |
|
|---|
| 121 |
$user->clearCredentials(); |
|---|
| 122 |
$user->addCredential('admin'); |
|---|
| 123 |
$user->addCredential('user'); |
|---|
| 124 |
$t->is($user->hasCredential('admin'), true); |
|---|
| 125 |
$t->is($user->hasCredential('user'), true); |
|---|
| 126 |
|
|---|
| 127 |
$user->addCredentials('superadmin', 'subscriber'); |
|---|
| 128 |
$t->is($user->hasCredential('subscriber'), true); |
|---|
| 129 |
$t->is($user->hasCredential('superadmin'), true); |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
$t->is($user->hasCredential(array(array('admin', array('user', 'subscriber')))), true); |
|---|
| 133 |
|
|---|
| 134 |
$user->addCredentials(array('superadmin1', 'subscriber1')); |
|---|
| 135 |
$t->is($user->hasCredential('subscriber1'), true); |
|---|
| 136 |
$t->is($user->hasCredential('superadmin1'), true); |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
$t->is($user->hasCredential(array(array('admin', array('user', 'subscriber'), array('superadmin1', 'subscriber1')))), true); |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
$user->clearCredentials(); |
|---|
| 143 |
$user->addCredentials(array('1', 2)); |
|---|
| 144 |
$t->is($user->hasCredential(1), true, '->hasCrendential() supports numerical credentials'); |
|---|
| 145 |
$t->is($user->hasCredential('2'), true, '->hasCrendential() supports numerical credentials'); |
|---|
| 146 |
$t->is($user->hasCredential(array('1', 2)), true, '->hasCrendential() supports numerical credentials'); |
|---|
| 147 |
$t->is($user->hasCredential(array(1, '2')), true, '->hasCrendential() supports numerical credentials'); |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
$t->diag('->removeCredential()'); |
|---|
| 151 |
$user->removeCredential('user'); |
|---|
| 152 |
$t->is($user->hasCredential('user'), false); |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
$t->diag('->clearCredentials()'); |
|---|
| 156 |
$user->clearCredentials(); |
|---|
| 157 |
$t->is($user->hasCredential('subscriber'), false); |
|---|
| 158 |
$t->is($user->hasCredential('superadmin'), false); |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
$user->setAuthenticated(true); |
|---|
| 162 |
$user->shutdown(); |
|---|
| 163 |
$user = new sfBasicSecurityUser($dispatcher, $storage, array('timeout' => 0)); |
|---|
| 164 |
$t->is($user->isTimedOut(), true, '->initialize() times out the user if no request made for a long time'); |
|---|
| 165 |
|
|---|
| 166 |
$user = new sfBasicSecurityUser($dispatcher, $storage, array('timeout' => false)); |
|---|
| 167 |
$t->is($user->isTimedOut(), false, '->initialize() takes a timeout parameter which can be false to disable session timeout'); |
|---|
| 168 |
|
|---|
| 169 |
sfToolkit::clearDirectory($sessionPath); |
|---|
| 170 |
|
|---|