UploadTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  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 <contractfrombelow@gmail.com>
  13. * @copyright (c) Kohana Team
  14. * @license https://koseven.ga/LICENSE.md
  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 [
  26. // $field, $bytes, $environment, $expected
  27. [
  28. 'unit_test',
  29. 5,
  30. ['_FILES' => ['unit_test' => ['error' => UPLOAD_ERR_INI_SIZE]]],
  31. FALSE
  32. ],
  33. [
  34. 'unit_test',
  35. 5,
  36. ['_FILES' => ['unit_test' => ['error' => UPLOAD_ERR_NO_FILE]]],
  37. TRUE
  38. ],
  39. [
  40. 'unit_test',
  41. '6K',
  42. ['_FILES' => [
  43. 'unit_test' => [
  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. [
  55. 'unit_test',
  56. '1B',
  57. ['_FILES' => [
  58. 'unit_test' => [
  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([
  97. '_FILES' => [
  98. 'unit_test' => [
  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. $this->markAsRisky();
  118. return [
  119. [
  120. TRUE,
  121. [
  122. 'error' => UPLOAD_ERR_OK,
  123. 'name' => 'Unit_Test File',
  124. 'type' => 'image/png',
  125. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  126. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  127. ]
  128. ],
  129. [
  130. FALSE,
  131. [
  132. 'name' => 'Unit_Test File',
  133. 'type' => 'image/png',
  134. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  135. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  136. ]
  137. ],
  138. [
  139. FALSE,
  140. [
  141. 'error' => UPLOAD_ERR_OK,
  142. 'type' => 'image/png',
  143. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  144. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  145. ]
  146. ],
  147. [
  148. FALSE,
  149. [
  150. 'name' => 'Unit_Test File',
  151. 'error' => UPLOAD_ERR_OK,
  152. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  153. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  154. ]
  155. ],
  156. [
  157. FALSE,
  158. [
  159. 'error' => UPLOAD_ERR_OK,
  160. 'name' => 'Unit_Test File',
  161. 'type' => 'image/png',
  162. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  163. ]
  164. ],
  165. [
  166. FALSE,
  167. [
  168. 'error' => UPLOAD_ERR_OK,
  169. 'name' => 'Unit_Test File',
  170. 'type' => 'image/png',
  171. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  172. ]
  173. ],
  174. ];
  175. }
  176. /**
  177. * Test Upload::valid
  178. *
  179. * @test
  180. * @dataProvider provider_valid
  181. * @covers Upload::valid
  182. */
  183. public function test_valid($expected, $file)
  184. {
  185. $this->setEnvironment([
  186. '_FILES' => [
  187. 'unit_test' => $file,
  188. ],
  189. ]);
  190. $this->assertSame($expected, Upload::valid($_FILES['unit_test']));
  191. }
  192. /**
  193. * Tests Upload::type
  194. *
  195. * @test
  196. * @covers Upload::type
  197. */
  198. public function test_type()
  199. {
  200. $this->setEnvironment([
  201. '_FILES' => [
  202. 'unit_test' => [
  203. 'error' => UPLOAD_ERR_OK,
  204. 'name' => 'github.png',
  205. 'type' => 'image/png',
  206. 'tmp_name' => Kohana::find_file('tests', 'test_data/github', 'png'),
  207. 'size' => filesize(Kohana::find_file('tests', 'test_data/github', 'png')),
  208. ]
  209. ]
  210. ]);
  211. $this->assertTrue(Upload::type($_FILES['unit_test'], ['jpg', 'png', 'gif']));
  212. $this->assertFalse(Upload::type($_FILES['unit_test'], ['docx']));
  213. }
  214. }