Skip to content

Commit 61fc036

Browse files
committed
Handle exceptions
Fix tests Fix again
1 parent 99ea306 commit 61fc036

2 files changed

Lines changed: 28 additions & 25 deletions

File tree

controller/admin_input.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,28 @@ protected function send_ajax_response($success, $text)
325325
*/
326326
protected function validate_date($date, $type)
327327
{
328-
$timestamp = 0;
329-
if (preg_match('#^\d{4}-\d{2}-\d{2}$#', $date))
328+
if ($date === '')
330329
{
331-
// Create a UTC midnight timestamp for storage consistency
332-
$datetime = \DateTime::createFromFormat(ext::DATE_FORMAT, $date, new \DateTimeZone('UTC'));
333-
$datetime->setTime(0, 0, 0); // Ensure midnight (00:00:00)
334-
$timestamp = $datetime->getTimestamp();
335-
336-
// Compare against a user's timezone to prevent timezone confusion
337-
// 'today' creates today's date at midnight in the user's timezone
338-
$user_today = new \DateTime('today', $this->user->timezone);
339-
if ($timestamp < $user_today->getTimestamp())
340-
{
341-
$this->errors[] = 'AD_' . $type . '_DATE_INVALID';
342-
}
330+
return 0;
331+
}
332+
333+
if (!preg_match('#^\d{4}-\d{2}-\d{2}$#', $date))
334+
{
335+
$this->errors[] = 'AD_' . $type . '_DATE_INVALID';
336+
return 0;
343337
}
344-
else if ($date !== '')
338+
339+
// Create a UTC midnight timestamp for storage consistency
340+
$datetime = \DateTime::createFromFormat(ext::DATE_FORMAT, $date, new \DateTimeZone('UTC'));
341+
$datetime->setTime(0, 0, 0); // Ensure midnight (00:00:00)
342+
$timestamp = $datetime->getTimestamp();
343+
344+
// Compare against user's current day to avoid timezone confusion
345+
$user_today = new \DateTime('today', $this->user->timezone);
346+
if ($timestamp < $user_today->getTimestamp())
345347
{
346348
$this->errors[] = 'AD_' . $type . '_DATE_INVALID';
349+
return 0;
347350
}
348351

349352
return $timestamp;

tests/functional/acp_manage_test.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function test_acp_add()
7272

7373
// Confirm error when submitting older end date than start date
7474
$form_data = array(
75-
'ad_start_date' => '2018-01-01',
76-
'ad_end_date' => '2017-01-01',
75+
'ad_start_date' => '2035-01-01',
76+
'ad_end_date' => '2034-01-01',
7777
);
7878
$this->submit_with_error($crawler, $form_data, $this->lang('END_DATE_TOO_SOON'));
7979

@@ -119,8 +119,8 @@ public function test_acp_add()
119119
'ad_note' => 'Functional test note',
120120
'ad_code' => '<!-- SAMPLE ADD CODE -->',
121121
'ad_enabled' => 1,
122-
'ad_start_date' => '2030-01-01',
123-
'ad_end_date' => '2035-01-01',
122+
'ad_start_date' => '2035-01-01',
123+
'ad_end_date' => '2036-01-01',
124124
'ad_priority' => 1,
125125
'ad_views_limit' => 0,
126126
'ad_clicks_limit' => 0,
@@ -145,7 +145,7 @@ public function test_acp_add()
145145
$crawler = $this->get_manage_page();
146146
self::assertStringContainsString('Functional test name', $crawler->text());
147147
$this->assertContainsLang('ENABLED', $crawler->text());
148-
self::assertStringContainsString('2035-01-01', $crawler->text());
148+
self::assertStringContainsString('2036-01-01', $crawler->text());
149149

150150
// Confirm the log entry has been added correctly
151151
$crawler = self::request('GET', "adm/index.php?i=acp_logs&mode=admin&sid={$this->sid}");
@@ -190,8 +190,8 @@ public function test_acp_edit()
190190

191191
// Confirm error when submitting older end date than start date
192192
$form_data = array(
193-
'ad_start_date' => '2018-01-01',
194-
'ad_end_date' => '2017-01-01',
193+
'ad_start_date' => '2035-01-01',
194+
'ad_end_date' => '2034-01-01',
195195
);
196196
$this->submit_with_error($crawler, $form_data, $this->lang('END_DATE_TOO_SOON'));
197197

@@ -237,8 +237,8 @@ public function test_acp_edit()
237237
'ad_note' => 'Functional test note',
238238
'ad_code' => '<!-- SAMPLE ADD CODE EDITED -->',
239239
'ad_enabled' => 0,
240-
'ad_start_date' => '2030-01-02',
241-
'ad_end_date' => '2035-01-02',
240+
'ad_start_date' => '2035-01-02',
241+
'ad_end_date' => '2036-01-02',
242242
'ad_priority' => 2,
243243
'ad_views_limit' => 0,
244244
'ad_clicks_limit' => 0,
@@ -263,8 +263,8 @@ public function test_acp_edit()
263263
$crawler = $this->get_manage_page();
264264
self::assertStringContainsString('Functional test name edited', $crawler->text());
265265
$this->assertContainsLang('DISABLED', $crawler->text());
266-
self::assertStringContainsString('2030-01-02', $crawler->text());
267266
self::assertStringContainsString('2035-01-02', $crawler->text());
267+
self::assertStringContainsString('2036-01-02', $crawler->text());
268268

269269
// Confirm the log entry has been added correctly
270270
$crawler = self::request('GET', "adm/index.php?i=acp_logs&mode=admin&sid={$this->sid}");

0 commit comments

Comments
 (0)