ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilForumTopicTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
27 {
28  private MockObject&ilDBInterface $mockDatabase;
29  private MockObject&ilObjUser $mockUser;
30  private ?Container $dic = null;
31 
32  public function testConstruct(): void
33  {
34  $id = 78;
35 
36  $valueAsObject = new stdClass();
37 
38  $valueAsObject->thr_top_fk = 8;
39  $valueAsObject->thr_display_user_id = 8;
40  $valueAsObject->thr_usr_alias = '';
41  $valueAsObject->thr_subject = '';
42  $valueAsObject->thr_date = '';
43  $valueAsObject->thr_update = '';
44  $valueAsObject->import_name = '';
45  $valueAsObject->thr_num_posts = 8;
46  $valueAsObject->thr_last_post = '';
47  $valueAsObject->visits = 8;
48  $valueAsObject->is_sticky = false;
49  $valueAsObject->is_closed = false;
50  $valueAsObject->frm_obj_id = 8;
51  $valueAsObject->avg_rating = 9;
52  $valueAsObject->thr_author_id = 8;
53 
54  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
55  $mockStatement->expects(self::once())
56  ->method('fetchRow')
58  ->willReturn($valueAsObject);
59 
60  $this->withIgnoredQuery(
61  $this->mockDatabase->expects(self::once())->method('queryF'),
62  ['integer'],
63  [$id]
64  )->willReturn($mockStatement);
65 
66  $instance = new ilForumTopic($id);
67 
68  $this->assertInstanceOf(ilForumTopic::class, $instance);
69  }
70 
71  public function testAssignData(): void
72  {
73  $data = [
74  'thr_pk' => '',
75  'thr_top_fk' => '',
76  'thr_subject' => '',
77  'thr_display_user_id' => '',
78  'thr_usr_alias' => '',
79  'thr_last_post' => '',
80  'thr_date' => '',
81  'thr_update' => '',
82  'visits' => '',
83  'import_name' => '',
84  'is_sticky' => '',
85  'is_closed' => '',
86  'avg_rating' => '',
87  'thr_author_id' => '',
88 
89  'num_posts' => '',
90  'num_unread_posts' => '',
91  'usr_notification_is_enabled' => '',
92  ];
93 
94  $instance = new ilForumTopic();
95  $instance->assignData($data);
96 
97  $this->assertSame(0, $instance->getId());
98  $this->assertSame(0, $instance->getForumId());
99  $this->assertSame('', $instance->getSubject());
100  $this->assertSame(0, $instance->getDisplayUserId());
101  $this->assertSame('', $instance->getUserAlias());
102  $this->assertSame('', $instance->getLastPostString());
103  $this->assertSame('', $instance->getCreateDate());
104  $this->assertSame('', $instance->getChangeDate());
105  $this->assertSame(0, $instance->getVisits());
106  $this->assertSame('', $instance->getImportName());
107  $this->assertFalse($instance->isSticky());
108  $this->assertFalse($instance->isClosed());
109  $this->assertSame(0.0, $instance->getAverageRating());
110  $this->assertSame(0, $instance->getThrAuthorId());
111 
112  $this->assertSame(0, $instance->getNumPosts());
113  $this->assertSame(0, $instance->getNumUnreadPosts());
114  $this->assertFalse($instance->isUserNotificationEnabled());
115  }
116 
117  public function testInsert(): void
118  {
119  $instance = new ilForumTopic();
120  $nextId = 8;
121  $instance->setId(9);
122  $instance->setForumId(10);
123  $instance->setSubject('aa');
124  $instance->setDisplayUserId(188);
125  $instance->setUserAlias('jl');
126  $instance->setNumPosts(86);
127  $instance->setLastPostString('ahssh');
128  $instance->setCreateDate('some date');
129  $instance->setImportName('xaii');
130  $instance->setSticky(true);
131  $instance->setClosed(true);
132  $instance->setAverageRating(78);
133  $instance->setThrAuthorId(8890);
134 
135  $this->mockDatabase->expects(self::once())->method('nextId')->with('frm_threads')->willReturn($nextId);
136 
137  $this->mockDatabase->expects(self::once())->method('insert')->with(
138  'frm_threads',
139  [
140  'thr_pk' => ['integer', $nextId],
141  'thr_top_fk' => ['integer', 10],
142  'thr_subject' => ['text', 'aa'],
143  'thr_display_user_id' => ['integer', 188],
144  'thr_usr_alias' => ['text', 'jl'],
145  'thr_num_posts' => ['integer', 86],
146  'thr_last_post' => ['text', 'ahssh'],
147  'thr_date' => ['timestamp', 'some date'],
148  'thr_update' => ['timestamp', null],
149  'import_name' => ['text', 'xaii'],
150  'is_sticky' => ['integer', 1],
151  'is_closed' => ['integer', 1],
152  'avg_rating' => ['text', 78],
153  'thr_author_id' => ['integer', 8890],
154  ]
155  );
156 
157  $this->assertTrue($instance->insert());
158  }
159 
160  public function testInsertFalse(): void
161  {
162  $instance = new ilForumTopic();
163  $this->mockDatabase->expects(self::never())->method('nextId');
164  $this->mockDatabase->expects(self::never())->method('insert');
165  $instance->setForumId(0);
166  $this->assertFalse($instance->insert());
167  }
168 
169  public function testUpdate(): void
170  {
171  $instance = new ilForumTopic();
172  $instance->setId(8);
173  $instance->setForumId(789);
174  $instance->setSubject('abc');
175  $instance->setNumPosts(67);
176  $instance->setLastPostString('hej');
177  $instance->setAverageRating(27);
178 
179  $this->withIgnoredQuery(
180  $this->mockDatabase->expects(self::once())->method('manipulateF'),
181  ['integer', 'text', 'timestamp', 'integer', 'text', 'text', 'integer'],
182  [
183  789,
184  'abc',
185  date('Y-m-d H:i:s'),
186  67,
187  'hej',
188  '27',
189  8,
190  ]
191  )->willReturn(0);
192  $this->assertTrue($instance->update());
193  }
194 
195  public function testUpdateFalse(): void
196  {
197  $instance = new ilForumTopic();
198  $this->mockDatabase->expects(self::never())->method('manipulateF');
199  $instance->setForumId(0);
200  $this->assertFalse($instance->update());
201  }
202 
203  public function testReload(): void
204  {
205  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
206  $mockStatement->expects(self::once())->method('fetchRow')->willReturn(null);
207  $this->mockDatabase->expects(self::once())->method('queryF')->willReturn($mockStatement);
208  $instance = new ilForumTopic();
209  $instance->setId(89);
210  $instance->reload();
211  }
212 
213  public function testGetPostRootId(): void
214  {
215  $id = 909;
216  $stdObject = new stdClass();
217  $stdObject->pos_fk = 5678;
218  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
219  $this->mockDatabase->expects(self::once())->method('queryF')->with(
220  'SELECT pos_fk FROM frm_posts_tree WHERE thr_fk = %s AND parent_pos = %s AND depth = %s ORDER BY rgt DESC',
221  ['integer', 'integer', 'integer'],
222  [$id, 0, 1]
223  )->willReturn($mockStatement);
224  $this->mockDatabase->expects(self::once())->method('fetchObject')->with($mockStatement)->willReturn($stdObject);
225 
226  $instance = new ilForumTopic();
227  $instance->setId($id);
228  $this->assertSame($stdObject->pos_fk, $instance->getPostRootId());
229  }
230 
231  public function testGetFirstVisiblePostId(): void
232  {
233  $id = 909;
234  $stdObject = new stdClass();
235  $stdObject->pos_fk = 5678;
236  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
237  $this->mockDatabase->expects(self::once())->method('queryF')->with(
238  'SELECT pos_fk FROM frm_posts_tree WHERE thr_fk = %s AND parent_pos != %s AND depth = %s ORDER BY rgt DESC',
239  ['integer', 'integer', 'integer'],
240  [$id, 0, 2]
241  )->willReturn($mockStatement);
242  $this->mockDatabase->expects(self::once())->method('fetchObject')->with($mockStatement)->willReturn($stdObject);
243 
244  $instance = new ilForumTopic();
245  $instance->setId($id);
246  $this->assertSame($stdObject->pos_fk, $instance->getFirstVisiblePostId());
247  }
248 
249  public function testGetPostRootIdFailed(): void
250  {
251  $id = 909;
252  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
253  $this->mockDatabase->expects(self::once())->method('queryF')->with(
254  'SELECT pos_fk FROM frm_posts_tree WHERE thr_fk = %s AND parent_pos = %s AND depth = %s ORDER BY rgt DESC',
255  ['integer', 'integer', 'integer'],
256  [$id, 0, 1]
257  )->willReturn($mockStatement);
258  $this->mockDatabase->expects(self::once())->method('fetchObject')->with($mockStatement)->willReturn(null);
259 
260  $instance = new ilForumTopic();
261  $instance->setId($id);
262  $this->assertSame(0, $instance->getPostRootId());
263  }
264 
265  public function testGetFirstVisiblePostIdFailed(): void
266  {
267  $id = 909;
268  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
269  $this->mockDatabase->expects(self::once())->method('queryF')->with(
270  'SELECT pos_fk FROM frm_posts_tree WHERE thr_fk = %s AND parent_pos != %s AND depth = %s ORDER BY rgt DESC',
271  ['integer', 'integer', 'integer'],
272  [$id, 0, 2]
273  )->willReturn($mockStatement);
274  $this->mockDatabase->expects(self::once())->method('fetchObject')->with($mockStatement)->willReturn(null);
275 
276  $instance = new ilForumTopic();
277  $instance->setId($id);
278  $this->assertSame(0, $instance->getFirstVisiblePostId());
279  }
280 
281  public function testCountPosts(): void
282  {
283  $id = 789;
284  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
285  $this->withIgnoredQuery(
286  $this->mockDatabase->expects(self::once())->method('queryF'),
287  ['integer'],
288  [$id]
289  )->willReturn($mockStatement);
290  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 678]);
291 
292  $instance = new ilForumTopic();
293  $instance->setId($id);
294  $this->assertSame(678, $instance->countPosts(true));
295  }
296 
297  public function testCountPostsFailed(): void
298  {
299  $id = 789;
300  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
301  $this->withIgnoredQuery(
302  $this->mockDatabase->expects(self::once())->method('queryF'),
303  ['integer'],
304  [$id]
305  )->willReturn($mockStatement);
306  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
307 
308  $instance = new ilForumTopic();
309  $instance->setId($id);
310  $this->assertSame(0, $instance->countPosts(true));
311  }
312 
313  public function testCountActivePosts(): void
314  {
315  $id = 789;
316  $userId = 354;
317  $this->mockUser->expects(self::once())->method('getId')->willReturn($userId);
318  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
319  $this->withIgnoredQuery(
320  $this->mockDatabase->expects(self::once())->method('queryF'),
321  ['integer', 'integer', 'integer', 'integer'],
322  ['1', '0', $userId, $id]
323  )->willReturn($mockStatement);
324  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 79]);
325 
326  $instance = new ilForumTopic();
327  $instance->setId($id);
328  $this->assertSame(79, $instance->countActivePosts(true));
329  }
330 
331  public function testCountActivePostsFailed(): void
332  {
333  $id = 789;
334  $userId = 354;
335  $this->mockUser->expects(self::once())->method('getId')->willReturn($userId);
336  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
337  $this->withIgnoredQuery(
338  $this->mockDatabase->expects(self::once())->method('queryF'),
339  ['integer', 'integer', 'integer', 'integer'],
340  ['1', '0', $userId, $id]
341  )->willReturn($mockStatement);
342  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
343 
344  $instance = new ilForumTopic();
345  $instance->setId($id);
346  $this->assertSame(0, $instance->countActivePosts(true));
347  }
348 
349  public function testGetAllPostIds(): void
350  {
351  $firstRow = new stdClass();
352  $firstRow->pos_pk = 89;
353  $id = 284;
354  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
355  $mockStatement->expects(self::exactly(2))
356  ->method('fetchRow')
358  ->willReturnOnConsecutiveCalls($firstRow, null);
359  $this->mockDatabase->expects(self::once())->method('queryF')->with(
360  'SELECT pos_pk FROM frm_posts WHERE pos_thr_fk = %s',
361  ['integer'],
362  [$id]
363  )->willReturn($mockStatement);
364 
365  $instance = new ilForumTopic();
366  $instance->setId($id);
367  $this->assertSame([$firstRow->pos_pk => $firstRow->pos_pk], $instance->getAllPostIds());
368  }
369 
370  public function testIsNotificationEnabled(): void
371  {
372  $id = 723;
373  $userId = 639;
374 
375  $instance = new ilForumTopic();
376  $instance->setId($id);
377 
378  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
379  $this->mockDatabase->expects(self::once())->method('queryF')->with(
380  'SELECT COUNT(notification_id) cnt FROM frm_notification WHERE user_id = %s AND thread_id = %s',
381  ['integer', 'integer'],
382  [$userId, $id]
383  )->willReturn($mockStatement);
384  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 46]);
385 
386  $this->assertTrue($instance->isNotificationEnabled($userId));
387  }
388 
389  public function testIsNotificationEnabledNoResult(): void
390  {
391  $id = 723;
392  $userId = 639;
393 
394  $instance = new ilForumTopic();
395  $instance->setId($id);
396 
397  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
398  $this->mockDatabase->expects(self::once())->method('queryF')->with(
399  'SELECT COUNT(notification_id) cnt FROM frm_notification WHERE user_id = %s AND thread_id = %s',
400  ['integer', 'integer'],
401  [$userId, $id]
402  )->willReturn($mockStatement);
403  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
404 
405  $this->assertFalse($instance->isNotificationEnabled($userId));
406  }
407 
408  public function testIsNotificationEnabledInvalidIds(): void
409  {
410  $id = 723;
411  $userId = 0;
412 
413  $instance = new ilForumTopic();
414  $instance->setId($id);
415 
416  $this->mockDatabase->expects(self::never())->method('queryF');
417  $this->mockDatabase->expects(self::never())->method('fetchAssoc');
418 
419  $this->assertFalse($instance->isNotificationEnabled($userId));
420  }
421 
422  public function testEnableNotification(): void
423  {
424  $nextId = 3847;
425  $id = 3739;
426  $userId = 8283;
427 
428  $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
429  $this->mockDatabase->expects(self::once())->method('nextId')->with('frm_notification')->willReturn($nextId);
430  $this->mockDatabase->expects(self::once())->method('queryF')->willReturn($mockStatement);
431  $this->withIgnoredQuery(
432  $this->mockDatabase->expects(self::once())->method('manipulateF'),
433  ['integer', 'integer', 'integer'],
434  [$nextId, $userId, $id]
435  )->willReturn(0);
436  $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 0]);
437 
438  $instance = new ilForumTopic();
439  $instance->setId($id);
440  $instance->enableNotification($userId);
441  }
442 
443  public function testDisableNotification(): void
444  {
445  $id = 384;
446  $userId = 48475;
447 
448  $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
449  'DELETE FROM frm_notification WHERE user_id = %s AND thread_id = %s',
450  ['integer', 'integer'],
451  [$userId, $id]
452  );
453 
454  $instance = new ilForumTopic();
455  $instance->setId($id);
456  $instance->disableNotification($userId);
457  }
458 
459  public function testMakeSticky(): void
460  {
461  $id = 1929;
462 
463  $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
464  'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
465  ['integer', 'integer'],
466  [1, $id]
467  );
468 
469  $instance = new ilForumTopic();
470  $instance->setId($id);
471  $this->assertTrue($instance->makeSticky());
472  }
473 
474  public function testMakeStickyFailed(): void
475  {
476  $this->mockDatabase->expects(self::never())->method('manipulateF');
477 
478  $instance = new ilForumTopic();
479  $this->assertFalse($instance->makeSticky());
480  }
481 
482  public function testUnmakeSticky(): void
483  {
484  $id = 1929;
485 
486  $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
487  'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
488  ['integer', 'integer'],
489  [0, $id]
490  );
491 
492  $instance = new ilForumTopic();
493  $instance->setId($id);
494  $instance->setSticky(true);
495  $this->assertTrue($instance->unmakeSticky());
496  }
497 
498  public function testUnmakeStickyFalse(): void
499  {
500  $id = 1929;
501 
502  $this->mockDatabase->expects(self::never())->method('manipulateF');
503 
504  $instance = new ilForumTopic();
505  $instance->setId($id);
506  $this->assertFalse($instance->unmakeSticky());
507  }
508 
509  public function testClose(): void
510  {
511  $id = 1929;
512 
513  $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
514  'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
515  ['integer', 'integer'],
516  [1, $id]
517  );
518 
519  $instance = new ilForumTopic();
520  $instance->setId($id);
521  $instance->setClosed(false);
522  $instance->close();
523  }
524 
525  public function testReopen(): void
526  {
527  $id = 1929;
528 
529  $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
530  'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
531  ['integer', 'integer'],
532  [0, $id]
533  );
534 
535  $instance = new ilForumTopic();
536  $instance->setId($id);
537  $instance->setClosed(true);
538  $instance->reopen();
539  }
540 
541  protected function setUp(): void
542  {
543  global $DIC;
544 
545  $this->dic = is_object($DIC) ? clone $DIC : $DIC;
546 
547  $DIC = new Container();
548 
549  $this->mockDatabase = $this->getMockBuilder(ilDBInterface::class)->getMock();
550  $this->mockUser = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock();
551 
552  $DIC['ilDB'] = $this->mockDatabase;
553  $DIC['ilUser'] = $this->mockUser;
554  }
555 
556  protected function tearDown(): void
557  {
558  global $DIC;
559 
560  $DIC = $this->dic;
561 
562  parent::tearDown();
563  }
564 
565  private function withIgnoredQuery(InvocationMocker $mock, array ...$expected): InvocationMocker
566  {
567  return $mock->willReturnCallback(function ($ignored, ...$actual) use ($expected): void {
568  $this->assertSame($expected, $actual);
569  });
570  }
571 }
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
withIgnoredQuery(InvocationMocker $mock, array ... $expected)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
MockObject &ilObjUser $mockUser
MockObject &ilDBInterface $mockDatabase
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23