summaryrefslogtreecommitdiff
path: root/tests/unit/Security/TUserTest.php
blob: c707cb25108cecc5095789d698c711d635a9bc3c (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
<?php

use Prado\Prado;
use Prado\Security\TUser;
use Prado\Security\TUserManager;
use Prado\TApplication;
use Prado\Xml\TXmlDocument;


/**
 * @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() {
		$user = new TUser (self::$mgr);
		self::assertEquals('Guest', $user->getName());
		self::assertEquals(self::$mgr, $user->getManager());
	}

	public function testManager() {
		$user = new TUser (self::$mgr);
		self::assertEquals(self::$mgr, $user->getManager());
	}

	public function testName() {
		$user = new TUser (self::$mgr);
		$user->setName('joe');
		self::assertEquals('joe', $user->getName());
	}

	public function testIsGuest() {
		$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() {
		$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() {
		$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() {
		$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() {
		$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() {
		$user = new TUser (self::$mgr);
		$user->setName('John');
		self::assertTrue($user->getStateChanged());
		$user->setStateChanged(false);
		self::assertFalse($user->getStateChanged());
	}

}