From 4b29c0514ee3764f679a87968452b81818b04131 Mon Sep 17 00:00:00 2001 From: Dave Earley Date: Mon, 22 Jun 2020 00:05:00 +0100 Subject: [PATCH] Check for DNS A/AAA records (closes #22) --- src/EmailAddress.php | 4 ++-- src/Validations/MxRecordsValidator.php | 14 +++++++++++--- tests/Validations/MxRecordsValidatorTest.php | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/EmailAddress.php b/src/EmailAddress.php index 86f4880..81525ee 100644 --- a/src/EmailAddress.php +++ b/src/EmailAddress.php @@ -35,10 +35,10 @@ private function getEmailPart(int $partNumber): string return (explode('@', $this->emailAddress))[$partNumber]; } - public function getHostPart(): ?string + public function getHostPart(bool $returnFqdn = false): ?string { if ($this->isValidEmailAddressFormat()) { - return $this->getEmailPart(self::EMAIL_HOST_PART); + return $this->getEmailPart(self::EMAIL_HOST_PART) . ($returnFqdn ? '.' : ''); } return null; diff --git a/src/Validations/MxRecordsValidator.php b/src/Validations/MxRecordsValidator.php index d5e7812..704652a 100644 --- a/src/Validations/MxRecordsValidator.php +++ b/src/Validations/MxRecordsValidator.php @@ -6,6 +6,8 @@ class MxRecordsValidator extends Validator implements ValidatorInterface { + private const VALID_DNS_RECORDS = ['MX', 'A', 'AAAA', 'NS']; + public function getValidatorName(): string { return 'valid_mx_records'; // @codeCoverageIgnore @@ -13,11 +15,17 @@ public function getValidatorName(): string public function getResultResponse(): bool { - if ($this->getEmailAddress()->isValidEmailAddressFormat()) { - return $this->checkDns($this->getEmailAddress()->getHostPart(), 'MX'); + if (!$this->getEmailAddress()->isValidEmailAddressFormat()) { + return false; // @codeCoverageIgnore + } + + $results = []; + foreach (self::VALID_DNS_RECORDS as $dns) { + $results[] = $this->checkDns($this->getEmailAddress()->getHostPart(true), $dns); } - return false; // @codeCoverageIgnore + // To be considered valid we needs an NS record and at least one MX, A or AAA record + return count(array_filter($results)) >= 2; } protected function checkDns(string $host, string $type = null): bool diff --git a/tests/Validations/MxRecordsValidatorTest.php b/tests/Validations/MxRecordsValidatorTest.php index 7cb45ca..3f916f0 100644 --- a/tests/Validations/MxRecordsValidatorTest.php +++ b/tests/Validations/MxRecordsValidatorTest.php @@ -16,7 +16,12 @@ class MxRecordsValidatorTest extends TestCase public function testMxRecordIsChecked(): void { - $this->mxValidator->shouldReceive('checkDns')->with('dave@gmail.com', 'MX'); + foreach (['MX', 'AAAA', 'NS', 'A'] as $dns) { + $this->mxValidator + ->shouldReceive('checkDns') + ->with('gmail.com.', $dns); + } + $this->mxValidator->getResultResponse(); } @@ -26,6 +31,11 @@ protected function setUp(): void new EmailAddress('dave@gmail.com'), ]) ->shouldAllowMockingProtectedMethods() - ->makePartial(); + ->shouldReceive('getResultResponse') + ->passthru() + ->getMock() + ->shouldReceive('getEmailAddress') + ->passthru() + ->getMock(); } } \ No newline at end of file