From 8ee06b86c971a04e07f5bd57509eacd82c4c1329 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 25 Aug 2020 12:04:25 +0200 Subject: [PATCH 1/2] Fix dimension ratio calculation --- .../Validation/Concerns/ValidatesAttributes.php | 2 +- tests/Validation/ValidationValidatorTest.php | 12 ++++++++++-- tests/Validation/fixtures/image4.png | Bin 0 -> 1768 bytes 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 tests/Validation/fixtures/image4.png diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 5f621a7bccc4..bc340ddebf72 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -558,7 +558,7 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - $precision = 1 / max($width, $height); + $precision = 1 / (max($width, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index c6878b17890e..2eb883f427b1 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2798,7 +2798,7 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => $svgXmlUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']); $this->assertTrue($v->passes()); - $svgXmlFile = new File(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, null, true); + $svgXmlFile = new UploadedFile(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, null, true); $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => $svgXmlFile], ['x' => 'dimensions:max_width=1,max_height=1']); @@ -2811,11 +2811,19 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => $svgUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']); $this->assertTrue($v->passes()); - $svgFile = new File(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, null, true); + $svgFile = new UploadedFile(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, null, true); $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => $svgFile], ['x' => 'dimensions:max_width=1,max_height=1']); $this->assertTrue($v->passes()); + + // Knowing that demo image4.png has width = 64 and height = 65 + $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image4.png', '', null, null, true); + $trans = $this->getIlluminateArrayTranslator(); + + // Ensure validation doesn't erroneously fail when ratio has no fractional part + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']); + $this->assertFalse($v->passes()); } /** diff --git a/tests/Validation/fixtures/image4.png b/tests/Validation/fixtures/image4.png new file mode 100644 index 0000000000000000000000000000000000000000..e7b5e4665fe6ff40409d53f6164ba60a4e85f465 GIT binary patch literal 1768 zcmeAS@N?(olHy`uVBq!ia0vp^4nXY4!3HD~O&3)&FtA8wIy(nsJG(pQ=j0a!7bGU9 zx~Jx)7G)*}xjQj1SoHRW#%9QbirDQgPFGer7o20#TC&2PSCw~in90H|yXx=WHOrgk zX?9ELMas8#=VqKdXJNei??QjqfEvSBvqg^V-`=30@se^W69Is^9HQf6w}2=cm% zRlg?6+rNsE`7wKui7_%$=a=5ignLh%s%Tp((O2J z{`^$%!5gbrO+Gs3VA+xFrcs|hS+gAI+!a0R3xxw>3J%9Sjbk^zWN|!&s_EdU& zt1X&u`SxYai_b07blLwboj#>m^hk3@{X#d#Z_gk7+jz%IfLSt2>ZxbF-lWBR(fW^L zbC^iBIu){+PHPr^TLFFGD|X7?ta!*6mT zipxW#a@ih>eYiODXy;Mc&xIeKg!8-AO*=XB`T8%hHj(*yd~R!|v;NBZ87Ar1>$2uc z^OyE5I(yY?HU1vkd$#se?b+IMwFhIFCR(4K`*`0&y#o2-_+#I)K1)U2uNJG6*q;7! zTJ{<*Mdch(IkBQcW)A}sjXtUKa3nhg-e{b*_VsVE`TFy0W%-ijpDq6#vrjLKvGeyv zrZ2w#LPJAd7EIBU4a@hn*ykwRqvD*>GsP_A`;PF>3zeq&KMl$h2(}IuUb*p#BY*qm zWn~L;nSTF~y}B}QQ_!nR$@glRA}?Fzz2-eDTJC*ma+1_~Gp%W}UcKm8A<=fir!OP8 z{Z?+2H0%3&U;4kyHrmZE9IfX*=Qq!RefA&U{MLKaw}oMvOhe;w!D; zHKEKg-0y*9MMY|aXPU1sgBFm@!NAHW#J~(>F#;h2qZ9)xn9ac8#V8GD2Qg|u)i5zI zv}ZD~K-EM6X%O%LVxVp)&9s0CuEJpfGo0-RQhNMw-$o$CS>O>_%)r1c48n{Iv*t(u zO^D14i71Ki^|4CM&(%vz$xlkvtH>0+>{umUo3Q%e#RDspr3imfVamB1>jfNYSk zzLEl1NlCV?QiN}Sf^&XRs)DJWiJpOy9hZWFf=y9MnpKdC8&o@xXRDM^Qc_^0uU}qX zu2*iXmtT~wZ)j<0sc&GUZ)BtkRH0j3nOBlnp_^B%3^4>|j!SBBa#3bMNoIbY0?6FN zr2NtnTO}osMQ{LdXGvxn!lt}psJDO~)CbAv8|oS8!_5Y2wE>A*`4?rT0&NDFZ)a!& zR*518wZ}#uWI2*!AU*|)0=;U-Wup%dHajk#L+X*hknHhvaSW+oe0!BqkU@cmd1GV! z`2&fMo14E%?K)R2TxVVL+dh^-O2D6?v4YW!*+gL;1Jg&QA1s0n5LN^ui$I5ZgTo$% zFa}1JN1O@*`VFEEK*dZPN4OJM1&CMXFz*2C^)D=S0`)Q(peXiq^>bP0l+XkKZ#c#s literal 0 HcmV?d00001 From c58d3eea326bd06587b95c30eaf8ad2bd4fb03a8 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 25 Aug 2020 12:06:49 +0200 Subject: [PATCH 2/2] Update comment --- tests/Validation/ValidationValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 2eb883f427b1..d9a02a78389e 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2821,7 +2821,7 @@ public function testValidateImageDimensions() $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image4.png', '', null, null, true); $trans = $this->getIlluminateArrayTranslator(); - // Ensure validation doesn't erroneously fail when ratio has no fractional part + // Ensure validation doesn't erroneously fail when ratio doesn't matches $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']); $this->assertFalse($v->passes()); }