ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilBuddyListTestCase.php
Go to the documentation of this file.
1 <?php
2 
19 declare(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 
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 
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 
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 
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 
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 
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 
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 
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 
513  private function setPriorRelationState(
516  ): void {
517  $object = new ReflectionObject($relation);
518  $property = $object->getProperty('priorState');
519  $property->setAccessible(true);
520 
521  $property->setValue($relation, $state);
522  }
523 
525  {
526  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
527 
528  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
529  $buddyList->reset();
530 
531  $state = new ilBuddySystemLinkedRelationState();
532 
534  $state,
535  self::BUDDY_LIST_OWNER_ID,
536  self::BUDDY_LIST_BUDDY_ID,
537  false,
538  time()
539  );
540 
541  $this->setPriorRelationState($relation, $state);
542 
543  $buddyList->link($relation);
544  }
545 
547  {
548  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
549 
550  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
551  $buddyList->reset();
552 
554 
556  $state,
557  self::BUDDY_LIST_BUDDY_ID,
558  self::BUDDY_LIST_OWNER_ID,
559  false,
560  time()
561  );
562 
563  $this->setPriorRelationState($relation, $state);
564 
565  $buddyList->ignore($relation);
566  }
567 
569  {
570  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
571 
572  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
573  $buddyList->reset();
574 
575  $state = new ilBuddySystemUnlinkedRelationState();
576 
578  $state,
579  self::BUDDY_LIST_OWNER_ID,
580  self::BUDDY_LIST_BUDDY_ID,
581  false,
582  time()
583  );
584 
585  $this->setPriorRelationState($relation, $state);
586 
587  $buddyList->unlink($relation);
588  }
589 
591  {
592  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
593 
594  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
595  $buddyList->reset();
596 
598 
600  $state,
601  self::BUDDY_LIST_BUDDY_ID,
602  self::BUDDY_LIST_OWNER_ID,
603  false,
604  time()
605  );
606 
607  $this->setPriorRelationState($relation, $state);
608 
609  $db = $this->createMock(ilDBInterface::class);
610  $db->method('fetchAssoc')->willReturn([
611  'login' => 'phpunit'
612  ]);
613  $this->setGlobalVariable('ilDB', $db);
614 
615  $buddyList->request($relation);
616  }
617 
619  {
620  $this->expectException(ilBuddySystemRelationStateTransitionException::class);
621 
622  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
623  $buddyList->reset();
624 
625  $state = new ilBuddySystemLinkedRelationState();
626 
628  $state,
629  self::BUDDY_LIST_OWNER_ID,
630  self::BUDDY_LIST_BUDDY_ID,
631  false,
632  time()
633  );
634 
635  $this->setPriorRelationState($relation, $state);
636 
637  $buddyList->ignore($relation);
638  }
639 }
const ANONYMOUS_USER_ID
Definition: constants.php:27
$relation
testRepositoryIsEnquiredToFetchRelationsWhenRequestedExplicitly()
testRepositoryIsEnquiredOnlyOnceToFetchRelationsWhenCalledImplicitly()
testAlreadyGivenStateExceptionIsThrownWhenAnIgnoredRelationShouldBeMarkedAsIgnored()
Class ilBuddySystemUnlinkedRelationState.
testRelationRequestCannotBeApprovedByTheRelationOwner()
setPriorRelationState(ilBuddySystemRelation $relation, ilBuddySystemRelationState $state)
testRepositoryIsEnquiredWhenBuddyListShouldBeDestroyed()
testUnlinkedRelationIsReturnedWhenRelationWasRequestedForAnUnknownBuddyId()
static getInstanceByUserId(int $usrId)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
testAlreadyGivenStateExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsLinked()
setGlobalVariable(string $name, mixed $value)
testStateTransitionExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsIgnored()
testAlreadyGivenStateExceptionIsThrownWhenAnUnlinkedRelationShouldBeMarkedAsUnlinked()
Class ilBuddyListTest.
testInstanceCannotBeCreatedByAnonymousGlobalUserObject()
Interface ilBuddySystemRelationState.
testAlreadyGivenStateExceptionIsThrownWhenARequestedRelationShouldBeMarkedAsRequested()
testRelationRequestCannotBeIgnoredByTheRelationOwner()
static getInstanceByGlobalUser(?ilObjUser $user=null)