Files
global-autoresponder/tests/rule_validator_test.php
T
2026-07-04 15:19:34 +02:00

175 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
require_once PLUGIN_ROOT . '/exec/lib/Settings.php';
require_once PLUGIN_ROOT . '/exec/lib/RuleValidator.php';
/** @return array{0:string,1:string} */
function future_exact_schedule(Settings $settings, string $startTime = '09:37', string $endTime = '17:12'): array
{
$tz = new DateTimeZone($settings->timezone());
[$startHour, $startMinute] = array_map('intval', explode(':', $startTime));
[$endHour, $endMinute] = array_map('intval', explode(':', $endTime));
$start = (new DateTimeImmutable('tomorrow', $tz))->setTime($startHour, $startMinute);
$end = (new DateTimeImmutable('+15 days', $tz))->setTime($endHour, $endMinute);
return [$start->format('Y-m-d\TH:i'), $end->format('Y-m-d\TH:i')];
}
test('rule validator normalizes exact hour schedule', function (): void {
$settingsPath = TEST_TMP . '/validator.conf';
test_write($settingsPath, "GLOBAL_AUTORESPONDER_TIMEZONE=Europe/Warsaw\n");
$settings = Settings::load($settingsPath);
[$startValue, $endValue] = future_exact_schedule($settings);
$rule = RuleValidator::validate([
'name' => 'Urlop czerwcowy',
'subject_prefix' => 'Urlop',
'reply_frequency' => '120',
'body' => "Dzień dobry\r\nWracam jutro",
'start_value' => $startValue,
'end_value' => $endValue,
'enabled' => '1',
], $settings);
assert_same('Urlop czerwcowy', $rule['name']);
assert_same('Urlop', $rule['subject_prefix']);
assert_same(120, $rule['repeat_minutes']);
assert_same("Dzień dobry\nWracam jutro", $rule['body']);
assert_same('exact_hour', $rule['schedule_mode']);
assert_same($startValue, $rule['start_value']);
assert_true($rule['start_ts'] < $rule['end_ts']);
});
test('rule validator accepts exact hour schedule', function (): void {
$settings = Settings::load(TEST_TMP . '/validator-da.conf');
[$startValue, $endValue] = future_exact_schedule($settings, '00:00', '23:59');
$rule = RuleValidator::validate([
'subject_prefix' => 'Urlop',
'reply_frequency' => '1',
'body' => 'Body',
'start_value' => $startValue,
'end_value' => $endValue,
'enabled' => '1',
], $settings);
assert_same('exact_hour', $rule['schedule_mode']);
assert_same(1, $rule['repeat_minutes']);
assert_same($startValue, $rule['start_value']);
assert_same($endValue, $rule['end_value']);
});
test('rule validator rejects unsupported non-interval frequency', function (): void {
$settings = Settings::load(TEST_TMP . '/validator-da-every.conf');
[$startValue, $endValue] = future_exact_schedule($settings, '00:00', '23:59');
try {
RuleValidator::validate([
'subject_prefix' => 'Urlop',
'reply_frequency' => 'every',
'body' => 'Body',
'start_value' => $startValue,
'end_value' => $endValue,
], $settings);
} catch (InvalidArgumentException $e) {
assert_contains('Częstość odpowiedzi', $e->getMessage());
return;
}
throw new RuntimeException('Expected every-message frequency to fail');
});
test('rule validator rejects header injection and reversed dates', function (): void {
$settings = Settings::load(TEST_TMP . '/missing.conf');
try {
RuleValidator::validate([
'subject_prefix' => "Bad\nSubject",
'body' => 'Body',
'start_value' => '2026-06-24T17:00',
'end_value' => '2026-06-10T09:00',
], $settings);
} catch (InvalidArgumentException $e) {
assert_contains('Przedrostek tematu', $e->getMessage());
return;
}
throw new RuntimeException('Expected validation failure');
});
test('rule validator rejects unsupported response frequency', function (): void {
$settings = Settings::load(TEST_TMP . '/missing-frequency.conf');
[$startValue, $endValue] = future_exact_schedule($settings, '00:00', '23:59');
try {
RuleValidator::validate([
'subject_prefix' => 'Urlop',
'reply_frequency' => '45',
'body' => 'Body',
'start_value' => $startValue,
'end_value' => $endValue,
], $settings);
} catch (InvalidArgumentException $e) {
assert_contains('Częstość odpowiedzi', $e->getMessage());
return;
}
throw new RuntimeException('Expected invalid frequency to fail');
});
test('rule validator rejects start date before current date', function (): void {
$settingsPath = TEST_TMP . '/validator-past.conf';
test_write($settingsPath, "GLOBAL_AUTORESPONDER_TIMEZONE=Europe/Warsaw\n");
$settings = Settings::load($settingsPath);
$tz = new DateTimeZone($settings->timezone());
$yesterday = (new DateTimeImmutable('yesterday', $tz))->format('Y-m-d');
$tomorrow = (new DateTimeImmutable('tomorrow', $tz))->format('Y-m-d');
try {
RuleValidator::validate([
'subject_prefix' => 'Urlop',
'reply_frequency' => '1440',
'body' => 'Body',
'start_value' => $yesterday . 'T09:00',
'end_value' => $tomorrow . 'T17:00',
], $settings);
} catch (InvalidArgumentException $e) {
assert_contains('wcześniejsza niż aktualna', $e->getMessage());
return;
}
throw new RuntimeException('Expected past start date to fail');
});
test('rule validator rejects end date before current date when creating', function (): void {
$settingsPath = TEST_TMP . '/validator-past-end.conf';
test_write($settingsPath, "GLOBAL_AUTORESPONDER_TIMEZONE=Europe/Warsaw\n");
$settings = Settings::load($settingsPath);
$tz = new DateTimeZone($settings->timezone());
$yesterday = (new DateTimeImmutable('yesterday', $tz))->format('Y-m-d');
$today = (new DateTimeImmutable('today', $tz))->format('Y-m-d');
try {
RuleValidator::validate([
'subject_prefix' => 'Urlop',
'reply_frequency' => '1440',
'body' => 'Body',
'start_value' => $today . 'T09:00',
'end_value' => $yesterday . 'T17:00',
], $settings);
} catch (InvalidArgumentException $e) {
assert_contains('Data zakończenia nie może być wcześniejsza niż aktualna data.', $e->getMessage());
return;
}
throw new RuntimeException('Expected past end date to fail on create');
});
test('rule validator allows past schedule when editing', function (): void {
$settingsPath = TEST_TMP . '/validator-edit-past.conf';
test_write($settingsPath, "GLOBAL_AUTORESPONDER_TIMEZONE=Europe/Warsaw\n");
$settings = Settings::load($settingsPath);
$rule = RuleValidator::validate([
'subject_prefix' => 'Urlop',
'reply_frequency' => '1440',
'body' => 'Body',
'start_value' => '2000-01-10T09:00',
'end_value' => '2000-01-11T17:00',
], $settings, true);
assert_same('2000-01-10T09:00', $rule['start_value']);
assert_same('2000-01-11T17:00', $rule['end_value']);
assert_true($rule['start_ts'] < $rule['end_ts']);
});