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