19declare(strict_types=1);
22use PHPUnit\Framework\TestCase;
23use PHPUnit\Framework\MockObject\MockObject;
24use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
36 $valueAsObject =
new stdClass();
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;
54 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
55 $mockStatement->expects(self::once())
58 ->willReturn($valueAsObject);
61 $this->mockDatabase->expects(self::once())->method(
'queryF'),
64 )->willReturn($mockStatement);
68 $this->assertInstanceOf(ilForumTopic::class, $instance);
77 'thr_display_user_id' =>
'',
78 'thr_usr_alias' =>
'',
79 'thr_last_post' =>
'',
87 'thr_author_id' =>
'',
90 'num_unread_posts' =>
'',
91 'usr_notification_is_enabled' =>
'',
95 $instance->assignData(
$data);
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());
112 $this->assertSame(0, $instance->getNumPosts());
113 $this->assertSame(0, $instance->getNumUnreadPosts());
114 $this->assertFalse($instance->isUserNotificationEnabled());
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);
135 $this->mockDatabase->expects(self::once())->method(
'nextId')->with(
'frm_threads')->willReturn($nextId);
137 $this->mockDatabase->expects(self::once())->method(
'insert')->with(
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],
157 $this->assertTrue($instance->insert());
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());
173 $instance->setForumId(789);
174 $instance->setSubject(
'abc');
175 $instance->setNumPosts(67);
176 $instance->setLastPostString(
'hej');
177 $instance->setAverageRating(27);
180 $this->mockDatabase->expects(self::once())->method(
'manipulateF'),
181 [
'integer',
'text',
'timestamp',
'integer',
'text',
'text',
'integer'],
192 $this->assertTrue($instance->update());
198 $this->mockDatabase->expects(self::never())->method(
'manipulateF');
199 $instance->setForumId(0);
200 $this->assertFalse($instance->update());
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);
209 $instance->setId(89);
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'],
223 )->willReturn($mockStatement);
224 $this->mockDatabase->expects(self::once())->method(
'fetchObject')->with($mockStatement)->willReturn($stdObject);
227 $instance->setId(
$id);
228 $this->assertSame($stdObject->pos_fk, $instance->getPostRootId());
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'],
239 )->willReturn($mockStatement);
240 $this->mockDatabase->expects(self::once())->method(
'fetchObject')->with($mockStatement)->willReturn(
null);
243 $instance->setId(
$id);
244 $this->assertSame(0, $instance->getPostRootId());
250 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
252 $this->mockDatabase->expects(self::once())->method(
'queryF'),
255 )->willReturn($mockStatement);
256 $this->mockDatabase->expects(self::once())->method(
'fetchAssoc')->with($mockStatement)->willReturn([
'cnt' => 678]);
259 $instance->setId(
$id);
260 $this->assertSame(678, $instance->countPosts(
true));
266 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
268 $this->mockDatabase->expects(self::once())->method(
'queryF'),
271 )->willReturn($mockStatement);
272 $this->mockDatabase->expects(self::once())->method(
'fetchAssoc')->with($mockStatement)->willReturn(
null);
275 $instance->setId(
$id);
276 $this->assertSame(0, $instance->countPosts(
true));
283 $this->mockUser->expects(self::once())->method(
'getId')->willReturn($userId);
284 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
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]);
293 $instance->setId(
$id);
294 $this->assertSame(79, $instance->countActivePosts(
true));
301 $this->mockUser->expects(self::once())->method(
'getId')->willReturn($userId);
302 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
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);
311 $instance->setId(
$id);
312 $this->assertSame(0, $instance->countActivePosts(
true));
317 $firstRow =
new stdClass();
318 $firstRow->pos_pk = 89;
320 $mockStatement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
321 $mockStatement->expects(self::exactly(2))
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',
329 )->willReturn($mockStatement);
332 $instance->setId(
$id);
333 $this->assertSame([$firstRow->pos_pk => $firstRow->pos_pk], $instance->getAllPostIds());
342 $instance->setId(
$id);
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'],
349 )->willReturn($mockStatement);
350 $this->mockDatabase->expects(self::once())->method(
'fetchAssoc')->with($mockStatement)->willReturn([
'cnt' => 46]);
352 $this->assertTrue($instance->isNotificationEnabled($userId));
361 $instance->setId(
$id);
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'],
368 )->willReturn($mockStatement);
369 $this->mockDatabase->expects(self::once())->method(
'fetchAssoc')->with($mockStatement)->willReturn(
null);
371 $this->assertFalse($instance->isNotificationEnabled($userId));
380 $instance->setId(
$id);
382 $this->mockDatabase->expects(self::never())->method(
'queryF');
383 $this->mockDatabase->expects(self::never())->method(
'fetchAssoc');
385 $this->assertFalse($instance->isNotificationEnabled($userId));
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);
398 $this->mockDatabase->expects(self::once())->method(
'manipulateF'),
399 [
'integer',
'integer',
'integer'],
400 [$nextId, $userId,
$id]
402 $this->mockDatabase->expects(self::once())->method(
'fetchAssoc')->with($mockStatement)->willReturn([
'cnt' => 0]);
405 $instance->setId(
$id);
406 $instance->enableNotification($userId);
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'],
421 $instance->setId(
$id);
422 $instance->disableNotification($userId);
429 $this->mockDatabase->expects(self::once())->method(
'manipulateF')->with(
430 'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
431 [
'integer',
'integer'],
436 $instance->setId(
$id);
437 $this->assertTrue($instance->makeSticky());
442 $this->mockDatabase->expects(self::never())->method(
'manipulateF');
445 $this->assertFalse($instance->makeSticky());
452 $this->mockDatabase->expects(self::once())->method(
'manipulateF')->with(
453 'UPDATE frm_threads SET is_sticky = %s WHERE thr_pk = %s',
454 [
'integer',
'integer'],
459 $instance->setId(
$id);
460 $instance->setSticky(
true);
461 $this->assertTrue($instance->unmakeSticky());
468 $this->mockDatabase->expects(self::never())->method(
'manipulateF');
471 $instance->setId(
$id);
472 $this->assertFalse($instance->unmakeSticky());
479 $this->mockDatabase->expects(self::once())->method(
'manipulateF')->with(
480 'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
481 [
'integer',
'integer'],
486 $instance->setId(
$id);
487 $instance->setClosed(
false);
495 $this->mockDatabase->expects(self::once())->method(
'manipulateF')->with(
496 'UPDATE frm_threads SET is_closed = %s WHERE thr_pk = %s',
497 [
'integer',
'integer'],
502 $instance->setId(
$id);
503 $instance->setClosed(
true);
515 $this->mockDatabase = $this->getMockBuilder(ilDBInterface::class)->getMock();
516 $this->mockUser = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock();
531 private function withIgnoredQuery(InvocationMocker $mock, array ...$expected): InvocationMocker
533 return $mock->willReturnCallback(
function ($ignored, ...$actual) use ($expected):
void {
534 $this->assertSame($expected, $actual);
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Customizing of pimple-DIC for ILIAS.
testCountActivePostsFailed()
testDisableNotification()
MockObject &ilDBInterface $mockDatabase
MockObject &ilObjUser $mockUser
testIsNotificationEnabledNoResult()
testGetPostRootIdFailed()
withIgnoredQuery(InvocationMocker $mock, array ... $expected)
testIsNotificationEnabled()
testIsNotificationEnabledInvalidIds()