PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_el_finder.rb

http://github.com/phallstrom/el_finder
Ruby | 282 lines | 229 code | 47 blank | 6 comment | 13 complexity | 1c25379a721d10b5e8413ce8f54e14bc MD5 | raw file
  1. require 'el_finder_test_case'
  2. class TestElFinder < Test::Unit::TestCase
  3. include ElFinderTestCase
  4. ################################################################################
  5. def test_should_fail_initialization_if_required_options_not_passed
  6. assert_raise ArgumentError do
  7. ElFinder::Connector.new()
  8. end
  9. end
  10. def test_should_fail_initialization_if_no_root_specified
  11. assert_raise ArgumentError do
  12. ElFinder::Connector.new({:url => '/elfinder'})
  13. end
  14. end
  15. def test_should_fail_initialization_if_no_url_specified
  16. assert_raise ArgumentError do
  17. ElFinder::Connector.new({:root => '/tmp/elfinder'})
  18. end
  19. end
  20. def test_should_fail_initialization_if_mime_handler_is_invalid
  21. assert_raise ArgumentError do
  22. ElFinder::Connector.new({:root => '/tmp/elfinder', :url => '/elfinder', :mime_handler => Object})
  23. end
  24. end
  25. ################################################################################
  26. def test_should_return_two_hashes
  27. h, r = @elfinder.run({})
  28. assert_instance_of Hash, h
  29. assert_instance_of Hash, r
  30. end
  31. def test_should_return_invalid_request_if_command_is_invalid
  32. h, r = @elfinder.run({:cmd => 'INVALID'})
  33. assert_not_nil r[:error]
  34. assert_match(/invalid command/i, r[:error])
  35. end
  36. ################################################################################
  37. def test_init_via_open
  38. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  39. assert_not_nil r[:cwd]
  40. assert_not_nil r[:cdc]
  41. assert_not_nil r[:disabled]
  42. assert_not_nil r[:params]
  43. r[:cdc].each do |e|
  44. case e[:name]
  45. when 'foo'
  46. assert_nil e[:dim]
  47. assert_nil e[:resize]
  48. assert_equal 'directory', e[:mime]
  49. assert_equal 0, e[:size]
  50. when 'pjkh.png'
  51. assert_equal '100x100', e[:dim]
  52. assert e[:resize]
  53. assert_equal 'image/png', e[:mime]
  54. assert_equal 1142, e[:size]
  55. end
  56. end
  57. end
  58. def test_cdc_is_sorted
  59. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  60. assert_equal 'elfinder.png', r[:cdc].first[:name]
  61. assert_equal 'sample.zip', r[:cdc].last[:name]
  62. end
  63. # by default we list the subfolders in the tree
  64. def test_list_nested_items
  65. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '', :tree => true)
  66. assert_equal 1, r[:tree][:dirs].count
  67. end
  68. # trun off loading the subfolders in the tree
  69. def test_list_root_items
  70. elfinder = ElFinder::Connector.new({
  71. :root => @vroot,
  72. :url => '/elfinder',
  73. :tree_sub_folders => false,
  74. :original_filename_method => lambda {|file| File.basename(file.path)}
  75. })
  76. h, r = elfinder.run(:cmd => 'open', :init => 'true', :target => '', :tree => true)
  77. assert_equal 0, r[:tree][:dirs].count
  78. end
  79. def test_cwd_name_for_root
  80. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  81. assert r[:cwd][:name], 'Home'
  82. end
  83. def test_cwd_name_for_sub_directory
  84. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  85. target = r[:cdc].find{|e| e[:name] == 'foo'}
  86. h, r = @elfinder.run(:cmd => 'open', :target => target[:hash])
  87. assert r[:cwd][:name], 'Home/foo'
  88. end
  89. def test_mkdir
  90. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  91. h1, r1 = @elfinder.run(:cmd => 'mkdir', :current => r[:cwd][:hash], :name => 'dir1')
  92. assert File.directory?(File.join(@vroot, 'dir1'))
  93. assert_not_nil r1[:select]
  94. h1, r1 = @elfinder.run(:cmd => 'mkdir', :current => r[:cwd][:hash], :name => 'dir1')
  95. assert_match(/unable/i, r1[:error])
  96. h1, r1 = @elfinder.run(:cmd => 'mkdir', :current => r[:cwd][:hash], :name => 'foo')
  97. assert_match(/unable/i, r1[:error])
  98. end
  99. def test_mkfile
  100. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  101. h1, r1 = @elfinder.run(:cmd => 'mkfile', :current => r[:cwd][:hash], :name => 'file1')
  102. assert File.file?(File.join(@vroot, 'file1'))
  103. assert_not_nil r1[:select]
  104. h1, r1 = @elfinder.run(:cmd => 'mkfile', :current => r[:cwd][:hash], :name => 'file1')
  105. assert_match(/unable/i, r1[:error])
  106. h1, r1 = @elfinder.run(:cmd => 'mkfile', :current => r[:cwd][:hash], :name => 'README.txt')
  107. assert_match(/unable/i, r1[:error])
  108. end
  109. def test_rename_ok
  110. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  111. target = r[:cdc].find{|e| e[:name] == 'README.txt'}
  112. h1, r1 = @elfinder.run(:cmd => 'rename', :target => target[:hash], :current => r[:cwd][:hash], :name => 'file1')
  113. assert File.file?(File.join(@vroot, 'file1'))
  114. assert_not_nil r1[:select]
  115. end
  116. def test_rename_fail
  117. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  118. target = r[:cdc].find{|e| e[:name] == 'README.txt'}
  119. h1, r1 = @elfinder.run(:cmd => 'rename', :target => target[:hash], :current => r[:cwd][:hash], :name => 'foo')
  120. assert_match(/unable.*already exists/i, r1[:error])
  121. assert File.file?(File.join(@vroot, 'README.txt'))
  122. assert_nil r1[:select]
  123. end
  124. def test_upload
  125. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  126. uploads = []
  127. uploads << File.open(File.join(@vroot, 'foo/philip.txt'))
  128. uploads << File.open(File.join(@vroot, 'foo/sandy.txt'))
  129. h, r = @elfinder.run(:cmd => 'upload', :upload => uploads, :current => r[:cwd][:hash])
  130. assert File.exist?(File.join(@vroot, 'philip.txt'))
  131. assert File.exist?(File.join(@vroot, 'sandy.txt'))
  132. assert_not_nil r[:select]
  133. end
  134. def test_upload_too_big
  135. @elfinder.options = {:upload_max_size => 5}
  136. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  137. uploads = []
  138. uploads << File.open(File.join(@vroot, 'foo/philip.txt'))
  139. uploads << File.open(File.join(@vroot, 'foo/sam.txt'))
  140. h, r = @elfinder.run(:cmd => 'upload', :upload => uploads, :current => r[:cwd][:hash])
  141. assert !File.exist?(File.join(@vroot, 'philip.txt'))
  142. assert File.exist?(File.join(@vroot, 'sam.txt'))
  143. assert_not_nil r[:select]
  144. assert_match(/some files were not uploaded/i, r[:error])
  145. end
  146. def test_ping
  147. h, r = @elfinder.run(:cmd => 'ping')
  148. assert r.empty?
  149. assert_equal 'Close', h['Connection']
  150. end
  151. def test_paste_copy
  152. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  153. targets = r[:cdc].select{|e| e[:mime] != 'directory'}
  154. dst = r[:cdc].find{|e| e[:name] == 'foo'}
  155. h, r = @elfinder.run(:cmd => 'paste', :targets => targets.map{|e| e[:hash]}, :dst => dst[:hash])
  156. assert_not_nil r[:tree]
  157. assert File.exist?(File.join(@vroot, 'README.txt'))
  158. assert File.exist?(File.join(@vroot, 'pjkh.png'))
  159. assert File.exist?(File.join(@vroot, 'elfinder.png'))
  160. assert File.exist?(File.join(@vroot, 'foo', 'README.txt'))
  161. assert File.exist?(File.join(@vroot, 'foo', 'pjkh.png'))
  162. assert File.exist?(File.join(@vroot, 'foo', 'elfinder.png'))
  163. end
  164. def test_paste_cut
  165. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  166. targets = r[:cdc].select{|e| e[:mime] != 'directory'}
  167. dst = r[:cdc].find{|e| e[:name] == 'foo'}
  168. h, r = @elfinder.run(:cmd => 'paste', :targets => targets.map{|e| e[:hash]}, :dst => dst[:hash], :cut => '1')
  169. assert_not_nil r[:tree]
  170. assert !File.exist?(File.join(@vroot, 'README.txt'))
  171. assert !File.exist?(File.join(@vroot, 'pjkh.png'))
  172. assert !File.exist?(File.join(@vroot, 'elfinder.png'))
  173. assert File.exist?(File.join(@vroot, 'foo', 'README.txt'))
  174. assert File.exist?(File.join(@vroot, 'foo', 'pjkh.png'))
  175. assert File.exist?(File.join(@vroot, 'foo', 'elfinder.png'))
  176. end
  177. def test_paste_partial_failure
  178. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  179. h, r = @elfinder.run(:cmd => 'mkfile', :current => r[:cwd][:hash], :name => 'philip.txt')
  180. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  181. targets = r[:cdc].select{|e| e[:mime] != 'directory'}
  182. dst = r[:cdc].find{|e| e[:name] == 'foo'}
  183. h, r = @elfinder.run(:cmd => 'paste', :targets => targets.map{|e| e[:hash]}, :dst => dst[:hash])
  184. assert_not_nil r[:tree]
  185. assert_match(/unable to be copied/i, r[:error])
  186. assert_not_nil r[:errorData]
  187. assert_equal 1, r[:errorData].size
  188. assert File.exist?(File.join(@vroot, 'philip.txt'))
  189. assert File.exist?(File.join(@vroot, 'foo', 'philip.txt'))
  190. assert File.exist?(File.join(@vroot, 'foo', 'README.txt'))
  191. assert File.exist?(File.join(@vroot, 'foo', 'pjkh.png'))
  192. assert File.exist?(File.join(@vroot, 'foo', 'elfinder.png'))
  193. end
  194. def test_rm
  195. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  196. h, r1 = @elfinder.run(:cmd => 'rm', :current => r[:cwd][:hash], :targets => [])
  197. assert_match(/no files/i, r1[:error])
  198. h, r = @elfinder.run(:cmd => 'rm', :targets => r[:cdc].reject{|e| e[:mime] =~ /image/}.map{|e| e[:hash]})
  199. assert !File.exist?(File.join(@vroot, 'README.txt'))
  200. assert File.exist?(File.join(@vroot, 'pjkh.png'))
  201. assert File.exist?(File.join(@vroot, 'elfinder.png'))
  202. assert !File.exist?(File.join(@vroot, 'foo'))
  203. end
  204. def test_duplicate
  205. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  206. duplicate = r[:cdc].find{|e| e[:name] == 'README.txt'}
  207. assert File.exist?(File.join(@vroot, 'README.txt'))
  208. h, r = @elfinder.run(:cmd => 'duplicate', :target => duplicate[:hash])
  209. assert File.exist?(File.join(@vroot, 'README copy 1.txt'))
  210. assert_not_nil r[:select]
  211. h, r = @elfinder.run(:cmd => 'duplicate', :target => duplicate[:hash])
  212. assert File.exist?(File.join(@vroot, 'README copy 2.txt'))
  213. end
  214. def test_read
  215. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  216. file = r[:cdc].find{|e| e[:name] == 'README.txt'}
  217. h, r = @elfinder.run(:cmd => 'read', :target => file[:hash])
  218. assert_equal r[:content], File.read(File.join(@vroot, 'README.txt'))
  219. end
  220. def test_edit
  221. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  222. file = r[:cdc].find{|e| e[:name] == 'README.txt'}
  223. h, r = @elfinder.run(:cmd => 'edit', :target => file[:hash], :content => 'Hello')
  224. assert_equal 'Hello', File.read(File.join(@vroot, 'README.txt'))
  225. assert_not_nil r[:file]
  226. end
  227. def test_resize
  228. h, r = @elfinder.run(:cmd => 'open', :init => 'true', :target => '')
  229. file = r[:cdc].find{|e| e[:name] == 'pjkh.png'}
  230. h, r = @elfinder.run(:cmd => 'resize', :target => file[:hash], :current => r[:cwd][:hash], :width => '50', :height => '25')
  231. assert File.exist?(File.join(@vroot, 'pjkh.png'))
  232. assert_equal '50x25', ElFinder::Image.size(File.join(@vroot, 'pjkh.png')).to_s
  233. end
  234. ################################################################################
  235. end