ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
4require_once 'Services/Contact/BuddySystem/test/ilBuddySystemBaseTest.php';
5
12{
15
19 protected $buddylist;
20
25 protected function setGlobalVariable($name, $value)
26 {
27 global $DIC;
28
29 $GLOBALS[$name] = $value;
30
31 unset($DIC[$name]);
32 $DIC[$name] = function ($c) use ($name) {
33 return $GLOBALS[$name];
34 };
35 }
36
40 public function setUp()
41 {
42 $this->setGlobalVariable(
43 'ilAppEventHandler',
44 $this->getMockBuilder('ilAppEventHandler')->disableOriginalConstructor()->setMethods(array('raise'))->getMock()
45 );
46 $this->setGlobalVariable('ilDB', $this->getMockBuilder('ilDBInterface')->getMock());
47 $this->setGlobalVariable(
48 'lng',
49 $this->getMockBuilder('ilLanguage')
50 ->disableOriginalConstructor()
51 ->setMethods(array('txt', 'loadLanguageModule'))
52 ->getMock()
53 );
54 }
55
60 {
61 $user = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(array('getId'))->getMock();
62 $user->expects($this->once())->method('getId')->will($this->returnValue(self::BUDDY_LIST_OWNER_ID));
63 $this->setGlobalVariable('ilUser', $user);
64
66 }
67
72 {
73 $this->assertException(ilBuddySystemException::class);
74 $user = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(array('getId'))->getMock();
75 $user->expects($this->once())->method('getId')->will($this->returnValue(ANONYMOUS_USER_ID));
76 $this->setGlobalVariable('ilUser', $user);
77
79 }
80
85 {
86 $relations = array(
89 );
90
91 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
92 $buddylist->setRelations(new ilBuddySystemRelationCollection($relations));
93 $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
94 $other_buddylist->setRelations(new ilBuddySystemRelationCollection());
95
96 $this->assertEquals($buddylist, $other_buddylist);
97 }
98
102 public function testListIsInitiallyEmpty()
103 {
104 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
105 $repo->expects($this->exactly(1))->method('getAll')->willReturn(array());
106
107 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
108 $buddylist->reset();
109 $buddylist->setRepository($repo);
110
111 $this->assertEmpty($buddylist->getRelations());
112 }
113
118 {
119 $relations = array(
122 );
123
124 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
125 $repo->expects($this->exactly(1))->method('getAll')->willReturn($relations);
126
127 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
128 $buddylist->reset();
129 $buddylist->setRepository($repo);
130 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
131 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
132 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
133 }
134
139 {
140 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
141 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
142 $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
143
144 $relations = array(
145 $expected_relation->getBuddyUserId() => $expected_relation
146 );
147
148 $db = $this->getMockBuilder('ilDBInterface')->getMock();
149 $db->expects($this->exactly(2))->method('queryF');
150 $db->expects($this->exactly(2))->method('fetchAssoc')->will($this->returnValue(array(
151 'login' => 'phpunit'
152 )));
153 $this->setGlobalVariable('ilDB', $db);
154
155 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
156 $repo->expects($this->once())->method('getAll')->willReturn($relations);
157 $repo->expects($this->exactly(3))->method('save')->with($expected_relation);
158
159 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
160 $buddylist->reset();
161 $buddylist->setRepository($repo);
162
163 $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
164 $buddylist->request($relation);
165 $buddylist->unlink($relation);
166 $buddylist->request($relation);
167 }
168
173 {
174 $this->assertException(ilBuddySystemException::class);
175 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
176 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
177 $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
178
179 $relations = array(
180 $expected_relation->getBuddyUserId() => $expected_relation
181 );
182
183 $db = $this->getMockBuilder('ilDBInterface')->getMock();
184 $db->expects($this->once())->method('queryF');
185 $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(array(
186 'login' => 'phpunit'
187 )));
188 $this->setGlobalVariable('ilDB', $db);
189
190 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
191 $repo->expects($this->once())->method('getAll')->willReturn($relations);
192 $repo->expects($this->exactly(1))->method('save')->with($expected_relation);
193
194 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
195 $buddylist->reset();
196 $buddylist->setRepository($repo);
197
198 $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
199 $buddylist->request($relation);
200 $buddylist->link($relation);
201 }
202
207 {
208 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
209 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
210 $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
211
212 $relations = array(
213 $expected_relation->getBuddyUserId() => $expected_relation
214 );
215
216 $db = $this->getMockBuilder('ilDBInterface')->getMock();
217 $db->expects($this->any())->method('queryF');
218 $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
219 'login' => 'phpunit'
220 )));
221 $this->setGlobalVariable('ilDB', $db);
222
223 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
224 $repo->expects($this->any())->method('getAll')->willReturn($relations);
225 $repo->expects($this->any())->method('save')->with($expected_relation);
226
227 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
228 $buddylist->reset();
229 $buddylist->setRepository($repo);
230 $buddylist->request($expected_relation);
231
232 $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
233 $other_buddylist->reset();
234 $other_buddylist->setRepository($repo);
235 $other_buddylist->link($expected_relation);
236 }
237
242 {
243 $this->assertException(ilBuddySystemException::class);
244 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
245 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
246 $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
247
248 $relations = array(
249 $expected_relation->getBuddyUserId() => $expected_relation
250 );
251
252 $db = $this->getMockBuilder('ilDBInterface')->getMock();
253 $db->expects($this->once())->method('queryF');
254 $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(array(
255 'login' => 'phpunit'
256 )));
257 $this->setGlobalVariable('ilDB', $db);
258
259 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
260 $repo->expects($this->once())->method('getAll')->willReturn($relations);
261 $repo->expects($this->exactly(1))->method('save')->with($expected_relation);
262
263 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
264 $buddylist->reset();
265 $buddylist->setRepository($repo);
266
267 $relation = $buddylist->getRelationByUserId($expected_relation->getBuddyUserId());
268 $buddylist->request($relation);
269 $buddylist->ignore($relation);
270 }
271
276 {
277 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
278 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
279 $expected_relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
280
281 $relations = array(
282 $expected_relation->getBuddyUserId() => $expected_relation
283 );
284
285 $db = $this->getMockBuilder('ilDBInterface')->getMock();
286 $db->expects($this->any())->method('queryF');
287 $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
288 'login' => 'phpunit'
289 )));
290 $this->setGlobalVariable('ilDB', $db);
291
292 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
293 $repo->expects($this->any())->method('getAll')->willReturn($relations);
294 $repo->expects($this->any())->method('save')->with($expected_relation);
295
296 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
297 $buddylist->reset();
298 $buddylist->setRepository($repo);
299 $buddylist->request($expected_relation);
300
301 $other_buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_BUDDY_ID);
302 $other_buddylist->reset();
303 $other_buddylist->setRepository($repo);
304 $other_buddylist->ignore($expected_relation);
305 }
306
311 {
312 $this->assertException(ilBuddySystemException::class);
313 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
314 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
315 $expected_relation->setBuddyUserId(ANONYMOUS_USER_ID);
316
317 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
318 $repo->expects($this->never())->method('getAll')->willReturn(array());
319 $repo->expects($this->never())->method('save');
320
321 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
322 $buddylist->reset();
323 $buddylist->setRepository($repo);
324 $buddylist->request($expected_relation);
325 }
326
331 {
332 $this->assertException(ilBuddySystemException::class);
333 $expected_relation = new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState());
334 $expected_relation->setUserId(self::BUDDY_LIST_OWNER_ID);
335 $expected_relation->setBuddyUserId(-3);
336
337 $db = $this->getMockBuilder('ilDBInterface')->getMock();
338 $db->expects($this->once())->method('queryF');
339 $db->expects($this->once())->method('fetchAssoc')->will($this->returnValue(null));
340 $this->setGlobalVariable('ilDB', $db);
341
342 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
343 $repo->expects($this->never())->method('getAll')->willReturn(array());
344 $repo->expects($this->never())->method('save');
345
346 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
347 $buddylist->reset();
348 $buddylist->setRepository($repo);
349 $buddylist->request($expected_relation);
350 }
351
356 {
357 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
358 $repo->expects($this->once())->method('destroy');
359
360 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
361 $buddylist->reset();
362 $buddylist->setRepository($repo);
363 $buddylist->destroy();
364 }
365
370 {
371 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
372 $repo->expects($this->once())->method('getAll')->willReturn(array());
373
374 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
375 $buddylist->reset();
376 $buddylist->setRepository($repo);
377 $this->assertInstanceOf('ilBuddySystemUnlinkedRelationState', $buddylist->getRelationByUserId(-3)->getState());
378 }
379
384 {
385 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
386 $buddylist->reset();
387 $buddylist->setOwnerId(self::BUDDY_LIST_BUDDY_ID);
388 $this->assertEquals(self::BUDDY_LIST_BUDDY_ID, $buddylist->getOwnerId());
389
390 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
391 $repo->expects($this->never())->method('getAll')->willReturn(array());
392 $buddylist->setRepository($repo);
393 $this->assertEquals($repo, $buddylist->getRepository());
394
395 $relations = array(
396 self::BUDDY_LIST_BUDDY_ID => new ilBuddySystemRelation(new ilBuddySystemUnlinkedRelationState())
397 );
398 $buddylist->setRelations(new ilBuddySystemRelationCollection($relations));
399 $this->assertEquals(new ilBuddySystemRelationCollection($relations), $buddylist->getRelations());
400 }
401
406 {
407 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
408 $buddylist->reset();
409
410 $relations = array();
411
413 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
414 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
415 $relations[self::BUDDY_LIST_BUDDY_ID] = $relation;
416
418 $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 1);
419 $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
420 $relations[self::BUDDY_LIST_BUDDY_ID + 1] = $relation;
421
423 $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 2);
424 $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
425 $relations[self::BUDDY_LIST_BUDDY_ID + 2] = $relation;
426
428 $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 3);
429 $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
430 $relations[self::BUDDY_LIST_BUDDY_ID + 3] = $relation;
431
433 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
434 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 4);
435 $relations[self::BUDDY_LIST_BUDDY_ID + 4] = $relation;
436
438 $relation->setUserId(self::BUDDY_LIST_BUDDY_ID + 5);
439 $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
440 $relations[self::BUDDY_LIST_BUDDY_ID + 5] = $relation;
441
443 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
444 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 6);
445 $relations[self::BUDDY_LIST_BUDDY_ID + 6] = $relation;
446
448 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
449 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID + 7);
450 $relations[self::BUDDY_LIST_BUDDY_ID + 7] = $relation;
451
452 $repo = $this->getMockBuilder('ilBuddySystemRelationRepository')->disableOriginalConstructor()->getMock();
453 $repo->expects($this->any())->method('getAll')->willReturn($relations);
454 $buddylist->setRepository($repo);
455
456 $this->assertCount(3, $buddylist->getLinkedRelations());
457 $this->assertCount(1, $buddylist->getRequestRelationsForOwner());
458 $this->assertCount(1, $buddylist->getRequestRelationsByOwner());
459 $this->assertCount(1, $buddylist->getIgnoredRelationsForOwner());
460 $this->assertCount(2, $buddylist->getIgnoredRelationsByOwner());
461 $this->assertEquals(array_keys($relations), $buddylist->getRelationUserIds());
462 }
463
468 {
469 $this->assertException(InvalidArgumentException::class);
470 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
471 $buddylist->reset();
472 $buddylist->setOwnerId("phpunit");
473 }
474
479 {
480 $this->assertException(InvalidArgumentException::class);
481 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
482 $buddylist->reset();
483 $buddylist->getRelationByUserId("phpunit");
484 }
485
487 {
488 $object = new ReflectionObject($relation);
489 $property = $object->getProperty('prior_state');
490 $property->setAccessible(true);
491
492 $property->setValue($relation, $state);
493 }
494
499 {
500 $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
501 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
502 $buddylist->reset();
503
505
506 $relation = new ilBuddySystemRelation($state);
507 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
508 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
509
510 $this->setPriorRelationState($relation, $state);
511
512 $buddylist->link($relation);
513 }
514
519 {
520 $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
521 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
522 $buddylist->reset();
523
525
526 $relation = new ilBuddySystemRelation($state);
527 $relation->setUserId(self::BUDDY_LIST_BUDDY_ID);
528 $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
529
530 $this->setPriorRelationState($relation, $state);
531
532 $buddylist->ignore($relation);
533 }
534
539 {
540 $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
541 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
542 $buddylist->reset();
543
545
546 $relation = new ilBuddySystemRelation($state);
547 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
548 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
549
550 $this->setPriorRelationState($relation, $state);
551
552 $buddylist->unlink($relation);
553 }
554
559 {
560 $this->assertException(ilBuddySystemRelationStateAlreadyGivenException::class);
561 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
562 $buddylist->reset();
563
565
566 $relation = new ilBuddySystemRelation($state);
567 $relation->setUserId(self::BUDDY_LIST_BUDDY_ID);
568 $relation->setBuddyUserId(self::BUDDY_LIST_OWNER_ID);
569
570 $this->setPriorRelationState($relation, $state);
571
572 $db = $this->getMockBuilder('ilDBInterface')->getMock();
573 $db->expects($this->any())->method('fetchAssoc')->will($this->returnValue(array(
574 'login' => 'phpunit'
575 )));
576 $this->setGlobalVariable('ilDB', $db);
577
578 $buddylist->request($relation);
579 }
580
585 {
586 $this->assertException(ilBuddySystemRelationStateTransitionException::class);
587 $buddylist = ilBuddyList::getInstanceByUserId(self::BUDDY_LIST_OWNER_ID);
588 $buddylist->reset();
589
591
592 $relation = new ilBuddySystemRelation($state);
593 $relation->setUserId(self::BUDDY_LIST_OWNER_ID);
594 $relation->setBuddyUserId(self::BUDDY_LIST_BUDDY_ID);
595
596 $this->setPriorRelationState($relation, $state);
597
598 $buddylist->ignore($relation);
599 }
600}
An exception for terminatinating execution or to throw for unit testing.
Class ilBuddyListTest.
testRepositoryIsEnquiredWhenBuddyListShouldBeDestroyed()
testAlreadyGivenStateExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsLinked()
@expectedException ilBuddySystemRelationStateAlreadyGivenException
testRelationRequestCanBeIgnoredByTheRelationTarget()
testDifferentRelationStatesCanBeRetrieved()
testRelationCannotBeRequestedForAnonymous()
@expectedException ilBuddySystemException
testRelationRequestCannotBeIgnoredByTheRelationOwner()
@expectedException ilBuddySystemException
testAlreadyGivenStateExceptionIsThrownWhenAnUnlinkedRelationShouldBeMarkedAsUnlinked()
@expectedException ilBuddySystemRelationStateAlreadyGivenException
testAlreadyGivenStateExceptionIsThrownWhenARequestedRelationShouldBeMarkedAsRequested()
@expectedException ilBuddySystemRelationStateAlreadyGivenException
testInstanceCannotBeCreatedByAnonymousGlobalUserObject()
@expectedException ilBuddySystemException
testRepositoryIsEnquiredToFetchRelationsWhenRequestedExplicitly()
testExceptionIsThrownWhenRelationIsRequestedForANonNumericUserId()
@expectedException InvalidArgumentException
testRelationRequestCannotBeApprovedByTheRelationOwner()
@expectedException ilBuddySystemException
testAlreadyGivenStateExceptionIsThrownWhenAnIgnoredRelationShouldBeMarkedAsIgnored()
@expectedException ilBuddySystemRelationStateAlreadyGivenException
testRelationCannotBeRequestedForUnknownUserAccounts()
@expectedException ilBuddySystemException
testValuesCanBeFetchedByGettersWhenSetBySetters()
testStateTransitionExceptionIsThrownWhenALinkedRelationShouldBeMarkedAsIgnored()
@expectedException ilBuddySystemRelationStateTransitionException
testInstanceByBeCreatedBySingletonMethod()
testRepositoryIsEnquiredOnlyOnceToFetchRelationsWhenCalledImplicitly()
testUnlinkedRelationIsReturnedWhenRelationWasRequestedForAUknownBuddyId()
testInstanceCanBeCreatedByGlobalUserObject()
testRelationRequestCanBeApprovedByTheRelationTarget()
testExceptionIsThrownWhenNonNumericOwnerIdIsPassed()
@expectedException InvalidArgumentException
setPriorRelationState(ilBuddySystemRelation $relation, ilBuddySystemRelationState $state)
setGlobalVariable($name, $value)
static getInstanceByUserId($usr_id)
static getInstanceByGlobalUser()
assertException($exception_class)
Class ilBuddySystemRelationCollection A collection which contains all entries of a buddy list.
Class ilBuddySystemRelation.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
Interface ilBuddySystemRelationState.
global $DIC