diff options
author | tof <> | 2007-06-15 11:24:56 +0000 |
---|---|---|
committer | tof <> | 2007-06-15 11:24:56 +0000 |
commit | 64418ade2e1e87f835e9ba25c393d359f0c99487 (patch) | |
tree | 766130c35e8d1b069a2b18f6e4f0bc14d6b6cb06 /tests/unit/Security/TUserManagerTest.php | |
parent | 5221bdcec986254d443018a5ad8d1dc25ca55226 (diff) |
Implement unit tests for TUser, TUserManager
Start implement unit tests for TAuthManager (still incomplete)
Diffstat (limited to 'tests/unit/Security/TUserManagerTest.php')
-rw-r--r-- | tests/unit/Security/TUserManagerTest.php | 101 |
1 files changed, 92 insertions, 9 deletions
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()); } } |