FileFieldTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. use Symfony\Component\Form\FileField;
  4. class FileFieldTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public static $tmpFiles = array();
  7. protected static $tmpDir;
  8. protected $field;
  9. public static function setUpBeforeClass()
  10. {
  11. self::$tmpDir = sys_get_temp_dir();
  12. // we need a session ID
  13. @session_start();
  14. }
  15. protected function setUp()
  16. {
  17. $this->field = new FileField('file', array(
  18. 'secret' => '$secret$',
  19. 'tmp_dir' => self::$tmpDir,
  20. ));
  21. }
  22. protected function tearDown()
  23. {
  24. foreach (self::$tmpFiles as $key => $file) {
  25. @unlink($file);
  26. unset(self::$tmpFiles[$key]);
  27. }
  28. }
  29. public function createTmpFile($path)
  30. {
  31. self::$tmpFiles[] = $path;
  32. file_put_contents($path, 'foobar');
  33. }
  34. public function testBindUploadsNewFiles()
  35. {
  36. $tmpPath = realpath(self::$tmpDir) . '/' . md5(session_id() . '$secret$' . '12345');
  37. $that = $this;
  38. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  39. $file->expects($this->once())
  40. ->method('move')
  41. ->with($this->equalTo($tmpPath))
  42. ->will($this->returnCallback(function ($path) use ($that) {
  43. $that->createTmpFile($path);
  44. }));
  45. $file->expects($this->any())
  46. ->method('getOriginalName')
  47. ->will($this->returnValue('original_name.jpg'));
  48. $this->field->bind(array(
  49. 'file' => $file,
  50. 'token' => '12345',
  51. 'original_name' => '',
  52. ));
  53. $this->assertTrue(file_exists($tmpPath));
  54. $this->assertEquals(array(
  55. 'file' => '',
  56. 'token' => '12345',
  57. 'original_name' => 'original_name.jpg',
  58. ), $this->field->getDisplayedData());
  59. $this->assertEquals($tmpPath, $this->field->getData());
  60. $this->assertFalse($this->field->isIniSizeExceeded());
  61. $this->assertFalse($this->field->isFormSizeExceeded());
  62. $this->assertTrue($this->field->isUploadComplete());
  63. }
  64. public function testBindKeepsUploadedFilesOnErrors()
  65. {
  66. $tmpPath = self::$tmpDir . '/' . md5(session_id() . '$secret$' . '12345');
  67. $this->createTmpFile($tmpPath);
  68. $this->field->bind(array(
  69. 'file' => '',
  70. 'token' => '12345',
  71. 'original_name' => 'original_name.jpg',
  72. ));
  73. $this->assertTrue(file_exists($tmpPath));
  74. $this->assertEquals(array(
  75. 'file' => '',
  76. 'token' => '12345',
  77. 'original_name' => 'original_name.jpg',
  78. ), $this->field->getDisplayedData());
  79. $this->assertEquals(realpath($tmpPath), realpath($this->field->getData()));
  80. }
  81. public function testBindKeepsOldFileIfNotOverwritten()
  82. {
  83. $oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest');
  84. $this->createTmpFile($oldPath);
  85. $this->field->setData($oldPath);
  86. $this->assertEquals($oldPath, $this->field->getData());
  87. $this->field->bind(array(
  88. 'file' => '',
  89. 'token' => '12345',
  90. 'original_name' => '',
  91. ));
  92. $this->assertTrue(file_exists($oldPath));
  93. $this->assertEquals(array(
  94. 'file' => '',
  95. 'token' => '12345',
  96. 'original_name' => '',
  97. ), $this->field->getDisplayedData());
  98. $this->assertEquals($oldPath, $this->field->getData());
  99. }
  100. public function testBindHandlesUploadErrIniSize()
  101. {
  102. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  103. $file->expects($this->any())
  104. ->method('getError')
  105. ->will($this->returnValue(UPLOAD_ERR_INI_SIZE));
  106. $this->field->bind(array(
  107. 'file' => $file,
  108. 'token' => '12345',
  109. 'original_name' => ''
  110. ));
  111. $this->assertTrue($this->field->isIniSizeExceeded());
  112. }
  113. public function testBindHandlesUploadErrFormSize()
  114. {
  115. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  116. $file->expects($this->any())
  117. ->method('getError')
  118. ->will($this->returnValue(UPLOAD_ERR_FORM_SIZE));
  119. $this->field->bind(array(
  120. 'file' => $file,
  121. 'token' => '12345',
  122. 'original_name' => ''
  123. ));
  124. $this->assertTrue($this->field->isFormSizeExceeded());
  125. }
  126. public function testBindHandlesUploadErrPartial()
  127. {
  128. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  129. $file->expects($this->any())
  130. ->method('getError')
  131. ->will($this->returnValue(UPLOAD_ERR_PARTIAL));
  132. $this->field->bind(array(
  133. 'file' => $file,
  134. 'token' => '12345',
  135. 'original_name' => ''
  136. ));
  137. $this->assertFalse($this->field->isUploadComplete());
  138. }
  139. public function testBindThrowsExceptionOnUploadErrNoTmpDir()
  140. {
  141. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  142. $file->expects($this->any())
  143. ->method('getError')
  144. ->will($this->returnValue(UPLOAD_ERR_NO_TMP_DIR));
  145. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  146. $this->field->bind(array(
  147. 'file' => $file,
  148. 'token' => '12345',
  149. 'original_name' => ''
  150. ));
  151. }
  152. public function testBindThrowsExceptionOnUploadErrCantWrite()
  153. {
  154. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  155. $file->expects($this->any())
  156. ->method('getError')
  157. ->will($this->returnValue(UPLOAD_ERR_CANT_WRITE));
  158. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  159. $this->field->bind(array(
  160. 'file' => $file,
  161. 'token' => '12345',
  162. 'original_name' => ''
  163. ));
  164. }
  165. public function testBindThrowsExceptionOnUploadErrExtension()
  166. {
  167. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  168. $file->expects($this->any())
  169. ->method('getError')
  170. ->will($this->returnValue(UPLOAD_ERR_EXTENSION));
  171. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  172. $this->field->bind(array(
  173. 'file' => $file,
  174. 'token' => '12345',
  175. 'original_name' => ''
  176. ));
  177. }
  178. }