Skip to content

Commit bd9924e

Browse files
Merge branch '2.7' into 2.8
* 2.7: move provider after test update dataProvider function name cast substr result to string and remove empty function use rename dataset provider Add a test to prevent future regressions Switch to `empty` native function to check emptiness remove non relevant test case Switch to `is_string` native method Remove unnecessary parentheses Add a test case to prevent future regressions Move empty condition in return statement Use LF line separator fix coding standard to comply with fabbot Remove malformed EmailValidatorTest + Update UrlValidator test Add empty check on host in other methods + add unit tests [Validator] Allow checkMX() to return false when $host is empty [DI] Fix the xml schema [Translation] avoid creating cache files for fallback locales.
2 parents b1e28d0 + ecb88dd commit bd9924e

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Constraints/EmailValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function validate($value, Constraint $constraint)
9393
return;
9494
}
9595

96-
$host = substr($value, strrpos($value, '@') + 1);
96+
$host = (string) substr($value, strrpos($value, '@') + 1);
9797

9898
// Check for host DNS resource records
9999
if ($constraint->checkMX) {
@@ -138,7 +138,7 @@ public function validate($value, Constraint $constraint)
138138
*/
139139
private function checkMX($host)
140140
{
141-
return checkdnsrr($host, 'MX');
141+
return '' !== $host && checkdnsrr($host, 'MX');
142142
}
143143

144144
/**
@@ -150,6 +150,6 @@ private function checkMX($host)
150150
*/
151151
private function checkHost($host)
152152
{
153-
return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
153+
return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')));
154154
}
155155
}

Constraints/UrlValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function validate($value, Constraint $constraint)
8282
if ($constraint->checkDNS) {
8383
$host = parse_url($value, PHP_URL_HOST);
8484

85-
if (!checkdnsrr($host, 'ANY')) {
85+
if (!is_string($host) || !checkdnsrr($host, 'ANY')) {
8686
if ($this->context instanceof ExecutionContextInterface) {
8787
$this->context->buildViolation($constraint->dnsMessage)
8888
->setParameter('{{ value }}', $this->formatValue($host))

Tests/Constraints/EmailValidatorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,32 @@ public function testHostnameIsProperlyParsed()
159159

160160
$this->assertNoViolation();
161161
}
162+
163+
/**
164+
* @dataProvider provideCheckTypes
165+
*/
166+
public function testEmptyHostIsNotValid($checkType, $violation)
167+
{
168+
$this->validator->validate(
169+
170+
new Email(array(
171+
'message' => 'myMessage',
172+
$checkType => true,
173+
))
174+
);
175+
176+
$this
177+
->buildViolation('myMessage')
178+
->setParameter('{{ value }}', '"[email protected]@"')
179+
->setCode($violation)
180+
->assertRaised();
181+
}
182+
183+
public function provideCheckTypes()
184+
{
185+
return array(
186+
array('checkMX', Email::MX_CHECK_FAILED_ERROR),
187+
array('checkHost', Email::HOST_CHECK_FAILED_ERROR),
188+
);
189+
}
162190
}

Tests/Constraints/UrlValidatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public function getInvalidUrls()
172172
array('http://example.com/exploit.html?<script>alert(1);</script>'),
173173
array('http://example.com/exploit.html?hel lo'),
174174
array('http://example.com/exploit.html?not_a%hex'),
175+
array('http://'),
175176
);
176177
}
177178

0 commit comments

Comments
 (0)