UploadTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Validate_File_UploadTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_UploadTest::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_File_Upload
  32. */
  33. require_once 'Zend/Validate/File/Upload.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Validate_File
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Validate
  41. */
  42. class Zend_Validate_File_UploadTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_UploadTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. /**
  55. * Ensures that the validator follows expected behavior
  56. *
  57. * @return void
  58. */
  59. public function testBasic()
  60. {
  61. $_FILES = array(
  62. 'test' => array(
  63. 'name' => 'test1',
  64. 'type' => 'text',
  65. 'size' => 200,
  66. 'tmp_name' => 'tmp_test1',
  67. 'error' => 0),
  68. 'test2' => array(
  69. 'name' => 'test2',
  70. 'type' => 'text2',
  71. 'size' => 202,
  72. 'tmp_name' => 'tmp_test2',
  73. 'error' => 1),
  74. 'test3' => array(
  75. 'name' => 'test3',
  76. 'type' => 'text3',
  77. 'size' => 203,
  78. 'tmp_name' => 'tmp_test3',
  79. 'error' => 2),
  80. 'test4' => array(
  81. 'name' => 'test4',
  82. 'type' => 'text4',
  83. 'size' => 204,
  84. 'tmp_name' => 'tmp_test4',
  85. 'error' => 3),
  86. 'test5' => array(
  87. 'name' => 'test5',
  88. 'type' => 'text5',
  89. 'size' => 205,
  90. 'tmp_name' => 'tmp_test5',
  91. 'error' => 4),
  92. 'test6' => array(
  93. 'name' => 'test6',
  94. 'type' => 'text6',
  95. 'size' => 206,
  96. 'tmp_name' => 'tmp_test6',
  97. 'error' => 5),
  98. 'test7' => array(
  99. 'name' => 'test7',
  100. 'type' => 'text7',
  101. 'size' => 207,
  102. 'tmp_name' => 'tmp_test7',
  103. 'error' => 6),
  104. 'test8' => array(
  105. 'name' => 'test8',
  106. 'type' => 'text8',
  107. 'size' => 208,
  108. 'tmp_name' => 'tmp_test8',
  109. 'error' => 7),
  110. 'test9' => array(
  111. 'name' => 'test9',
  112. 'type' => 'text9',
  113. 'size' => 209,
  114. 'tmp_name' => 'tmp_test9',
  115. 'error' => 8)
  116. );
  117. $validator = new Zend_Validate_File_Upload();
  118. $this->assertFalse($validator->isValid('test'));
  119. $this->assertTrue(array_key_exists('fileUploadErrorAttack', $validator->getMessages()));
  120. $validator = new Zend_Validate_File_Upload();
  121. $this->assertFalse($validator->isValid('test2'));
  122. $this->assertTrue(array_key_exists('fileUploadErrorIniSize', $validator->getMessages()));
  123. $validator = new Zend_Validate_File_Upload();
  124. $this->assertFalse($validator->isValid('test3'));
  125. $this->assertTrue(array_key_exists('fileUploadErrorFormSize', $validator->getMessages()));
  126. $validator = new Zend_Validate_File_Upload();
  127. $this->assertFalse($validator->isValid('test4'));
  128. $this->assertTrue(array_key_exists('fileUploadErrorPartial', $validator->getMessages()));
  129. $validator = new Zend_Validate_File_Upload();
  130. $this->assertFalse($validator->isValid('test5'));
  131. $this->assertTrue(array_key_exists('fileUploadErrorNoFile', $validator->getMessages()));
  132. $validator = new Zend_Validate_File_Upload();
  133. $this->assertFalse($validator->isValid('test6'));
  134. $this->assertTrue(array_key_exists('fileUploadErrorUnknown', $validator->getMessages()));
  135. $validator = new Zend_Validate_File_Upload();
  136. $this->assertFalse($validator->isValid('test7'));
  137. $this->assertTrue(array_key_exists('fileUploadErrorNoTmpDir', $validator->getMessages()));
  138. $validator = new Zend_Validate_File_Upload();
  139. $this->assertFalse($validator->isValid('test8'));
  140. $this->assertTrue(array_key_exists('fileUploadErrorCantWrite', $validator->getMessages()));
  141. $validator = new Zend_Validate_File_Upload();
  142. $this->assertFalse($validator->isValid('test9'));
  143. $this->assertTrue(array_key_exists('fileUploadErrorExtension', $validator->getMessages()));
  144. $validator = new Zend_Validate_File_Upload();
  145. $this->assertFalse($validator->isValid('test1'));
  146. $this->assertTrue(array_key_exists('fileUploadErrorAttack', $validator->getMessages()));
  147. $validator = new Zend_Validate_File_Upload();
  148. $this->assertFalse($validator->isValid('tmp_test1'));
  149. $this->assertTrue(array_key_exists('fileUploadErrorAttack', $validator->getMessages()));
  150. $validator = new Zend_Validate_File_Upload();
  151. $this->assertFalse($validator->isValid('test000'));
  152. $this->assertTrue(array_key_exists('fileUploadErrorFileNotFound', $validator->getMessages()));
  153. }
  154. /**
  155. * Ensures that getFiles() returns expected value
  156. *
  157. * @return void
  158. */
  159. public function testGetFiles()
  160. {
  161. $_FILES = array(
  162. 'test' => array(
  163. 'name' => 'test1',
  164. 'type' => 'text',
  165. 'size' => 200,
  166. 'tmp_name' => 'tmp_test1',
  167. 'error' => 0),
  168. 'test2' => array(
  169. 'name' => 'test3',
  170. 'type' => 'text2',
  171. 'size' => 202,
  172. 'tmp_name' => 'tmp_test2',
  173. 'error' => 1));
  174. $files = array(
  175. 'test' => array(
  176. 'name' => 'test1',
  177. 'type' => 'text',
  178. 'size' => 200,
  179. 'tmp_name' => 'tmp_test1',
  180. 'error' => 0));
  181. $files1 = array(
  182. 'test2' => array(
  183. 'name' => 'test3',
  184. 'type' => 'text2',
  185. 'size' => 202,
  186. 'tmp_name' => 'tmp_test2',
  187. 'error' => 1));
  188. $validator = new Zend_Validate_File_Upload();
  189. $this->assertEquals($files, $validator->getFiles('test'));
  190. $this->assertEquals($files, $validator->getFiles('test1'));
  191. $this->assertEquals($files1, $validator->getFiles('test3'));
  192. try {
  193. $this->assertEquals(array(), $validator->getFiles('test5'));
  194. $this->fail("Missing exception");
  195. } catch (Zend_Validate_Exception $e) {
  196. $this->assertContains("was not found", $e->getMessage());
  197. }
  198. }
  199. /**
  200. * Ensures that setFiles() returns expected value
  201. *
  202. * @return void
  203. */
  204. public function testSetFiles()
  205. {
  206. $files = array(
  207. 'test' => array(
  208. 'name' => 'test1',
  209. 'type' => 'text',
  210. 'size' => 200,
  211. 'tmp_name' => 'tmp_test1',
  212. 'error' => 0),
  213. 'test2' => array(
  214. 'name' => 'test2',
  215. 'type' => 'text2',
  216. 'size' => 202,
  217. 'tmp_name' => 'tmp_test2',
  218. 'error' => 1));
  219. $_FILES = array(
  220. 'test' => array(
  221. 'name' => 'test3',
  222. 'type' => 'text3',
  223. 'size' => 203,
  224. 'tmp_name' => 'tmp_test3',
  225. 'error' => 2));
  226. $validator = new Zend_Validate_File_Upload();
  227. $validator->setFiles(array());
  228. $this->assertEquals($_FILES, $validator->getFiles());
  229. $validator->setFiles();
  230. $this->assertEquals($_FILES, $validator->getFiles());
  231. $validator->setFiles($files);
  232. $this->assertEquals($files, $validator->getFiles());
  233. }
  234. }
  235. // Call Zend_Validate_File_UploadTest::main() if this source file is executed directly.
  236. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_UploadTest::main") {
  237. Zend_Validate_File_UploadTest::main();
  238. }