ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilMailOptionsTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
28 {
29  protected stdClass $object;
30  protected MockObject&ilDBInterface $database;
31  protected MockObject&ilSetting $settings;
32 
33  protected function setUp(): void
34  {
35  parent::setUp();
36 
37  $this->database = $this->getMockBuilder(ilDBInterface::class)
38  ->getMock();
39  $queryMock = $this->getMockBuilder(ilDBStatement::class)
40  ->getMock();
41 
42  $this->object = new stdClass();
43  $this->object->cronjob_notification = false;
44  $this->object->signature = 'smth';
45  $this->object->incoming_type = 1;
46  $this->object->mail_address_option = 0;
47  $this->object->email = 'test@test.com';
48  $this->object->second_email = 'ilias@ilias.com';
49  $this->object->absence_status = false;
50  $this->object->absent_from = time();
51  $this->object->absent_until = time();
52  $this->object->absence_ar_subject = 'subject';
53  $this->object->absence_ar_body = 'body';
54 
55 
56  $this->database->expects($this->once())->method('queryF')->willReturn($queryMock);
57  $this->database->expects($this->once())->method('fetchObject')->willReturn($this->object);
58  $this->database->method('replace')->willReturn(0);
59  $this->setGlobalVariable('ilDB', $this->database);
60  }
61 
62  public function testConstructor(): void
63  {
64  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods(['get'])->getMock();
65  $settings->method('get')->willReturnCallback(static function (string $key, ?string $default = null) {
66  if ($key === 'mail_incoming_mail' || $key === 'mail_address_option') {
67  return $default;
68  }
69 
70  if ($key === 'show_mail_settings') {
71  return '0';
72  }
73 
74  return $default;
75  });
76 
77  $mailOptions = new ilMailOptions(
78  1,
79  null,
80  $this->createMock(ClockInterface::class),
81  $settings
82  );
83 
84  $this->assertSame('', $mailOptions->getSignature());
85  $this->assertSame(ilMailOptions::INCOMING_LOCAL, $mailOptions->getIncomingType());
86  $this->assertFalse($mailOptions->isCronJobNotificationEnabled());
87  }
88 
89  public function testConstructorWithUserSettings(): void
90  {
91  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods(['get'])->getMock();
92  $settings->method('get')->willReturnCallback(static function (string $key, ?string $default = null) {
93  if ($key === 'mail_incoming_mail' || $key === 'mail_address_option') {
94  return $default;
95  }
96 
97  if ($key === 'show_mail_settings') {
98  return '1';
99  }
100 
101  if ($key === 'usr_settings_disable_mail_incoming_mail') {
102  return '0';
103  }
104 
105  return $default;
106  });
107 
108  $mailOptions = new ilMailOptions(
109  1,
110  null,
111  $this->createMock(ClockInterface::class),
112  $settings
113  );
114 
115  $this->assertSame($this->object->signature, $mailOptions->getSignature());
116  $this->assertSame($this->object->incoming_type, $mailOptions->getIncomingType());
117  $this->assertSame($this->object->cronjob_notification, $mailOptions->isCronJobNotificationEnabled());
118  $this->assertSame($this->object->absence_status, $mailOptions->getAbsenceStatus());
119  $this->assertSame($this->object->absent_from, $mailOptions->getAbsentFrom());
120  $this->assertSame($this->object->absent_until, $mailOptions->getAbsentUntil());
121  $this->assertSame($this->object->absence_ar_subject, $mailOptions->getAbsenceAutoresponderSubject());
122  $this->assertSame($this->object->absence_ar_body, $mailOptions->getAbsenceAutoresponderBody());
123  }
124 
128  public function testIsAbsent(bool $absence_status, int $absent_from, int $absent_until, bool $result): void
129  {
130  $userId = 1;
131  $this->object->absence_status = $absence_status;
132  $this->object->absent_from = $absent_from;
133  $this->object->absent_until = $absent_until;
134  $this->object->absence_ar_subject = 'subject';
135  $this->object->absence_ar_body = 'body';
136 
137  $clockService = $this->createMock(ClockInterface::class);
138  $clockService->method('now')->willReturn((new DateTimeImmutable())->setTimestamp(100));
139 
140  $settings = $this->getMockBuilder(ilSetting::class)->disableOriginalConstructor()->onlyMethods(['get'])->getMock();
141  $settings->method('get')->willReturnCallback(static function (string $key, ?string $default = null) {
142  if ($key === 'mail_incoming_mail' || $key === 'mail_address_option') {
143  return $default;
144  }
145 
146  if ($key === 'show_mail_settings') {
147  return '1';
148  }
149 
150  if ($key === 'usr_settings_disable_mail_incoming_mail') {
151  return '0';
152  }
153 
154  return $default;
155  });
156 
157  $mailOptions = new ilMailOptions(
158  $userId,
159  null,
160  $clockService,
161  $settings
162  );
163 
164  $this->assertEquals($result, $mailOptions->isAbsent());
165  }
166 
167  public static function provideMailOptionsData(): Generator
168  {
169  yield 'correct configuration' => [
170  'absence_status' => true,
171  'absent_from' => 100,
172  'absent_until' => 100,
173  'result' => true,
174  ];
175 
176  yield 'not absent' => [
177  'absence_status' => false,
178  'absent_from' => 100,
179  'absent_until' => 100,
180  'result' => false,
181  ];
182 
183  yield 'absent, absent_from is in the future' => [
184  'absence_status' => true,
185  'absent_from' => 100 + 1,
186  'absent_until' => 100,
187  'result' => false,
188  ];
189 
190  yield 'absent, absent_until is in the past' => [
191  'absence_status' => true,
192  'absent_from' => 100,
193  'absent_until' => 100 - 1,
194  'result' => false,
195  ];
196 
197  yield 'absent, absent_from is in the past, absent_until is in the future' => [
198  'absence_status' => true,
199  'absent_from' => 100 - 1,
200  'absent_until' => 100 + 1,
201  'result' => true,
202  ];
203  }
204 }
MockObject &ilSetting $settings
Class ilMailBaseTest.
testIsAbsent(bool $absence_status, int $absent_from, int $absent_until, bool $result)
provideMailOptionsData
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setGlobalVariable(string $name, $value)
final const INCOMING_LOCAL
MockObject &ilDBInterface $database