|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\DataUri; |
| 15 | +use Symfony\Component\Validator\Constraints\DataUriValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Kev <https://github.com/symfonyaml> |
| 20 | + */ |
| 21 | +class DataUriValidatorTest extends ConstraintValidatorTestCase |
| 22 | +{ |
| 23 | + protected function createValidator(): DataUriValidator |
| 24 | + { |
| 25 | + return new DataUriValidator(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testNullIsValid() |
| 29 | + { |
| 30 | + $this->validator->validate(null, new DataUri()); |
| 31 | + |
| 32 | + $this->assertNoViolation(); |
| 33 | + } |
| 34 | + |
| 35 | + public function testBlankIsValid() |
| 36 | + { |
| 37 | + $this->validator->validate('', new DataUri()); |
| 38 | + |
| 39 | + $this->assertNoViolation(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dataProvider getValidValues |
| 44 | + */ |
| 45 | + public function testValidValues(string $value) |
| 46 | + { |
| 47 | + $this->validator->validate($value, new DataUri()); |
| 48 | + |
| 49 | + $this->assertNoViolation(); |
| 50 | + } |
| 51 | + |
| 52 | + public static function getValidValues() |
| 53 | + { |
| 54 | + return [ |
| 55 | + 'mime type is omitted' => ['data:,FooBar'], |
| 56 | + 'just charset' => ['data:;charset=UTF-8,FooBar'], |
| 57 | + 'plain text' => ['data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='], |
| 58 | + 'text html' => ['data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E'], |
| 59 | + 'plain text with charset' => ['data:text/plain;charset=UTF-8,the%20data:1234,5678'], |
| 60 | + 'with meta key=value' => ['data:image/jpeg;key=value;base64,UEsDBBQAAAAI'], |
| 61 | + 'without base64 key name' => ['data:image/jpeg;key=value,UEsDBBQAAAAI'], |
| 62 | + 'jpeg image' => ['data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD'], |
| 63 | + 'png image' => ['data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='], |
| 64 | + 'gif image' => ['data:image/gif;base64,R0lGODlhyAAiALM...DfD0QAADs='], |
| 65 | + 'svg' => ['data:image/svg+xml,%3Csvg%20version%3D%221.1%22%3E%3C%2Fsvg%3E'], |
| 66 | + 'networking applications' => ['data:application/vnd-xxx-query,select_vcount,fcol_from_fieldtable/local,123456789'], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @dataProvider getInvalidValues |
| 72 | + */ |
| 73 | + public function testInvalidValues($value, $valueAsString) |
| 74 | + { |
| 75 | + $constraint = new DataUri([ |
| 76 | + 'message' => 'myMessage', |
| 77 | + ]); |
| 78 | + |
| 79 | + $this->validator->validate($value, $constraint); |
| 80 | + |
| 81 | + $this->buildViolation('myMessage') |
| 82 | + ->setParameter('{{ value }}', $valueAsString) |
| 83 | + ->setCode(DataUri::INVALID_DATA_URI_ERROR) |
| 84 | + ->assertRaised(); |
| 85 | + } |
| 86 | + |
| 87 | + public static function getInvalidValues() |
| 88 | + { |
| 89 | + return [ |
| 90 | + 'random string' => ['foobar', '"foobar"'], |
| 91 | + 'zero' => [0, '"0"'], |
| 92 | + 'integer' => [1234, '"1234"'], |
| 93 | + 'truncated invalid value' => [ |
| 94 | + '1234567890123456789012345678901', // 31 chars |
| 95 | + '"123456789012345678901234567890..." (truncated)', |
| 96 | + ], |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
0 commit comments