PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/codeigniter/libraries/Upload_test.php

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