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