ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
ilServicesAdministrativeNotificationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\MockObject\MockObject;
22use PHPUnit\Framework\TestCase;
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;
47 }
48
49 public function testBasisc(): void
50 {
51 $notification = new ilADNNotification();
52 $this->assertSame(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
133 {
134 $now = new DateTimeImmutable();
135 $dates = static fn(?string $display_start, ?string $display_end, ?string $event_start, ?string $event_end): array => [
136 ilADNNotificationUIFormGUI::F_DISPLAY_DATE_START => $display_start === null ? null : $now->modify($display_start),
137 ilADNNotificationUIFormGUI::F_DISPLAY_DATE_END => $display_end === null ? null : $now->modify($display_end),
138 ilADNNotificationUIFormGUI::F_EVENT_DATE_START => $event_start === null ? null : $now->modify($event_start),
139 ilADNNotificationUIFormGUI::F_EVENT_DATE_END => $event_end === null ? null : $now->modify($event_end),
140 ];
141
143 // no event at all
144 $this->assertTrue(ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+1 day', null, null)));
145 // event within the display time
146 $this->assertTrue(
147 ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+3 hours', '+1 hour', '+2 hours'))
148 );
149 // display time is required to have a duration
150 $this->assertFalse(ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+0 days', null, null)));
151 // only one event date
152 $this->assertFalse(ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+1 day', '+1 hour', null)));
153 $this->assertFalse(ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+1 day', null, '+1 hour')));
154 // event without duration or outside of the display time
155 $this->assertFalse(
156 ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+1 day', '+1 hour', '+1 hour'))
157 );
158 $this->assertFalse(
159 ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+1 day', '-1 hour', '+1 hour'))
160 );
161 $this->assertFalse(
162 ilADNNotificationUIFormGUI::isValidDateConfiguration($dates('+0 days', '+1 day', '+1 hour', '+2 days'))
163 );
164 }
165
170 {
171 $notification = new ilADNNotification();
172 $notification->setPermanent(false);
173 $notification->setType(ilADNNotification::TYPE_WARNING);
174 $notification->setTypeDuringEvent(ilADNNotification::TYPE_ERROR);
175 $display_start = new DateTimeImmutable('-1 hours');
176 $notification->setDisplayStart($display_start);
177 $notification->setDisplayEnd(new DateTimeImmutable('+1 hours'));
178 $notification->setEventStart($display_start);
179 $notification->setEventEnd($display_start);
180
181 $this->assertFalse($notification->isDuringEvent());
182 $this->assertSame(ilADNNotification::TYPE_WARNING, $notification->getActiveType());
183 }
184}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:38
static isValidDateConfiguration(?array $v)
Display dates are required, event dates are optional (both empty), but if given, the event must be wi...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testDateConfigurationWithOptionalEvent()
Event dates are optional, see https://mantis.ilias.de/view.php?id=48295.
testEventWithoutDurationIsNeverActive()
Without event dates, the form stores an event without duration, which must never be active.
global $DIC
Definition: shib_login.php:26