ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilForumNotificationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\TestCase;
23use PHPUnit\Framework\MockObject\MockObject;
24
25class ilForumNotificationTest extends TestCase
26{
27 private MockObject&ilDBInterface $database;
28 private MockObject&ilObjUser $user;
29 private MockObject&ilTree $tree;
30 private ?Container $dic = null;
31
32 public function testConstruct(): void
33 {
34 $this->assertInstanceOf(ilForumNotification::class, new ilForumNotification(938));
35 }
36
37 public function testGetterAndSetter(): void
38 {
39 $instance = new ilForumNotification(940);
40 $instance->setNotificationId(1);
41 $this->assertSame(1, $instance->getNotificationId());
42 $instance->setUserId(2);
43 $this->assertSame(2, $instance->getUserId());
44 $instance->setForumId(3);
45 $this->assertSame(3, $instance->getForumId());
46 $instance->setThreadId(4);
47 $this->assertSame(4, $instance->getThreadId());
48 $instance->setInterestedEvents(5);
49 $this->assertSame(5, $instance->getInterestedEvents());
50 $instance->setAdminForce(true);
51 $this->assertTrue($instance->getAdminForce());
52 $instance->setUserToggle(true);
53 $this->assertTrue($instance->getUserToggle());
54 $instance->setForumRefId(6);
55 $this->assertSame(6, $instance->getForumRefId());
56 $instance->setUserIdNoti(7);
57 $this->assertSame(7, $instance->getUserIdNoti());
58 }
59
60 public function testIsAdminForceNotification(): void
61 {
62 $forumId = 745;
63 $userId = 271;
64
65 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
66 $this->database->expects(self::once())->method('queryF')->with(
67 '
68 SELECT admin_force_noti FROM frm_notification
69 WHERE user_id = %s
70 AND frm_id = %s
71 AND user_id_noti > %s ',
72 ['integer', 'integer', 'integer'],
73 [$userId, $forumId, 0]
74 )->willReturn($mockStatement);
75 $this->database->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['admin_force_noti' => '1']);
76
77 $instance = new ilForumNotification(375);
78 $instance->setForumId($forumId);
79 $instance->setUserId($userId);
80
81 $this->assertTrue($instance->isAdminForceNotification());
82 }
83
84 public function testIsAdminForceNotificationFailed(): void
85 {
86 $forumId = 745;
87 $userId = 271;
88
89 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
90 $this->database->expects(self::once())->method('queryF')->with(
91 '
92 SELECT admin_force_noti FROM frm_notification
93 WHERE user_id = %s
94 AND frm_id = %s
95 AND user_id_noti > %s ',
96 ['integer', 'integer', 'integer'],
97 [$userId, $forumId, 0]
98 )->willReturn($mockStatement);
99 $this->database->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
100
101 $instance = new ilForumNotification(375);
102 $instance->setForumId($forumId);
103 $instance->setUserId($userId);
104
105 $this->assertFalse($instance->isAdminForceNotification());
106 }
107
108 public function testIsUserToggleNotification(): void
109 {
110 $forumId = 745;
111 $userId = 271;
112
113 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
114 $this->database->expects(self::once())->method('queryF')->with(
115 '
116 SELECT user_toggle_noti FROM frm_notification
117 WHERE user_id = %s
118 AND frm_id = %s
119 AND user_id_noti > %s',
120 ['integer', 'integer', 'integer'],
121 [$userId, $forumId, 0]
122 )->willReturn($mockStatement);
123 $this->database->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['user_toggle_noti' => '1']);
124
125 $instance = new ilForumNotification(375);
126 $instance->setForumId($forumId);
127 $instance->setUserId($userId);
128
129 $this->assertTrue($instance->isUserToggleNotification());
130 }
131
133 {
134 $forumId = 745;
135 $userId = 271;
136
137 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
138 $this->database->expects(self::once())->method('queryF')->with(
139 '
140 SELECT user_toggle_noti FROM frm_notification
141 WHERE user_id = %s
142 AND frm_id = %s
143 AND user_id_noti > %s',
144 ['integer', 'integer', 'integer'],
145 [$userId, $forumId, 0]
146 )->willReturn($mockStatement);
147 $this->database->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
148
149 $instance = new ilForumNotification(375);
150 $instance->setForumId($forumId);
151 $instance->setUserId($userId);
152
153 $this->assertFalse($instance->isUserToggleNotification());
154 }
155
156 public function testInsertAdminForce(): void
157 {
158 $userToggle = true;
159 $adminForce = false;
160 $forumId = 970;
161 $userId = 530;
162 $objUserId = 3627;
163 $interested_in_events = 15;
164 $nextId = 3737;
165
166 $this->user->expects(self::once())->method('getId')->willReturn($objUserId);
167
168 $this->database->expects(self::once())->method('nextId')->willReturn($nextId);
169 $this->database->expects(self::once())->method('manipulateF')->with(
170 '
171 INSERT INTO frm_notification
172 (notification_id, user_id, frm_id, admin_force_noti, user_toggle_noti, interested_events, user_id_noti)
173 VALUES(%s, %s, %s, %s, %s, %s, %s)',
174 ['integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'integer'],
175 [
176 $nextId,
177 $userId,
178 $forumId,
179 $adminForce,
180 $userToggle,
181 $interested_in_events,
182 $objUserId
183 ]
184 );
185
186 $instance = new ilForumNotification(480);
187 $instance->setUserId($userId);
188 $instance->setForumId($forumId);
189 $instance->setAdminForce($adminForce);
190 $instance->setUserToggle($userToggle);
191 $instance->setInterestedEvents($interested_in_events);
192
193 $instance->insertAdminForce();
194 }
195
196 public function testDeleteAdminForce(): void
197 {
198 $userId = 739;
199 $forumId = 48849;
200
201 $this->database->expects(self::once())->method('manipulateF')->with(
202 '
203 DELETE FROM frm_notification
204 WHERE user_id = %s
205 AND frm_id = %s
206 AND admin_force_noti = %s
207 AND user_id_noti > %s',
208 ['integer', 'integer', 'integer', 'integer'],
209 [$userId, $forumId, 1, 0]
210 );
211
212 $instance = new ilForumNotification(292);
213 $instance->setUserId($userId);
214 $instance->setForumId($forumId);
215
216 $instance->deleteAdminForce();
217 }
218
219 public function testDeleteUserToggle(): void
220 {
221 $forumId = 3877;
222 $userId = 3839;
223 $this->database->expects(self::once())->method('manipulateF')->with(
224 '
225 DELETE FROM frm_notification
226 WHERE user_id = %s
227 AND frm_id = %s
228 AND admin_force_noti = %s
229 AND user_toggle_noti = %s
230 AND user_id_noti > %s',
231 ['integer', 'integer', 'integer', 'integer', 'integer'],
232 [$userId, $forumId, 1, 1, 0]
233 );
234
235 $instance = new ilForumNotification(3830);
236 $instance->setUserId($userId);
237 $instance->setForumId($forumId);
238 $instance->deleteUserToggle();
239 }
240
241 public function testupdateUserToggle(): void
242 {
243 $userToggle = true;
244 $forumId = 3877;
245 $userId = 3839;
246
247 $this->database->expects(self::once())->method('manipulateF')->with(
248 'UPDATE frm_notification SET user_toggle_noti = %s WHERE user_id = %s AND frm_id = %s AND admin_force_noti = %s',
249 ['integer', 'integer', 'integer', 'integer'],
250 [$userToggle, $userId, $forumId, 1]
251 );
252
253 $instance = new ilForumNotification(3830);
254 $instance->setUserId($userId);
255 $instance->setForumId($forumId);
256 $instance->setUserToggle($userToggle);
257 $instance->updateUserToggle();
258 }
259
260 public function testCheckForumsExistsInsert(): void
261 {
262 $userId = 927;
263 $refId = 847;
264 $nodeData = [
265 'child' => $refId
266 ];
267 $subTree = [['child' => 3719, 'ref_id' => 3738, 'obj_id' => 182]];
268 $pathNode = [['child' => $refId, 'type' => 'aa']];
269
270 $this->tree->expects(self::once())->method('getNodeData')->with($refId)->willReturn($nodeData);
271 $this->tree->expects(self::once())->method('getNodePath')->with($subTree[0]['child'], $refId)->willReturn($pathNode);
272 $this->tree->expects(self::once())->method('getSubTree')->with(
273 $nodeData,
274 true,
275 ['frm']
276 )->willReturn($subTree);
277
279 }
280
281 public function testUpdate(): void
282 {
283 $forumId = 1122;
284 $userId = 484;
285 $events = 848;
286 $userToggle = true;
287 $adminForce = false;
288 $this->database->expects(self::once())->method('manipulateF')->with(
289 'UPDATE frm_notification SET admin_force_noti = %s, user_toggle_noti = %s, ' .
290 'interested_events = %s WHERE user_id = %s AND frm_id = %s',
291 ['integer', 'integer', 'integer', 'integer', 'integer'],
292 [
293 (int) $adminForce,
294 (int) $userToggle,
295 $events,
296 $userId,
297 $forumId
298 ]
299 );
300
301 $instance = new ilForumNotification(8380);
302 $instance->setAdminForce($adminForce);
303 $instance->setUserToggle($userToggle);
304 $instance->setInterestedEvents($events);
305 $instance->setUserId($userId);
306 $instance->setForumId($forumId);
307
308 $instance->update();
309 }
310
311 public function testDeleteNotificationAllUsers(): void
312 {
313 $forumId = 490;
314 $this->database->expects(self::once())->method('manipulateF')->with(
315 'DELETE FROM frm_notification WHERE frm_id = %s AND user_id_noti > %s',
316 ['integer', 'integer'],
317 [$forumId, 0]
318 );
319
320 $instance = new ilForumNotification(3490);
321 $instance->setForumId($forumId);
322
323 $instance->deleteNotificationAllUsers();
324 }
325
326
327 public function testRead(): void
328 {
329 $forumId = 4859;
330 $row = [
331 'notification_id' => 789,
332 'user_id' => 490,
333 'frm_id' => 380,
334 'thread_id' => 280,
335 'admin_force_noti' => 20,
336 'user_toggle_noti' => 90,
337 'interested_events' => 8,
338 'user_id_noti' => 6,
339 ];
340 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
341 $this->database->expects(self::exactly(2))->method('fetchAssoc')->willReturn(
342 $row,
343 null
344 );
345 $this->database->expects(self::once())->method('queryF')->with(
346 'SELECT * FROM frm_notification WHERE frm_id = %s',
347 ['integer'],
348 [$forumId]
349 )->willReturn($mockStatement);
350
351 $instance = new ilForumNotification(84849);
352 $instance->setForumId($forumId);
353
354 $this->assertSame([
355 $row['user_id'] => $row,
356 ], $instance->read());
357 }
358
359 public function testMergeThreadNotifications(): void
360 {
361 $srcRow = ['user_id' => 47349];
362 $mismatchUserIdRow = ['user_id' => 37, 'notification_id' => 48];
363 $matchUserIdRow = ['user_id' => $srcRow['user_id'], 'notification_id' => 380];
364 $targetId = 840;
365 $srcId = 5749;
366 $srcStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
367 $targetStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
368 $consecutive_query_string = [
369 'SELECT notification_id, user_id FROM frm_notification WHERE frm_id = %s AND thread_id = %s ORDER BY user_id ASC',
370 'SELECT DISTINCT user_id FROM frm_notification WHERE frm_id = %s AND thread_id = %s ORDER BY user_id ASC'
371 ];
372 $consecutive_query_value = [[0, $srcId], [0, $targetId]];
373 $this->database->expects(self::exactly(2))->method('queryF')->with(
374 $this->callback(function ($value) use (&$consecutive_query_string) {
375 $this->assertSame(array_shift($consecutive_query_string), $value);
376 return true;
377 }),
378 $this->identicalTo(['integer', 'integer']),
379 $this->callback(function ($value) use (&$consecutive_query_value) {
380 $this->assertSame(array_shift($consecutive_query_value), $value);
381 return true;
382 })
383 )->willReturnOnConsecutiveCalls($srcStatement, $targetStatement);
384
385 $consecutive_fetch = [
386 $targetStatement,
387 $targetStatement,
388 $srcStatement,
389 $srcStatement,
390 $srcStatement
391 ];
392 $this->database->expects(self::exactly(5))
393 ->method('fetchAssoc')
394 ->with(
395 $this->callback(function ($value) use (&$consecutive_fetch) {
396 $this->assertSame(array_shift($consecutive_fetch), $value);
397 return true;
398 })
399 )
400 ->willReturnOnConsecutiveCalls($srcRow, null, $matchUserIdRow, $mismatchUserIdRow, null);
401
402 $this->database->expects(self::once())->method('manipulateF')->with(
403 'DELETE FROM frm_notification WHERE notification_id = %s',
404 ['integer'],
405 [$matchUserIdRow['notification_id']]
406 );
407
408 $this->database->expects(self::once())->method('update')->with(
409 'frm_notification',
410 ['thread_id' => ['integer', $targetId]],
411 ['thread_id' => ['integer', $srcId]]
412 );
413
415 }
416
417 public function testExistsNotification(): void
418 {
419 $adminForce = false;
420 $forumId = 7332;
421 $userId = 5758;
422
423 $statement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
424 $this->database->expects(self::once())->method('queryF')->with(
425 'SELECT user_id FROM frm_notification WHERE user_id = %s AND frm_id = %s AND admin_force_noti = %s',
426 ['integer', 'integer', 'integer'],
427 [$userId, $forumId, (int) $adminForce]
428 )->willReturn($statement);
429
430 $this->database->expects(self::once())->method('numRows')->with($statement)->willReturn(8);
431
432 $instance = new ilForumNotification(434);
433 $instance->setForumId($forumId);
434 $instance->setUserId($userId);
435
436 $this->assertTrue($instance->existsNotification());
437 }
438
439 protected function setUp(): void
440 {
441 global $DIC;
442
443 $this->dic = is_object($DIC) ? clone $DIC : $DIC;
444
445 $DIC = new Container();
446
447 $DIC['ilDB'] = ($this->database = $this->createMock(ilDBInterface::class));
448 $DIC['ilUser'] = ($this->user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock());
449 $DIC['ilObjDataCache'] = $this->getMockBuilder(ilObjectDataCache::class)->disableOriginalConstructor()->getMock();
450 $DIC['tree'] = ($this->tree = $this->getMockBuilder(ilTree::class)->disableOriginalConstructor()->getMock());
451 }
452
453 protected function tearDown(): void
454 {
455 global $DIC;
456
458
459 parent::tearDown();
460 }
461}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
MockObject &ilDBInterface $database
Class ilForumNotification.
static checkForumsExistsInsert(int $ref_id, int $user_id)
static mergeThreadNotifications($merge_source_thread_id, $merge_target_thread_id)
User class.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26
$refId
Definition: xapitoken.php:58