Skip to content

Commit

Permalink
[9.x] Mock dns check during tests (#43781)
Browse files Browse the repository at this point in the history
* Mock dns check during tests

* Update ValidatesAttributes.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
TheLevti and taylorotwell authored Aug 19, 2022
1 parent 76e427a commit ca1b6c4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
18 changes: 17 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ public function validateActiveUrl($attribute, $value)

if ($url = parse_url($value, PHP_URL_HOST)) {
try {
return count(dns_get_record($url.'.', DNS_A | DNS_AAAA)) > 0;
$records = $this->getDnsRecords($url.'.', DNS_A | DNS_AAAA);

if (is_array($records) && count($records) > 0) {
return true;
}
} catch (Exception $e) {
return false;
}
Expand All @@ -126,6 +130,18 @@ public function validateActiveUrl($attribute, $value)
return false;
}

/**
* Get the DNS records for the given hostname.
*
* @param string $hostname
* @param int $type
* @return array|false
*/
protected function getDnsRecords($hostname, $type)
{
return dns_get_record($hostname, $type);
}

/**
* "Break" on first validation fail.
*
Expand Down
60 changes: 44 additions & 16 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Illuminate\Validation\Validator;
use InvalidArgumentException;
use Mockery as m;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
Expand Down Expand Up @@ -3533,23 +3534,50 @@ public function invalidUrls()
];
}

public function testValidateActiveUrl()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'active_url']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => ['fdsfs', 'fdsfds']], ['x' => 'active_url']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'active_url']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => 'http://www.google.com'], ['x' => 'active_url']);
$this->assertTrue($v->passes());
/**
* @dataProvider activeUrlDataProvider
*/
public function testValidateActiveUrl($data, $outcome)
{
$trans = $this->getIlluminateArrayTranslator();
$v = m::mock(
new Validator($trans, $data, ['x' => 'active_url']),
function (MockInterface $mock) {
$mock
->shouldAllowMockingProtectedMethods()
->shouldReceive('getDnsRecords')
->withAnyArgs()
->zeroOrMoreTimes()
->andReturn(['hit']);
}
);
$this->assertEquals($outcome, $v->passes());
}

$v = new Validator($trans, ['x' => 'http://www.google.com/about'], ['x' => 'active_url']);
$this->assertTrue($v->passes());
public function activeUrlDataProvider()
{
return [
'Invalid Url' => [
['x' => 'aslsdlks'],
false,
],
'Invalid Urls' => [
['x' => 'fdsfs', 'fdsfds'],
false,
],
'Google Without Subdomain' => [
['x' => 'http://google.com'],
true,
],
'Google With Subdomain' => [
['x' => 'http://www.google.com'],
true,
],
'Google With Subdomain About Page' => [
['x' => 'http://www.google.com/about'],
true,
],
];
}

public function testValidateImage()
Expand Down

0 comments on commit ca1b6c4

Please sign in to comment.