Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Use getClientOriginalExtension() when available #20400

Merged
merged 2 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@ protected function shouldBlockPhpUpload($value, $parameters)
return false;
}

return strtolower($value->getExtension()) === 'php';
return ($value instanceof UploadedFile)
? strtolower($value->getClientOriginalExtension()) === 'php'
: strtolower($value->getExtension()) === 'php';
}

/**
Expand Down
44 changes: 22 additions & 22 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1950,39 +1950,39 @@ public function testValidateImage()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('php'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('php'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file], ['x' => 'Image']);
$this->assertFalse($v->passes());

$file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2->expects($this->any())->method('guessExtension')->will($this->returnValue('jpeg'));
$file2->expects($this->any())->method('getExtension')->will($this->returnValue('jpeg'));
$file2->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('jpeg'));
$v = new Validator($trans, ['x' => $file2], ['x' => 'Image']);
$this->assertTrue($v->passes());

$file3 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file3 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file3->expects($this->any())->method('guessExtension')->will($this->returnValue('gif'));
$file3->expects($this->any())->method('getExtension')->will($this->returnValue('gif'));
$file3->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('gif'));
$v = new Validator($trans, ['x' => $file3], ['x' => 'Image']);
$this->assertTrue($v->passes());

$file4 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file4 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file4->expects($this->any())->method('guessExtension')->will($this->returnValue('bmp'));
$file4->expects($this->any())->method('getExtension')->will($this->returnValue('bmp'));
$file4->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('bmp'));
$v = new Validator($trans, ['x' => $file4], ['x' => 'Image']);
$this->assertTrue($v->passes());

$file5 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file5 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file5->expects($this->any())->method('guessExtension')->will($this->returnValue('png'));
$file5->expects($this->any())->method('getExtension')->will($this->returnValue('png'));
$file5->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('png'));
$v = new Validator($trans, ['x' => $file5], ['x' => 'Image']);
$this->assertTrue($v->passes());

$file6 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file6 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file6->expects($this->any())->method('guessExtension')->will($this->returnValue('svg'));
$file6->expects($this->any())->method('getExtension')->will($this->returnValue('svg'));
$file6->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('svg'));
$v = new Validator($trans, ['x' => $file6], ['x' => 'Image']);
$this->assertTrue($v->passes());
}
Expand All @@ -1992,9 +1992,9 @@ public function testValidateImageDoesNotAllowPhpExtensionsOnImageMime()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('jpeg'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('php'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file], ['x' => 'Image']);
$this->assertFalse($v->passes());
}
Expand Down Expand Up @@ -2085,9 +2085,9 @@ public function testValidatePhpMimetypes()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('rtf'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('rtf'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('rtf'));

$v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/*']);
$this->assertTrue($v->passes());
Expand All @@ -2098,9 +2098,9 @@ public function testValidateMime()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('pdf'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('pdf'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('pdf'));
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
$this->assertTrue($v->passes());

Expand All @@ -2116,15 +2116,15 @@ public function testValidateMimeEnforcesPhpCheck()
$trans = $this->getIlluminateArrayTranslator();
$uploadedFile = [__FILE__, '', null, null, null, true];

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->will($this->returnValue('pdf'));
$file->expects($this->any())->method('getExtension')->will($this->returnValue('php'));
$file->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
$this->assertFalse($v->passes());

$file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file2->expects($this->any())->method('guessExtension')->will($this->returnValue('php'));
$file2->expects($this->any())->method('getExtension')->will($this->returnValue('php'));
$file2->expects($this->any())->method('getClientOriginalExtension')->will($this->returnValue('php'));
$v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:pdf,php']);
$this->assertTrue($v->passes());
}
Expand Down