ILIAS  release_8 Revision v8.24
ilBuddyListTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 private const BUDDY_LIST_OWNER_ID = -1;
28 private const BUDDY_LIST_BUDDY_ID = -2;
29
30 protected function setUp(): void
31 {
32 parent::setUp();
33
34 $this->setGlobalVariable(
35 'ilAppEventHandler',
36 $this->getMockBuilder(ilAppEventHandler::class)->disableOriginalConstructor()->onlyMethods(['raise'])->getMock()
37 );
38 $this->setGlobalVariable('ilDB', $this->createMock(ilDBInterface::class));
39 $this->setGlobalVariable(
40 'lng',
41 $this->getMockBuilder(ilLanguage::class)
42 ->disableOriginalConstructor()
43 ->onlyMethods(['txt', 'loadLanguageModule'])
44 ->getMock()
45 );
46 }
47
49 {
50 $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(['getId'])->getMock();
51 $user->expects($this->once())->method('getId')->willReturn(self::BUDDY_LIST_OWNER_ID);
52 $this->setGlobalVariable('ilUser', $user);
53
55 }
56
58 {
59 $this->expectException(ilBuddySystemException::class);
60
61 $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(['getId'])->getMock();
62 $user->expects($this->once())->method('getId')->willReturn(ANONYMOUS_USER_ID);
63 $this->setGlobalVariable('ilUser', $user);
64
66 }
67
69 {
70 $relations = [
71 4711 => new ilBuddySystemRelation(
73 self::BUDDY_LIST_BUDDY_ID,
74 self::BUDDY_LIST_OWNER_ID,
75 false,
76 time()
77 ),
78 4712 => new ilBuddySystemRelation(
80 self::BUDDY_LIST_BUDDY_ID,
81 self::BUDDY_LIST_OWNER_ID,
82 false,
83 time()
84 )
85 ];
86
87 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
88 $buddyList->setRelations(new ilBuddySystemRelationCollection($relations));
89 $otherBuddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
90 $otherBuddylist->setRelations(new ilBuddySystemRelationCollection());
91
92 $this->assertEquals($buddyList, $otherBuddylist);
93 }
94
95 public function testListIsInitiallyEmpty(): void
96 {
97 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
98 $repo->expects($this->once())->method('getAll')->willReturn([]);
99
100 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
101 $buddyList->reset();
102 $buddyList->setRepository($repo);
103
104 $this->assertEmpty($buddyList->getRelations());
105 }
106
108 {
109 $relations = [
110 4711 => new ilBuddySystemRelation(
112 self::BUDDY_LIST_BUDDY_ID,
113 self::BUDDY_LIST_OWNER_ID,
114 false,
115 time()
116 ),
117 4712 => new ilBuddySystemRelation(
119 self::BUDDY_LIST_BUDDY_ID,
120 self::BUDDY_LIST_OWNER_ID,
121 false,
122 time()
123 )
124 ];
125
126 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
127 $repo->expects($this->once())->method('getAll')->willReturn($relations);
128
129 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
130 $buddyList->reset();
131 $buddyList->setRepository($repo);
132 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
133 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
134 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
135 }
136
138 {
139 $expectedRelation = new ilBuddySystemRelation(
141 self::BUDDY_LIST_BUDDY_ID,
142 self::BUDDY_LIST_OWNER_ID,
143 false,
144 time()
145 );
146 $expectedRelation = $expectedRelation->withUsrId(self::BUDDY_LIST_OWNER_ID);
147 $expectedRelation = $expectedRelation->withBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
148
149 $relations = [
150 $expectedRelation->getBuddyUsrId() => $expectedRelation
151 ];
152
153 $db = $this->createMock(ilDBInterface::class);
154 $db->expects($this->exactly(2))->method('queryF');
155 $db->expects($this->exactly(2))->method('fetchAssoc')->willReturn([
156 'login' => 'phpunit'
157 ]);
158 $this->setGlobalVariable('ilDB', $db);
159
160 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
161 $repo->expects($this->once())->method('getAll')->willReturn($relations);
162 $repo->expects($this->exactly(3))->method('save')->with($expectedRelation);
163
164 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
165 $buddyList->reset();
166 $buddyList->setRepository($repo);
167
168 $relation = $buddyList->getRelationByUserId($expectedRelation->getBuddyUsrId());
169 $buddyList->request($relation);
170 $buddyList->unlink($relation);
171 $buddyList->request($relation);
172 }
173
175 {
176 $this->expectException(ilBuddySystemException::class);
177
178 $expectedRelation = new ilBuddySystemRelation(
180 self::BUDDY_LIST_OWNER_ID,
181 self::BUDDY_LIST_BUDDY_ID,
182 false,
183 time()
184 );
185
186 $relations = [
187 $expectedRelation->getBuddyUsrId() => $expectedRelation
188 ];
189
190 $db = $this->createMock(ilDBInterface::class);
191 $db->expects($this->once())->method('queryF');
192 $db->expects($this->once())->method('fetchAssoc')->willReturn([
193 'login' => 'phpunit'
194 ]);
195 $this->setGlobalVariable('ilDB', $db);
196
197 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
198 $repo->expects($this->once())->method('getAll')->willReturn($relations);
199 $repo->expects($this->once())->method('save')->with($expectedRelation);
200
201 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
202 $buddyList->reset();
203 $buddyList->setRepository($repo);
204
205 $relation = $buddyList->getRelationByUserId($expectedRelation->getBuddyUsrId());
206 $buddyList->request($relation);
207 $buddyList->link($relation);
208 }
209
211 {
212 $expectedRelation = new ilBuddySystemRelation(
214 self::BUDDY_LIST_OWNER_ID,
215 self::BUDDY_LIST_BUDDY_ID,
216 false,
217 time()
218 );
219
220 $relations = [
221 $expectedRelation->getBuddyUsrId() => $expectedRelation
222 ];
223
224 $db = $this->createMock(ilDBInterface::class);
225 $db->method('queryF');
226 $db->method('fetchAssoc')->willReturn([
227 'login' => 'phpunit'
228 ]);
229 $this->setGlobalVariable('ilDB', $db);
230
231 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
232 $repo->method('getAll')->willReturn($relations);
233 $repo->method('save')->with($expectedRelation);
234
235 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
236 $buddyList->reset();
237 $buddyList->setRepository($repo);
238 $buddyList->request($expectedRelation);
239
240 $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
241 $other_buddylist->reset();
242 $other_buddylist->setRepository($repo);
243 $other_buddylist->link($expectedRelation);
244
245 $this->assertEquals(new ilBuddySystemLinkedRelationState(), $expectedRelation->getState());
246 }
247
249 {
250 $this->expectException(ilBuddySystemException::class);
251
252 $expectedRelation = new ilBuddySystemRelation(
254 self::BUDDY_LIST_OWNER_ID,
255 self::BUDDY_LIST_BUDDY_ID,
256 false,
257 time()
258 );
259
260 $relations = [
261 $expectedRelation->getBuddyUsrId() => $expectedRelation
262 ];
263
264 $db = $this->createMock(ilDBInterface::class);
265 $db->expects($this->once())->method('queryF');
266 $db->expects($this->once())->method('fetchAssoc')->willReturn([
267 'login' => 'phpunit'
268 ]);
269 $this->setGlobalVariable('ilDB', $db);
270
271 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
272 $repo->expects($this->once())->method('getAll')->willReturn($relations);
273 $repo->expects($this->once())->method('save')->with($expectedRelation);
274
275 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
276 $buddyList->reset();
277 $buddyList->setRepository($repo);
278
279 $relation = $buddyList->getRelationByUserId($expectedRelation->getBuddyUsrId());
280 $buddyList->request($relation);
281 $buddyList->ignore($relation);
282 }
283
285 {
286 $expectedRelation = new ilBuddySystemRelation(
288 self::BUDDY_LIST_OWNER_ID,
289 self::BUDDY_LIST_BUDDY_ID,
290 false,
291 time()
292 );
293
294 $relations = [
295 $expectedRelation->getBuddyUsrId() => $expectedRelation
296 ];
297
298 $db = $this->createMock(ilDBInterface::class);
299 $db->method('queryF');
300 $db->method('fetchAssoc')->willReturn([
301 'login' => 'phpunit'
302 ]);
303 $this->setGlobalVariable('ilDB', $db);
304
305 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
306 $repo->method('getAll')->willReturn($relations);
307 $repo->method('save')->with($expectedRelation);
308
309 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
310 $buddyList->reset();
311 $buddyList->setRepository($repo);
312 $buddyList->request($expectedRelation);
313
314 $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
315 $other_buddylist->reset();
316 $other_buddylist->setRepository($repo);
317 $other_buddylist->ignore($expectedRelation);
318
319 $this->assertEquals(new ilBuddySystemIgnoredRequestRelationState(), $expectedRelation->getState());
320 }
321
323 {
324 $this->expectException(ilBuddySystemException::class);
325
326 $expectedRelation = new ilBuddySystemRelation(
328 self::BUDDY_LIST_OWNER_ID,
330 false,
331 time()
332 );
333
334 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
335 $repo->expects($this->never())->method('getAll')->willReturn([]);
336 $repo->expects($this->never())->method('save');
337
338 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
339 $buddyList->reset();
340 $buddyList->setRepository($repo);
341 $buddyList->request($expectedRelation);
342 }
343
345 {
346 $this->expectException(ilBuddySystemException::class);
347
348 $expectedRelation = new ilBuddySystemRelation(
350 self::BUDDY_LIST_BUDDY_ID,
351 self::BUDDY_LIST_OWNER_ID,
352 false,
353 time()
354 );
355 $expectedRelation = $expectedRelation->withUsrId(self::BUDDY_LIST_OWNER_ID);
356 $expectedRelation = $expectedRelation->withBuddyUsrId(-3);
357
358 $db = $this->createMock(ilDBInterface::class);
359 $db->expects($this->once())->method('queryF');
360 $db->expects($this->once())->method('fetchAssoc')->willReturn(null);
361 $this->setGlobalVariable('ilDB', $db);
362
363 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
364 $repo->expects($this->never())->method('getAll')->willReturn([]);
365 $repo->expects($this->never())->method('save');
366
367 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
368 $buddyList->reset();
369 $buddyList->setRepository($repo);
370 $buddyList->request($expectedRelation);
371
372 $this->assertEquals(new ilBuddySystemRequestedRelationState(), $expectedRelation->getState());
373 }
374
376 {
377 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
378 $repo->expects($this->once())->method('destroy');
379
380 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
381 $buddyList->reset();
382 $buddyList->setRepository($repo);
383 $buddyList->destroy();
384 }
385
387 {
388 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
389 $repo->expects($this->once())->method('getAll')->willReturn([]);
390
391 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
392 $buddyList->reset();
393 $buddyList->setRepository($repo);
394 $this->assertInstanceOf(ilBuddySystemUnlinkedRelationState::class, $buddyList->getRelationByUserId(-3)->getState());
395 }
396
398 {
399 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
400 $buddyList->reset();
401 $buddyList->setOwnerId(self::BUDDY_LIST_BUDDY_ID);
402 $this->assertSame(self::BUDDY_LIST_BUDDY_ID, $buddyList->getOwnerId());
403
404 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
405 $repo->expects($this->never())->method('getAll')->willReturn([]);
406 $buddyList->setRepository($repo);
407 $this->assertEquals($repo, $buddyList->getRepository());
408
409 $relations = [
410 self::BUDDY_LIST_BUDDY_ID => new ilBuddySystemRelation(
412 self::BUDDY_LIST_BUDDY_ID,
413 self::BUDDY_LIST_OWNER_ID,
414 false,
415 time()
416 )
417 ];
418 $buddyList->setRelations(new ilBuddySystemRelationCollection($relations));
419 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
420 }
421
423 {
424 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
425 $buddyList->reset();
426
427 $relations = [];
428
429 $relation = new ilBuddySystemRelation(
431 self::BUDDY_LIST_OWNER_ID,
432 self::BUDDY_LIST_BUDDY_ID,
433 false,
434 time()
435 );
436 $relations[self::BUDDY_LIST_BUDDY_ID] = $relation;
437
438 $relation = new ilBuddySystemRelation(
440 self::BUDDY_LIST_BUDDY_ID + 1,
441 self::BUDDY_LIST_OWNER_ID,
442 false,
443 time()
444 );
445 $relations[self::BUDDY_LIST_BUDDY_ID + 1] = $relation;
446
447 $relation = new ilBuddySystemRelation(
449 self::BUDDY_LIST_BUDDY_ID + 2,
450 self::BUDDY_LIST_OWNER_ID,
451 false,
452 time()
453 );
454 $relations[self::BUDDY_LIST_BUDDY_ID + 2] = $relation;
455
456 $relation = new ilBuddySystemRelation(
458 self::BUDDY_LIST_BUDDY_ID + 3,
459 self::BUDDY_LIST_OWNER_ID,
460 false,
461 time()
462 );
463 $relations[self::BUDDY_LIST_BUDDY_ID + 3] = $relation;
464
465 $relation = new ilBuddySystemRelation(
467 self::BUDDY_LIST_OWNER_ID,
468 self::BUDDY_LIST_BUDDY_ID + 4,
469 false,
470 time()
471 );
472 $relations[self::BUDDY_LIST_BUDDY_ID + 4] = $relation;
473
474 $relation = new ilBuddySystemRelation(
476 self::BUDDY_LIST_BUDDY_ID + 5,
477 self::BUDDY_LIST_OWNER_ID,
478 false,
479 time()
480 );
481 $relations[self::BUDDY_LIST_BUDDY_ID + 5] = $relation;
482
483 $relation = new ilBuddySystemRelation(
485 self::BUDDY_LIST_OWNER_ID,
486 self::BUDDY_LIST_BUDDY_ID + 6,
487 false,
488 time()
489 );
490 $relations[self::BUDDY_LIST_BUDDY_ID + 6] = $relation;
491
492 $relation = new ilBuddySystemRelation(
494 self::BUDDY_LIST_OWNER_ID,
495 self::BUDDY_LIST_BUDDY_ID + 7,
496 false,
497 time()
498 );
499 $relations[self::BUDDY_LIST_BUDDY_ID + 7] = $relation;
500
501 $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
502 $repo->method('getAll')->willReturn($relations);
503 $buddyList->setRepository($repo);
504
505 $this->assertCount(3, $buddyList->getLinkedRelations());
506 $this->assertCount(1, $buddyList->getRequestRelationsForOwner());
507 $this->assertCount(1, $buddyList->getRequestRelationsByOwner());
508 $this->assertCount(1, $buddyList->getIgnoredRelationsForOwner());
509 $this->assertCount(2, $buddyList->getIgnoredRelationsByOwner());
510 $this->assertEquals(array_keys($relations), $buddyList->getRelationUserIds());
511 }
512
518 private function setPriorRelationState(
519 ilBuddySystemRelation $relation,
521 ): void {
522 $object = new ReflectionObject($relation);
523 $property = $object->getProperty('priorState');
524 $property->setAccessible(true);
525
526 $property->setValue($relation, $state);
527 }
528
530 {
531 $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
532
533 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
534 $buddyList->reset();
535
537
538 $relation = new ilBuddySystemRelation(
539 $state,
540 self::BUDDY_LIST_OWNER_ID,
541 self::BUDDY_LIST_BUDDY_ID,
542 false,
543 time()
544 );
545
546 $this->setPriorRelationState($relation, $state);
547
548 $buddyList->link($relation);
549 }
550
552 {
553 $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
554
555 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
556 $buddyList->reset();
557
559
560 $relation = new ilBuddySystemRelation(
561 $state,
562 self::BUDDY_LIST_BUDDY_ID,
563 self::BUDDY_LIST_OWNER_ID,
564 false,
565 time()
566 );
567
568 $this->setPriorRelationState($relation, $state);
569
570 $buddyList->ignore($relation);
571 }
572
574 {
575 $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
576
577 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
578 $buddyList->reset();
579
581
582 $relation = new ilBuddySystemRelation(
583 $state,
584 self::BUDDY_LIST_OWNER_ID,
585 self::BUDDY_LIST_BUDDY_ID,
586 false,
587 time()
588 );
589
590 $this->setPriorRelationState($relation, $state);
591
592 $buddyList->unlink($relation);
593 }
594
596 {
597 $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
598
599 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
600 $buddyList->reset();
601
603
604 $relation = new ilBuddySystemRelation(
605 $state,
606 self::BUDDY_LIST_BUDDY_ID,
607 self::BUDDY_LIST_OWNER_ID,
608 false,
609 time()
610 );
611
612 $this->setPriorRelationState($relation, $state);
613
614 $db = $this->createMock(ilDBInterface::class);
615 $db->method('fetchAssoc')->willReturn([
616 'login' => 'phpunit'
617 ]);
618 $this->setGlobalVariable('ilDB', $db);
619
620 $buddyList->request($relation);
621 }
622
624 {
625 $this->expectException(ilBuddySystemRelationStateTransitionException::class);
626
627 $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
628 $buddyList->reset();
629
631
632 $relation = new ilBuddySystemRelation(
633 $state,
634 self::BUDDY_LIST_OWNER_ID,
635 self::BUDDY_LIST_BUDDY_ID,
636 false,
637 time()
638 );
639
640 $this->setPriorRelationState($relation, $state);
641
642 $buddyList->ignore($relation);
643 }
644}
Class ilBuddyListTest.
testRepositoryIsEnquiredWhenBuddyListShouldBeDestroyed()
testUnlinkedRelationIsReturnedWhenRelationWasRequestedForAnUnknownBuddyId()
testAlreadyGivenStateExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsLinked()
testRelationRequestCanBeIgnoredByTheRelationTarget()
testDifferentRelationStatesCanBeRetrieved()
testRelationCannotBeRequestedForAnonymous()
testRelationRequestCannotBeIgnoredByTheRelationOwner()
testAlreadyGivenStateExceptionIsThrownWhenAnUnlinkedRelationShouldBeMarkedAsUnlinked()
testAlreadyGivenStateExceptionIsThrownWhenARequestedRelationShouldBeMarkedAsRequested()
testInstanceCannotBeCreatedByAnonymousGlobalUserObject()
testRepositoryIsEnquiredToFetchRelationsWhenRequestedExplicitly()
testRelationRequestCannotBeApprovedByTheRelationOwner()
testAlreadyGivenStateExceptionIsThrownWhenAnIgnoredRelationShouldBeMarkedAsIgnored()
testRelationCannotBeRequestedForUnknownUserAccounts()
testValuesCanBeFetchedByGettersWhenSetBySetters()
testStateTransitionExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsIgnored()
testInstanceByBeCreatedBySingletonMethod()
testRepositoryIsEnquiredOnlyOnceToFetchRelationsWhenCalledImplicitly()
testInstanceCanBeCreatedByGlobalUserObject()
testRelationRequestCanBeApprovedByTheRelationTarget()
setPriorRelationState(ilBuddySystemRelation $relation, ilBuddySystemRelationState $state)
static getInstanceByUserId(int $usrId)
static getInstanceByGlobalUser()
setGlobalVariable(string $name, $value)
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list.
Class ilBuddySystemRelation.
const ANONYMOUS_USER_ID
Definition: constants.php:27
Interface ilBuddySystemRelationState.