summaryrefslogtreecommitdiff
path: root/tests/unit/Security/TUserManagerTest.php
blob: 2952927bc941257d61dfa85a311eb8ac45e1084f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
require_once dirname(__FILE__).'/../phpunit.php';

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() {
		$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() {
		$userManager=new TUserManager ();
		$userManager->init (self::$config);
		self::assertEquals(array('joe'=>'demo','john'=>'demo', 'test' => 'test'), $userManager->getUsers());
	}
	
	public function testRoles() {
		$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() {
		$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() {
		$userManager=new TUserManager ();
		self::assertEquals('Guest', $userManager->getGuestName());
		$userManager->setGuestName('Invite');
		self::assertEquals('Invite', $userManager->getGuestName());
	}
	
	public function testPasswordMode() {
		$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() {
		$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() {
		$userManager=new TUserManager ();
		$userManager->init (self::$config);
		$guest=$userManager->getUser(null);
		self::assertInstanceOf('TUser', $guest);
		self::assertTrue($guest->getIsGuest());
		$user=$userManager->getUser('joe');
		self::assertInstanceOf('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() {
		$userManager=new TUserManager ();
		$userManager->init (self::$config);
		$user=$userManager->getUser('joe');
		$userManager->switchToGuest($user);
		self::assertTrue($user->getIsGuest());
	}

}

?>