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