ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilBuddyListTest.php
Go to the documentation of this file.
1 <?php
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 
19  protected $buddylist;
20 
24  public function setUp()
25  {
26  $this->setGlobalVariable(
27  'ilAppEventHandler',
28  $this->getMockBuilder('ilAppEventHandler')->disableOriginalConstructor()->setMethods(array('raise'))->getMock()
29  );
30  $this->setGlobalVariable('ilDB', $this->getMockBuilder('ilDBInterface')->getMock());
31  $this->setGlobalVariable(
32  'lng',
33  $this->getMockBuilder('ilLanguage')
34  ->disableOriginalConstructor()
35  ->setMethods(array('txt', 'loadLanguageModule'))
36  ->getMock()
37  );
38  }
39 
44  {
45  $user = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(array('getId'))->getMock();
46  $user->expects($this->once())->method('getId')->will($this->returnValue(self::BUDDY_LIST_OWNER_ID));
47  $this->setGlobalVariable('ilUser', $user);
48 
50  }
51 
56  {
57  $this->assertException(ilBuddySystemException::class);
58  $user = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(array('getId'))->getMock();
59  $user->expects($this->once())->method('getId')->will($this->returnValue(ANONYMOUS_USER_ID));
60  $this->setGlobalVariable('ilUser', $user);
61 
63  }
64 
69  {
70  $relations = array(
73  );
74 
75  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
76  $buddylist->setRelations(new ilBuddySystemRelationCollection($relations));
77  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
78  $other_buddylist->setRelations(new ilBuddySystemRelationCollection());
79 
80  $this->assertEquals($buddylist, $other_buddylist);
81  }
82 
86  public function testListIsInitiallyEmpty()
87  {
88  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
89  $repo->expects($this->exactly(1))->method('getAll')->willReturn(array());
90 
91  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
92  $buddylist->reset();
93  $buddylist->setRepository($repo);
94 
95  $this->assertEmpty($buddylist->getRelations());
96  }
97 
102  {
103  $relations = array(
106  );
107 
108  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
109  $repo->expects($this->exactly(1))->method('getAll')->willReturn($relations);
110 
111  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
112  $buddylist->reset();
113  $buddylist->setRepository($repo);
114  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
115  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
116  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
117  }
118 
123  {
124  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
125  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
126  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
127 
128  $relations = array(
129  $expected_relation->getBuddyUserId() => $expected_relation
130  );
131 
132  $db = $this->getMockBuilder('ilDBInterface')->getMock();
133  $db->expects($this->exactly(2))->method('queryF');
134  $db->expects($this->exactly(2))->method('fetchAssoc')->will($this->returnValue(array(
135  'login' => 'phpunit'
136  )));
137  $this->setGlobalVariable('ilDB', $db);
138 
139  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
140  $repo->expects($this->once())->method('getAll')->willReturn($relations);
141  $repo->expects($this->exactly(3))->method('save')->with($expected_relation);
142 
143  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
144  $buddylist->reset();
145  $buddylist->setRepository($repo);
146 
147  $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
148  $buddylist->request($relation);
149  $buddylist->unlink($relation);
150  $buddylist->request($relation);
151  }
152 
157  {
158  $this->assertException(ilBuddySystemException::class);
159  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
160  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
161  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
162 
163  $relations = array(
164  $expected_relation->getBuddyUserId() => $expected_relation
165  );
166 
167  $db = $this->getMockBuilder('ilDBInterface')->getMock();
168  $db->expects($this->once())->method('queryF');
169  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(array(
170  'login' => 'phpunit'
171  )));
172  $this->setGlobalVariable('ilDB', $db);
173 
174  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
175  $repo->expects($this->once())->method('getAll')->willReturn($relations);
176  $repo->expects($this->exactly(1))->method('save')->with($expected_relation);
177 
178  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
179  $buddylist->reset();
180  $buddylist->setRepository($repo);
181 
182  $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
183  $buddylist->request($relation);
184  $buddylist->link($relation);
185  }
186 
191  {
192  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
193  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
194  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
195 
196  $relations = array(
197  $expected_relation->getBuddyUserId() => $expected_relation
198  );
199 
200  $db = $this->getMockBuilder('ilDBInterface')->getMock();
201  $db->expects($this->any())->method('queryF');
202  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
203  'login' => 'phpunit'
204  )));
205  $this->setGlobalVariable('ilDB', $db);
206 
207  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
208  $repo->expects($this->any())->method('getAll')->willReturn($relations);
209  $repo->expects($this->any())->method('save')->with($expected_relation);
210 
211  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
212  $buddylist->reset();
213  $buddylist->setRepository($repo);
214  $buddylist->request($expected_relation);
215 
216  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
217  $other_buddylist->reset();
218  $other_buddylist->setRepository($repo);
219  $other_buddylist->link($expected_relation);
220  }
221 
226  {
227  $this->assertException(ilBuddySystemException::class);
228  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
229  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
230  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
231 
232  $relations = array(
233  $expected_relation->getBuddyUserId() => $expected_relation
234  );
235 
236  $db = $this->getMockBuilder('ilDBInterface')->getMock();
237  $db->expects($this->once())->method('queryF');
238  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(array(
239  'login' => 'phpunit'
240  )));
241  $this->setGlobalVariable('ilDB', $db);
242 
243  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
244  $repo->expects($this->once())->method('getAll')->willReturn($relations);
245  $repo->expects($this->exactly(1))->method('save')->with($expected_relation);
246 
247  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
248  $buddylist->reset();
249  $buddylist->setRepository($repo);
250 
251  $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
252  $buddylist->request($relation);
253  $buddylist->ignore($relation);
254  }
255 
260  {
261  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
262  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
263  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
264 
265  $relations = array(
266  $expected_relation->getBuddyUserId() => $expected_relation
267  );
268 
269  $db = $this->getMockBuilder('ilDBInterface')->getMock();
270  $db->expects($this->any())->method('queryF');
271  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
272  'login' => 'phpunit'
273  )));
274  $this->setGlobalVariable('ilDB', $db);
275 
276  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
277  $repo->expects($this->any())->method('getAll')->willReturn($relations);
278  $repo->expects($this->any())->method('save')->with($expected_relation);
279 
280  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
281  $buddylist->reset();
282  $buddylist->setRepository($repo);
283  $buddylist->request($expected_relation);
284 
285  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
286  $other_buddylist->reset();
287  $other_buddylist->setRepository($repo);
288  $other_buddylist->ignore($expected_relation);
289  }
290 
295  {
296  $this->assertException(ilBuddySystemException::class);
297  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
298  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
299  $expected_relation->setBuddyUserId(ANONYMOUS_USER_ID);
300 
301  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
302  $repo->expects($this->never())->method('getAll')->willReturn(array());
303  $repo->expects($this->never())->method('save');
304 
305  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
306  $buddylist->reset();
307  $buddylist->setRepository($repo);
308  $buddylist->request($expected_relation);
309  }
310 
315  {
316  $this->assertException(ilBuddySystemException::class);
317  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
318  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
319  $expected_relation->setBuddyUserId(-3);
320 
321  $db = $this->getMockBuilder('ilDBInterface')->getMock();
322  $db->expects($this->once())->method('queryF');
323  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(null));
324  $this->setGlobalVariable('ilDB', $db);
325 
326  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
327  $repo->expects($this->never())->method('getAll')->willReturn(array());
328  $repo->expects($this->never())->method('save');
329 
330  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
331  $buddylist->reset();
332  $buddylist->setRepository($repo);
333  $buddylist->request($expected_relation);
334  }
335 
340  {
341  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
342  $repo->expects($this->once())->method('destroy');
343 
344  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
345  $buddylist->reset();
346  $buddylist->setRepository($repo);
347  $buddylist->destroy();
348  }
349 
354  {
355  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
356  $repo->expects($this->once())->method('getAll')->willReturn(array());
357 
358  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
359  $buddylist->reset();
360  $buddylist->setRepository($repo);
361  $this->assertInstanceOf('ilBuddySystemUnlinkedRelationState', $buddylist->getRelationByUserId(-3)->getState());
362  }
363 
368  {
369  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
370  $buddylist->reset();
371  $buddylist->setOwnerId(self::BUDDY_LIST_BUDDY_ID);
372  $this->assertEquals(self::BUDDY_LIST_BUDDY_ID, $buddylist->getOwnerId());
373 
374  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
375  $repo->expects($this->never())->method('getAll')->willReturn(array());
376  $buddylist->setRepository($repo);
377  $this->assertEquals($repo, $buddylist->getRepository());
378 
379  $relations = array(
380  self::BUDDY_LIST_BUDDY_ID => new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState())
381  );
382  $buddylist->setRelations(new ilBuddySystemRelationCollection($relations));
383  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
384  }
385 
390  {
391  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
392  $buddylist->reset();
393 
394  $relations = array();
395 
397  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
398  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
399  $relations[self::BUDDY_LIST_BUDDY_ID] = $relation;
400 
402  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 1);
403  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
404  $relations[self::BUDDY_LIST_BUDDY_ID + 1] = $relation;
405 
407  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 2);
408  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
409  $relations[self::BUDDY_LIST_BUDDY_ID + 2] = $relation;
410 
412  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 3);
413  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
414  $relations[self::BUDDY_LIST_BUDDY_ID + 3] = $relation;
415 
417  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
418  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 4);
419  $relations[self::BUDDY_LIST_BUDDY_ID + 4] = $relation;
420 
422  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 5);
423  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
424  $relations[self::BUDDY_LIST_BUDDY_ID + 5] = $relation;
425 
427  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
428  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 6);
429  $relations[self::BUDDY_LIST_BUDDY_ID + 6] = $relation;
430 
432  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
433  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 7);
434  $relations[self::BUDDY_LIST_BUDDY_ID + 7] = $relation;
435 
436  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
437  $repo->expects($this->any())->method('getAll')->willReturn($relations);
438  $buddylist->setRepository($repo);
439 
440  $this->assertCount(3, $buddylist->getLinkedRelations());
441  $this->assertCount(1, $buddylist->getRequestRelationsForOwner());
442  $this->assertCount(1, $buddylist->getRequestRelationsByOwner());
443  $this->assertCount(1, $buddylist->getIgnoredRelationsForOwner());
444  $this->assertCount(2, $buddylist->getIgnoredRelationsByOwner());
445  $this->assertEquals(array_keys($relations), $buddylist->getRelationUserIds());
446  }
447 
452  {
453  $this->assertException(InvalidArgumentException::class);
454  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
455  $buddylist->reset();
456  $buddylist->setOwnerId("phpunit");
457  }
458 
463  {
464  $this->assertException(InvalidArgumentException::class);
465  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
466  $buddylist->reset();
467  $buddylist->getRelationByUserId("phpunit");
468  }
469 
471  {
472  $object = new ReflectionObject($relation);
473  $property = $object->getProperty('prior_state');
474  $property->setAccessible(true);
475 
476  $property->setValue($relation, $state);
477  }
478 
483  {
484  $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
485  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
486  $buddylist->reset();
487 
489 
490  $relation = new ilBuddySystemRelation($state);
491  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
492  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
493 
494  $this->setPriorRelationState($relation, $state);
495 
496  $buddylist->link($relation);
497  }
498 
503  {
504  $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
505  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
506  $buddylist->reset();
507 
509 
510  $relation = new ilBuddySystemRelation($state);
511  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID);
512  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
513 
514  $this->setPriorRelationState($relation, $state);
515 
516  $buddylist->ignore($relation);
517  }
518 
523  {
524  $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
525  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
526  $buddylist->reset();
527 
529 
530  $relation = new ilBuddySystemRelation($state);
531  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
532  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
533 
534  $this->setPriorRelationState($relation, $state);
535 
536  $buddylist->unlink($relation);
537  }
538 
543  {
544  $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
545  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
546  $buddylist->reset();
547 
549 
550  $relation = new ilBuddySystemRelation($state);
551  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID);
552  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
553 
554  $this->setPriorRelationState($relation, $state);
555 
556  $db = $this->getMockBuilder('ilDBInterface')->getMock();
557  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
558  'login' => 'phpunit'
559  )));
560  $this->setGlobalVariable('ilDB', $db);
561 
562  $buddylist->request($relation);
563  }
564 
569  {
570  $this->assertException(ilBuddySystemRelationStateTransitionException::class);
571  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
572  $buddylist->reset();
573 
575 
576  $relation = new ilBuddySystemRelation($state);
577  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
578  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
579 
580  $this->setPriorRelationState($relation, $state);
581 
582  $buddylist->ignore($relation);
583  }
584 }
testRelationCannotBeRequestedForAnonymous()
ilBuddySystemException
testInstanceCanBeCreatedByGlobalUserObject()
static getInstanceByGlobalUser()
testValuesCanBeFetchedByGettersWhenSetBySetters()
Class ilBuddyListTest.
assertException($exception_class)
testAlreadyGivenStateExceptionIsThrownWhenAnIgnoredRelationShouldBeMarkedAsIgnored()
ilBuddySystemRelationStateAlreadyGivenException
Class ilBuddySystemUnlinkedRelationState.
testUnlinkedRelationIsReturnedWhenRelationWasRequestedForAUknownBuddyId()
testInstanceCannotBeCreatedByAnonymousGlobalUserObject()
ilBuddySystemException
testRelationRequestCannotBeIgnoredByTheRelationOwner()
ilBuddySystemException
testAlreadyGivenStateExceptionIsThrownWhenAnUnlinkedRelationShouldBeMarkedAsUnlinked()
ilBuddySystemRelationStateAlreadyGivenException
testRepositoryIsEnquiredWhenBuddyListShouldBeDestroyed()
testAlreadyGivenStateExceptionIsThrownWhenARequestedRelationShouldBeMarkedAsRequested()
ilBuddySystemRelationStateAlreadyGivenException
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
testRelationRequestCannotBeApprovedByTheRelationOwner()
ilBuddySystemException
static getInstanceByUserId($usr_id)
testRelationCannotBeRequestedForUnknownUserAccounts()
ilBuddySystemException
testInstanceByBeCreatedBySingletonMethod()
testExceptionIsThrownWhenRelationIsRequestedForANonNumericUserId()
InvalidArgumentException
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
$user
Definition: migrateto20.php:57
testExceptionIsThrownWhenNonNumericOwnerIdIsPassed()
InvalidArgumentException
testRepositoryIsEnquiredToFetchRelationsWhenRequestedExplicitly()
Class ilBuddySystemRelation.
testRepositoryIsEnquiredOnlyOnceToFetchRelationsWhenCalledImplicitly()
testDifferentRelationStatesCanBeRetrieved()
setPriorRelationState(ilBuddySystemRelation $relation, ilBuddySystemRelationState $state)
testRelationRequestCanBeIgnoredByTheRelationTarget()
testStateTransitionExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsIgnored()
ilBuddySystemRelationStateTransitionException
Interface ilBuddySystemRelationState.
testRelationRequestCanBeApprovedByTheRelationTarget()
testAlreadyGivenStateExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsLinked()
ilBuddySystemRelationStateAlreadyGivenException