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.

timelog_report_test.rb 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class TimelogReportTest < Redmine::ControllerTest
  20. tests TimelogController
  21. fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
  22. :email_addresses,
  23. :issues, :time_entries, :users, :trackers, :enumerations,
  24. :issue_statuses, :custom_fields, :custom_values,
  25. :projects_trackers, :custom_fields_trackers,
  26. :custom_fields_projects
  27. include Redmine::I18n
  28. def setup
  29. Setting.default_language = "en"
  30. end
  31. def test_report_at_project_level
  32. get :report, :params => {:project_id => 'ecookbook'}
  33. assert_response :success
  34. # query form
  35. assert_select 'form#query_form' do
  36. assert_select 'div#query_form_with_buttons.hide-when-print' do
  37. assert_select 'div#query_form_content' do
  38. assert_select 'fieldset#filters.collapsible'
  39. assert_select 'fieldset#options'
  40. end
  41. assert_select 'p.buttons'
  42. end
  43. end
  44. assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries/report'
  45. end
  46. def test_report_all_projects
  47. get :report
  48. assert_response :success
  49. assert_select 'form#query_form[action=?]', '/time_entries/report'
  50. end
  51. def test_report_all_projects_denied
  52. r = Role.anonymous
  53. r.permissions.delete(:view_time_entries)
  54. r.permissions_will_change!
  55. r.save
  56. get :report
  57. assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
  58. end
  59. def test_report_all_projects_one_criteria
  60. get :report, :params => {:columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']}
  61. assert_response :success
  62. assert_select 'tr.total td:last', :text => '8:39'
  63. assert_select 'tr td.name a[href=?]', '/projects/ecookbook', :text => 'eCookbook'
  64. end
  65. def test_report_all_time
  66. get :report, :params => {:project_id => 1, :criteria => ['project', 'issue']}
  67. assert_response :success
  68. assert_select 'tr.total td:last', :text => '162:54'
  69. end
  70. def test_report_all_time_by_day
  71. get :report, :params => {:project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'}
  72. assert_response :success
  73. assert_select 'tr.total td:last', :text => '162:54'
  74. assert_select 'th', :text => '2007-03-12'
  75. end
  76. def test_report_one_criteria
  77. get :report, :params => {:project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']}
  78. assert_response :success
  79. assert_select 'tr.total td:last', :text => '8:39'
  80. end
  81. def test_report_two_criteria
  82. get :report, :params => {:project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]}
  83. assert_response :success
  84. assert_select 'tr.total td:last', :text => '162:54'
  85. end
  86. def test_report_should_show_locked_users
  87. @request.session[:user_id] = 1
  88. user = User.find(2)
  89. user.status = User::STATUS_LOCKED
  90. user.save
  91. get :report, :params => {:project_id => 1, :columns => 'month', :criteria => ["user", "activity"]}
  92. assert_response :success
  93. assert_select 'td.name a.user.active[href=?]', '/users/1', 1, :text => 'Redmine Admin'
  94. assert_select 'td.name a.user.locked[href=?]', '/users/2', 1, :text => 'John Smith'
  95. end
  96. def test_report_custom_field_criteria_with_multiple_values_on_single_value_custom_field_should_not_fail
  97. field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
  98. entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
  99. CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
  100. CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
  101. get :report, :params => {:project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]}
  102. assert_response :success
  103. end
  104. def test_report_multiple_values_custom_fields_should_not_be_proposed
  105. TimeEntryCustomField.create!(:name => 'Single', :field_format => 'list', :possible_values => ['value1', 'value2'])
  106. TimeEntryCustomField.create!(:name => 'Multi', :field_format => 'list', :multiple => true, :possible_values => ['value1', 'value2'])
  107. get :report, :params => {:project_id => 1}
  108. assert_response :success
  109. assert_select 'select[name=?]', 'criteria[]' do
  110. assert_select 'option', :text => 'Single'
  111. assert_select 'option', :text => 'Multi', :count => 0
  112. end
  113. end
  114. def test_hidden_custom_fields_should_not_be_proposed
  115. TimeEntryCustomField.create!(name: 'shown', field_format: 'list', possible_values: ['value1', 'value2'], visible: true)
  116. TimeEntryCustomField.create!(name: 'Hidden', field_format: 'list', possible_values: ['value1', 'value2'], visible: false, role_ids: [3])
  117. get :report, :params => {:project_id => 1}
  118. assert_response :success
  119. assert_select 'select[name=?]', 'criteria[]' do
  120. assert_select 'option', :text => 'Shown'
  121. assert_select 'option', :text => 'Hidden', :count => 0
  122. end
  123. end
  124. def test_report_one_day
  125. get :report, :params => {:project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]}
  126. assert_response :success
  127. assert_select 'tr.total td:last', :text => '4:15'
  128. end
  129. def test_report_by_week_should_use_commercial_year
  130. TimeEntry.delete_all
  131. TimeEntry.generate!(:hours => '2', :spent_on => '2009-12-25') # 2009-52
  132. TimeEntry.generate!(:hours => '4', :spent_on => '2009-12-31') # 2009-53
  133. TimeEntry.generate!(:hours => '8', :spent_on => '2010-01-01') # 2009-53
  134. TimeEntry.generate!(:hours => '16', :spent_on => '2010-01-05') # 2010-1
  135. get :report, :params => {:columns => 'week', :from => "2009-12-25", :to => "2010-01-05", :criteria => ["project"]}
  136. assert_response :success
  137. assert_select '#time-report thead tr' do
  138. assert_select 'th:nth-child(1)', :text => 'Project'
  139. assert_select 'th:nth-child(2)', :text => '2009-52'
  140. assert_select 'th:nth-child(3)', :text => '2009-53'
  141. assert_select 'th:nth-child(4)', :text => '2010-1'
  142. assert_select 'th:nth-child(5)', :text => 'Total time'
  143. end
  144. assert_select '#time-report tbody tr' do
  145. assert_select 'td:nth-child(1)', :text => 'eCookbook'
  146. assert_select 'td:nth-child(2)', :text => '2:00'
  147. assert_select 'td:nth-child(3)', :text => '12:00'
  148. assert_select 'td:nth-child(4)', :text => '16:00'
  149. assert_select 'td:nth-child(5)', :text => '30:00' # Total
  150. end
  151. end
  152. def test_report_should_propose_association_custom_fields
  153. get :report
  154. assert_response :success
  155. assert_select 'select[name=?]', 'criteria[]' do
  156. assert_select 'option[value=cf_1]', {:text => 'Database'}, 'Issue custom field not found'
  157. assert_select 'option[value=cf_3]', {:text => 'Development status'}, 'Project custom field not found'
  158. assert_select 'option[value=cf_7]', {:text => 'Billable'}, 'TimeEntryActivity custom field not found'
  159. end
  160. end
  161. def test_report_with_association_custom_fields
  162. get :report, :params => {:criteria => ['cf_1', 'cf_3', 'cf_7']}
  163. assert_response :success
  164. assert_select 'tr.total td:last', :text => '162:54'
  165. # Custom fields columns
  166. assert_select 'th', :text => 'Database'
  167. assert_select 'th', :text => 'Development status'
  168. assert_select 'th', :text => 'Billable'
  169. # Custom field row
  170. assert_select 'tr' do
  171. assert_select 'td', :text => 'MySQL'
  172. assert_select 'td.hours', :text => '1:00'
  173. end
  174. end
  175. def test_report_one_criteria_no_result
  176. get :report, :params => {:project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']}
  177. assert_response :success
  178. assert_select '.nodata'
  179. end
  180. def test_report_status_criterion
  181. get :report, :params => {:project_id => 1, :criteria => ['status']}
  182. assert_response :success
  183. assert_select 'th', :text => 'Status'
  184. assert_select 'td', :text => 'New'
  185. end
  186. def test_report_all_projects_csv_export
  187. get :report, :params => {
  188. :columns => 'month',
  189. :from => "2007-01-01",
  190. :to => "2007-06-30",
  191. :criteria => ["project", "user", "activity"],
  192. :format => "csv"
  193. }
  194. assert_response :success
  195. assert_equal 'text/csv; header=present', @response.media_type
  196. lines = @response.body.chomp.split("\n")
  197. # Headers
  198. assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
  199. # Total row
  200. assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
  201. end
  202. def test_report_csv_export
  203. get :report, :params => {
  204. :project_id => 1,
  205. :columns => 'month',
  206. :from => "2007-01-01",
  207. :to => "2007-06-30",
  208. :criteria => ["project", "user", "cf_10"],
  209. :format => "csv"
  210. }
  211. assert_response :success
  212. assert_equal 'text/csv; header=present', @response.media_type
  213. lines = @response.body.chomp.split("\n")
  214. # Headers
  215. assert_equal 'Project,User,Overtime,2007-3,2007-4,Total time', lines.first
  216. # Total row
  217. assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
  218. end
  219. def test_report_csv_should_fill_issue_criteria_with_tracker_id_and_subject
  220. get :report, :params => {
  221. :project_id => 1,
  222. :columns => 'month',
  223. :from => "2007-01-01",
  224. :to => "2007-06-30",
  225. :criteria => ["issue"],
  226. :format => "csv"
  227. }
  228. assert_response :success
  229. lines = @response.body.chomp.split("\n")
  230. assert lines.detect {|line| line.include?('Bug #1: Cannot print recipes')}
  231. end
  232. def test_csv_big_5
  233. str_big5 = (+"\xa4@\xa4\xeb").force_encoding('Big5')
  234. user = User.find_by_id(3)
  235. user.firstname = "一月"
  236. user.lastname = "test-lastname"
  237. assert user.save
  238. comments = "test_csv_big_5"
  239. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  240. :hours => 7.3,
  241. :project => Project.find(1),
  242. :user => user,
  243. :activity => TimeEntryActivity.find_by_name('Design'),
  244. :comments => comments)
  245. te2 = TimeEntry.find_by_comments(comments)
  246. assert_not_nil te2
  247. assert_equal 7.3, te2.hours
  248. assert_equal 3, te2.user_id
  249. with_settings :default_language => "zh-TW" do
  250. get :report, :params => {
  251. :project_id => 1,
  252. :columns => 'day',
  253. :from => "2011-11-11",
  254. :to => "2011-11-11",
  255. :criteria => ["user"],
  256. :format => "csv"
  257. }
  258. end
  259. assert_response :success
  260. assert_equal 'text/csv; header=present', @response.media_type
  261. lines = @response.body.chomp.split("\n")
  262. # Headers
  263. s1 = (+"\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
  264. s2 = (+"\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
  265. assert_equal s1, lines.first
  266. # Total row
  267. assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
  268. assert_equal "#{s2},7.30,7.30", lines[2]
  269. assert_equal 'Chinese/Traditional (繁體中文)', l(:general_lang_name)
  270. assert_equal 'Big5', l(:general_csv_encoding)
  271. assert_equal ',', l(:general_csv_separator)
  272. assert_equal '.', l(:general_csv_decimal_separator)
  273. end
  274. def test_csv_cannot_convert_should_be_replaced_big_5
  275. user = User.find_by_id(3)
  276. user.firstname = "以内"
  277. user.lastname = "test-lastname"
  278. assert user.save
  279. comments = "test_replaced"
  280. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  281. :hours => 7.3,
  282. :project => Project.find(1),
  283. :user => user,
  284. :activity => TimeEntryActivity.find_by_name('Design'),
  285. :comments => comments)
  286. te2 = TimeEntry.find_by_comments(comments)
  287. assert_not_nil te2
  288. assert_equal 7.3, te2.hours
  289. assert_equal 3, te2.user_id
  290. with_settings :default_language => "zh-TW" do
  291. get :report, :params => {
  292. :project_id => 1,
  293. :columns => 'day',
  294. :from => "2011-11-11",
  295. :to => "2011-11-11",
  296. :criteria => ["user"],
  297. :format => "csv"
  298. }
  299. end
  300. assert_response :success
  301. assert_equal 'text/csv; header=present', @response.media_type
  302. lines = @response.body.chomp.split("\n")
  303. # Headers
  304. s1 = (+"\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
  305. assert_equal s1, lines.first
  306. # Total row
  307. s2 = (+"\xa5H?").force_encoding('Big5')
  308. assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
  309. end
  310. def test_csv_fr
  311. with_settings :default_language => "fr" do
  312. str1 = "test_csv_fr"
  313. user = User.find_by_id(3)
  314. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  315. :hours => 7.3,
  316. :project => Project.find(1),
  317. :user => user,
  318. :activity => TimeEntryActivity.find_by_name('Design'),
  319. :comments => str1)
  320. te2 = TimeEntry.find_by_comments(str1)
  321. assert_not_nil te2
  322. assert_equal 7.3, te2.hours
  323. assert_equal 3, te2.user_id
  324. get :report, :params => {
  325. :project_id => 1,
  326. :columns => 'day',
  327. :from => "2011-11-11",
  328. :to => "2011-11-11",
  329. :criteria => ["user"],
  330. :format => "csv"
  331. }
  332. assert_response :success
  333. assert_equal 'text/csv; header=present', @response.media_type
  334. lines = @response.body.chomp.split("\n")
  335. # Headers
  336. s1 = (+"Utilisateur;2011-11-11;Temps total").force_encoding('ISO-8859-1')
  337. s2 = (+"Temps total").force_encoding('ISO-8859-1')
  338. assert_equal s1, lines.first
  339. # Total row
  340. assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
  341. assert_equal "#{s2};7,30;7,30", lines[2]
  342. assert_equal 'French (Français)', l(:general_lang_name)
  343. assert_equal 'ISO-8859-1', l(:general_csv_encoding)
  344. assert_equal ';', l(:general_csv_separator)
  345. assert_equal ',', l(:general_csv_decimal_separator)
  346. end
  347. end
  348. end