Files
global-autoresponder/tests/handler_test.php
T
2026-06-02 21:28:31 +02:00

178 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
test('mockup and planned UI do not expose backend terms', function (): void {
foreach (glob(dirname(__DIR__, 2) . '/docs/mockups/*.html') ?: [] as $file) {
$html = file_get_contents($file) ?: '';
assert_false(stripos($html, 'Exim') !== false, basename($file) . ' exposes Exim');
assert_false(stripos($html, 'DirectAdmin API') !== false, basename($file) . ' exposes API');
assert_false(stripos($html, 'backend.sh') !== false, basename($file) . ' exposes backend script');
}
});
test('mockup forms include calendar classes and delete confirmation view', function (): void {
$create = file_get_contents(dirname(__DIR__, 2) . '/docs/mockups/02-dodaj.html') ?: '';
$delete = file_get_contents(dirname(__DIR__, 2) . '/docs/mockups/04-podglad-usun.html') ?: '';
assert_contains('br-restore-calendar', $create);
assert_contains('br-calendar-grid', $create);
assert_contains('Potwierdź usunięcie', $delete);
assert_false(stripos($delete, 'checkbox') !== false);
});
test('directadmin user hooks are clickable and point to reserved icon path', function (): void {
$img = file_get_contents(PLUGIN_ROOT . '/hooks/user_img.html') ?: '';
$txt = file_get_contents(PLUGIN_ROOT . '/hooks/user_txt.html') ?: '';
assert_contains('<a href="/CMD_PLUGINS/global-autoresponder/index.html"', $img);
assert_contains('<img src="/CMD_PLUGINS/global-autoresponder/images/user_icon.svg"', $img);
assert_contains('<a href="/CMD_PLUGINS/global-autoresponder/index.html"', $txt);
assert_contains('Globalne autorespondery', $txt);
});
test('rule form uses minute time input and no summary box in exim mode', function (): void {
require_once PLUGIN_ROOT . '/exec/lib/Settings.php';
require_once PLUGIN_ROOT . '/exec/lib/Lang.php';
require_once PLUGIN_ROOT . '/exec/lib/DirectAdminUser.php';
require_once PLUGIN_ROOT . '/exec/lib/CsrfGuard.php';
require_once PLUGIN_ROOT . '/exec/lib/RuleRepository.php';
require_once PLUGIN_ROOT . '/exec/lib/AuditLog.php';
require_once PLUGIN_ROOT . '/exec/lib/AppContext.php';
require_once PLUGIN_ROOT . '/exec/handlers/_form_helpers.php';
$settingsPath = TEST_TMP . '/form.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=pl\nGLOBAL_AUTORESPONDER_BACKEND=exim\n");
$settings = Settings::load($settingsPath);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'pl', TEST_TMP . '/config'),
$settings,
new RuleRepository(TEST_TMP . '/data'),
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('pl', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$html = render_rule_form($ctx, 'create', null);
assert_contains('type="time"', $html);
assert_contains('step="60"', $html);
assert_false(strpos($html, 'summary-card') !== false, 'Summary box should not be rendered in create/edit form');
assert_contains('Przedrostek tematu', $html);
assert_contains(': pierwotny temat', $html);
assert_contains('Częstość odpowiedzi', $html);
assert_contains('Wysyłaj automatyczną odpowiedź dla każdej wiadomości', $html);
assert_contains('1 dzień', $html);
assert_contains('Treść wiadomości', $html);
});
test('rule form uses native period selector in da mode', function (): void {
$settingsPath = TEST_TMP . '/form-da.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=pl\nGLOBAL_AUTORESPONDER_BACKEND=da\n");
$settings = Settings::load($settingsPath);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'pl', TEST_TMP . '/config'),
$settings,
new RuleRepository(TEST_TMP . '/data'),
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('pl', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$html = render_rule_form($ctx, 'create', null);
assert_contains('name="start_value_period"', $html);
assert_contains('Rano', $html);
assert_false(strpos($html, 'type="time"') !== false);
});
test('mutating handlers synchronize active backend after storage changes', function (): void {
foreach (['create.php', 'update.php', 'delete.php', 'toggle.php'] as $handler) {
$source = file_get_contents(PLUGIN_ROOT . '/exec/handlers/' . $handler) ?: '';
assert_contains('syncActiveBackend', $source, $handler . ' must synchronize backend after mutation');
}
});
test('calendar picker is based on selected rule month and supports month navigation', function (): void {
$settingsPath = TEST_TMP . '/form-calendar.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=pl\nGLOBAL_AUTORESPONDER_BACKEND=exim\n");
$settings = Settings::load($settingsPath);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'pl', TEST_TMP . '/config'),
$settings,
new RuleRepository(TEST_TMP . '/data'),
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('pl', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$html = render_rule_form($ctx, 'update', [
'id' => 'abc',
'subject_prefix' => 'Urlop',
'reply_every_message' => false,
'repeat_minutes' => 1440,
'body' => 'Body',
'enabled' => true,
'start_value' => '2027-02-14T20:00',
'end_value' => '2027-03-01T09:30',
]);
assert_contains('data-calendar-picker', $html);
assert_contains('data-calendar-prev', $html);
assert_contains('data-calendar-next', $html);
assert_contains('Luty 2027', $html);
assert_false(strpos($html, 'Czerwiec 2026') !== false);
});
test('rule form localizes calendar and preview labels without translating user content globally', function (): void {
$settingsPath = TEST_TMP . '/form-i18n.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=en\nGLOBAL_AUTORESPONDER_BACKEND=exim\n");
$settings = Settings::load($settingsPath);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'en', TEST_TMP . '/config'),
$settings,
new RuleRepository(TEST_TMP . '/data'),
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('en', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$html = render_rule_form($ctx, 'create', null);
assert_contains('<th>Mon</th>', $html);
assert_contains('Selected:', $html);
assert_false(strpos($html, 'Wybrano') !== false);
assert_false(strpos($html, '<th>Pn</th>') !== false);
$preview = render_preview_box($ctx, ['subject_prefix' => 'Delete', 'body' => 'Cancel']);
assert_contains('<strong>Subject prefix:</strong> Delete', $preview);
assert_contains('Cancel', $preview);
});
test('directadmin backend allows only one enabled global rule per account', function (): void {
$settingsPath = TEST_TMP . '/form-da-single.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=pl\nGLOBAL_AUTORESPONDER_BACKEND=da\n");
$settings = Settings::load($settingsPath);
$repo = new RuleRepository(TEST_TMP . '/single-data');
$repo->create('alice', [
'subject_prefix' => 'Pierwsza',
'reply_every_message' => false,
'repeat_minutes' => 1440,
'body' => 'Body',
'enabled' => true,
'start_value' => '2026-06-10|morning',
'end_value' => '2026-06-24|evening',
]);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'pl', TEST_TMP . '/config'),
$settings,
$repo,
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('pl', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$thrown = false;
try {
enforce_rule_backend_constraints($ctx, null, ['enabled' => true]);
} catch (RuntimeException $e) {
$thrown = true;
assert_contains('Backend DirectAdmin obsługuje tylko jedną aktywną regułę globalną.', $e->getMessage());
}
assert_true($thrown);
});