UploadTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests Kohana upload class
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.upload
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author Jeremy Bush <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_UploadTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Provides test data for test_size()
  20. *
  21. * @return array
  22. */
  23. public function provider_size()
  24. {
  25. return array(
  26. // $field, $bytes, $environment, $expected
  27. array(
  28. 'unit_test',
  29. 5,
  30. array('_FILES' => array('unit_test' => array('error' => UPLOAD_ERR_INI_SIZE))),
  31. FALSE
  32. ),
  33. array(
  34. 'unit_test',
  35. 5,
  36. array('_FILES' => array('unit_test' => array('error' => UPLOAD_ERR_NO_FILE))),
  37. TRUE
  38. ),
  39. array(
  40. 'unit_test',
  41. '6K',
  42. array('_FILES' => array(
  43. 'unit_test' => array(
  44. 'error' => UPLOAD_ERR_OK,
  45. 'name' => 'Unit_Test File',
  46. 'type' => 'image/png',
  47. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  48. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  49. )
  50. )
  51. ),
  52. TRUE
  53. ),
  54. array(
  55. 'unit_test',
  56. '1B',
  57. array('_FILES' => array(
  58. 'unit_test' => array(
  59. 'error' => UPLOAD_ERR_OK,
  60. 'name' => 'Unit_Test File',
  61. 'type' => 'image/png',
  62. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  63. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  64. )
  65. )
  66. ),
  67. FALSE
  68. ),
  69. );
  70. }
  71. /**
  72. * Tests Upload::size
  73. *
  74. * @test
  75. * @dataProvider provider_size
  76. * @covers upload::size
  77. * @param string $field the files field to test
  78. * @param string $bytes valid bite size
  79. * @param array $environment set the $_FILES array
  80. * @param bool $expected what to expect
  81. */
  82. public function test_size($field, $bytes, $environment, $expected)
  83. {
  84. $this->setEnvironment($environment);
  85. $this->assertSame($expected, Upload::size($_FILES[$field], $bytes));
  86. }
  87. /**
  88. * size() should throw an exception of the supplied max size is invalid
  89. *
  90. * @test
  91. * @covers upload::size
  92. * @expectedException Kohana_Exception
  93. */
  94. public function test_size_throws_exception_for_invalid_size()
  95. {
  96. $this->setEnvironment(array(
  97. '_FILES' => array(
  98. 'unit_test' => array(
  99. 'error' => UPLOAD_ERR_OK,
  100. 'name' => 'Unit_Test File',
  101. 'type' => 'image/png',
  102. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  103. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  104. )
  105. )
  106. ));
  107. Upload::size($_FILES['unit_test'], '1DooDah');
  108. }
  109. /**
  110. * Provides test data for test_vali()
  111. *
  112. * @test
  113. * @return array
  114. */
  115. public function provider_valid()
  116. {
  117. return array(
  118. array(
  119. TRUE,
  120. array(
  121. 'error' => UPLOAD_ERR_OK,
  122. 'name' => 'Unit_Test File',
  123. 'type' => 'image/png',
  124. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  125. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  126. )
  127. ),
  128. array(
  129. FALSE,
  130. array(
  131. 'name' => 'Unit_Test File',
  132. 'type' => 'image/png',
  133. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  134. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  135. )
  136. ),
  137. array(
  138. FALSE,
  139. array(
  140. 'error' => UPLOAD_ERR_OK,
  141. 'type' => 'image/png',
  142. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  143. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  144. )
  145. ),
  146. array(
  147. FALSE,
  148. array(
  149. 'name' => 'Unit_Test File',
  150. 'error' => UPLOAD_ERR_OK,
  151. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  152. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  153. )
  154. ),
  155. array(
  156. FALSE,
  157. array(
  158. 'error' => UPLOAD_ERR_OK,
  159. 'name' => 'Unit_Test File',
  160. 'type' => 'image/png',
  161. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  162. )
  163. ),
  164. array(
  165. FALSE,
  166. array(
  167. 'error' => UPLOAD_ERR_OK,
  168. 'name' => 'Unit_Test File',
  169. 'type' => 'image/png',
  170. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  171. )
  172. ),
  173. );
  174. }
  175. /**
  176. * Test Upload::valid
  177. *
  178. * @test
  179. * @dataProvider provider_valid
  180. * @covers Upload::valid
  181. */
  182. public function test_valid($expected, $file)
  183. {
  184. $this->setEnvironment(array(
  185. '_FILES' => array(
  186. 'unit_test' => $file,
  187. ),
  188. ));
  189. $this->assertSame($expected, Upload::valid($_FILES['unit_test']));
  190. }
  191. /**
  192. * Tests Upload::type
  193. *
  194. * @test
  195. * @covers Upload::type
  196. */
  197. public function test_type()
  198. {
  199. $this->setEnvironment(array(
  200. '_FILES' => array(
  201. 'unit_test' => array(
  202. 'error' => UPLOAD_ERR_OK,
  203. 'name' => 'github.png',
  204. 'type' => 'image/png',
  205. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  206. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  207. )
  208. )
  209. ));
  210. $this->assertTrue(Upload::type($_FILES['unit_test'], array('jpg', 'png', 'gif')));
  211. $this->assertFalse(Upload::type($_FILES['unit_test'], array('docx')));
  212. }
  213. }