Skip to content

Commit

Permalink
Merge pull request #25513 from Alymosul/5.6
Browse files Browse the repository at this point in the history
[5.7] Replace all placeholders for comparison rules (gt/gte/lt/lte) properly
  • Loading branch information
themsaid authored Sep 10, 2018
2 parents b2fa4e7 + 1ac97b8 commit adc49ac
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($attribute, $value), $message);
}

/**
Expand All @@ -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($attribute, $value), $message);
}

/**
Expand All @@ -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($attribute, $value), $message);
}

/**
Expand All @@ -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($attribute, $value), $message);
}

/**
Expand Down
148 changes: 148 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit adc49ac

Please sign in to comment.