ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilChatroomUserTest.php
Go to the documentation of this file.
1<?php
2
8{
9
13 protected $ilUserMock;
14
18 protected $ilChatroomMock;
19
23 protected $user;
24
25 protected function setUp()
26 {
27 if (defined('ILIAS_PHPUNIT_CONTEXT')) {
28 include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
29 ilUnitUtil::performInitialisation();
30 } else {
31 chdir(dirname(__FILE__));
32 chdir('../../../');
33 }
34
35 require_once './Modules/Chatroom/classes/class.ilChatroomUser.php';
36 //require_once 'Services/User/classes/class.ilObjUser.php';
37 $this->ilUserMock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(
38 array('getId', 'isAnonymous', 'getLogin', 'getPublicName', 'getFirstname', 'getLastname')
39 )->getMock();
40 $this->ilChatroomMock = $this->getMockBuilder('ilChatroom')->disableOriginalConstructor()->setMethods(
41 array('getRoomId', 'getSetting')
42 )->getMock();
43
44 $this->user = new ilChatroomUser($this->ilUserMock, $this->ilChatroomMock);
45 }
46
47 public function testConstructor()
48 {
49 $this->assertInstanceOf('ilChatroomUser', $this->user);
50 }
51
53 {
54 $userId = 6;
55
56 $this->ilUserMock->expects($this->once())->method('getId')->will($this->returnValue($userId));
57 $this->ilUserMock->expects($this->once())->method('isAnonymous')->will($this->returnValue(false));
58
59 $this->assertEquals($userId, $this->user->getUserId());
60 }
61
63 {
64 $userId = 6;
65 $roomId = 99;
66
67 $this->ilUserMock->expects($this->once())->method('getId')->will($this->returnValue($userId));
68 $this->ilUserMock->expects($this->once())->method('isAnonymous')->will($this->returnValue(true));
69
70 $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue($roomId));
71
72 $_SESSION['chat'] = array(
73 $roomId => array(
74 'user_id' => $userId,
75 ),
76 );
77
78 $this->assertEquals($userId, $this->user->getUserId());
79 }
80
82 {
83 $this->ilUserMock->expects($this->once())->method('getId')->will($this->returnValue(null));
84 $this->ilUserMock->expects($this->once())->method('isAnonymous')->will($this->returnValue(true));
85
86 $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue(99));
87
88 $this->assertNotNull($this->user->getUserId());
89 }
90
96 public function testSetUsername($username, $expected)
97 {
98 $this->user->setUsername($username);
99 $this->assertEquals($expected, $this->user->getUsername());
100 }
101
103 {
104 $username = 'username';
105 $roomId = 99;
106 $_SESSION['chat'][$roomId]['username'] = $username;
107
108 $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue(99));
109
110 $this->assertEquals($username, $this->user->getUsername());
111 }
112
118 {
119 $username = 'login';
120 $roomId = 99;
121 $_SESSION['chat'][$roomId]['username'] = ''; // Fix missing key warning
122
123 $this->ilUserMock->expects($this->once())->method('getLogin')->will($this->returnValue($username));
124 $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue($roomId));
125
126 $this->assertEquals($username, $this->user->getUsername());
127 }
128
129 public function testBuildAnonymousName()
130 {
131 $this->ilChatroomMock->expects($this->any())->method('getSetting')->will($this->returnValue('#_anonymous'));
132
133 $firstName = $this->user->buildAnonymousName();
134 $secondName = $this->user->buildAnonymousName();
135
136 $this->assertNotEquals($firstName, $secondName);
137 }
138
139 public function testBuildLogin()
140 {
141 $username = 'username';
142 $this->ilUserMock->expects($this->once())->method('getLogin')->will($this->returnValue($username));
143
144 $this->assertEquals($username, $this->user->buildLogin());
145 }
146
147 public function testBuildFullname()
148 {
149 $fullname = 'John Doe';
150 $this->ilUserMock->expects($this->once())->method('getPublicName')->will($this->returnValue($fullname));
151
152 $this->assertEquals($fullname, $this->user->buildFullname());
153 }
154
155 public function testBuildShortname()
156 {
157 $firstname = 'John';
158 $lastname = 'Doe';
159 $this->ilUserMock->expects($this->once())->method('getFirstname')->will($this->returnValue($firstname));
160 $this->ilUserMock->expects($this->once())->method('getLastname')->will($this->returnValue($lastname));
161
162 $this->assertEquals('J. Doe', $this->user->buildShortname());
163 }
164
166 {
167 $this->ilUserMock->expects($this->any())->method('isAnonymous')->will($this->returnValue(true));
168 $this->ilChatroomMock->expects($this->any())->method('getSetting')->will($this->returnValue('#_anonymous'));
169
170 $first = $this->user->getChatNameSuggestions();
171 $second = $this->user->getChatNameSuggestions();
172
173 $this->assertNotEquals($first, $second);
174 }
175
177 {
178 $this->ilUserMock->expects($this->any())->method('isAnonymous')->will($this->returnValue(false));
179 $this->ilUserMock->expects($this->once())->method('getFirstname')->will($this->returnValue('John'));
180 $this->ilUserMock->expects($this->once())->method('getLastname')->will($this->returnValue('Doe'));
181 $this->ilUserMock->expects($this->once())->method('getPublicName')->will($this->returnValue('John Doe'));
182 $this->ilUserMock->expects($this->once())->method('getLogin')->will($this->returnValue('jdoe'));
183 $this->ilChatroomMock->expects($this->any())->method('getSetting')->will($this->returnValue('#_anonymous'));
184
185 $suggestions = $this->user->getChatNameSuggestions();
186
187 $this->assertEquals('John Doe', $suggestions['fullname']);
188 $this->assertEquals('J. Doe', $suggestions['shortname']);
189 $this->assertEquals('jdoe', $suggestions['login']);
190 }
191
195 public function usernameDataProvider()
196 {
197 return array(
198 array('username', 'username'),
199 array('>username<', '&gt;username&lt;'),
200 );
201 }
202}
user()
Definition: user.php:4
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Class ilChatroomUserTest.
testSetUsername($username, $expected)
@dataProvider usernameDataProvider
Class ilChatroomUser.
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.