ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
ilForumTopicTest.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;
24use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
25
26class ilForumTopicTest extends TestCase
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 testGetPostRootIdFailed(): void
232 {
233 $id = 909;
234 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
235 $this->mockDatabase->expects(self::once())->method('queryF')->with(
236 'SELECT pos_fk FROM frm_posts_tree WHERE thr_fk = %s AND parent_pos = %s AND depth = %s ORDER BY rgt DESC',
237 ['integer', 'integer', 'integer'],
238 [$id, 0, 1]
239 )->willReturn($mockStatement);
240 $this->mockDatabase->expects(self::once())->method('fetchObject')->with($mockStatement)->willReturn(null);
241
242 $instance = new ilForumTopic();
243 $instance->setId($id);
244 $this->assertSame(0, $instance->getPostRootId());
245 }
246
247 public function testCountPosts(): void
248 {
249 $id = 789;
250 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
251 $this->withIgnoredQuery(
252 $this->mockDatabase->expects(self::once())->method('queryF'),
253 ['integer'],
254 [$id]
255 )->willReturn($mockStatement);
256 $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 678]);
257
258 $instance = new ilForumTopic();
259 $instance->setId($id);
260 $this->assertSame(678, $instance->countPosts(true));
261 }
262
263 public function testCountPostsFailed(): void
264 {
265 $id = 789;
266 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
267 $this->withIgnoredQuery(
268 $this->mockDatabase->expects(self::once())->method('queryF'),
269 ['integer'],
270 [$id]
271 )->willReturn($mockStatement);
272 $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
273
274 $instance = new ilForumTopic();
275 $instance->setId($id);
276 $this->assertSame(0, $instance->countPosts(true));
277 }
278
279 public function testCountActivePosts(): void
280 {
281 $id = 789;
282 $userId = 354;
283 $this->mockUser->expects(self::once())->method('getId')->willReturn($userId);
284 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
285 $this->withIgnoredQuery(
286 $this->mockDatabase->expects(self::once())->method('queryF'),
287 ['integer', 'integer', 'integer', 'integer'],
288 ['1', '0', $userId, $id]
289 )->willReturn($mockStatement);
290 $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 79]);
291
292 $instance = new ilForumTopic();
293 $instance->setId($id);
294 $this->assertSame(79, $instance->countActivePosts(true));
295 }
296
297 public function testCountActivePostsFailed(): void
298 {
299 $id = 789;
300 $userId = 354;
301 $this->mockUser->expects(self::once())->method('getId')->willReturn($userId);
302 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
303 $this->withIgnoredQuery(
304 $this->mockDatabase->expects(self::once())->method('queryF'),
305 ['integer', 'integer', 'integer', 'integer'],
306 ['1', '0', $userId, $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->countActivePosts(true));
313 }
314
315 public function testGetAllPostIds(): void
316 {
317 $firstRow = new stdClass();
318 $firstRow->pos_pk = 89;
319 $id = 284;
320 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
321 $mockStatement->expects(self::exactly(2))
322 ->method('fetchRow')
324 ->willReturnOnConsecutiveCalls($firstRow, null);
325 $this->mockDatabase->expects(self::once())->method('queryF')->with(
326 'SELECT pos_pk FROM frm_posts WHERE pos_thr_fk = %s',
327 ['integer'],
328 [$id]
329 )->willReturn($mockStatement);
330
331 $instance = new ilForumTopic();
332 $instance->setId($id);
333 $this->assertSame([$firstRow->pos_pk => $firstRow->pos_pk], $instance->getAllPostIds());
334 }
335
336 public function testIsNotificationEnabled(): void
337 {
338 $id = 723;
339 $userId = 639;
340
341 $instance = new ilForumTopic();
342 $instance->setId($id);
343
344 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
345 $this->mockDatabase->expects(self::once())->method('queryF')->with(
346 'SELECT COUNT(notification_id) cnt FROM frm_notification WHERE user_id = %s AND thread_id = %s',
347 ['integer', 'integer'],
348 [$userId, $id]
349 )->willReturn($mockStatement);
350 $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 46]);
351
352 $this->assertTrue($instance->isNotificationEnabled($userId));
353 }
354
355 public function testIsNotificationEnabledNoResult(): void
356 {
357 $id = 723;
358 $userId = 639;
359
360 $instance = new ilForumTopic();
361 $instance->setId($id);
362
363 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
364 $this->mockDatabase->expects(self::once())->method('queryF')->with(
365 'SELECT COUNT(notification_id) cnt FROM frm_notification WHERE user_id = %s AND thread_id = %s',
366 ['integer', 'integer'],
367 [$userId, $id]
368 )->willReturn($mockStatement);
369 $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(null);
370
371 $this->assertFalse($instance->isNotificationEnabled($userId));
372 }
373
375 {
376 $id = 723;
377 $userId = 0;
378
379 $instance = new ilForumTopic();
380 $instance->setId($id);
381
382 $this->mockDatabase->expects(self::never())->method('queryF');
383 $this->mockDatabase->expects(self::never())->method('fetchAssoc');
384
385 $this->assertFalse($instance->isNotificationEnabled($userId));
386 }
387
388 public function testEnableNotification(): void
389 {
390 $nextId = 3847;
391 $id = 3739;
392 $userId = 8283;
393
394 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
395 $this->mockDatabase->expects(self::once())->method('nextId')->with('frm_notification')->willReturn($nextId);
396 $this->mockDatabase->expects(self::once())->method('queryF')->willReturn($mockStatement);
397 $this->withIgnoredQuery(
398 $this->mockDatabase->expects(self::once())->method('manipulateF'),
399 ['integer', 'integer', 'integer'],
400 [$nextId, $userId, $id]
401 )->willReturn(0);
402 $this->mockDatabase->expects(self::once())->method('fetchAssoc')->with($mockStatement)->willReturn(['cnt' => 0]);
403
404 $instance = new ilForumTopic();
405 $instance->setId($id);
406 $instance->enableNotification($userId);
407 }
408
409 public function testDisableNotification(): void
410 {
411 $id = 384;
412 $userId = 48475;
413
414 $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
415 'DELETE FROM frm_notification WHERE user_id = %s AND thread_id = %s',
416 ['integer', 'integer'],
417 [$userId, $id]
418 );
419
420 $instance = new ilForumTopic();
421 $instance->setId($id);
422 $instance->disableNotification($userId);
423 }
424
425 public function testMakeSticky(): void
426 {
427 $id = 1929;
428
429 $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
430 'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
431 ['integer', 'integer'],
432 [1, $id]
433 );
434
435 $instance = new ilForumTopic();
436 $instance->setId($id);
437 $this->assertTrue($instance->makeSticky());
438 }
439
440 public function testMakeStickyFailed(): void
441 {
442 $this->mockDatabase->expects(self::never())->method('manipulateF');
443
444 $instance = new ilForumTopic();
445 $this->assertFalse($instance->makeSticky());
446 }
447
448 public function testUnmakeSticky(): void
449 {
450 $id = 1929;
451
452 $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
453 'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
454 ['integer', 'integer'],
455 [0, $id]
456 );
457
458 $instance = new ilForumTopic();
459 $instance->setId($id);
460 $instance->setSticky(true);
461 $this->assertTrue($instance->unmakeSticky());
462 }
463
464 public function testUnmakeStickyFalse(): void
465 {
466 $id = 1929;
467
468 $this->mockDatabase->expects(self::never())->method('manipulateF');
469
470 $instance = new ilForumTopic();
471 $instance->setId($id);
472 $this->assertFalse($instance->unmakeSticky());
473 }
474
475 public function testClose(): void
476 {
477 $id = 1929;
478
479 $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
480 'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
481 ['integer', 'integer'],
482 [1, $id]
483 );
484
485 $instance = new ilForumTopic();
486 $instance->setId($id);
487 $instance->setClosed(false);
488 $instance->close();
489 }
490
491 public function testReopen(): void
492 {
493 $id = 1929;
494
495 $this->mockDatabase->expects(self::once())->method('manipulateF')->with(
496 'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
497 ['integer', 'integer'],
498 [0, $id]
499 );
500
501 $instance = new ilForumTopic();
502 $instance->setId($id);
503 $instance->setClosed(true);
504 $instance->reopen();
505 }
506
507 protected function setUp(): void
508 {
509 global $DIC;
510
511 $this->dic = is_object($DIC) ? clone $DIC : $DIC;
512
513 $DIC = new Container();
514
515 $this->mockDatabase = $this->getMockBuilder(ilDBInterface::class)->getMock();
516 $this->mockUser = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock();
517
518 $DIC['ilDB'] = $this->mockDatabase;
519 $DIC['ilUser'] = $this->mockUser;
520 }
521
522 protected function tearDown(): void
523 {
524 global $DIC;
525
527
528 parent::tearDown();
529 }
530
531 private function withIgnoredQuery(InvocationMocker $mock, array ...$expected): InvocationMocker
532 {
533 return $mock->willReturnCallback(function ($ignored, ...$actual) use ($expected): void {
534 $this->assertSame($expected, $actual);
535 });
536 }
537}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
MockObject &ilDBInterface $mockDatabase
MockObject &ilObjUser $mockUser
withIgnoredQuery(InvocationMocker $mock, array ... $expected)
User class.
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26