ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilForumNotificationTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
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 
132  public function testIsUserToggleNotificationFailed(): void
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  $nextId = 3737;
164 
165  $this->user->expects(self::once())->method('getId')->willReturn($objUserId);
166 
167  $this->database->expects(self::once())->method('nextId')->willReturn($nextId);
168  $this->database->expects(self::once())->method('manipulateF')->with(
169  '
170  INSERT INTO frm_notification
171  (notification_id, user_id, frm_id, admin_force_noti, user_toggle_noti, user_id_noti)
172  VALUES(%s, %s, %s, %s, %s, %s)',
173  ['integer', 'integer', 'integer', 'integer', 'integer', 'integer'],
174  [
175  $nextId,
176  $userId,
177  $forumId,
178  $adminForce,
179  $userToggle,
180  $objUserId
181  ]
182  );
183 
184  $instance = new ilForumNotification(480);
185  $instance->setUserId($userId);
186  $instance->setForumId($forumId);
187  $instance->setAdminForce($adminForce);
188  $instance->setUserToggle($userToggle);
189 
190  $instance->insertAdminForce();
191  }
192 
193  public function testDeleteAdminForce(): void
194  {
195  $userId = 739;
196  $forumId = 48849;
197 
198  $this->database->expects(self::once())->method('manipulateF')->with(
199  '
200  DELETE FROM frm_notification
201  WHERE user_id = %s
202  AND frm_id = %s
203  AND admin_force_noti = %s
204  AND user_id_noti > %s',
205  ['integer', 'integer', 'integer', 'integer'],
206  [$userId, $forumId, 1, 0]
207  );
208 
209  $instance = new ilForumNotification(292);
210  $instance->setUserId($userId);
211  $instance->setForumId($forumId);
212 
213  $instance->deleteAdminForce();
214  }
215 
216  public function testDeleteUserToggle(): void
217  {
218  $forumId = 3877;
219  $userId = 3839;
220  $this->database->expects(self::once())->method('manipulateF')->with(
221  '
222  DELETE FROM frm_notification
223  WHERE user_id = %s
224  AND frm_id = %s
225  AND admin_force_noti = %s
226  AND user_toggle_noti = %s
227  AND user_id_noti > %s',
228  ['integer', 'integer', 'integer', 'integer', 'integer'],
229  [$userId, $forumId, 1, 1, 0]
230  );
231 
232  $instance = new ilForumNotification(3830);
233  $instance->setUserId($userId);
234  $instance->setForumId($forumId);
235  $instance->deleteUserToggle();
236  }
237 
238  public function testupdateUserToggle(): void
239  {
240  $userToggle = true;
241  $forumId = 3877;
242  $userId = 3839;
243 
244  $this->database->expects(self::once())->method('manipulateF')->with(
245  'UPDATE frm_notification SET user_toggle_noti = %s WHERE user_id = %s AND frm_id = %s AND admin_force_noti = %s',
246  ['integer', 'integer', 'integer', 'integer'],
247  [$userToggle, $userId, $forumId, 1]
248  );
249 
250  $instance = new ilForumNotification(3830);
251  $instance->setUserId($userId);
252  $instance->setForumId($forumId);
253  $instance->setUserToggle($userToggle);
254  $instance->updateUserToggle();
255  }
256 
257  public function testCheckForumsExistsInsert(): void
258  {
259  $userId = 927;
260  $refId = 847;
261  $nodeData = [
262  'child' => $refId
263  ];
264  $subTree = [['child' => 3719, 'ref_id' => 3738, 'obj_id' => 182]];
265  $pathNode = [['child' => $refId, 'type' => 'aa']];
266 
267  $this->tree->expects(self::once())->method('getNodeData')->with($refId)->willReturn($nodeData);
268  $this->tree->expects(self::once())->method('getNodePath')->with($subTree[0]['child'], $refId)->willReturn($pathNode);
269  $this->tree->expects(self::once())->method('getSubTree')->with(
270  $nodeData,
271  true,
272  ['frm']
273  )->willReturn($subTree);
274 
276  }
277 
278  public function testUpdate(): void
279  {
280  $forumId = 1122;
281  $userId = 484;
282  $events = 848;
283  $userToggle = true;
284  $adminForce = false;
285  $this->database->expects(self::once())->method('manipulateF')->with(
286  'UPDATE frm_notification SET admin_force_noti = %s, user_toggle_noti = %s, ' .
287  'interested_events = %s WHERE user_id = %s AND frm_id = %s',
288  ['integer', 'integer', 'integer', 'integer', 'integer'],
289  [
290  (int) $adminForce,
291  (int) $userToggle,
292  $events,
293  $userId,
294  $forumId
295  ]
296  );
297 
298  $instance = new ilForumNotification(8380);
299  $instance->setAdminForce($adminForce);
300  $instance->setUserToggle($userToggle);
301  $instance->setInterestedEvents($events);
302  $instance->setUserId($userId);
303  $instance->setForumId($forumId);
304 
305  $instance->update();
306  }
307 
308  public function testDeleteNotificationAllUsers(): void
309  {
310  $forumId = 490;
311  $this->database->expects(self::once())->method('manipulateF')->with(
312  'DELETE FROM frm_notification WHERE frm_id = %s AND user_id_noti > %s',
313  ['integer', 'integer'],
314  [$forumId, 0]
315  );
316 
317  $instance = new ilForumNotification(3490);
318  $instance->setForumId($forumId);
319 
320  $instance->deleteNotificationAllUsers();
321  }
322 
323 
324  public function testRead(): void
325  {
326  $forumId = 4859;
327  $row = [
328  'notification_id' => 789,
329  'user_id' => 490,
330  'frm_id' => 380,
331  'thread_id' => 280,
332  'admin_force_noti' => 20,
333  'user_toggle_noti' => 90,
334  'interested_events' => 8,
335  'user_id_noti' => 6,
336  ];
337  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
338  $this->database->expects(self::exactly(2))->method('fetchAssoc')->willReturn(
339  $row,
340  null
341  );
342  $this->database->expects(self::once())->method('queryF')->with(
343  'SELECT * FROM frm_notification WHERE frm_id = %s',
344  ['integer'],
345  [$forumId]
346  )->willReturn($mockStatement);
347 
348  $instance = new ilForumNotification(84849);
349  $instance->setForumId($forumId);
350 
351  $this->assertSame([
352  $row['user_id'] => $row,
353  ], $instance->read());
354  }
355 
356  public function testMergeThreadNotifications(): void
357  {
358  $srcRow = ['user_id' => 47349];
359  $mismatchUserIdRow = ['user_id' => 37, 'notification_id' => 48];
360  $matchUserIdRow = ['user_id' => $srcRow['user_id'], 'notification_id' => 380];
361  $targetId = 840;
362  $srcId = 5749;
363  $srcStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
364  $targetStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
365  $consecutive_query_string = [
366  'SELECT notification_id, user_id FROM frm_notification WHERE frm_id = %s AND thread_id = %s ORDER BY user_id ASC',
367  'SELECT DISTINCT user_id FROM frm_notification WHERE frm_id = %s AND thread_id = %s ORDER BY user_id ASC'
368  ];
369  $consecutive_query_value = [[0, $srcId], [0, $targetId]];
370  $this->database->expects(self::exactly(2))->method('queryF')->with(
371  $this->callback(function ($value) use (&$consecutive_query_string) {
372  $this->assertSame(array_shift($consecutive_query_string), $value);
373  return true;
374  }),
375  $this->identicalTo(['integer', 'integer']),
376  $this->callback(function ($value) use (&$consecutive_query_value) {
377  $this->assertSame(array_shift($consecutive_query_value), $value);
378  return true;
379  })
380  )->willReturnOnConsecutiveCalls($srcStatement, $targetStatement);
381 
382  $consecutive_fetch = [
383  $targetStatement,
384  $targetStatement,
385  $srcStatement,
386  $srcStatement,
387  $srcStatement
388  ];
389  $this->database->expects(self::exactly(5))
390  ->method('fetchAssoc')
391  ->with(
392  $this->callback(function ($value) use (&$consecutive_fetch) {
393  $this->assertSame(array_shift($consecutive_fetch), $value);
394  return true;
395  })
396  )
397  ->willReturnOnConsecutiveCalls($srcRow, null, $matchUserIdRow, $mismatchUserIdRow, null);
398 
399  $this->database->expects(self::once())->method('manipulateF')->with(
400  'DELETE FROM frm_notification WHERE notification_id = %s',
401  ['integer'],
402  [$matchUserIdRow['notification_id']]
403  );
404 
405  $this->database->expects(self::once())->method('update')->with(
406  'frm_notification',
407  ['thread_id' => ['integer', $targetId]],
408  ['thread_id' => ['integer', $srcId]]
409  );
410 
412  }
413 
414  public function testExistsNotification(): void
415  {
416  $adminForce = false;
417  $forumId = 7332;
418  $userId = 5758;
419 
420  $statement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
421  $this->database->expects(self::once())->method('queryF')->with(
422  'SELECT user_id FROM frm_notification WHERE user_id = %s AND frm_id = %s AND admin_force_noti = %s',
423  ['integer', 'integer', 'integer'],
424  [$userId, $forumId, (int) $adminForce]
425  )->willReturn($statement);
426 
427  $this->database->expects(self::once())->method('numRows')->with($statement)->willReturn(8);
428 
429  $instance = new ilForumNotification(434);
430  $instance->setForumId($forumId);
431  $instance->setUserId($userId);
432 
433  $this->assertTrue($instance->existsNotification());
434  }
435 
436  protected function setUp(): void
437  {
438  global $DIC;
439 
440  $this->dic = is_object($DIC) ? clone $DIC : $DIC;
441 
442  $DIC = new Container();
443 
444  $DIC['ilDB'] = ($this->database = $this->createMock(ilDBInterface::class));
445  $DIC['ilUser'] = ($this->user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock());
446  $DIC['ilObjDataCache'] = $this->getMockBuilder(ilObjectDataCache::class)->disableOriginalConstructor()->getMock();
447  $DIC['tree'] = ($this->tree = $this->getMockBuilder(ilTree::class)->disableOriginalConstructor()->getMock());
448  }
449 
450  protected function tearDown(): void
451  {
452  global $DIC;
453 
454  $DIC = $this->dic;
455 
456  parent::tearDown();
457  }
458 }
$refId
Definition: xapitoken.php:58
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
MockObject &ilDBInterface $database
global $DIC
Definition: shib_login.php:26
static mergeThreadNotifications($merge_source_thread_id, $merge_target_thread_id)
static checkForumsExistsInsert(int $ref_id, int $user_id)