You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/RegistrationEmailWhitelistT...

55 lines
2.3 KiB
PHTML

<?php
require dirname(__DIR__) . '/app/Services/RegistrationEmailWhitelist.php';
use App\Services\RegistrationEmailWhitelist;
function expectEmailWhitelist($condition, $message)
{
if (!$condition) {
fwrite(STDERR, $message . PHP_EOL);
exit(1);
}
}
expectEmailWhitelist(RegistrationEmailWhitelist::normalizeEnabled(null) === '1', 'Missing setting should preserve the enabled default.');
expectEmailWhitelist(RegistrationEmailWhitelist::validateEnabled('0') === '0', 'The whitelist should be disableable.');
expectEmailWhitelist(RegistrationEmailWhitelist::validateEnabled(1) === '1', 'The whitelist should be enableable.');
$domains = RegistrationEmailWhitelist::validateDomains("@QQ.com, gmail.com;\n@qq.com\t@163.COM");
expectEmailWhitelist($domains === '@qq.com,@gmail.com,@163.com', 'Domain list normalization failed.');
expectEmailWhitelist(RegistrationEmailWhitelist::allows('Member@QQ.COM', explode(',', $domains)), 'Email-domain matching must be case-insensitive.');
expectEmailWhitelist(!RegistrationEmailWhitelist::allows('member@notqq.com', explode(',', $domains)), 'Only exact configured domains are allowed.');
expectEmailWhitelist(!RegistrationEmailWhitelist::allows('not-an-email', explode(',', $domains)), 'Malformed addresses must not match.');
foreach (['2', true, null, 'true'] as $invalid) {
try {
RegistrationEmailWhitelist::validateEnabled($invalid);
expectEmailWhitelist(false, 'Invalid enabled setting accepted.');
} catch (InvalidArgumentException $exception) {
// Expected.
}
}
$oversized = implode(',', array_map(static function ($index) {
return '@domain' . $index . '.example.com';
}, range(1, 40)));
foreach (['*@example.com', 'foo@@example.com', 'https://example.com', 'foo@example.com', $oversized] as $invalid) {
try {
RegistrationEmailWhitelist::validateDomains($invalid);
expectEmailWhitelist(false, 'Invalid or oversized domain list accepted.');
} catch (InvalidArgumentException $exception) {
// Expected.
}
}
try {
RegistrationEmailWhitelist::assertValidConfiguration('1', '');
expectEmailWhitelist(false, 'An active empty whitelist was accepted.');
} catch (InvalidArgumentException $exception) {
// Expected.
}
RegistrationEmailWhitelist::assertValidConfiguration('0', '');
echo 'Registration email whitelist checks passed.' . PHP_EOL;