Skip to content

Commit 1d85016

Browse files
Expose isDomainBlocked (#583)
* Expose isDomainBlocked * Add test * Fix dataprovider
1 parent 248fb5d commit 1d85016

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

platform/php/MailChecker.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public static function isBlacklisted(string $email): bool
4141
$parts = explode('@', $email);
4242
$domain = end($parts);
4343

44-
foreach (self::allDomainSuffixes($domain) as $domainSuffix) {
44+
return self::isDomainBlocked($domain, true);
45+
}
46+
47+
public static function isDomainBlocked(string $domain, bool $checkSubdomain): bool
48+
{
49+
$domainSuffixes = $checkSubdomain ? self::allDomainSuffixes($domain) : [$domain];
50+
foreach ($domainSuffixes as $domainSuffix) {
4551
if (isset(self::$blocklist[$domainSuffix])) {
4652
return true;
4753
}

test/platform.php.test.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ public function testReturnFalseIfThrowableDomain()
6464

6565
public static function provideBlackListTests()
6666
{
67-
foreach (array_rand(MailChecker::blacklist(), 1000) as $blacklistedDomain) {
68-
yield [$blacklistedDomain];
67+
$blacklist = MailChecker::blacklist();
68+
foreach (array_rand($blacklist, 1000) as $key) {
69+
yield [$blacklist[$key]];
6970
}
7071
}
7172

@@ -90,4 +91,13 @@ public function testAddCustomDomains()
9091
$this->isInvalid('foo@google.com');
9192
$this->isValid('ok@gmail.com');
9293
}
94+
95+
#[DataProvider('provideBlackListTests')]
96+
public function testIsDomainBlocked($blacklistedDomain)
97+
{
98+
$this->assertEquals(true, MailChecker::isDomainBlocked($blacklistedDomain, true));
99+
$this->assertEquals(true, MailChecker::isDomainBlocked('subdomain.'.$blacklistedDomain, true));
100+
$this->assertEquals(true, MailChecker::isDomainBlocked($blacklistedDomain, false));
101+
$this->assertEquals(false, MailChecker::isDomainBlocked('subdomain.'.$blacklistedDomain, false));
102+
}
93103
}

0 commit comments

Comments
 (0)