summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authortof <>2007-06-15 11:24:56 +0000
committertof <>2007-06-15 11:24:56 +0000
commit64418ade2e1e87f835e9ba25c393d359f0c99487 (patch)
tree766130c35e8d1b069a2b18f6e4f0bc14d6b6cb06 /tests
parent5221bdcec986254d443018a5ad8d1dc25ca55226 (diff)
Implement unit tests for TUser, TUserManager
Start implement unit tests for TAuthManager (still incomplete)
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/Security/TAuthManagerTest.php56
-rw-r--r--tests/unit/Security/TUserManagerTest.php101
-rw-r--r--tests/unit/Security/TUserTest.php76
3 files changed, 211 insertions, 22 deletions
diff --git a/tests/unit/Security/TAuthManagerTest.php b/tests/unit/Security/TAuthManagerTest.php
index 6e3c05a9..7e628a61 100644
--- a/tests/unit/Security/TAuthManagerTest.php
+++ b/tests/unit/Security/TAuthManagerTest.php
@@ -2,32 +2,82 @@
require_once dirname(__FILE__).'/../phpunit.php';
Prado::using('System.Security.TAuthManager');
+Prado::using('System.Security.TUserManager');
+Prado::using('System.Xml.TXmlDocument');
/**
* @package System.Security
*/
class TAuthManagerTest extends PHPUnit_Framework_TestCase {
+
+ public static $app = null;
+ public static $usrMgr = null;
public function setUp() {
+ ini_set('session.use_cookies',0);
+ ini_set('session.cache_limiter', 'none');
+ if(self::$app === null) {
+ self::$app = new TApplication(dirname(__FILE__).'/app');
+ }
+
+ // Make a fake user manager module
+ if (self::$usrMgr === null) {
+ self::$usrMgr=new TUserManager ();
+ $config=new TXmlDocument('1.0','utf8');
+ $config->loadFromString('<users><user name="Joe" password="demo"/><user name="John" password="demo" /><role name="Administrator" users="John" /><role name="Writer" users="Joe,John" /></users>');
+ self::$usrMgr->init($config);
+ self::$app->setModule('users', self::$usrMgr);
+ }
}
public function tearDown() {
}
public function testInit() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $authManager=new TAuthManager ();
+ // Catch exception with null usermgr
+ try {
+ $authManager->init(null);
+ self::fail ('Expected TConfigurationException not thrown');
+ } catch (TConfigurationException $e) {}
+
+ $authManager->setUserManager('users');
+ $authManager->init (null);
+ self::assertEquals(self::$usrMgr, $authManager->getUserManager());
}
public function testUserManager() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $authManager=new TAuthManager ();
+ $authManager->setUserManager('users');
+ $authManager->init(null);
+ self::assertEquals(self::$usrMgr, $authManager->getUserManager());
+
+ // test change
+ try {
+ $authManager->setUserManager('invalid');
+ self::fail ('Expected TInvalidOperationException not thrown');
+ } catch (TInvalidOperationException $e) {}
+
}
public function testLoginPage() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $authManager=new TAuthManager ();
+ $authManager->setUserManager('users');
+ $authManager->init(null);
+ $authManager->setLoginPage ('LoginPage');
+ self::assertEquals('LoginPage', $authManager->getLoginPage());
}
public function testDoAuthentication() {
throw new PHPUnit_Framework_IncompleteTestError();
+ // Not yet finished, Session won't start because of headers :( :(
+
+ $authManager=new TAuthManager ();
+ $authManager->setUserManager('users');
+ $authManager->init(null);
+ $authManager->setLoginPage ('LoginPage');
+ self::$app->raiseEvent ('onAuthentication', self::$app, null);
+
}
public function testDoAuthorization() {
diff --git a/tests/unit/Security/TUserManagerTest.php b/tests/unit/Security/TUserManagerTest.php
index 8937ba22..68f2b32b 100644
--- a/tests/unit/Security/TUserManagerTest.php
+++ b/tests/unit/Security/TUserManagerTest.php
@@ -7,47 +7,130 @@ Prado::using('System.Security.TUserManager');
* @package System.Security
*/
class TUserManagerTest extends PHPUnit_Framework_TestCase {
+
+ public static $app=null;
+ public static $config=null;
+
public function setUp() {
+
+ if(self::$app === null) {
+ self::$app = new TApplication(dirname(__FILE__).'/app');
+ prado::setPathofAlias('App', dirname(__FILE__));
+ }
+
+ if (self::$config === null) {
+ // Simulate a config file
+ self::$config=new TXmlDocument('1.0','utf8');
+ self::$config->loadFromString('<users><user name="Joe" password="demo"/><user name="John" password="demo" /><user name="test" password="test" roles="Reader, User"/><role name="Administrator" users="John" /><role name="Writer" users="Joe, John" /></users>');
+ }
}
public function tearDown() {
}
public function testInit() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ self::assertEquals(array('joe'=>'demo','john'=>'demo', 'test' => 'test'), $userManager->getUsers());
+ $userManager=null;
+ // Test with a file
+ if (is_writable (dirname(__FILE__))) {
+ self::$config->saveToFile(dirname(__FILE__).'/users.xml');
+ $userManager=new TUserManager();
+ $userManager->setUserFile('App.users');
+ $userManager->init (new TXmlDocument()); // Empty config
+ self::assertEquals(array('joe'=>'demo','john'=>'demo', 'test' => 'test'), $userManager->getUsers());
+ unlink (dirname(__FILE__).'/users.xml');
+ }
}
public function testUsers() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ self::assertEquals(array('joe'=>'demo','john'=>'demo', 'test' => 'test'), $userManager->getUsers());
}
public function testRoles() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ self::assertEquals(array('joe'=>array ('Writer'), 'john'=>array ('Administrator','Writer'), 'test' => array ('Reader', 'User')), $userManager->getRoles());
}
public function testUserFile() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ try {
+ $userManager->setUserFile('invalidFile');
+ self::fail ('Exception TConfigurationException not thrown');
+ } catch (TConfigurationException $e) {}
+ $userManager=null;
+ if (is_writable (dirname(__FILE__))) {
+ self::$config->saveToFile(dirname(__FILE__).'/users.xml');
+ $userManager=new TUserManager();
+ $userManager->setUserFile('App.users');
+ $userManager->init (new TXmlDocument()); // Empty config
+ self::assertEquals(dirname(__FILE__).'/users.xml', $userManager->getUserFile());
+ unlink (dirname(__FILE__).'/users.xml');
+ $userManager=null;
+ }
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ try {
+ $userManager->setUserFile('App.users');
+ self::fail ('Exception TInvalidOperationException not thrown');
+ } catch (TInvalidOperationException $e) {}
}
public function testGuestName() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ self::assertEquals('Guest', $userManager->getGuestName());
+ $userManager->setGuestName('Invite');
+ self::assertEquals('Invite', $userManager->getGuestName());
}
public function testPasswordMode() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->setPasswordMode('Clear');
+ self::assertEquals('Clear', $userManager->getPasswordMode());
+ $userManager->setPasswordMode('MD5');
+ self::assertEquals('MD5', $userManager->getPasswordMode());
+ $userManager->setPasswordMode('SHA1');
+ self::assertEquals('SHA1', $userManager->getPasswordMode());
+ try {
+ $userManager->setPasswordMode('Invalid');
+ self::fail ('Exception TInvalidDataValueException not thrown');
+ } catch (TInvalidDataValueException $e) {}
+
}
public function testValidateUser() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ $userManager->setPasswordMode('Clear');
+ self::assertTrue($userManager->validateUser('Joe', 'demo'));
+ self::assertFalse($userManager->validateUser('John', 'bad'));
}
public function testUser() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ $guest=$userManager->getUser(null);
+ self::assertType('TUser', $guest);
+ self::assertTrue($guest->getIsGuest());
+ $user=$userManager->getUser('joe');
+ self::assertType('TUser', $user);
+ self::assertEquals('joe', $user->getName());
+ self::assertEquals(array ('Writer'), $user->getRoles());
+ self::assertFalse($user->getIsGuest());
+ self::assertNull($userManager->getUser('badUser'));
}
public function testSwitchToGuest() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $userManager=new TUserManager ();
+ $userManager->init (self::$config);
+ $user=$userManager->getUser('joe');
+ $userManager->switchToGuest($user);
+ self::assertTrue($user->getIsGuest());
}
}
diff --git a/tests/unit/Security/TUserTest.php b/tests/unit/Security/TUserTest.php
index 67ac6eb4..597fac97 100644
--- a/tests/unit/Security/TUserTest.php
+++ b/tests/unit/Security/TUserTest.php
@@ -2,56 +2,112 @@
require_once dirname(__FILE__).'/../phpunit.php';
Prado::using('System.Security.TUser');
+Prado::using('System.Security.TUserManager');
/**
* @package System.Security
*/
class TUserTest extends PHPUnit_Framework_TestCase {
+ public static $app=null;
+ public static $mgr=null;
public function setUp() {
+ if(self::$app === null) {
+ self::$app = new TApplication(dirname(__FILE__).'/app');
+ prado::setPathofAlias('App', dirname(__FILE__));
+ }
+
+ if (self::$mgr===null) {
+ $config=new TXmlDocument('1.0','utf8');
+ $config->loadFromString('<users><user name="Joe" password="demo"/><user name="John" password="demo" /><role name="Administrator" users="John" /><role name="Writer" users="Joe,John" /></users>');
+ self::$mgr=new TUserManager();
+ self::$mgr->init($config);
+ }
}
public function tearDown() {
}
public function testConstruct() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ self::assertEquals('Guest', $user->getName());
+ self::assertEquals(self::$mgr, $user->getManager());
}
public function testManager() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ self::assertEquals(self::$mgr, $user->getManager());
}
public function testName() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ $user->setName('joe');
+ self::assertEquals('joe', $user->getName());
}
public function testIsGuest() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ $user->setName('John');
+ $user->setIsGuest(false);
+ $user->setRoles('Administrator, Writer');
+ self::assertFalse($user->getIsGuest());
+ $user->setIsGuest(true);
+ self::assertTrue($user->getIsGuest());
+ self::assertEquals(array(),$user->getRoles());
}
public function testRoles() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user=new TUser(self::$mgr);
+ $user->setRoles(array('Administrator','Writer'));
+ self::assertEquals(array('Administrator','Writer'), $user->getRoles());
+ $user->setRoles('Reader,User');
+ self::assertEquals(array('Reader','User'), $user->getRoles());
}
public function testIsInRole() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user=new TUser(self::$mgr);
+ $user->setRoles(array('Administrator','Writer'));
+ // Roles are case insensitive
+ self::assertTrue($user->IsInRole('writer'));
+ self::assertTrue($user->IsInRole('Writer'));
+ self::assertFalse($user->isInRole('Reader'));
}
public function testSaveToString() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ $user->setName('John');
+ $user->setIsGuest(false);
+ $user->setRoles('Administrator, Writer');
+ // State array should now be :
+ $assumedState=array ('Name' => 'John', 'IsGuest' => false, 'Roles' => array ('Administrator', 'Writer'));
+ self::assertEquals(serialize($assumedState), $user->saveToString());
}
public function testLoadFromString() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ $user->setName('John');
+ $user->setIsGuest(false);
+ $user->setRoles('Administrator, Writer');
+ $save=$user->saveToString();
+
+ $user2 = new TUser (self::$mgr);
+ $user2->loadFromString($save);
+
+ self::assertEquals($user, $user2);
}
+ /* getState & setState are protected methods, will be tested with other tests.
public function testState() {
throw new PHPUnit_Framework_IncompleteTestError();
}
-
+ */
+
public function testStateChanged() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $user = new TUser (self::$mgr);
+ $user->setName('John');
+ self::assertTrue($user->getStateChanged());
+ $user->setStateChanged(false);
+ self::assertFalse($user->getStateChanged());
}
}