ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilServicesAdministrativeNotificationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 
25 {
30  protected $rbacreview_mock;
31 
32  protected function setUp(): void
33  {
34  global $DIC;
35  $this->dic_backup = is_object($DIC) ? clone $DIC : $DIC;
36 
37  $DIC = new Container();
38  $DIC['ilDB'] = $this->createMock(ilDBInterface::class);
39  $this->rbacreview_mock = $DIC['rbacreview'] = $this->createMock(ilRbacReview::class);
40  }
41 
42  protected function tearDown(): void
43  {
44  global $DIC;
45  $DIC = $this->dic_backup;
46  }
47 
48  public function testBasisc(): void
49  {
50  $notification = new ilADNNotification();
51  $this->assertEquals(0, $notification->getId());
52 
53  $notification->setTitle('Title');
54  $notification->setActive(true);
55  $notification->setType(ilADNNotification::TYPE_WARNING);
56  $notification->setTypeDuringEvent(ilADNNotification::TYPE_ERROR);
57 
58  $this->assertTrue($notification->isActive());
59  $this->assertFalse($notification->isDuringEvent());
60  }
61 
62  public function testVisibilityByDate(): void
63  {
64  $notification = new ilADNNotification();
65  $user_mock = $this->createMock(ilObjUser::class);
66  $user_mock->expects($this->atLeast(1))
67  ->method('getId')
68  ->willReturn(42);
69 
70  $notification->setPermanent(true);
71  $this->assertTrue($notification->isVisibleForUser($user_mock));
72 
73  $notification->setPermanent(false);
74 
75  $notification->setDisplayStart(new DateTimeImmutable("-2 hours"));
76  $notification->setEventStart(new DateTimeImmutable("-1 hours"));
77  $notification->setDisplayEnd(new DateTimeImmutable("+2 hours"));
78  $notification->setEventEnd(new DateTimeImmutable("+1 hours"));
79  $this->assertTrue($notification->isVisibleForUser($user_mock));
80 
81  $notification->setDisplayStart(new DateTimeImmutable("+1 hours"));
82  $notification->setEventStart(new DateTimeImmutable("+2 hours"));
83  $notification->setDisplayEnd(new DateTimeImmutable("+4 hours"));
84  $notification->setEventEnd(new DateTimeImmutable("+3 hours"));
85  $this->assertFalse($notification->isVisibleForUser($user_mock));
86  }
87 
88  public function testVisibilityByRole(): void
89  {
90  $notification = new ilADNNotification();
91  $user_mock = $this->createMock(ilObjUser::class);
92  $user_mock->expects($this->atLeast(1))
93  ->method('getId')
94  ->willReturn(42);
95 
96  $notification->setPermanent(true);
97  $notification->setLimitToRoles(true);
98  $notification->setLimitedToRoleIds([2, 22, 222]);
99 
100  $this->rbacreview_mock->expects($this->once())
101  ->method('isAssignedToAtLeastOneGivenRole')
102  ->with(42, [2, 22, 222])
103  ->willReturn(true);
104 
105  $this->assertTrue($notification->isVisibleForUser($user_mock));
106  }
107 
108  public function testVisibilityByRoleNotGranted(): void
109  {
110  $notification = new ilADNNotification();
111  $user_mock = $this->createMock(ilObjUser::class);
112  $user_mock->expects($this->atLeast(1))
113  ->method('getId')
114  ->willReturn(42);
115 
116  $notification->setPermanent(true);
117  $notification->setLimitToRoles(true);
118  $notification->setLimitedToRoleIds([2, 22, 222]);
119 
120  $this->rbacreview_mock->expects($this->once())
121  ->method('isAssignedToAtLeastOneGivenRole')
122  ->with(42, [2, 22, 222])
123  ->willReturn(false);
124 
125  $this->assertFalse($notification->isVisibleForUser($user_mock));
126  }
127 }
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:31
global $DIC
Definition: feed.php:28