UploadTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Tests KO7 upload class
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.upload
  8. *
  9. * @package KO7
  10. * @category Tests
  11. *
  12. * @author Jeremy Bush <contractfrombelow@gmail.com>
  13. * @copyright (c) 2007-2016 Kohana Team
  14. * @copyright (c) since 2016 Koseven Team
  15. * @license https://koseven.dev/LICENSE
  16. */
  17. class KO7_UploadTest extends Unittest_TestCase
  18. {
  19. /**
  20. * Provides test data for test_size()
  21. *
  22. * @return array
  23. */
  24. public function provider_size()
  25. {
  26. return [
  27. // $field, $bytes, $environment, $expected
  28. [
  29. 'unit_test',
  30. 5,
  31. ['_FILES' => ['unit_test' => ['error' => UPLOAD_ERR_INI_SIZE]]],
  32. FALSE
  33. ],
  34. [
  35. 'unit_test',
  36. 5,
  37. ['_FILES' => ['unit_test' => ['error' => UPLOAD_ERR_NO_FILE]]],
  38. TRUE
  39. ],
  40. [
  41. 'unit_test',
  42. '6K',
  43. ['_FILES' => [
  44. 'unit_test' => [
  45. 'error' => UPLOAD_ERR_OK,
  46. 'name' => 'Unit_Test File',
  47. 'type' => 'image/png',
  48. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  49. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  50. ]
  51. ]
  52. ],
  53. TRUE
  54. ],
  55. [
  56. 'unit_test',
  57. '1B',
  58. ['_FILES' => [
  59. 'unit_test' => [
  60. 'error' => UPLOAD_ERR_OK,
  61. 'name' => 'Unit_Test File',
  62. 'type' => 'image/png',
  63. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  64. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  65. ]
  66. ]
  67. ],
  68. FALSE
  69. ],
  70. ];
  71. }
  72. /**
  73. * Tests Upload::size
  74. *
  75. * @test
  76. * @dataProvider provider_size
  77. * @covers upload::size
  78. * @param string $field the files field to test
  79. * @param string $bytes valid bite size
  80. * @param array $environment set the $_FILES array
  81. * @param bool $expected what to expect
  82. */
  83. public function test_size($field, $bytes, $environment, $expected)
  84. {
  85. $this->setEnvironment($environment);
  86. $this->assertSame($expected, Upload::size($_FILES[$field], $bytes));
  87. }
  88. /**
  89. * size() should throw an exception of the supplied max size is invalid
  90. *
  91. * @covers upload::size
  92. */
  93. public function test_size_throws_exception_for_invalid_size()
  94. {
  95. $this->expectException(KO7_Exception::class);
  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' => KO7::find_file('tests', 'test_data/github', 'png'),
  103. 'size' => filesize(KO7::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_valid()
  111. *
  112. * @return array
  113. */
  114. public function provider_valid()
  115. {
  116. $this->markAsRisky();
  117. return [
  118. [
  119. TRUE,
  120. [
  121. 'error' => UPLOAD_ERR_OK,
  122. 'name' => 'Unit_Test File',
  123. 'type' => 'image/png',
  124. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  125. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  126. ]
  127. ],
  128. [
  129. FALSE,
  130. [
  131. 'name' => 'Unit_Test File',
  132. 'type' => 'image/png',
  133. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  134. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  135. ]
  136. ],
  137. [
  138. FALSE,
  139. [
  140. 'error' => UPLOAD_ERR_OK,
  141. 'type' => 'image/png',
  142. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  143. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  144. ]
  145. ],
  146. [
  147. FALSE,
  148. [
  149. 'name' => 'Unit_Test File',
  150. 'error' => UPLOAD_ERR_OK,
  151. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  152. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  153. ]
  154. ],
  155. [
  156. FALSE,
  157. [
  158. 'error' => UPLOAD_ERR_OK,
  159. 'name' => 'Unit_Test File',
  160. 'type' => 'image/png',
  161. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  162. ]
  163. ],
  164. [
  165. FALSE,
  166. [
  167. 'error' => UPLOAD_ERR_OK,
  168. 'name' => 'Unit_Test File',
  169. 'type' => 'image/png',
  170. 'tmp_name' => KO7::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([
  185. '_FILES' => [
  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([
  200. '_FILES' => [
  201. 'unit_test' => [
  202. 'error' => UPLOAD_ERR_OK,
  203. 'name' => 'github.png',
  204. 'type' => 'image/png',
  205. 'tmp_name' => KO7::find_file('tests', 'test_data/github', 'png'),
  206. 'size' => filesize(KO7::find_file('tests', 'test_data/github', 'png')),
  207. ]
  208. ]
  209. ]);
  210. $this->assertTrue(Upload::type($_FILES['unit_test'], ['jpg', 'png', 'gif']));
  211. $this->assertFalse(Upload::type($_FILES['unit_test'], ['docx']));
  212. }
  213. }