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