FileValidatorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. use Symfony\Component\Validator\Constraints\File;
  13. use Symfony\Component\Validator\Constraints\FileValidator;
  14. use Symfony\Component\Validator\Validation;
  15. abstract class FileValidatorTest extends AbstractConstraintValidatorTest
  16. {
  17. protected $path;
  18. protected $file;
  19. protected function getApiVersion()
  20. {
  21. return Validation::API_VERSION_2_5;
  22. }
  23. protected function createValidator()
  24. {
  25. return new FileValidator();
  26. }
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->path = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'FileValidatorTest';
  31. $this->file = fopen($this->path, 'w');
  32. fwrite($this->file, ' ', 1);
  33. }
  34. protected function tearDown()
  35. {
  36. parent::tearDown();
  37. if (\is_resource($this->file)) {
  38. fclose($this->file);
  39. }
  40. if (file_exists($this->path)) {
  41. unlink($this->path);
  42. }
  43. $this->path = null;
  44. $this->file = null;
  45. }
  46. public function testNullIsValid()
  47. {
  48. $this->validator->validate(null, new File());
  49. $this->assertNoViolation();
  50. }
  51. public function testEmptyStringIsValid()
  52. {
  53. $this->validator->validate('', new File());
  54. $this->assertNoViolation();
  55. }
  56. /**
  57. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  58. */
  59. public function testExpectsStringCompatibleTypeOrFile()
  60. {
  61. $this->validator->validate(new \stdClass(), new File());
  62. }
  63. public function testValidFile()
  64. {
  65. $this->validator->validate($this->path, new File());
  66. $this->assertNoViolation();
  67. }
  68. public function testValidUploadedfile()
  69. {
  70. $file = new UploadedFile($this->path, 'originalName', null, null, null, true);
  71. $this->validator->validate($file, new File());
  72. $this->assertNoViolation();
  73. }
  74. public function provideMaxSizeExceededTests()
  75. {
  76. // We have various interesting limit - size combinations to test.
  77. // Assume a limit of 1000 bytes (1 kB). Then the following table
  78. // lists the violation messages for different file sizes:
  79. // -----------+--------------------------------------------------------
  80. // Size | Violation Message
  81. // -----------+--------------------------------------------------------
  82. // 1000 bytes | No violation
  83. // 1001 bytes | "Size of 1001 bytes exceeded limit of 1000 bytes"
  84. // 1004 bytes | "Size of 1004 bytes exceeded limit of 1000 bytes"
  85. // | NOT: "Size of 1 kB exceeded limit of 1 kB"
  86. // 1005 bytes | "Size of 1.01 kB exceeded limit of 1 kB"
  87. // -----------+--------------------------------------------------------
  88. // As you see, we have two interesting borders:
  89. // 1000/1001 - The border as of which a violation occurs
  90. // 1004/1005 - The border as of which the message can be rounded to kB
  91. // Analogous for kB/MB.
  92. // Prior to Symfony 2.5, violation messages are always displayed in the
  93. // same unit used to specify the limit.
  94. // As of Symfony 2.5, the above logic is implemented.
  95. return array(
  96. // limit in bytes
  97. array(1001, 1000, '1001', '1000', 'bytes'),
  98. array(1004, 1000, '1004', '1000', 'bytes'),
  99. array(1005, 1000, '1.01', '1', 'kB'),
  100. array(1000001, 1000000, '1000001', '1000000', 'bytes'),
  101. array(1004999, 1000000, '1005', '1000', 'kB'),
  102. array(1005000, 1000000, '1.01', '1', 'MB'),
  103. // limit in kB
  104. array(1001, '1k', '1001', '1000', 'bytes'),
  105. array(1004, '1k', '1004', '1000', 'bytes'),
  106. array(1005, '1k', '1.01', '1', 'kB'),
  107. array(1000001, '1000k', '1000001', '1000000', 'bytes'),
  108. array(1004999, '1000k', '1005', '1000', 'kB'),
  109. array(1005000, '1000k', '1.01', '1', 'MB'),
  110. // limit in MB
  111. array(1000001, '1M', '1000001', '1000000', 'bytes'),
  112. array(1004999, '1M', '1005', '1000', 'kB'),
  113. array(1005000, '1M', '1.01', '1', 'MB'),
  114. // limit in KiB
  115. array(1025, '1Ki', '1025', '1024', 'bytes'),
  116. array(1029, '1Ki', '1029', '1024', 'bytes'),
  117. array(1030, '1Ki', '1.01', '1', 'KiB'),
  118. array(1048577, '1024Ki', '1048577', '1048576', 'bytes'),
  119. array(1053818, '1024Ki', '1029.12', '1024', 'KiB'),
  120. array(1053819, '1024Ki', '1.01', '1', 'MiB'),
  121. // limit in MiB
  122. array(1048577, '1Mi', '1048577', '1048576', 'bytes'),
  123. array(1053818, '1Mi', '1029.12', '1024', 'KiB'),
  124. array(1053819, '1Mi', '1.01', '1', 'MiB'),
  125. );
  126. }
  127. /**
  128. * @dataProvider provideMaxSizeExceededTests
  129. */
  130. public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limitAsString, $suffix)
  131. {
  132. fseek($this->file, $bytesWritten - 1, SEEK_SET);
  133. fwrite($this->file, '0');
  134. fclose($this->file);
  135. $constraint = new File(array(
  136. 'maxSize' => $limit,
  137. 'maxSizeMessage' => 'myMessage',
  138. ));
  139. $this->validator->validate($this->getFile($this->path), $constraint);
  140. $this->buildViolation('myMessage')
  141. ->setParameter('{{ limit }}', $limitAsString)
  142. ->setParameter('{{ size }}', $sizeAsString)
  143. ->setParameter('{{ suffix }}', $suffix)
  144. ->setParameter('{{ file }}', '"'.$this->path.'"')
  145. ->setCode(File::TOO_LARGE_ERROR)
  146. ->assertRaised();
  147. }
  148. public function provideMaxSizeNotExceededTests()
  149. {
  150. return array(
  151. // limit in bytes
  152. array(1000, 1000),
  153. array(1000000, 1000000),
  154. // limit in kB
  155. array(1000, '1k'),
  156. array(1000000, '1000k'),
  157. // limit in MB
  158. array(1000000, '1M'),
  159. // limit in KiB
  160. array(1024, '1Ki'),
  161. array(1048576, '1024Ki'),
  162. // limit in MiB
  163. array(1048576, '1Mi'),
  164. );
  165. }
  166. /**
  167. * @dataProvider provideMaxSizeNotExceededTests
  168. */
  169. public function testMaxSizeNotExceeded($bytesWritten, $limit)
  170. {
  171. fseek($this->file, $bytesWritten - 1, SEEK_SET);
  172. fwrite($this->file, '0');
  173. fclose($this->file);
  174. $constraint = new File(array(
  175. 'maxSize' => $limit,
  176. 'maxSizeMessage' => 'myMessage',
  177. ));
  178. $this->validator->validate($this->getFile($this->path), $constraint);
  179. $this->assertNoViolation();
  180. }
  181. /**
  182. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  183. */
  184. public function testInvalidMaxSize()
  185. {
  186. $constraint = new File(array(
  187. 'maxSize' => '1abc',
  188. ));
  189. $this->validator->validate($this->path, $constraint);
  190. }
  191. public function provideBinaryFormatTests()
  192. {
  193. return array(
  194. array(11, 10, null, '11', '10', 'bytes'),
  195. array(11, 10, true, '11', '10', 'bytes'),
  196. array(11, 10, false, '11', '10', 'bytes'),
  197. // round(size) == 1.01kB, limit == 1kB
  198. array(ceil(1000 * 1.01), 1000, null, '1.01', '1', 'kB'),
  199. array(ceil(1000 * 1.01), '1k', null, '1.01', '1', 'kB'),
  200. array(ceil(1024 * 1.01), '1Ki', null, '1.01', '1', 'KiB'),
  201. array(ceil(1024 * 1.01), 1024, true, '1.01', '1', 'KiB'),
  202. array(ceil(1024 * 1.01 * 1000), '1024k', true, '1010', '1000', 'KiB'),
  203. array(ceil(1024 * 1.01), '1Ki', true, '1.01', '1', 'KiB'),
  204. array(ceil(1000 * 1.01), 1000, false, '1.01', '1', 'kB'),
  205. array(ceil(1000 * 1.01), '1k', false, '1.01', '1', 'kB'),
  206. array(ceil(1024 * 1.01 * 10), '10Ki', false, '10.34', '10.24', 'kB'),
  207. );
  208. }
  209. /**
  210. * @dataProvider provideBinaryFormatTests
  211. */
  212. public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsString, $limitAsString, $suffix)
  213. {
  214. fseek($this->file, $bytesWritten - 1, SEEK_SET);
  215. fwrite($this->file, '0');
  216. fclose($this->file);
  217. $constraint = new File(array(
  218. 'maxSize' => $limit,
  219. 'binaryFormat' => $binaryFormat,
  220. 'maxSizeMessage' => 'myMessage',
  221. ));
  222. $this->validator->validate($this->getFile($this->path), $constraint);
  223. $this->buildViolation('myMessage')
  224. ->setParameter('{{ limit }}', $limitAsString)
  225. ->setParameter('{{ size }}', $sizeAsString)
  226. ->setParameter('{{ suffix }}', $suffix)
  227. ->setParameter('{{ file }}', '"'.$this->path.'"')
  228. ->setCode(File::TOO_LARGE_ERROR)
  229. ->assertRaised();
  230. }
  231. public function testValidMimeType()
  232. {
  233. $file = $this
  234. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  235. ->setConstructorArgs(array(__DIR__.'/Fixtures/foo'))
  236. ->getMock();
  237. $file
  238. ->expects($this->once())
  239. ->method('getPathname')
  240. ->will($this->returnValue($this->path));
  241. $file
  242. ->expects($this->once())
  243. ->method('getMimeType')
  244. ->will($this->returnValue('image/jpg'));
  245. $constraint = new File(array(
  246. 'mimeTypes' => array('image/png', 'image/jpg'),
  247. ));
  248. $this->validator->validate($file, $constraint);
  249. $this->assertNoViolation();
  250. }
  251. public function testValidWildcardMimeType()
  252. {
  253. $file = $this
  254. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  255. ->setConstructorArgs(array(__DIR__.'/Fixtures/foo'))
  256. ->getMock();
  257. $file
  258. ->expects($this->once())
  259. ->method('getPathname')
  260. ->will($this->returnValue($this->path));
  261. $file
  262. ->expects($this->once())
  263. ->method('getMimeType')
  264. ->will($this->returnValue('image/jpg'));
  265. $constraint = new File(array(
  266. 'mimeTypes' => array('image/*'),
  267. ));
  268. $this->validator->validate($file, $constraint);
  269. $this->assertNoViolation();
  270. }
  271. public function testInvalidMimeType()
  272. {
  273. $file = $this
  274. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  275. ->setConstructorArgs(array(__DIR__.'/Fixtures/foo'))
  276. ->getMock();
  277. $file
  278. ->expects($this->once())
  279. ->method('getPathname')
  280. ->will($this->returnValue($this->path));
  281. $file
  282. ->expects($this->once())
  283. ->method('getMimeType')
  284. ->will($this->returnValue('application/pdf'));
  285. $constraint = new File(array(
  286. 'mimeTypes' => array('image/png', 'image/jpg'),
  287. 'mimeTypesMessage' => 'myMessage',
  288. ));
  289. $this->validator->validate($file, $constraint);
  290. $this->buildViolation('myMessage')
  291. ->setParameter('{{ type }}', '"application/pdf"')
  292. ->setParameter('{{ types }}', '"image/png", "image/jpg"')
  293. ->setParameter('{{ file }}', '"'.$this->path.'"')
  294. ->setCode(File::INVALID_MIME_TYPE_ERROR)
  295. ->assertRaised();
  296. }
  297. public function testInvalidWildcardMimeType()
  298. {
  299. $file = $this
  300. ->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
  301. ->setConstructorArgs(array(__DIR__.'/Fixtures/foo'))
  302. ->getMock();
  303. $file
  304. ->expects($this->once())
  305. ->method('getPathname')
  306. ->will($this->returnValue($this->path));
  307. $file
  308. ->expects($this->once())
  309. ->method('getMimeType')
  310. ->will($this->returnValue('application/pdf'));
  311. $constraint = new File(array(
  312. 'mimeTypes' => array('image/*', 'image/jpg'),
  313. 'mimeTypesMessage' => 'myMessage',
  314. ));
  315. $this->validator->validate($file, $constraint);
  316. $this->buildViolation('myMessage')
  317. ->setParameter('{{ type }}', '"application/pdf"')
  318. ->setParameter('{{ types }}', '"image/*", "image/jpg"')
  319. ->setParameter('{{ file }}', '"'.$this->path.'"')
  320. ->setCode(File::INVALID_MIME_TYPE_ERROR)
  321. ->assertRaised();
  322. }
  323. public function testDisallowEmpty()
  324. {
  325. ftruncate($this->file, 0);
  326. $constraint = new File(array(
  327. 'disallowEmptyMessage' => 'myMessage',
  328. ));
  329. $this->validator->validate($this->getFile($this->path), $constraint);
  330. $this->buildViolation('myMessage')
  331. ->setParameter('{{ file }}', '"'.$this->path.'"')
  332. ->setCode(File::EMPTY_ERROR)
  333. ->assertRaised();
  334. }
  335. /**
  336. * @dataProvider uploadedFileErrorProvider
  337. */
  338. public function testUploadedFileError($error, $message, array $params = array(), $maxSize = null)
  339. {
  340. $file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);
  341. $constraint = new File(array(
  342. $message => 'myMessage',
  343. 'maxSize' => $maxSize,
  344. ));
  345. $this->validator->validate($file, $constraint);
  346. $this->buildViolation('myMessage')
  347. ->setParameters($params)
  348. ->setCode($error)
  349. ->assertRaised();
  350. }
  351. public function uploadedFileErrorProvider()
  352. {
  353. $tests = array(
  354. array(UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'),
  355. array(UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'),
  356. array(UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'),
  357. array(UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'),
  358. array(UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'),
  359. array(UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'),
  360. );
  361. if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
  362. // when no maxSize is specified on constraint, it should use the ini value
  363. $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
  364. '{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576,
  365. '{{ suffix }}' => 'MiB',
  366. ));
  367. // it should use the smaller limitation (maxSize option in this case)
  368. $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
  369. '{{ limit }}' => 1,
  370. '{{ suffix }}' => 'bytes',
  371. ), '1');
  372. // access FileValidator::factorizeSizes() private method to format max file size
  373. $reflection = new \ReflectionClass(\get_class(new FileValidator()));
  374. $method = $reflection->getMethod('factorizeSizes');
  375. $method->setAccessible(true);
  376. list($sizeAsString, $limit, $suffix) = $method->invokeArgs(new FileValidator(), array(0, UploadedFile::getMaxFilesize(), false));
  377. // it correctly parses the maxSize option and not only uses simple string comparison
  378. // 1000M should be bigger than the ini value
  379. $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
  380. '{{ limit }}' => $limit,
  381. '{{ suffix }}' => $suffix,
  382. ), '1000M');
  383. // it correctly parses the maxSize option and not only uses simple string comparison
  384. // 1000M should be bigger than the ini value
  385. $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
  386. '{{ limit }}' => '0.1',
  387. '{{ suffix }}' => 'MB',
  388. ), '100K');
  389. }
  390. return $tests;
  391. }
  392. abstract protected function getFile($filename);
  393. }