ILIAS  release_8 Revision v8.24
class.ilChatroomUserTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\MockObject\MockObject;
22
28{
32
33 public function testGetUserIdIfNotAnonymous(): void
34 {
35 $userId = 6;
36
37 $this->ilUserMock->expects($this->once())->method('getId')->willReturn($userId);
38 $this->ilUserMock->expects($this->once())->method('isAnonymous')->willReturn(false);
39
40 $this->assertSame($userId, $this->user->getUserId());
41 }
42
44 {
45 $userId = 6;
46 $roomId = 99;
47
48 $this->ilUserMock->expects($this->once())->method('getId')->willReturn($userId);
49 $this->ilUserMock->expects($this->once())->method('isAnonymous')->willReturn(true);
50
51 $this->ilChatroomMock->method('getRoomId')->willReturn($roomId);
52
53 $session = [
54 $roomId => [
55 'user_id' => $userId,
56 ],
57 ];
58 ilSession::set('chat', $session);
59
60 $this->assertSame($userId, $this->user->getUserId());
61 }
62
64 {
65 $this->ilUserMock->expects($this->once())->method('getId')->willReturn(0);
66 $this->ilUserMock->expects($this->once())->method('isAnonymous')->willReturn(true);
67
68 $this->ilChatroomMock->method('getRoomId')->willReturn(99);
69
70 $this->assertNotNull($this->user->getUserId());
71 }
72
78 public function testSetUsername(string $username, string $expected): void
79 {
80 $this->user->setUsername($username);
81 $this->assertSame($expected, $this->user->getUsername());
82 }
83
84 public function testGetUsernameFromSession(): void
85 {
86 $username = 'username';
87 $roomId = 99;
88
89 ilSession::set('chat', [
90 $roomId => [
91 'username' => $username,
92 ],
93 ]);
94
95 $this->ilChatroomMock->method('getRoomId')->willReturn(99);
96
97 $this->assertSame($username, $this->user->getUsername());
98 }
99
104 public function testGetUsernameFromIlObjUser(): void
105 {
106 $username = 'login';
107 $roomId = 99;
108 ilSession::set('chat', [
109 $roomId => [
110 'username' => '',
111 ],
112 ]);
113
114 $this->ilUserMock->expects($this->once())->method('getLogin')->willReturn($username);
115 $this->ilChatroomMock->method('getRoomId')->willReturn($roomId);
116
117 $this->assertSame($username, $this->user->getUsername());
118 }
119
120 public function testBuildAnonymousName(): void
121 {
122 $this->ilChatroomMock->method('getSetting')->willReturn('#_anonymous');
123
124 $firstName = $this->user->buildAnonymousName();
125 $secondName = $this->user->buildAnonymousName();
126
127 $this->assertNotEquals($firstName, $secondName);
128 }
129
130 public function testBuildLogin(): void
131 {
132 $username = 'username';
133 $this->ilUserMock->expects($this->once())->method('getLogin')->willReturn($username);
134
135 $this->assertSame($username, $this->user->buildLogin());
136 }
137
138 public function testBuildFullname(): void
139 {
140 $fullname = 'John Doe';
141 $this->ilUserMock->expects($this->once())->method('getPublicName')->willReturn($fullname);
142
143 $this->assertSame($fullname, $this->user->buildFullname());
144 }
145
146 public function testBuildShortname(): void
147 {
148 $firstname = 'John';
149 $lastname = 'Doe';
150 $this->ilUserMock->expects($this->once())->method('getFirstname')->willReturn($firstname);
151 $this->ilUserMock->expects($this->once())->method('getLastname')->willReturn($lastname);
152
153 $this->assertSame('J. Doe', $this->user->buildShortname());
154 }
155
157 {
158 $this->ilUserMock->method('isAnonymous')->willReturn(true);
159 $this->ilChatroomMock->method('getSetting')->willReturn('#_anonymous');
160
161 $first = $this->user->getChatNameSuggestions();
162 $second = $this->user->getChatNameSuggestions();
163
164 $this->assertNotEquals($first, $second);
165 }
166
168 {
169 $this->ilUserMock->method('isAnonymous')->willReturn(false);
170 $this->ilUserMock->expects($this->once())->method('getFirstname')->willReturn('John');
171 $this->ilUserMock->expects($this->once())->method('getLastname')->willReturn('Doe');
172 $this->ilUserMock->expects($this->once())->method('getPublicName')->willReturn('John Doe');
173 $this->ilUserMock->expects($this->once())->method('getLogin')->willReturn('jdoe');
174 $this->ilChatroomMock->method('getSetting')->willReturn('#_anonymous');
175
176 $suggestions = $this->user->getChatNameSuggestions();
177
178 $this->assertSame('John Doe', $suggestions['fullname']);
179 $this->assertSame('J. Doe', $suggestions['shortname']);
180 $this->assertSame('jdoe', $suggestions['login']);
181 }
182
186 public function usernameDataProvider(): array
187 {
188 return [
189 ['username', 'username'],
190 ['>username<', '&gt;username&lt;'],
191 ];
192 }
193
194 protected function setUp(): void
195 {
196 parent::setUp();
197
198 $this->ilUserMock = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(
199 ['getId', 'isAnonymous', 'getLogin', 'getPublicName', 'getFirstname', 'getLastname']
200 )->getMock();
201 $this->ilChatroomMock = $this->getMockBuilder(ilChatroom::class)->disableOriginalConstructor()->onlyMethods(
202 ['getRoomId', 'getSetting']
203 )->getMock();
204
205 $this->user = new ilChatroomUser($this->ilUserMock, $this->ilChatroomMock);
206 }
207}
Class ilChatroomAbstractTest.
Class ilChatroomUserTest.
testSetUsername(string $username, string $expected)
@dataProvider usernameDataProvider
Class ilChatroomUser.
User class.
static set(string $a_var, $a_val)
Set a value.
$session