|
| 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\Serializer\Tests\Normalizer; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\File\File; |
| 15 | +use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Kévin Dunglas <dunglas@gmail.com> |
| 19 | + */ |
| 20 | +class DataUriNormalizerTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + const TEST_GIF_DATA = 'data:image/gif;base64,R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs='; |
| 23 | + const TEST_TXT_DATA = 'data:text/plain,K%C3%A9vin%20Dunglas%0A'; |
| 24 | + const TEST_TXT_CONTENT = "Kévin Dunglas\n"; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var DataUriNormalizer |
| 28 | + */ |
| 29 | + private $normalizer; |
| 30 | + |
| 31 | + public function setUp() |
| 32 | + { |
| 33 | + $this->normalizer = new DataUriNormalizer(); |
| 34 | + } |
| 35 | + |
| 36 | + public function testInterface() |
| 37 | + { |
| 38 | + $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer); |
| 39 | + $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer); |
| 40 | + } |
| 41 | + |
| 42 | + public function testSupportNormalization() |
| 43 | + { |
| 44 | + $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); |
| 45 | + $this->assertTrue($this->normalizer->supportsNormalization(new \SplFileObject('data:,Hello%2C
10000
%20World!'))); |
| 46 | + } |
| 47 | + |
| 48 | + public function testNormalizeHttpFoundationFile() |
| 49 | + { |
| 50 | + $file = new File(__DIR__.'/../Fixtures/test.gif'); |
| 51 | + |
| 52 | + $this->assertSame(self::TEST_GIF_DATA, $this->normalizer->normalize($file)); |
| 53 | + } |
| 54 | + |
| 55 | + public function testNormalizeSplFileInfo() |
| 56 | + { |
| 57 | + $file = new \SplFileInfo(__DIR__.'/../Fixtures/test.gif'); |
| 58 | + |
| 59 | + $this->assertSame(self::TEST_GIF_DATA, $this->normalizer->normalize($file)); |
| 60 | + } |
| 61 | + |
| 62 | + public function testNormalizeText() |
| 63 | + { |
| 64 | + $file = new \SplFileObject(__DIR__.'/../Fixtures/test.txt'); |
| 65 | + |
| 66 | + $data = $this->normalizer->normalize($file); |
| 67 | + |
| 68 | + $this->assertSame(self::TEST_TXT_DATA, $data); |
| 69 | + $this->assertSame(self::TEST_TXT_CONTENT, file_get_contents($data)); |
| 70 | + } |
| 71 | + |
| 72 | + public function testSupportsDenormalization() |
| 73 | + { |
| 74 | + $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar')); |
| 75 | + $this->assertTrue($this->normalizer->supportsDenormalization(self::TEST_GIF_DATA, 'SplFileInfo')); |
| 76 | + $this->assertTrue($this->normalizer->supportsDenormalization(self::TEST_GIF_DATA, 'SplFileObject')); |
| 77 | + $this->assertTrue($this->normalizer->supportsDenormalization(self::TEST_TXT_DATA, 'Symfony\Component\HttpFoundation\File\File')); |
| 78 | + } |
| 79 | + |
| 80 | + public function testDenormalizeSplFileInfo() |
| 81 | + { |
| 82 | + $file = $this->normalizer->denormalize(self::TEST_TXT_DATA, 'SplFileInfo'); |
| 83 | + |
| 84 | + $this->assertInstanceOf('SplFileInfo', $file); |
| 85 | + $this->assertSame(file_get_contents(self::TEST_TXT_DATA), $this->getContent($file)); |
| 86 | + } |
| 87 | + |
| 88 | + public function testDenormalizeSplFileObject() |
| 89 | + { |
| 90 | + $file = $this->normalizer->denormalize(self::TEST_TXT_DATA, 'SplFileObject'); |
| 91 | + |
| 92 | + $this->assertInstanceOf('SplFileObject', $file); |
| 93 | + $this->assertEquals(file_get_contents(self::TEST_TXT_DATA), $this->getContent($file)); |
| 94 | + } |
| 95 | + |
| 96 | + public function testDenormalizeHttpFoundationFile() |
| 97 | + { |
| 98 | + $file = $this->normalizer->denormalize(self::TEST_GIF_DATA, 'Symfony\Component\HttpFoundation\File\File'); |
| 99 | + |
| 100 | + $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $file); |
| 101 | + $this->assertSame(file_get_contents(self::TEST_GIF_DATA), $this->getContent($file->openFile())); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException |
| 106 | + * @expectedExceptionMessage The provided "data:" URI is not valid. |
| 107 | + */ |
| 108 | + public function testGiveNotAccessToLocalFiles() |
| 109 | + { |
| 110 | + $this->normalizer->denormalize('/etc/shadow', 'SplFileObject'); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException |
| 115 | + * @dataProvider invalidUriProvider |
| 116 | + */ |
| 117 | + public function testInvalidData($uri) |
| 118 | + { |
| 119 | + $this->normalizer->denormalize($uri, 'SplFileObject'); |
| 120 | + } |
| 121 | + |
| 122 | + public function invalidUriProvider() |
| 123 | + { |
| 124 | + return array( |
| 125 | + array('dataxbase64'), |
| 126 | + array('data:HelloWorld'), |
| 127 | + array('data:text/html;charset=,%3Ch1%3EHello!%3C%2Fh1%3E'), |
| 128 | + array('data:text/html;charset,%3Ch1%3EHello!%3C%2Fh1%3E'), |
| 129 | + array('data:base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC'), |
| 130 | + array(''), |
| 131 | + array('http://wikipedia.org'), |
| 132 | + array('base64'), |
| 133 | + array('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC'), |
| 134 | + array(' data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC'), |
| 135 | + array(' data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC'), |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @dataProvider validUriProvider |
| 141 | + */ |
| 142 | + public function testValidData($uri) |
| 143 | + { |
| 144 | + $this->assertInstanceOf('SplFileObject', $this->normalizer->denormalize($uri, 'SplFileObject')); |
| 145 | + } |
| 146 | + |
| 147 | + public function validUriProvider() |
| 148 | + { |
| 149 | + $data = array( |
| 150 | + array('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC'), |
| 151 | + array('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC'), |
| 152 | + array('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC '), |
| 153 | + array('data:,Hello%2C%20World!'), |
| 154 | + array('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E'), |
| 155 | + array('data:,A%20brief%20note'), |
| 156 | + array('data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E'), |
| 157 | + ); |
| 158 | + |
| 159 | + if (!defined('HHVM_VERSION')) { |
| 160 | + // See https://github.com/facebook/hhvm/issues/6354 |
| 161 | + $data[] = array('data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQh'); |
| 162 | + } |
| 163 | + |
| 164 | + return $data; |
| 165 | + } |
| 166 | + |
| 167 | + private function getContent(\SplFileObject $file) |
| 168 | + { |
| 169 | + $buffer = ''; |
| 170 | + while (!$file->eof()) { |
| 171 | + $buffer .= $file->fgets(); |
| 172 | + } |
| 173 | + |
| 174 | + return $buffer; |
| 175 | + } |
| 176 | +} |
0 commit comments