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