ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
10 {
11  const BUDDY_LIST_OWNER_ID = -1;
12  const BUDDY_LIST_BUDDY_ID = -2;
13 
17  protected $buddylist;
18 
22  public function setUp()
23  {
24  $GLOBALS['ilAppEventHandler'] = $this->getMockBuilder('ilAppEventHandler')->disableOriginalConstructor()->setMethods(array('raise'))->getMock();
25  }
26 
31  {
32  $user = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(array('getId'))->getMock();
33  $user->expects($this->once())->method('getId')->will($this->returnValue(self::BUDDY_LIST_OWNER_ID));
34  $GLOBALS['ilUser'] = $user;
35 
36  ilBuddyList::getInstanceByGlobalUser();
37  }
38 
43  {
44  $user = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(array('getId'))->getMock();
45  $user->expects($this->once())->method('getId')->will($this->returnValue(ANONYMOUS_USER_ID));
46  $GLOBALS['ilUser'] = $user;
47 
48  ilBuddyList::getInstanceByGlobalUser();
49  }
50 
55  {
56  $relations = array(
59  );
60 
61  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
62  $buddylist->setRelations(new ilBuddySystemRelationCollection($relations));
63  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
64  $other_buddylist->setRelations(new ilBuddySystemRelationCollection());
65 
66  $this->assertEquals($buddylist, $other_buddylist);
67  }
68 
72  public function testListIsInitiallyEmpty()
73  {
74  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
75  $repo->expects($this->exactly(1))->method('getAll')->willReturn(array());
76 
77  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
78  $buddylist->reset();
79  $buddylist->setRepository($repo);
80 
81  $this->assertEmpty($buddylist->getRelations());
82  }
83 
88  {
89  $relations = array(
92  );
93 
94  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
95  $repo->expects($this->exactly(1))->method('getAll')->willReturn($relations);
96 
97  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
98  $buddylist->reset();
99  $buddylist->setRepository($repo);
100  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
101  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
102  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
103  }
104 
109  {
110  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
111  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
112  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
113 
114  $relations = array(
115  $expected_relation->getBuddyUserId() => $expected_relation
116  );
117 
118  $db = $this->getMockBuilder('ilDBMySQL')->disableOriginalConstructor()->setMethods(array('queryF', 'fetchAssoc'))->getMock();
119  $db->expects($this->exactly(2))->method('queryF');
120  $db->expects($this->exactly(2))->method('fetchAssoc')->will($this->returnValue(array(
121  'login' => 'phpunit'
122  )));
123  $GLOBALS['ilDB'] = $db;
124 
125  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
126  $repo->expects($this->once())->method('getAll')->willReturn($relations);
127  $repo->expects($this->exactly(3))->method('save')->with($expected_relation);
128 
129  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
130  $buddylist->reset();
131  $buddylist->setRepository($repo);
132 
133  $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
134  $buddylist->request($relation);
135  $buddylist->unlink($relation);
136  $buddylist->request($relation);
137  }
138 
143  {
144  $this->assertException(ilBuddySystemException::class);
145  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
146  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
147  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
148 
149  $relations = array(
150  $expected_relation->getBuddyUserId() => $expected_relation
151  );
152 
153  $db = $this->getMockBuilder('ilDBInterface')->getMock();
154  $db->expects($this->once())->method('queryF');
155  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(array(
156  'login' => 'phpunit'
157  )));
158  $this->setGlobalVariable('ilDB', $db);
159 
160  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
161  $repo->expects($this->once())->method('getAll')->willReturn($relations);
162  $repo->expects($this->exactly(1))->method('save')->with($expected_relation);
163 
164  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
165  $buddylist->reset();
166  $buddylist->setRepository($repo);
167 
168  $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
169  $buddylist->request($relation);
170  $buddylist->link($relation);
171  }
172 
177  {
178  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
179  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
180  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
181 
182  $relations = array(
183  $expected_relation->getBuddyUserId() => $expected_relation
184  );
185 
186  $db = $this->getMockBuilder('ilDBInterface')->getMock();
187  $db->expects($this->any())->method('queryF');
188  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
189  'login' => 'phpunit'
190  )));
191  $this->setGlobalVariable('ilDB', $db);
192 
193  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
194  $repo->expects($this->any())->method('getAll')->willReturn($relations);
195  $repo->expects($this->any())->method('save')->with($expected_relation);
196 
197  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
198  $buddylist->reset();
199  $buddylist->setRepository($repo);
200  $buddylist->request($expected_relation);
201 
202  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
203  $other_buddylist->reset();
204  $other_buddylist->setRepository($repo);
205  $other_buddylist->link($expected_relation);
206  }
207 
212  {
213  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
214  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
215  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
216 
217  $relations = array(
218  $expected_relation->getBuddyUserId() => $expected_relation
219  );
220 
221  $db = $this->getMockBuilder('ilDBMySQL')->disableOriginalConstructor()->setMethods(array('queryF', 'fetchAssoc'))->getMock();
222  $db->expects($this->once())->method('queryF');
223  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(array(
224  'login' => 'phpunit'
225  )));
226  $GLOBALS['ilDB'] = $db;
227 
228  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
229  $repo->expects($this->once())->method('getAll')->willReturn($relations);
230  $repo->expects($this->exactly(1))->method('save')->with($expected_relation);
231 
232  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
233  $buddylist->reset();
234  $buddylist->setRepository($repo);
235 
236  $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
237  $buddylist->request($relation);
238  $buddylist->ignore($relation);
239  }
240 
245  {
246  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
247  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
248  $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
249 
250  $relations = array(
251  $expected_relation->getBuddyUserId() => $expected_relation
252  );
253 
254  $db = $this->getMockBuilder('ilDBMySQL')->disableOriginalConstructor()->setMethods(array('queryF', 'fetchAssoc'))->getMock();
255  $db->expects($this->any())->method('queryF');
256  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
257  'login' => 'phpunit'
258  )));
259  $GLOBALS['ilDB'] = $db;
260 
261  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
262  $repo->expects($this->any())->method('getAll')->willReturn($relations);
263  $repo->expects($this->any())->method('save')->with($expected_relation);
264 
265  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
266  $buddylist->reset();
267  $buddylist->setRepository($repo);
268  $buddylist->request($expected_relation);
269 
270  $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
271  $other_buddylist->reset();
272  $other_buddylist->setRepository($repo);
273  $other_buddylist->ignore($expected_relation);
274  }
275 
280  {
281  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
282  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
283  $expected_relation->setBuddyUserId(ANONYMOUS_USER_ID);
284 
285  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
286  $repo->expects($this->never())->method('getAll')->willReturn(array());
287  $repo->expects($this->never())->method('save');
288 
289  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
290  $buddylist->reset();
291  $buddylist->setRepository($repo);
292  $buddylist->request($expected_relation);
293  }
294 
299  {
300  $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
301  $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
302  $expected_relation->setBuddyUserId(-3);
303 
304  $db = $this->getMockBuilder('ilDBMySQL')->disableOriginalConstructor()->setMethods(array('queryF', 'fetchAssoc'))->getMock();
305  $db->expects($this->once())->method('queryF');
306  $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(null));
307  $GLOBALS['ilDB'] = $db;
308 
309  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
310  $repo->expects($this->never())->method('getAll')->willReturn(array());
311  $repo->expects($this->never())->method('save');
312 
313  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
314  $buddylist->reset();
315  $buddylist->setRepository($repo);
316  $buddylist->request($expected_relation);
317  }
318 
323  {
324  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
325  $repo->expects($this->once())->method('destroy');
326 
327  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
328  $buddylist->reset();
329  $buddylist->setRepository($repo);
330  $buddylist->destroy();
331  }
332 
337  {
338  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
339  $repo->expects($this->once())->method('getAll')->willReturn(array());
340 
341  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
342  $buddylist->reset();
343  $buddylist->setRepository($repo);
344  $this->assertInstanceOf('ilBuddySystemUnlinkedRelationState', $buddylist->getRelationByUserId(-3)->getState());
345  }
346 
351  {
352  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
353  $buddylist->reset();
354  $buddylist->setOwnerId(self::BUDDY_LIST_BUDDY_ID);
355  $this->assertEquals(self::BUDDY_LIST_BUDDY_ID, $buddylist->getOwnerId());
356 
357  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
358  $repo->expects($this->never())->method('getAll')->willReturn(array());
359  $buddylist->setRepository($repo);
360  $this->assertEquals($repo, $buddylist->getRepository());
361 
362  $relations = array(
363  self::BUDDY_LIST_BUDDY_ID => new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState())
364  );
365  $buddylist->setRelations(new ilBuddySystemRelationCollection($relations));
366  $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
367  }
368 
373  {
374  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
375  $buddylist->reset();
376 
377  $relations = array();
378 
380  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
381  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
382  $relations[self::BUDDY_LIST_BUDDY_ID] = $relation;
383 
385  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 1);
386  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
387  $relations[self::BUDDY_LIST_BUDDY_ID + 1] = $relation;
388 
390  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 2);
391  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
392  $relations[self::BUDDY_LIST_BUDDY_ID + 2] = $relation;
393 
395  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 3);
396  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
397  $relations[self::BUDDY_LIST_BUDDY_ID + 3] = $relation;
398 
400  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
401  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 4);
402  $relations[self::BUDDY_LIST_BUDDY_ID + 4] = $relation;
403 
405  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 5);
406  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
407  $relations[self::BUDDY_LIST_BUDDY_ID + 5] = $relation;
408 
410  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
411  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 6);
412  $relations[self::BUDDY_LIST_BUDDY_ID + 6] = $relation;
413 
415  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
416  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 7);
417  $relations[self::BUDDY_LIST_BUDDY_ID + 7] = $relation;
418 
419  $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
420  $repo->expects($this->any())->method('getAll')->willReturn($relations);
421  $buddylist->setRepository($repo);
422 
423  $this->assertCount(3, $buddylist->getLinkedRelations());
424  $this->assertCount(1, $buddylist->getRequestRelationsForOwner());
425  $this->assertCount(1, $buddylist->getRequestRelationsByOwner());
426  $this->assertCount(1, $buddylist->getIgnoredRelationsForOwner());
427  $this->assertCount(2, $buddylist->getIgnoredRelationsByOwner());
428  $this->assertEquals(array_keys($relations), $buddylist->getRelationUserIds());
429  }
430 
435  {
436  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
437  $buddylist->reset();
438  $buddylist->setOwnerId("phpunit");
439  }
440 
445  {
446  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
447  $buddylist->reset();
448  $buddylist->getRelationByUserId("phpunit");
449  }
450 
452  {
453  $object = new ReflectionObject($relation);
454  $property = $object->getProperty('prior_state');
455  $property->setAccessible(true);
456 
457  $property->setValue($relation, $state);
458  }
459 
464  {
465  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
466  $buddylist->reset();
467 
468  $state = new ilBuddySystemLinkedRelationState();
469 
470  $relation = new ilBuddySystemRelation($state);
471  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
472  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
473 
474  $this->setPriorRelationState($relation, $state);
475 
476  $buddylist->link($relation);
477  }
478 
483  {
484  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
485  $buddylist->reset();
486 
488 
489  $relation = new ilBuddySystemRelation($state);
490  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID);
491  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
492 
493  $this->setPriorRelationState($relation, $state);
494 
495  $buddylist->ignore($relation);
496  }
497 
502  {
503  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
504  $buddylist->reset();
505 
506  $state = new ilBuddySystemUnlinkedRelationState();
507 
508  $relation = new ilBuddySystemRelation($state);
509  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
510  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
511 
512  $this->setPriorRelationState($relation, $state);
513 
514  $buddylist->unlink($relation);
515  }
516 
521  {
522  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
523  $buddylist->reset();
524 
526 
527  $relation = new ilBuddySystemRelation($state);
528  $relation->setUserId(self::BUDDY_LIST_BUDDY_ID);
529  $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
530 
531  $this->setPriorRelationState($relation, $state);
532 
533  $db = $this->getMockBuilder('ilDBInterface')->getMock();
534  $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
535  'login' => 'phpunit'
536  )));
537  $GLOBALS['ilDB'] = $db;
538 
539  $buddylist->request($relation);
540  }
541 
546  {
547  $this->assertException(ilBuddySystemRelationStateTransitionException::class);
548  $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
549  $buddylist->reset();
550 
551  $state = new ilBuddySystemLinkedRelationState();
552 
553  $relation = new ilBuddySystemRelation($state);
554  $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
555  $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
556 
557  $this->setPriorRelationState($relation, $state);
558 
559  $buddylist->ignore($relation);
560  }
561 }
testRelationCannotBeRequestedForAnonymous()
ilBuddySystemException
testInstanceCanBeCreatedByGlobalUserObject()
testValuesCanBeFetchedByGettersWhenSetBySetters()
Class ilBuddyListTest.
testAlreadyGivenStateExceptionIsThrownWhenAnIgnoredRelationShouldBeMarkedAsIgnored()
ilBuddySystemRelationStateAlreadyGivenException
Class ilBuddySystemUnlinkedRelationState.
testUnlinkedRelationIsReturnedWhenRelationWasRequestedForAUknownBuddyId()
testInstanceCannotBeCreatedByAnonymousGlobalUserObject()
ilBuddySystemException
testRelationRequestCannotBeIgnoredByTheRelationOwner()
ilBuddySystemException
testAlreadyGivenStateExceptionIsThrownWhenAnUnlinkedRelationShouldBeMarkedAsUnlinked()
ilBuddySystemRelationStateAlreadyGivenException
testRepositoryIsEnquiredWhenBuddyListShouldBeDestroyed()
testAlreadyGivenStateExceptionIsThrownWhenARequestedRelationShouldBeMarkedAsRequested()
ilBuddySystemRelationStateAlreadyGivenException
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list...
testRelationRequestCannotBeApprovedByTheRelationOwner()
ilBuddySystemException
static getInstanceByUserId($usr_id)
testRelationCannotBeRequestedForUnknownUserAccounts()
ilBuddySystemException
testInstanceByBeCreatedBySingletonMethod()
testExceptionIsThrownWhenRelationIsRequestedForANonNumericUserId()
InvalidArgumentException
testExceptionIsThrownWhenNonNumericOwnerIdIsPassed()
InvalidArgumentException
testRepositoryIsEnquiredToFetchRelationsWhenRequestedExplicitly()
Class ilBuddySystemRelation.
testRepositoryIsEnquiredOnlyOnceToFetchRelationsWhenCalledImplicitly()
testDifferentRelationStatesCanBeRetrieved()
setPriorRelationState(ilBuddySystemRelation $relation, ilBuddySystemRelationState $state)
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
testRelationRequestCanBeIgnoredByTheRelationTarget()
testStateTransitionExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsIgnored()
ilBuddySystemRelationStateTransitionException
Interface ilBuddySystemRelationState.
testRelationRequestCanBeApprovedByTheRelationTarget()
testAlreadyGivenStateExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsLinked()
ilBuddySystemRelationStateAlreadyGivenException