/tests/codeigniter/libraries/Upload_test.php

https://github.com/ekoisa/pyrocms · PHP · 270 lines · 213 code · 57 blank · 0 comment · 0 complexity · 347c712f5a451a0068f648b76f709921 MD5 · raw file

  1. <?php
  2. class Upload_test extends CI_TestCase {
  3. function set_up()
  4. {
  5. $obj = new stdClass;
  6. $obj->upload = new Mock_Libraries_Upload();
  7. $obj->security = new Mock_Core_Security();
  8. $obj->lang = new Mock_Core_Lang();
  9. $this->ci_instance($obj);
  10. $this->upload = $obj->upload;
  11. vfsStreamWrapper::register();
  12. vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir'));
  13. $this->_test_dir = vfsStreamWrapper::getRoot();
  14. }
  15. function test_do_upload()
  16. {
  17. $this->markTestIncomplete('We can\'t really test this at the moment because of the call to `is_uploaded_file` in do_upload which isn\'t supported by vfsStream');
  18. }
  19. function test_data()
  20. {
  21. $data = array(
  22. 'file_name' => 'hello.txt',
  23. 'file_type' => 'text/plain',
  24. 'file_path' => '/tmp/',
  25. 'full_path' => '/tmp/hello.txt',
  26. 'raw_name' => 'hello',
  27. 'orig_name' => 'hello.txt',
  28. 'client_name' => '',
  29. 'file_ext' => '.txt',
  30. 'file_size' => 100,
  31. 'is_image' => FALSE,
  32. 'image_width' => '',
  33. 'image_height' => '',
  34. 'image_type' => '',
  35. 'image_size_str' => ''
  36. );
  37. $this->upload->set_upload_path('/tmp/');
  38. foreach ($data as $k => $v)
  39. {
  40. $this->upload->{$k} = $v;
  41. }
  42. $this->assertEquals('hello.txt', $this->upload->data('file_name'));
  43. $this->assertEquals($data, $this->upload->data());
  44. }
  45. function test_set_upload_path()
  46. {
  47. $this->upload->set_upload_path('/tmp/');
  48. $this->assertEquals('/tmp/', $this->upload->upload_path);
  49. $this->upload->set_upload_path('/tmp');
  50. $this->assertEquals('/tmp/', $this->upload->upload_path);
  51. }
  52. function test_set_filename()
  53. {
  54. $file1 = vfsStream::newFile('hello-world.txt')->withContent('Hello world.')->at($this->_test_dir);
  55. $this->upload->file_ext = '.txt';
  56. $this->assertEquals('helloworld.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'helloworld.txt'));
  57. $this->assertEquals('hello-world1.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'hello-world.txt'));
  58. }
  59. function test_set_max_filesize()
  60. {
  61. $this->upload->set_max_filesize(100);
  62. $this->assertEquals(100, $this->upload->max_size);
  63. }
  64. function test_set_max_filename()
  65. {
  66. $this->upload->set_max_filename(100);
  67. $this->assertEquals(100, $this->upload->max_filename);
  68. }
  69. function test_set_max_width()
  70. {
  71. $this->upload->set_max_width(100);
  72. $this->assertEquals(100, $this->upload->max_width);
  73. }
  74. function test_set_max_height()
  75. {
  76. $this->upload->set_max_height(100);
  77. $this->assertEquals(100, $this->upload->max_height);
  78. }
  79. function test_set_allowed_types()
  80. {
  81. $this->upload->set_allowed_types('*');
  82. $this->assertEquals('*', $this->upload->allowed_types);
  83. $this->upload->set_allowed_types('foo|bar');
  84. $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
  85. }
  86. function test_set_image_properties()
  87. {
  88. $this->upload->file_type = 'image/gif';
  89. $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
  90. $props = array(
  91. 'image_width' => 170,
  92. 'image_height' => 73,
  93. 'image_type' => 'gif',
  94. 'image_size_str' => 'width="170" height="73"'
  95. );
  96. $this->upload->set_image_properties($this->upload->file_temp);
  97. $this->assertEquals($props['image_width'], $this->upload->image_width);
  98. $this->assertEquals($props['image_height'], $this->upload->image_height);
  99. $this->assertEquals($props['image_type'], $this->upload->image_type);
  100. $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
  101. }
  102. function test_set_xss_clean()
  103. {
  104. $this->upload->set_xss_clean(TRUE);
  105. $this->assertTrue($this->upload->xss_clean);
  106. $this->upload->set_xss_clean(FALSE);
  107. $this->assertFalse($this->upload->xss_clean);
  108. }
  109. function test_is_image()
  110. {
  111. $this->upload->file_type = 'image/x-png';
  112. $this->assertTrue($this->upload->is_image());
  113. $this->upload->file_type = 'text/plain';
  114. $this->assertFalse($this->upload->is_image());
  115. }
  116. function test_is_allowed_filetype()
  117. {
  118. $this->upload->allowed_types = array('html', 'gif');
  119. $this->upload->file_ext = '.txt';
  120. $this->upload->file_type = 'text/plain';
  121. $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
  122. $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
  123. $this->upload->file_ext = '.html';
  124. $this->upload->file_type = 'text/html';
  125. $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
  126. $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
  127. $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
  128. $this->upload->file_ext = '.gif';
  129. $this->upload->file_type = 'image/gif';
  130. $this->assertTrue($this->upload->is_allowed_filetype());
  131. }
  132. function test_is_allowed_filesize()
  133. {
  134. $this->upload->max_size = 100;
  135. $this->upload->file_size = 99;
  136. $this->assertTrue($this->upload->is_allowed_filesize());
  137. $this->upload->file_size = 101;
  138. $this->assertFalse($this->upload->is_allowed_filesize());
  139. }
  140. function test_is_allowed_dimensions()
  141. {
  142. $this->upload->file_type = 'text/plain';
  143. $this->assertTrue($this->upload->is_allowed_dimensions());
  144. $this->upload->file_type = 'image/gif';
  145. $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
  146. $this->upload->max_width = 10;
  147. $this->assertFalse($this->upload->is_allowed_dimensions());
  148. $this->upload->max_width = 170;
  149. $this->upload->max_height = 10;
  150. $this->assertFalse($this->upload->is_allowed_dimensions());
  151. $this->upload->max_height = 73;
  152. $this->assertTrue($this->upload->is_allowed_dimensions());
  153. }
  154. function test_validate_upload_path()
  155. {
  156. $this->upload->upload_path = '';
  157. $this->assertFalse($this->upload->validate_upload_path());
  158. $this->upload->upload_path = vfsStream::url('testDir');
  159. $this->assertTrue($this->upload->validate_upload_path());
  160. }
  161. function test_get_extension()
  162. {
  163. $this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
  164. $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
  165. $this->assertEquals('', $this->upload->get_extension('hello'));
  166. }
  167. function test_clean_file_name()
  168. {
  169. $this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt'));
  170. $this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt'));
  171. }
  172. function test_limit_filename_length()
  173. {
  174. $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
  175. $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
  176. }
  177. function test_do_xss_clean()
  178. {
  179. $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir);
  180. $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir);
  181. $file3 = vfsStream::newFile('file3.txt')->withContent('<script type="text/javascript">alert("Boo! said the billy goat")</script>')->at($this->_test_dir);
  182. $this->upload->file_temp = vfsStream::url('file1.txt');
  183. $this->assertTrue($this->upload->do_xss_clean());
  184. $this->upload->file_temp = vfsStream::url('file2.txt');
  185. $this->assertFalse($this->upload->do_xss_clean());
  186. $this->upload->file_temp = vfsStream::url('file3.txt');
  187. $this->assertFalse($this->upload->do_xss_clean());
  188. $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
  189. $this->assertTrue($this->upload->do_xss_clean());
  190. }
  191. function test_set_error()
  192. {
  193. $errors = array(
  194. 'An error!'
  195. );
  196. $another = 'Another error!';
  197. $this->upload->set_error($errors);
  198. $this->assertEquals($errors, $this->upload->error_msg);
  199. $errors[] = $another;
  200. $this->upload->set_error($another);
  201. $this->assertEquals($errors, $this->upload->error_msg);
  202. }
  203. function test_display_errors()
  204. {
  205. $this->upload->error_msg[] = 'Error test';
  206. $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
  207. }
  208. function test_mimes_types()
  209. {
  210. $this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
  211. $this->assertFalse($this->upload->mimes_types('foobar'));
  212. }
  213. }