summaryrefslogtreecommitdiffstats
path: root/test/functional/time_entry_reports_controller_test.rb
blob: 05c42d52782aa4e08a9deb059eed1ff5e0afdc42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
# Redmine - project management software
# Copyright (C) 2006-2012  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class TimeEntryReportsControllerTest < ActionController::TestCase
  tests TimelogController

  fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
           :issues, :time_entries, :users, :trackers, :enumerations,
           :issue_statuses, :custom_fields, :custom_values

  include Redmine::I18n

  def setup
    Setting.default_language = "en"
  end

  def test_report_at_project_level
    get :report, :project_id => 'ecookbook'
    assert_response :success
    assert_template 'report'
    assert_tag :form,
      :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
  end

  def test_report_all_projects
    get :report
    assert_response :success
    assert_template 'report'
    assert_tag :form,
      :attributes => {:action => "/time_entries/report", :id => 'query_form'}
  end

  def test_report_all_projects_denied
    r = Role.anonymous
    r.permissions.delete(:view_time_entries)
    r.permissions_will_change!
    r.save
    get :report
    assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
  end

  def test_report_all_projects_one_criteria
    get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "8.65", "%.2f" % assigns(:report).total_hours
  end

  def test_report_all_time
    get :report, :project_id => 1, :criteria => ['project', 'issue']
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "162.90", "%.2f" % assigns(:report).total_hours
  end

  def test_report_all_time_by_day
    get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "162.90", "%.2f" % assigns(:report).total_hours
    assert_tag :tag => 'th', :content => '2007-03-12'
  end

  def test_report_one_criteria
    get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "8.65", "%.2f" % assigns(:report).total_hours
  end

  def test_report_two_criteria
    get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "162.90", "%.2f" % assigns(:report).total_hours
  end

  def test_report_custom_field_criteria_with_multiple_values
    field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
    entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
    CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
    CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')

    get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
    assert_response :success
  end

  def test_report_one_day
    get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["member", "activity"]
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "4.25", "%.2f" % assigns(:report).total_hours
  end

  def test_report_at_issue_level
    get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "154.25", "%.2f" % assigns(:report).total_hours
    assert_tag :form,
      :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
  end

  def test_report_custom_field_criteria
    get :report, :project_id => 1, :criteria => ['project', 'cf_1', 'cf_7']
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal 3, assigns(:report).criteria.size
    assert_equal "162.90", "%.2f" % assigns(:report).total_hours
    # Custom field column
    assert_tag :tag => 'th', :content => 'Database'
    # Custom field row
    assert_tag :tag => 'td', :content => 'MySQL',
                             :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
                                                         :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
                                                                                     :content => '1' }}
    # Second custom field column
    assert_tag :tag => 'th', :content => 'Billable'
  end

  def test_report_one_criteria_no_result
    get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
    assert_response :success
    assert_template 'report'
    assert_not_nil assigns(:report)
    assert_equal "0.00", "%.2f" % assigns(:report).total_hours
  end

  def test_report_status_criterion
    get :report, :project_id => 1, :criteria => ['status']
    assert_response :success
    assert_template 'report'
    assert_tag :tag => 'th', :content => 'Status'
    assert_tag :tag => 'td', :content => 'New'
  end

  def test_report_all_projects_csv_export
    get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
        :criteria => ["project", "member", "activity"], :format => "csv"
    assert_response :success
    assert_equal 'text/csv; header=present', @response.content_type
    lines = @response.body.chomp.split("\n")
    # Headers
    assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
                 lines.first
    # Total row
    assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
  end

  def test_report_csv_export
    get :report, :project_id => 1, :columns => 'month',
        :from => "2007-01-01", :to => "2007-06-30",
        :criteria => ["project", "member", "activity"], :format => "csv"
    assert_response :success
    assert_equal 'text/csv; header=present', @response.content_type
    lines = @response.body.chomp.split("\n")
    # Headers
    assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
                 lines.first
    # Total row
    assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
  end

  def test_csv_big_5
    Setting.default_language = "zh-TW"
    str_utf8  = "\xe4\xb8\x80\xe6\x9c\x88"
    str_big5  = "\xa4@\xa4\xeb"
    if str_utf8.respond_to?(:force_encoding)
      str_utf8.force_encoding('UTF-8')
      str_big5.force_encoding('Big5')
    end
    user = User.find_by_id(3)
    user.firstname = str_utf8
    user.lastname  = "test-lastname"
    assert user.save
    comments = "test_csv_big_5"
    te1 = TimeEntry.create(:spent_on => '2011-11-11',
                           :hours    => 7.3,
                           :project  => Project.find(1),
                           :user     => user,
                           :activity => TimeEntryActivity.find_by_name('Design'),
                           :comments => comments)

    te2 = TimeEntry.find_by_comments(comments)
    assert_not_nil te2
    assert_equal 7.3, te2.hours
    assert_equal 3, te2.user_id

    get :report, :project_id => 1, :columns => 'day',
        :from => "2011-11-11", :to => "2011-11-11",
        :criteria => ["member"], :format => "csv"
    assert_response :success
    assert_equal 'text/csv; header=present', @response.content_type
    lines = @response.body.chomp.split("\n")    
    # Headers
    s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
    s2 = "\xc1`\xadp"
    if s1.respond_to?(:force_encoding)
      s1.force_encoding('Big5')
      s2.force_encoding('Big5')
    end
    assert_equal s1, lines.first
    # Total row
    assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
    assert_equal "#{s2},7.30,7.30", lines[2]

    str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
    if str_tw.respond_to?(:force_encoding)
      str_tw.force_encoding('UTF-8')
    end
    assert_equal str_tw, l(:general_lang_name)
    assert_equal 'Big5', l(:general_csv_encoding)
    assert_equal ',', l(:general_csv_separator)
    assert_equal '.', l(:general_csv_decimal_separator)
  end

  def test_csv_cannot_convert_should_be_replaced_big_5
    Setting.default_language = "zh-TW"
    str_utf8  = "\xe4\xbb\xa5\xe5\x86\x85"
    if str_utf8.respond_to?(:force_encoding)
      str_utf8.force_encoding('UTF-8')
    end
    user = User.find_by_id(3)
    user.firstname = str_utf8
    user.lastname  = "test-lastname"
    assert user.save
    comments = "test_replaced"
    te1 = TimeEntry.create(:spent_on => '2011-11-11',
                           :hours    => 7.3,
                           :project  => Project.find(1),
                           :user     => user,
                           :activity => TimeEntryActivity.find_by_name('Design'),
                           :comments => comments)

    te2 = TimeEntry.find_by_comments(comments)
    assert_not_nil te2
    assert_equal 7.3, te2.hours
    assert_equal 3, te2.user_id

    get :report, :project_id => 1, :columns => 'day',
        :from => "2011-11-11", :to => "2011-11-11",
        :criteria => ["member"], :format => "csv"
    assert_response :success
    assert_equal 'text/csv; header=present', @response.content_type
    lines = @response.body.chomp.split("\n")    
    # Headers
    s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
    if s1.respond_to?(:force_encoding)
      s1.force_encoding('Big5')
    end
    assert_equal s1, lines.first
    # Total row
    s2 = ""
    if s2.respond_to?(:force_encoding)
      s2 = "\xa5H?"
      s2.force_encoding('Big5')
    elsif RUBY_PLATFORM == 'java'
      s2 = "??"
    else
      s2 = "\xa5H???"
    end
    assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
  end

  def test_csv_fr
    with_settings :default_language => "fr" do
      str1  = "test_csv_fr"
      user = User.find_by_id(3)
      te1 = TimeEntry.create(:spent_on => '2011-11-11',
                             :hours    => 7.3,
                             :project  => Project.find(1),
                             :user     => user,
                             :activity => TimeEntryActivity.find_by_name('Design'),
                             :comments => str1)

      te2 = TimeEntry.find_by_comments(str1)
      assert_not_nil te2
      assert_equal 7.3, te2.hours
      assert_equal 3, te2.user_id

      get :report, :project_id => 1, :columns => 'day',
          :from => "2011-11-11", :to => "2011-11-11",
          :criteria => ["member"], :format => "csv"
      assert_response :success
      assert_equal 'text/csv; header=present', @response.content_type
      lines = @response.body.chomp.split("\n")    
      # Headers
      s1 = "Membre;2011-11-11;Total"
      s2 = "Total"
      if s1.respond_to?(:force_encoding)
        s1.force_encoding('ISO-8859-1')
        s2.force_encoding('ISO-8859-1')
      end
      assert_equal s1, lines.first
      # Total row
      assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
      assert_equal "#{s2};7,30;7,30", lines[2]

      str_fr = "Fran\xc3\xa7ais"
      if str_fr.respond_to?(:force_encoding)
        str_fr.force_encoding('UTF-8')
      end
      assert_equal str_fr, l(:general_lang_name)
      assert_equal 'ISO-8859-1', l(:general_csv_encoding)
      assert_equal ';', l(:general_csv_separator)
      assert_equal ',', l(:general_csv_decimal_separator)
    end
  end
end