You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time_entry_reports_controller_test.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # -*- coding: utf-8 -*-
  2. require File.expand_path('../../test_helper', __FILE__)
  3. class TimeEntryReportsControllerTest < ActionController::TestCase
  4. tests TimelogController
  5. fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
  6. :issues, :time_entries, :users, :trackers, :enumerations,
  7. :issue_statuses, :custom_fields, :custom_values
  8. include Redmine::I18n
  9. def setup
  10. Setting.default_language = "en"
  11. end
  12. def test_report_at_project_level
  13. get :report, :project_id => 'ecookbook'
  14. assert_response :success
  15. assert_template 'report'
  16. assert_tag :form,
  17. :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
  18. end
  19. def test_report_all_projects
  20. get :report
  21. assert_response :success
  22. assert_template 'report'
  23. assert_tag :form,
  24. :attributes => {:action => "/time_entries/report", :id => 'query_form'}
  25. end
  26. def test_report_all_projects_denied
  27. r = Role.anonymous
  28. r.permissions.delete(:view_time_entries)
  29. r.permissions_will_change!
  30. r.save
  31. get :report
  32. assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
  33. end
  34. def test_report_all_projects_one_criteria
  35. get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
  36. assert_response :success
  37. assert_template 'report'
  38. assert_not_nil assigns(:report)
  39. assert_equal "8.65", "%.2f" % assigns(:report).total_hours
  40. end
  41. def test_report_all_time
  42. get :report, :project_id => 1, :criteria => ['project', 'issue']
  43. assert_response :success
  44. assert_template 'report'
  45. assert_not_nil assigns(:report)
  46. assert_equal "162.90", "%.2f" % assigns(:report).total_hours
  47. end
  48. def test_report_all_time_by_day
  49. get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
  50. assert_response :success
  51. assert_template 'report'
  52. assert_not_nil assigns(:report)
  53. assert_equal "162.90", "%.2f" % assigns(:report).total_hours
  54. assert_tag :tag => 'th', :content => '2007-03-12'
  55. end
  56. def test_report_one_criteria
  57. get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
  58. assert_response :success
  59. assert_template 'report'
  60. assert_not_nil assigns(:report)
  61. assert_equal "8.65", "%.2f" % assigns(:report).total_hours
  62. end
  63. def test_report_two_criteria
  64. get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
  65. assert_response :success
  66. assert_template 'report'
  67. assert_not_nil assigns(:report)
  68. assert_equal "162.90", "%.2f" % assigns(:report).total_hours
  69. end
  70. def test_report_one_day
  71. get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["member", "activity"]
  72. assert_response :success
  73. assert_template 'report'
  74. assert_not_nil assigns(:report)
  75. assert_equal "4.25", "%.2f" % assigns(:report).total_hours
  76. end
  77. def test_report_at_issue_level
  78. get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
  79. assert_response :success
  80. assert_template 'report'
  81. assert_not_nil assigns(:report)
  82. assert_equal "154.25", "%.2f" % assigns(:report).total_hours
  83. assert_tag :form,
  84. :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
  85. end
  86. def test_report_custom_field_criteria
  87. get :report, :project_id => 1, :criteria => ['project', 'cf_1', 'cf_7']
  88. assert_response :success
  89. assert_template 'report'
  90. assert_not_nil assigns(:report)
  91. assert_equal 3, assigns(:report).criteria.size
  92. assert_equal "162.90", "%.2f" % assigns(:report).total_hours
  93. # Custom field column
  94. assert_tag :tag => 'th', :content => 'Database'
  95. # Custom field row
  96. assert_tag :tag => 'td', :content => 'MySQL',
  97. :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
  98. :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
  99. :content => '1' }}
  100. # Second custom field column
  101. assert_tag :tag => 'th', :content => 'Billable'
  102. end
  103. def test_report_one_criteria_no_result
  104. get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
  105. assert_response :success
  106. assert_template 'report'
  107. assert_not_nil assigns(:report)
  108. assert_equal "0.00", "%.2f" % assigns(:report).total_hours
  109. end
  110. def test_report_status_criterion
  111. get :report, :project_id => 1, :criteria => ['status']
  112. assert_response :success
  113. assert_template 'report'
  114. assert_tag :tag => 'th', :content => 'Status'
  115. assert_tag :tag => 'td', :content => 'New'
  116. end
  117. def test_report_all_projects_csv_export
  118. get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
  119. :criteria => ["project", "member", "activity"], :format => "csv"
  120. assert_response :success
  121. assert_equal 'text/csv; header=present', @response.content_type
  122. lines = @response.body.chomp.split("\n")
  123. # Headers
  124. assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
  125. lines.first
  126. # Total row
  127. assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
  128. end
  129. def test_report_csv_export
  130. get :report, :project_id => 1, :columns => 'month',
  131. :from => "2007-01-01", :to => "2007-06-30",
  132. :criteria => ["project", "member", "activity"], :format => "csv"
  133. assert_response :success
  134. assert_equal 'text/csv; header=present', @response.content_type
  135. lines = @response.body.chomp.split("\n")
  136. # Headers
  137. assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
  138. lines.first
  139. # Total row
  140. assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
  141. end
  142. def test_csv_big_5
  143. Setting.default_language = "zh-TW"
  144. str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
  145. str_big5 = "\xa4@\xa4\xeb"
  146. if str_utf8.respond_to?(:force_encoding)
  147. str_utf8.force_encoding('UTF-8')
  148. str_big5.force_encoding('Big5')
  149. end
  150. user = User.find_by_id(3)
  151. user.firstname = str_utf8
  152. user.lastname = "test-lastname"
  153. assert user.save
  154. comments = "test_csv_big_5"
  155. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  156. :hours => 7.3,
  157. :project => Project.find(1),
  158. :user => user,
  159. :activity => TimeEntryActivity.find_by_name('Design'),
  160. :comments => comments)
  161. te2 = TimeEntry.find_by_comments(comments)
  162. assert_not_nil te2
  163. assert_equal 7.3, te2.hours
  164. assert_equal 3, te2.user_id
  165. get :report, :project_id => 1, :columns => 'day',
  166. :from => "2011-11-11", :to => "2011-11-11",
  167. :criteria => ["member"], :format => "csv"
  168. assert_response :success
  169. assert_equal 'text/csv; header=present', @response.content_type
  170. lines = @response.body.chomp.split("\n")
  171. # Headers
  172. s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
  173. s2 = "\xc1`\xadp"
  174. if s1.respond_to?(:force_encoding)
  175. s1.force_encoding('Big5')
  176. s2.force_encoding('Big5')
  177. end
  178. assert_equal s1, lines.first
  179. # Total row
  180. assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
  181. assert_equal "#{s2},7.30,7.30", lines[2]
  182. str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
  183. if str_tw.respond_to?(:force_encoding)
  184. str_tw.force_encoding('UTF-8')
  185. end
  186. assert_equal str_tw, l(:general_lang_name)
  187. assert_equal 'Big5', l(:general_csv_encoding)
  188. assert_equal ',', l(:general_csv_separator)
  189. assert_equal '.', l(:general_csv_decimal_separator)
  190. end
  191. def test_csv_cannot_convert_should_be_replaced_big_5
  192. Setting.default_language = "zh-TW"
  193. str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
  194. if str_utf8.respond_to?(:force_encoding)
  195. str_utf8.force_encoding('UTF-8')
  196. end
  197. user = User.find_by_id(3)
  198. user.firstname = str_utf8
  199. user.lastname = "test-lastname"
  200. assert user.save
  201. comments = "test_replaced"
  202. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  203. :hours => 7.3,
  204. :project => Project.find(1),
  205. :user => user,
  206. :activity => TimeEntryActivity.find_by_name('Design'),
  207. :comments => comments)
  208. te2 = TimeEntry.find_by_comments(comments)
  209. assert_not_nil te2
  210. assert_equal 7.3, te2.hours
  211. assert_equal 3, te2.user_id
  212. get :report, :project_id => 1, :columns => 'day',
  213. :from => "2011-11-11", :to => "2011-11-11",
  214. :criteria => ["member"], :format => "csv"
  215. assert_response :success
  216. assert_equal 'text/csv; header=present', @response.content_type
  217. lines = @response.body.chomp.split("\n")
  218. # Headers
  219. s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
  220. if s1.respond_to?(:force_encoding)
  221. s1.force_encoding('Big5')
  222. end
  223. assert_equal s1, lines.first
  224. # Total row
  225. s2 = ""
  226. if s2.respond_to?(:force_encoding)
  227. s2 = "\xa5H?"
  228. s2.force_encoding('Big5')
  229. elsif RUBY_PLATFORM == 'java'
  230. s2 = "??"
  231. else
  232. s2 = "\xa5H???"
  233. end
  234. assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
  235. end
  236. def test_csv_fr
  237. with_settings :default_language => "fr" do
  238. str1 = "test_csv_fr"
  239. user = User.find_by_id(3)
  240. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  241. :hours => 7.3,
  242. :project => Project.find(1),
  243. :user => user,
  244. :activity => TimeEntryActivity.find_by_name('Design'),
  245. :comments => str1)
  246. te2 = TimeEntry.find_by_comments(str1)
  247. assert_not_nil te2
  248. assert_equal 7.3, te2.hours
  249. assert_equal 3, te2.user_id
  250. get :report, :project_id => 1, :columns => 'day',
  251. :from => "2011-11-11", :to => "2011-11-11",
  252. :criteria => ["member"], :format => "csv"
  253. assert_response :success
  254. assert_equal 'text/csv; header=present', @response.content_type
  255. lines = @response.body.chomp.split("\n")
  256. # Headers
  257. s1 = "Membre;2011-11-11;Total"
  258. s2 = "Total"
  259. if s1.respond_to?(:force_encoding)
  260. s1.force_encoding('ISO-8859-1')
  261. s2.force_encoding('ISO-8859-1')
  262. end
  263. assert_equal s1, lines.first
  264. # Total row
  265. assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
  266. assert_equal "#{s2};7,30;7,30", lines[2]
  267. str_fr = "Fran\xc3\xa7ais"
  268. if str_fr.respond_to?(:force_encoding)
  269. str_fr.force_encoding('UTF-8')
  270. end
  271. assert_equal str_fr, l(:general_lang_name)
  272. assert_equal 'ISO-8859-1', l(:general_csv_encoding)
  273. assert_equal ';', l(:general_csv_separator)
  274. assert_equal ',', l(:general_csv_decimal_separator)
  275. end
  276. end
  277. end