ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilChatroomUserTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 {
31  protected ilChatroomUser $user;
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 
43  public function testGetUserIdFromSessionIfAnonymous(): void
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 
76  public function testSetUsername(string $username, string $expected): void
77  {
78  $this->user->setUsername($username);
79  $this->assertSame($expected, $this->user->getUsername());
80  }
81 
82  public function testGetUsernameFromSession(): void
83  {
84  $username = 'username';
85  $roomId = 99;
86 
87  ilSession::set('chat', [
88  $roomId => [
89  'username' => $username,
90  ],
91  ]);
92 
93  $this->ilChatroomMock->method('getRoomId')->willReturn(99);
94 
95  $this->assertSame($username, $this->user->getUsername());
96  }
97 
102  public function testGetUsernameFromIlObjUser(): void
103  {
104  $username = 'login';
105  $roomId = 99;
106  ilSession::set('chat', [
107  $roomId => [
108  'username' => '',
109  ],
110  ]);
111 
112  $this->ilUserMock->expects($this->once())->method('getPublicName')->willReturn($username);
113  $this->ilChatroomMock->method('getRoomId')->willReturn($roomId);
114 
115  $this->assertSame($username, $this->user->getUsername());
116  }
117 
118  public function testBuildAnonymousName(): void
119  {
120  $this->ilChatroomMock->method('getSetting')->willReturn('#_anonymous');
121 
122  $firstName = $this->user->buildAnonymousName();
123  $secondName = $this->user->buildAnonymousName();
124 
125  $this->assertNotEquals($firstName, $secondName);
126  }
127 
128  public function testBuildLogin(): void
129  {
130  $username = 'username';
131  $this->ilUserMock->expects($this->once())->method('getLogin')->willReturn($username);
132 
133  $this->assertSame($username, $this->user->buildLogin());
134  }
135 
136  public function testBuildFullname(): void
137  {
138  $fullname = 'John Doe';
139  $this->ilUserMock->expects($this->once())->method('getPublicName')->willReturn($fullname);
140 
141  $this->assertSame($fullname, $this->user->buildFullname());
142  }
143 
144  public function testBuildShortname(): void
145  {
146  $firstname = 'John';
147  $lastname = 'Doe';
148  $this->ilUserMock->expects($this->once())->method('getFirstname')->willReturn($firstname);
149  $this->ilUserMock->expects($this->once())->method('getLastname')->willReturn($lastname);
150 
151  $this->assertSame('J. Doe', $this->user->buildShortname());
152  }
153 
155  {
156  $this->ilUserMock->method('isAnonymous')->willReturn(true);
157  $this->ilChatroomMock->method('getSetting')->willReturn('#_anonymous');
158 
159  $first = $this->user->getChatNameSuggestions();
160  $second = $this->user->getChatNameSuggestions();
161 
162  $this->assertNotEquals($first, $second);
163  }
164 
166  {
167  $this->ilUserMock->method('isAnonymous')->willReturn(false);
168  $this->ilUserMock->expects($this->once())->method('getFirstname')->willReturn('John');
169  $this->ilUserMock->expects($this->once())->method('getLastname')->willReturn('Doe');
170  $this->ilUserMock->expects($this->once())->method('getPublicName')->willReturn('John Doe');
171  $this->ilUserMock->expects($this->once())->method('getLogin')->willReturn('jdoe');
172  $this->ilChatroomMock->method('getSetting')->willReturn('#_anonymous');
173 
174  $suggestions = $this->user->getChatNameSuggestions();
175 
176  $this->assertSame('John Doe', $suggestions['fullname']);
177  $this->assertSame('J. Doe', $suggestions['shortname']);
178  $this->assertSame('jdoe', $suggestions['login']);
179  }
180 
181  public static function usernameDataProvider(): array
182  {
183  return [
184  ['username', 'username'],
185  ['>username<', '&gt;username&lt;'],
186  ];
187  }
188 
189  protected function setUp(): void
190  {
191  parent::setUp();
192 
193  $this->ilUserMock = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(
194  ['getId', 'isAnonymous', 'getLogin', 'getPublicName', 'getFirstname', 'getLastname']
195  )->getMock();
196  $this->ilChatroomMock = $this->getMockBuilder(ilChatroom::class)->disableOriginalConstructor()->onlyMethods(
197  ['getRoomId', 'getSetting']
198  )->getMock();
199 
200  $this->user = new ilChatroomUser($this->ilUserMock, $this->ilChatroomMock);
201  }
202 }
testSetUsername(string $username, string $expected)
usernameDataProvider
Class ilChatroomUserTest.
Class ilChatroomAbstractTest.
Class ilChatroomUser.
static set(string $a_var, $a_val)
Set a value.