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