ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilBuddyListTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Contact/BuddySystem/test/ilBuddySystemBaseTest.php';
5 
12 {
13  const BUDDY_LIST_OWNER_ID = -1;
14  const BUDDY_LIST_BUDDY_ID = -2;
15 
17  protected $buddyList;
18 
22  public function setUp() : void
23  {
24  $this->setGlobalVariable(
25  'ilAppEventHandler',
26  $this->getMockBuilder(ilAppEventHandler::class)->disableOriginalConstructor()->setMethods(['raise'])->getMock()
27  );
28  $this->setGlobalVariable('ilDB', $this->getMockBuilder(ilDBInterface::class)->getMock());
29  $this->setGlobalVariable(
30  'lng',
31  $this->getMockBuilder(ilLanguage::class)
32  ->disableOriginalConstructor()
33  ->setMethods(['txt', 'loadLanguageModule'])
34  ->getMock()
35  );
36  }
37 
42  {
43  $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->setMethods(['getId'])->getMock();
44  $user->expects($this->once())->method('getId')->will($this->returnValue(self::BUDDY_LIST_OWNER_ID));
45  $this->setGlobalVariable('ilUser', $user);
46 
48  }
49 
54  {
55  $this->expectException(ilBuddySystemException::class);
56  $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->setMethods(['getId'])->getMock();
57  $user->expects($this->once())->method('getId')->will($this->returnValue(ANONYMOUS_USER_ID));
58  $this->setGlobalVariable('ilUser', $user);
59 
61  }
62 
67  {
68  $relations = [
71  ];
72 
73  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
74  $buddyList->setRelations(new ilBuddySystemRelationCollection($relations));
75  $otherBuddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
76  $otherBuddylist->setRelations(new ilBuddySystemRelationCollection());
77 
78  $this->assertEquals($buddyList, $otherBuddylist);
79  }
80 
84  public function testListIsInitiallyEmpty() : void
85  {
86  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
87  $repo->expects($this->exactly(1))->method('getAll')->willReturn([]);
88 
89  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
90  $buddyList->reset();
91  $buddyList->setRepository($repo);
92 
93  $this->assertEmpty($buddyList->getRelations());
94  }
95 
100  {
101  $relations = [
104  ];
105 
106  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
107  $repo->expects($this->exactly(1))->method('getAll')->willReturn($relations);
108 
109  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
110  $buddyList->reset();
111  $buddyList->setRepository($repo);
112  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
113  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
114  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
115  }
116 
121  {
122  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
123  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
124  $expectedRelation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
125 
126  $relations = [
127  $expectedRelation->getBuddyUsrId() => $expectedRelation
128  ];
129 
130  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
131  $db->expects($this->exactly(2))->method('queryF');
132  $db->expects($this->exactly(2))->method('fetchAssoc')->will($this->returnValue([
133  'login' => 'phpunit'
134  ]));
135  $this->setGlobalVariable('ilDB', $db);
136 
137  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
138  $repo->expects($this->once())->method('getAll')->willReturn($relations);
139  $repo->expects($this->exactly(3))->method('save')->with($expectedRelation);
140 
141  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
142  $buddyList->reset();
143  $buddyList->setRepository($repo);
144 
145  $relation = $buddyList->getRelationByUserId($expectedRelation->getBuddyUsrId());
146  $buddyList->request($relation);
147  $buddyList->unlink($relation);
148  $buddyList->request($relation);
149  }
150 
155  {
156  $this->expectException(ilBuddySystemException::class);
157  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
158  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
159  $expectedRelation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
160 
161  $relations = [
162  $expectedRelation->getBuddyUsrId() => $expectedRelation
163  ];
164 
165  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
166  $db->expects($this->once())->method('queryF');
167  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue([
168  'login' => 'phpunit'
169  ]));
170  $this->setGlobalVariable('ilDB', $db);
171 
172  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
173  $repo->expects($this->once())->method('getAll')->willReturn($relations);
174  $repo->expects($this->exactly(1))->method('save')->with($expectedRelation);
175 
176  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
177  $buddyList->reset();
178  $buddyList->setRepository($repo);
179 
180  $relation = $buddyList->getRelationByUserId($expectedRelation->getBuddyUsrId());
181  $buddyList->request($relation);
182  $buddyList->link($relation);
183  }
184 
189  {
190  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
191  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
192  $expectedRelation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
193 
194  $relations = [
195  $expectedRelation->getBuddyUsrId() => $expectedRelation
196  ];
197 
198  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
199  $db->expects($this->any())->method('queryF');
200  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue([
201  'login' => 'phpunit'
202  ]));
203  $this->setGlobalVariable('ilDB', $db);
204 
205  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
206  $repo->expects($this->any())->method('getAll')->willReturn($relations);
207  $repo->expects($this->any())->method('save')->with($expectedRelation);
208 
209  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
210  $buddyList->reset();
211  $buddyList->setRepository($repo);
212  $buddyList->request($expectedRelation);
213 
214  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
215  $other_buddylist->reset();
216  $other_buddylist->setRepository($repo);
217  $other_buddylist->link($expectedRelation);
218  }
219 
224  {
225  $this->expectException(ilBuddySystemException::class);
226  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
227  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
228  $expectedRelation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
229 
230  $relations = [
231  $expectedRelation->getBuddyUsrId() => $expectedRelation
232  ];
233 
234  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
235  $db->expects($this->once())->method('queryF');
236  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue([
237  'login' => 'phpunit'
238  ]));
239  $this->setGlobalVariable('ilDB', $db);
240 
241  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
242  $repo->expects($this->once())->method('getAll')->willReturn($relations);
243  $repo->expects($this->exactly(1))->method('save')->with($expectedRelation);
244 
245  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
246  $buddyList->reset();
247  $buddyList->setRepository($repo);
248 
249  $relation = $buddyList->getRelationByUserId($expectedRelation->getBuddyUsrId());
250  $buddyList->request($relation);
251  $buddyList->ignore($relation);
252  }
253 
258  {
259  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
260  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
261  $expectedRelation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
262 
263  $relations = [
264  $expectedRelation->getBuddyUsrId() => $expectedRelation
265  ];
266 
267  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
268  $db->expects($this->any())->method('queryF');
269  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue([
270  'login' => 'phpunit'
271  ]));
272  $this->setGlobalVariable('ilDB', $db);
273 
274  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
275  $repo->expects($this->any())->method('getAll')->willReturn($relations);
276  $repo->expects($this->any())->method('save')->with($expectedRelation);
277 
278  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
279  $buddyList->reset();
280  $buddyList->setRepository($repo);
281  $buddyList->request($expectedRelation);
282 
283  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
284  $other_buddylist->reset();
285  $other_buddylist->setRepository($repo);
286  $other_buddylist->ignore($expectedRelation);
287  }
288 
293  {
294  $this->expectException(ilBuddySystemException::class);
295  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
296  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
297  $expectedRelation->setBuddyUsrId(ANONYMOUS_USER_ID);
298 
299  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
300  $repo->expects($this->never())->method('getAll')->willReturn([]);
301  $repo->expects($this->never())->method('save');
302 
303  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
304  $buddyList->reset();
305  $buddyList->setRepository($repo);
306  $buddyList->request($expectedRelation);
307  }
308 
313  {
314  $this->expectException(ilBuddySystemException::class);
315  $expectedRelation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
316  $expectedRelation->setUsrId(self::BUDDY_LIST_OWNER_ID);
317  $expectedRelation->setBuddyUsrId(-3);
318 
319  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
320  $db->expects($this->once())->method('queryF');
321  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(null));
322  $this->setGlobalVariable('ilDB', $db);
323 
324  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
325  $repo->expects($this->never())->method('getAll')->willReturn([]);
326  $repo->expects($this->never())->method('save');
327 
328  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
329  $buddyList->reset();
330  $buddyList->setRepository($repo);
331  $buddyList->request($expectedRelation);
332  }
333 
338  {
339  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
340  $repo->expects($this->once())->method('destroy');
341 
342  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
343  $buddyList->reset();
344  $buddyList->setRepository($repo);
345  $buddyList->destroy();
346  }
347 
352  {
353  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
354  $repo->expects($this->once())->method('getAll')->willReturn([]);
355 
356  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
357  $buddyList->reset();
358  $buddyList->setRepository($repo);
359  $this->assertInstanceOf('ilBuddySystemUnlinkedRelationState', $buddyList->getRelationByUserId(-3)->getState());
360  }
361 
366  {
367  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
368  $buddyList->reset();
369  $buddyList->setOwnerId(self::BUDDY_LIST_BUDDY_ID);
370  $this->assertEquals(self::BUDDY_LIST_BUDDY_ID, $buddyList->getOwnerId());
371 
372  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
373  $repo->expects($this->never())->method('getAll')->willReturn([]);
374  $buddyList->setRepository($repo);
375  $this->assertEquals($repo, $buddyList->getRepository());
376 
377  $relations = [
378  self::BUDDY_LIST_BUDDY_ID => new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState())
379  ];
380  $buddyList->setRelations(new ilBuddySystemRelationCollection($relations));
381  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddyList->getRelations());
382  }
383 
388  {
389  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
390  $buddyList->reset();
391 
392  $relations = [];
393 
395  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
396  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
397  $relations[self::BUDDY_LIST_BUDDY_ID] = $relation;
398 
400  $relation->setUsrId(self::BUDDY_LIST_BUDDY_ID + 1);
401  $relation->setBuddyUsrId(self::BUDDY_LIST_OWNER_ID);
402  $relations[self::BUDDY_LIST_BUDDY_ID + 1] = $relation;
403 
405  $relation->setUsrId(self::BUDDY_LIST_BUDDY_ID + 2);
406  $relation->setBuddyUsrId(self::BUDDY_LIST_OWNER_ID);
407  $relations[self::BUDDY_LIST_BUDDY_ID + 2] = $relation;
408 
410  $relation->setUsrId(self::BUDDY_LIST_BUDDY_ID + 3);
411  $relation->setBuddyUsrId(self::BUDDY_LIST_OWNER_ID);
412  $relations[self::BUDDY_LIST_BUDDY_ID + 3] = $relation;
413 
415  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
416  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID + 4);
417  $relations[self::BUDDY_LIST_BUDDY_ID + 4] = $relation;
418 
420  $relation->setUsrId(self::BUDDY_LIST_BUDDY_ID + 5);
421  $relation->setBuddyUsrId(self::BUDDY_LIST_OWNER_ID);
422  $relations[self::BUDDY_LIST_BUDDY_ID + 5] = $relation;
423 
425  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
426  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID + 6);
427  $relations[self::BUDDY_LIST_BUDDY_ID + 6] = $relation;
428 
430  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
431  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID + 7);
432  $relations[self::BUDDY_LIST_BUDDY_ID + 7] = $relation;
433 
434  $repo = $this->getMockBuilder(ilBuddySystemRelationRepository::class)->disableOriginalConstructor()->getMock();
435  $repo->expects($this->any())->method('getAll')->willReturn($relations);
436  $buddyList->setRepository($repo);
437 
438  $this->assertCount(3, $buddyList->getLinkedRelations());
439  $this->assertCount(1, $buddyList->getRequestRelationsForOwner());
440  $this->assertCount(1, $buddyList->getRequestRelationsByOwner());
441  $this->assertCount(1, $buddyList->getIgnoredRelationsForOwner());
442  $this->assertCount(2, $buddyList->getIgnoredRelationsByOwner());
443  $this->assertEquals(array_keys($relations), $buddyList->getRelationUserIds());
444  }
445 
451  private function setPriorRelationState(
452  ilBuddySystemRelation $relation,
454  ) : void {
455  $object = new ReflectionObject($relation);
456  $property = $object->getProperty('priorState');
457  $property->setAccessible(true);
458 
459  $property->setValue($relation, $state);
460  }
461 
466  {
467  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
468  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
469  $buddyList->reset();
470 
471  $state = new ilBuddySystemLinkedRelationState();
472 
473  $relation = new ilBuddySystemRelation($state);
474  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
475  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
476 
477  $this->setPriorRelationState($relation, $state);
478 
479  $buddyList->link($relation);
480  }
481 
486  {
487  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
488  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
489  $buddyList->reset();
490 
492 
493  $relation = new ilBuddySystemRelation($state);
494  $relation->setUsrId(self::BUDDY_LIST_BUDDY_ID);
495  $relation->setBuddyUsrId(self::BUDDY_LIST_OWNER_ID);
496 
497  $this->setPriorRelationState($relation, $state);
498 
499  $buddyList->ignore($relation);
500  }
501 
506  {
507  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
508  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
509  $buddyList->reset();
510 
511  $state = new ilBuddySystemUnlinkedRelationState();
512 
513  $relation = new ilBuddySystemRelation($state);
514  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
515  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
516 
517  $this->setPriorRelationState($relation, $state);
518 
519  $buddyList->unlink($relation);
520  }
521 
526  {
527  $this->expectException(ilBuddySystemRelationStateAlreadyGivenException::class);
528  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
529  $buddyList->reset();
530 
532 
533  $relation = new ilBuddySystemRelation($state);
534  $relation->setUsrId(self::BUDDY_LIST_BUDDY_ID);
535  $relation->setBuddyUsrId(self::BUDDY_LIST_OWNER_ID);
536 
537  $this->setPriorRelationState($relation, $state);
538 
539  $db = $this->getMockBuilder(ilDBInterface::class)->getMock();
540  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue([
541  'login' => 'phpunit'
542  ]));
543  $this->setGlobalVariable('ilDB', $db);
544 
545  $buddyList->request($relation);
546  }
547 
552  {
553  $this->expectException(ilBuddySystemRelationStateTransitionException::class);
554  $buddyList = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
555  $buddyList->reset();
556 
557  $state = new ilBuddySystemLinkedRelationState();
558 
559  $relation = new ilBuddySystemRelation($state);
560  $relation->setUsrId(self::BUDDY_LIST_OWNER_ID);
561  $relation->setBuddyUsrId(self::BUDDY_LIST_BUDDY_ID);
562 
563  $this->setPriorRelationState($relation, $state);
564 
565  $buddyList->ignore($relation);
566  }
567 }
testRelationCannotBeRequestedForAnonymous()
testInstanceCanBeCreatedByGlobalUserObject()
static getInstanceByGlobalUser()
testValuesCanBeFetchedByGettersWhenSetBySetters()
Class ilBuddyListTest.
testAlreadyGivenStateExceptionIsThrownWhenAnIgnoredRelationShouldBeMarkedAsIgnored()
Class ilBuddySystemUnlinkedRelationState.
testInstanceCannotBeCreatedByAnonymousGlobalUserObject()
testRelationRequestCannotBeIgnoredByTheRelationOwner()
testAlreadyGivenStateExceptionIsThrownWhenAnUnlinkedRelationShouldBeMarkedAsUnlinked()
testRepositoryIsEnquiredWhenBuddyListShouldBeDestroyed()
static getInstanceByUserId(int $usrId)
testAlreadyGivenStateExceptionIsThrownWhenARequestedRelationShouldBeMarkedAsRequested()
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
testRelationRequestCannotBeApprovedByTheRelationOwner()
testRelationCannotBeRequestedForUnknownUserAccounts()
testInstanceByBeCreatedBySingletonMethod()
Class ilBuddySystemRequestedRelationState.
testUnlinkedRelationIsReturnedWhenRelationWasRequestedForAnUnknownBuddyId()
testRepositoryIsEnquiredToFetchRelationsWhenRequestedExplicitly()
Class ilBuddySystemRelation.
setGlobalVariable(string $name, $value)
testRepositoryIsEnquiredOnlyOnceToFetchRelationsWhenCalledImplicitly()
testDifferentRelationStatesCanBeRetrieved()
setPriorRelationState(ilBuddySystemRelation $relation, ilBuddySystemRelationState $state)
testRelationRequestCanBeIgnoredByTheRelationTarget()
testStateTransitionExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsIgnored()
Interface ilBuddySystemRelationState.
testRelationRequestCanBeApprovedByTheRelationTarget()
testAlreadyGivenStateExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsLinked()