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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. # -*- coding: utf-8 -*-
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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.65'
  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.90'
  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.90'
  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.65'
  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.90'
  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)
  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.25'
  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.90'
  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.content_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", "activity"],
  209. :format => "csv"
  210. }
  211. assert_response :success
  212. assert_equal 'text/csv; header=present', @response.content_type
  213. lines = @response.body.chomp.split("\n")
  214. # Headers
  215. assert_equal 'Project,User,Activity,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_csv_big_5
  220. str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88".force_encoding('UTF-8')
  221. str_big5 = "\xa4@\xa4\xeb".force_encoding('Big5')
  222. user = User.find_by_id(3)
  223. user.firstname = str_utf8
  224. user.lastname = "test-lastname"
  225. assert user.save
  226. comments = "test_csv_big_5"
  227. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  228. :hours => 7.3,
  229. :project => Project.find(1),
  230. :user => user,
  231. :activity => TimeEntryActivity.find_by_name('Design'),
  232. :comments => comments)
  233. te2 = TimeEntry.find_by_comments(comments)
  234. assert_not_nil te2
  235. assert_equal 7.3, te2.hours
  236. assert_equal 3, te2.user_id
  237. with_settings :default_language => "zh-TW" do
  238. get :report, :params => {
  239. :project_id => 1,
  240. :columns => 'day',
  241. :from => "2011-11-11",
  242. :to => "2011-11-11",
  243. :criteria => ["user"],
  244. :format => "csv"
  245. }
  246. end
  247. assert_response :success
  248. assert_equal 'text/csv; header=present', @response.content_type
  249. lines = @response.body.chomp.split("\n")
  250. # Headers
  251. s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp".force_encoding('Big5')
  252. s2 = "\xa4u\xae\xc9\xc1`\xadp".force_encoding('Big5')
  253. assert_equal s1, lines.first
  254. # Total row
  255. assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
  256. assert_equal "#{s2},7.30,7.30", lines[2]
  257. str_tw = "Chinese/Traditional (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)".force_encoding('UTF-8')
  258. assert_equal str_tw, l(:general_lang_name)
  259. assert_equal 'Big5', l(:general_csv_encoding)
  260. assert_equal ',', l(:general_csv_separator)
  261. assert_equal '.', l(:general_csv_decimal_separator)
  262. end
  263. def test_csv_cannot_convert_should_be_replaced_big_5
  264. str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85".force_encoding('UTF-8')
  265. user = User.find_by_id(3)
  266. user.firstname = str_utf8
  267. user.lastname = "test-lastname"
  268. assert user.save
  269. comments = "test_replaced"
  270. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  271. :hours => 7.3,
  272. :project => Project.find(1),
  273. :user => user,
  274. :activity => TimeEntryActivity.find_by_name('Design'),
  275. :comments => comments)
  276. te2 = TimeEntry.find_by_comments(comments)
  277. assert_not_nil te2
  278. assert_equal 7.3, te2.hours
  279. assert_equal 3, te2.user_id
  280. with_settings :default_language => "zh-TW" do
  281. get :report, :params => {
  282. :project_id => 1,
  283. :columns => 'day',
  284. :from => "2011-11-11",
  285. :to => "2011-11-11",
  286. :criteria => ["user"],
  287. :format => "csv"
  288. }
  289. end
  290. assert_response :success
  291. assert_equal 'text/csv; header=present', @response.content_type
  292. lines = @response.body.chomp.split("\n")
  293. # Headers
  294. s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp".force_encoding('Big5')
  295. assert_equal s1, lines.first
  296. # Total row
  297. s2 = "\xa5H?".force_encoding('Big5')
  298. assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
  299. end
  300. def test_csv_fr
  301. with_settings :default_language => "fr" do
  302. str1 = "test_csv_fr"
  303. user = User.find_by_id(3)
  304. te1 = TimeEntry.create(:spent_on => '2011-11-11',
  305. :hours => 7.3,
  306. :project => Project.find(1),
  307. :user => user,
  308. :activity => TimeEntryActivity.find_by_name('Design'),
  309. :comments => str1)
  310. te2 = TimeEntry.find_by_comments(str1)
  311. assert_not_nil te2
  312. assert_equal 7.3, te2.hours
  313. assert_equal 3, te2.user_id
  314. get :report, :params => {
  315. :project_id => 1,
  316. :columns => 'day',
  317. :from => "2011-11-11",
  318. :to => "2011-11-11",
  319. :criteria => ["user"],
  320. :format => "csv"
  321. }
  322. assert_response :success
  323. assert_equal 'text/csv; header=present', @response.content_type
  324. lines = @response.body.chomp.split("\n")
  325. # Headers
  326. s1 = "Utilisateur;2011-11-11;Temps total".force_encoding('ISO-8859-1')
  327. s2 = "Temps total".force_encoding('ISO-8859-1')
  328. assert_equal s1, lines.first
  329. # Total row
  330. assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
  331. assert_equal "#{s2};7,30;7,30", lines[2]
  332. str_fr = "French (Fran\xc3\xa7ais)".force_encoding('UTF-8')
  333. assert_equal str_fr, l(:general_lang_name)
  334. assert_equal 'ISO-8859-1', l(:general_csv_encoding)
  335. assert_equal ';', l(:general_csv_separator)
  336. assert_equal ',', l(:general_csv_decimal_separator)
  337. end
  338. end
  339. end