From 30cd783dc32185d6cde138d87167709f8bfeff62 Mon Sep 17 00:00:00 2001 From: Alymosul Date: Fri, 7 Sep 2018 14:35:47 +0200 Subject: [PATCH 1/3] Check if the comparison rules have an attribute or a value before replacing the place holders. --- .../Concerns/ReplacesAttributes.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index 6ebdfddd7f20..42f8a31f94fe 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -259,7 +259,11 @@ protected function replaceSize($message, $attribute, $rule, $parameters) */ protected function replaceGt($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($parameters[0], $value), $message); } /** @@ -273,7 +277,11 @@ protected function replaceGt($message, $attribute, $rule, $parameters) */ protected function replaceLt($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($parameters[0], $value), $message); } /** @@ -287,7 +295,11 @@ protected function replaceLt($message, $attribute, $rule, $parameters) */ protected function replaceGte($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($parameters[0], $value), $message); } /** @@ -301,7 +313,11 @@ protected function replaceGte($message, $attribute, $rule, $parameters) */ protected function replaceLte($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($parameters[0], $value), $message); } /** From 4665d323ad4d71d1378e9b51cf572c9547302d95 Mon Sep 17 00:00:00 2001 From: Alymosul Date: Fri, 7 Sep 2018 17:05:18 +0200 Subject: [PATCH 2/3] Force the replacer to respect the numeric rule of the current input and apply it on the other input we're comparing against. --- .../Concerns/ReplacesAttributes.php | 8 +- tests/Validation/ValidationValidatorTest.php | 148 ++++++++++++++++++ 2 files changed, 152 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index 42f8a31f94fe..3594dd9b3540 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -263,7 +263,7 @@ protected function replaceGt($message, $attribute, $rule, $parameters) return str_replace(':value', $parameters[0], $message); } - return str_replace(':value', $this->getSize($parameters[0], $value), $message); + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** @@ -281,7 +281,7 @@ protected function replaceLt($message, $attribute, $rule, $parameters) return str_replace(':value', $parameters[0], $message); } - return str_replace(':value', $this->getSize($parameters[0], $value), $message); + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** @@ -299,7 +299,7 @@ protected function replaceGte($message, $attribute, $rule, $parameters) return str_replace(':value', $parameters[0], $message); } - return str_replace(':value', $this->getSize($parameters[0], $value), $message); + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** @@ -317,7 +317,7 @@ protected function replaceLte($message, $attribute, $rule, $parameters) return str_replace(':value', $parameters[0], $message); } - return str_replace(':value', $this->getSize($parameters[0], $value), $message); + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 7abf86bed271..0c2add00f8b9 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1533,6 +1533,154 @@ public function testProperMessagesAreReturnedForSizes() $this->assertEquals('file', $v->messages()->first('photo')); } + public function testValidateGtPlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.gt.numeric' => ':value', + 'validation.gt.string' => ':value', + 'validation.gt.file' => ':value', + 'validation.gt.array' => ':value' + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'gt:4']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gt:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gt:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); + $file->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $biggerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $biggerFile->expects($this->any())->method('getSize')->will($this->returnValue(5120)); + $biggerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gt:bigger']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1,2,3], 'more' => [0,1,2,3]], ['items' => 'gt:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + } + + public function testValidateLtPlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.lt.numeric' => ':value', + 'validation.lt.string' => ':value', + 'validation.lt.file' => ':value', + 'validation.lt.array' => ':value' + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'lt:2']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lt:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lt:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); + $file->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $smallerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $smallerFile->expects($this->any())->method('getSize')->will($this->returnValue(2048)); + $smallerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lt:smaller']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1,2,3], 'less' => [0,1]], ['items' => 'lt:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + } + + public function testValidateGtePlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.gte.numeric' => ':value', + 'validation.gte.string' => ':value', + 'validation.gte.file' => ':value', + 'validation.gte.array' => ':value' + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'gte:4']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gte:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gte:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); + $file->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $biggerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $biggerFile->expects($this->any())->method('getSize')->will($this->returnValue(5120)); + $biggerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gte:bigger']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1,2,3], 'more' => [0,1,2,3]], ['items' => 'gte:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + } + + public function testValidateLtePlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.lte.numeric' => ':value', + 'validation.lte.string' => ':value', + 'validation.lte.file' => ':value', + 'validation.lte.array' => ':value' + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'lte:2']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lte:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lte:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); + $file->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $smallerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $smallerFile->expects($this->any())->method('getSize')->will($this->returnValue(2048)); + $smallerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lte:smaller']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1,2,3], 'less' => [0,1]], ['items' => 'lte:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + } + public function testValidateIn() { $trans = $this->getIlluminateArrayTranslator(); From 1ac97b810cc502c71d7f6ae9d08a7f0b010c9ed3 Mon Sep 17 00:00:00 2001 From: Alymosul Date: Fri, 7 Sep 2018 17:13:09 +0200 Subject: [PATCH 3/3] Apply StyleCI fixes. --- tests/Validation/ValidationValidatorTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 0c2add00f8b9..c3189ca83e78 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1540,7 +1540,7 @@ public function testValidateGtPlaceHolderIsReplacedProperly() 'validation.gt.numeric' => ':value', 'validation.gt.string' => ':value', 'validation.gt.file' => ':value', - 'validation.gt.array' => ':value' + 'validation.gt.array' => ':value', ], 'en'); $v = new Validator($trans, ['items' => '3'], ['items' => 'gt:4']); @@ -1565,7 +1565,7 @@ public function testValidateGtPlaceHolderIsReplacedProperly() $this->assertFalse($v->passes()); $this->assertEquals(5, $v->messages()->first('photo')); - $v = new Validator($trans, ['items' => [1,2,3], 'more' => [0,1,2,3]], ['items' => 'gt:more']); + $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gt:more']); $this->assertFalse($v->passes()); $this->assertEquals(4, $v->messages()->first('items')); } @@ -1577,7 +1577,7 @@ public function testValidateLtPlaceHolderIsReplacedProperly() 'validation.lt.numeric' => ':value', 'validation.lt.string' => ':value', 'validation.lt.file' => ':value', - 'validation.lt.array' => ':value' + 'validation.lt.array' => ':value', ], 'en'); $v = new Validator($trans, ['items' => '3'], ['items' => 'lt:2']); @@ -1602,7 +1602,7 @@ public function testValidateLtPlaceHolderIsReplacedProperly() $this->assertFalse($v->passes()); $this->assertEquals(2, $v->messages()->first('photo')); - $v = new Validator($trans, ['items' => [1,2,3], 'less' => [0,1]], ['items' => 'lt:less']); + $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lt:less']); $this->assertFalse($v->passes()); $this->assertEquals(2, $v->messages()->first('items')); } @@ -1614,7 +1614,7 @@ public function testValidateGtePlaceHolderIsReplacedProperly() 'validation.gte.numeric' => ':value', 'validation.gte.string' => ':value', 'validation.gte.file' => ':value', - 'validation.gte.array' => ':value' + 'validation.gte.array' => ':value', ], 'en'); $v = new Validator($trans, ['items' => '3'], ['items' => 'gte:4']); @@ -1639,7 +1639,7 @@ public function testValidateGtePlaceHolderIsReplacedProperly() $this->assertFalse($v->passes()); $this->assertEquals(5, $v->messages()->first('photo')); - $v = new Validator($trans, ['items' => [1,2,3], 'more' => [0,1,2,3]], ['items' => 'gte:more']); + $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gte:more']); $this->assertFalse($v->passes()); $this->assertEquals(4, $v->messages()->first('items')); } @@ -1651,7 +1651,7 @@ public function testValidateLtePlaceHolderIsReplacedProperly() 'validation.lte.numeric' => ':value', 'validation.lte.string' => ':value', 'validation.lte.file' => ':value', - 'validation.lte.array' => ':value' + 'validation.lte.array' => ':value', ], 'en'); $v = new Validator($trans, ['items' => '3'], ['items' => 'lte:2']); @@ -1676,7 +1676,7 @@ public function testValidateLtePlaceHolderIsReplacedProperly() $this->assertFalse($v->passes()); $this->assertEquals(2, $v->messages()->first('photo')); - $v = new Validator($trans, ['items' => [1,2,3], 'less' => [0,1]], ['items' => 'lte:less']); + $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lte:less']); $this->assertFalse($v->passes()); $this->assertEquals(2, $v->messages()->first('items')); }