">
<%= format_date(entry.spent_on) %> |
+<%= format_time(entry.start_time) %> |
+<%= format_time(entry.end_time) %> |
+<%= hours(entry) %> |
<%=h entry.user %> |
<%=h entry.activity %> |
<%=h entry.project %> |
@@ -24,7 +29,6 @@
<% end -%>
-<%= html_hours("%.2f" % entry.hours) %> |
<% if entry.editable_by?(User.current) -%>
<%= link_to image_tag('edit.png'), {:controller => 'timelog', :action => 'edit', :id => entry, :project_id => nil},
Index: app/views/timelog/edit.rhtml
===================================================================
--- app/views/timelog/edit.rhtml (revision 2641)
+++ app/views/timelog/edit.rhtml (working copy)
@@ -3,11 +3,24 @@
<% labelled_tabular_form_for :time_entry, @time_entry, :url => {:action => 'edit', :id => @time_entry, :project_id => @time_entry.project} do |f| %>
<%= error_messages_for 'time_entry' %>
<%= back_url_hidden_field_tag %>
+<% content_for :header_tags do %>
+ <%= javascript_include_tag 'time' %>
+<% end %>
+<% content_for :header_tags do %>
+
+<% end %>
+
<%= f.text_field :issue_id, :size => 6 %> <%= h("#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}") if @time_entry.issue %>
<%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %>
- <%= f.text_field :hours, :size => 6, :required => true %>
+ <%= f.time_field :start_time %>
+ <%= f.time_field :end_time %>
+ <%= f.text_field :hours, :size => 6 %> <%= l(:text_clear_to_recalculate_time_by_range) %>
<%= f.text_field :comments, :size => 100 %>
<%= f.select :activity_id, activity_collection_for_select_options, :required => true %>
<% @time_entry.custom_field_values.each do |value| %>
Index: db/migrate/20090330000200_add_start_and_end_time_to_time_entries.rb
===================================================================
--- db/migrate/20090330000200_add_start_and_end_time_to_time_entries.rb (revision 0)
+++ db/migrate/20090330000200_add_start_and_end_time_to_time_entries.rb (revision 0)
@@ -0,0 +1,11 @@
+class AddStartAndEndTimeToTimeEntries < ActiveRecord::Migration
+ def self.up
+ add_column :time_entries, :start_time, :datetime
+ add_column :time_entries, :end_time, :datetime
+ end
+
+ def self.down
+ remove_column :time_entries, :start_time
+ remove_column :time_entries, :end_time
+ end
+end
Index: db/migrate/20090330000300_allow_null_time_entry_hours.rb
===================================================================
--- db/migrate/20090330000300_allow_null_time_entry_hours.rb (revision 0)
+++ db/migrate/20090330000300_allow_null_time_entry_hours.rb (revision 0)
@@ -0,0 +1,9 @@
+class AllowNullTimeEntryHours < ActiveRecord::Migration
+ def self.up
+ change_column :time_entries, :hours, :float, :null => true
+ end
+
+ def self.down
+ # not needed
+ end
+end
Index: lib/tabular_form_builder.rb
===================================================================
--- lib/tabular_form_builder.rb (revision 2641)
+++ lib/tabular_form_builder.rb (working copy)
@@ -33,6 +33,17 @@
END_SRC
class_eval src, __FILE__, __LINE__
end
+
+ def time_field(field)
+ value = nil
+ value = @object.send(field) if @object
+ value = value.strftime('%Y-%m-%d %H:%M') if value
+ result = text_field field, :size => 15, :value => value
+ result += (' ') % "#{@object_name}_#{field}"
+ result
+ end
def select(field, choices, options = {}, html_options = {})
label_for_field(field, options) + super
Index: public/javascripts/time.js
===================================================================
--- public/javascripts/time.js (revision 0)
+++ public/javascripts/time.js (revision 0)
@@ -0,0 +1,68 @@
+//taken from XPlanner
+var dateFormatChars = "dMyhHmsa";
+
+function formatDate(date, format) {
+ return formatDate2(date, format, 0);
+}
+
+function formatDate2(date, format, offset) {
+ if (offset >= format.length) {
+ return "";
+ } else if (dateFormatChars.indexOf(format.charAt(offset)) != -1) {
+ return formatDateElement(date, format, offset);
+ } else {
+ return formatDateLiteral(date, format, offset);
+ }
+}
+
+function formatDateElement(date, format, offset) {
+ var end = offset;
+ var ch = format.charAt(offset);
+ while (++end < format.length && format.charAt(end) == ch);
+ var count = end - offset;
+ var value;
+ if (ch == 'd') {
+ value = padValue(count, date.getDate());
+ }
+ else if (ch == 'M') {
+ value = padValue(count, date.getMonth()+1);
+ }
+ else if (ch == 'y') {
+ value = padValue(count, date.getFullYear());
+ }
+ else if (ch == 'H') {
+ value = padValue(count, date.getHours());
+ }
+ else if (ch == 'h') {
+ value = padValue(count, date.getHours() % 12);
+ }
+ else if (ch == 'm') {
+ value = padValue(count, date.getMinutes());
+ }
+ else if (ch == 's') {
+ value = padValue(count, date.getSeconds());
+ }
+ else if (ch == 'a') {
+ value = date.getHours() > 12 ? 'PM' : 'AM';
+ }
+ return value + formatDate2(date, format, end);
+}
+
+function padValue(count, value) {
+ for (i = value.toString().length; i < count; i++) {
+ value = '0'+value;
+ }
+ return value;
+}
+
+function formatDateLiteral(date, format, offset) {
+ end = offset;
+ while (++end < format.length && dateFormatChars.indexOf(format.charAt(end)) == -1);
+ return format.substr(offset, end - offset) + formatDate2(date, format, end);
+}
+
+function maybeSetTimeNow(field) {
+ if (field.value == "") {
+ field.value = formatDate(new Date(), "yyyy-MM-dd HH:mm");
+ }
+}
Index: test/fixtures/time_entries.yml
===================================================================
--- test/fixtures/time_entries.yml (revision 2641)
+++ test/fixtures/time_entries.yml (working copy)
@@ -55,4 +55,55 @@
hours: 7.65
user_id: 1
tyear: 2007
-
\ No newline at end of file
+
+#this is to run existing controller tests with Start/End time improvement - specific entries
+time_entry_in_progress:
+ created_on: 2007-04-22 12:20:48 +02:00
+ tweek: 16
+ tmonth: 4
+ project_id: 1
+ comments: Time spent on a subproject (in progress)
+ updated_on: 2007-04-22 12:20:48 +02:00
+ activity_id: 10
+ spent_on: 2007-04-22
+ issue_id: 1
+ id: 5
+ user_id: 1
+ tyear: 2007
+ start_time: 2007-07-11 16:20
+
+#same user but another issue and project, and intersects with time_entry_in_progress
+intersecting_time_entry:
+ created_on: 2007-04-22 12:20:48 +02:00
+ tweek: 16
+ tmonth: 4
+ project_id: 2
+ comments: Time entry intersecting with time_entry_in_progress
+ updated_on: 2007-04-22 12:20:48 +02:00
+ activity_id: 10
+ spent_on: 2007-04-22
+ issue_id: 4
+ id: 6
+ user_id: 1
+ tyear: 2007
+ start_time: 2007-07-11 15:20
+ end_time: 2007-07-11 17:20
+ hours: 0 #to not break time calculation tests
+
+#this spans several years - should intersect with time_entry_in_progress as well
+big_intersecting_time_entry:
+ created_on: 2007-04-22 12:20:48 +02:00
+ tweek: 16
+ tmonth: 4
+ project_id: 1
+ comments: Time entry intersecting with time_entry_in_progress
+ updated_on: 2007-04-22 12:20:48 +02:00
+ activity_id: 10
+ spent_on: 2007-04-22
+ issue_id: 1
+ id: 7
+ user_id: 1
+ tyear: 2007
+ start_time: 2005-07-11 16:30
+ end_time: 2007-07-11 17:20
+ hours: 0 #to not break time calculation tests
\ No newline at end of file
Index: test/functional/issues_controller_test.rb
===================================================================
--- test/functional/issues_controller_test.rb (revision 2641)
+++ test/functional/issues_controller_test.rb (working copy)
@@ -366,6 +366,19 @@
assert_not_nil assigns(:issue)
end
+ def test_show_in_progress
+ @request.session[:user_id] = 1
+ get :show, :id => 1
+ assert_equal time_entries(:time_entry_in_progress), assigns(:time_entry),
+ 'there should be an in-progress time entry for this user'
+ end
+
+ def test_show_not_in_progress
+ @request.session[:user_id] = 2
+ get :show, :id => 1
+ assert_nil assigns(:time_entry).id, 'time entry for this user should be new'
+ end
+
def test_get_new
@request.session[:user_id] = 2
get :new, :project_id => 1, :tracker_id => 1
@@ -680,7 +693,7 @@
issue = Issue.find(1)
assert_equal 1, issue.status_id
@request.session[:user_id] = 2
- assert_difference('TimeEntry.count', 0) do
+ assert_difference('TimeEntry.count', 0) do #time entries count should not change because given entry is empty (hence invalid)
post :edit,
:id => 1,
:issue => { :status_id => 2, :assigned_to_id => 3 },
@@ -698,6 +711,21 @@
assert mail.body.include?("Status changed from New to Assigned")
end
+ def test_post_edit_with_task_in_progress
+ user_id = 1
+ @request.session[:user_id] = user_id
+ issue = Issue.find(1)
+ time_entry = issue.time_entry_in_progress(User.find(user_id))
+ assert_not_nil time_entry, 'there should be time entry in progress'
+ assert_difference('TimeEntry.count', 0) do #time entries count should not change because entry in progress should be updated
+ post :edit,
+ :id => 1,
+ :issue => { },
+ :notes => 'Assigned to dlopper',
+ :time_entry => { :id => time_entry.id, :comments => 'xyz' }
+ end
+ end
+
def test_post_edit_with_note_only
notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
# anonymous user
@@ -727,11 +755,13 @@
issue = Issue.find(1)
- j = issue.journals.find(:first, :order => 'id DESC')
- assert_equal '2.5 hours added', j.notes
- assert_equal 0, j.details.size
+ #:first doesn't work sometimes here with MySQL 5.0.51b
+ e = issue.journals.find(:all, :order => 'id DESC')[0]
+ assert_equal '2.5 hours added', e.notes
+ assert_equal 0, e.details.size
- t = issue.time_entries.find(:first, :order => 'id DESC')
+ #:first doesn't work sometimes here with MySQL 5.0.51b
+ t = issue.time_entries.find(:all, :order => 'id DESC')[0]
assert_not_nil t
assert_equal 2.5, t.hours
assert_equal spent_hours_before + 2.5, issue.spent_hours
Index: test/functional/timelog_controller_test.rb
===================================================================
--- test/functional/timelog_controller_test.rb (revision 2641)
+++ test/functional/timelog_controller_test.rb (working copy)
@@ -62,6 +62,28 @@
:content => 'Development'
end
+ def test_get_edit_in_progress_by_entry_id
+ @request.session[:user_id] = 1 #to avoid 'no permission' error
+ get :edit, :id => time_entries(:time_entry_in_progress)
+ assert_response :success
+ assert_equal 'time_entry_end_time', assigns(:activate_field)
+ end
+
+ def test_get_edit_not_in_progress
+ @request.session[:user_id] = 2 #there are no in-progress entries of this user for the issue
+ get :edit, :issue_id => 1
+ assert_response :success
+ assert_equal 'time_entry_start_time', assigns(:activate_field)
+ end
+
+ def test_get_edit_in_progress_by_issue_id
+ user_id = 1
+ @request.session[:user_id] = user_id #there is in-progress entry of this user for the issue
+ issue_id = 1
+ get :edit, :issue_id => issue_id
+ assert_redirected_to :action => "edit", :id => Issue.find(issue_id).time_entry_in_progress(User.find(user_id)).id
+ end
+
def test_get_edit_existing_time
@request.session[:user_id] = 2
get :edit, :id => 2, :project_id => nil
@@ -103,6 +125,7 @@
post :edit, :id => 1,
:time_entry => {:issue_id => '2',
:hours => '8'}
+ assert_no_errors(assigns(:time_entry))
assert_redirected_to :action => 'details', :project_id => 'ecookbook'
entry.reload
@@ -273,7 +296,7 @@
assert_response :success
assert_template 'details'
assert_not_nil assigns(:entries)
- assert_equal 4, assigns(:entries).size
+ assert_equal 6, assigns(:entries).size
# project and subproject
assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
assert_not_nil assigns(:total_hours)
@@ -288,7 +311,7 @@
assert_response :success
assert_template 'details'
assert_not_nil assigns(:entries)
- assert_equal 3, assigns(:entries).size
+ assert_equal 5, assigns(:entries).size
assert_not_nil assigns(:total_hours)
assert_equal "12.90", "%.2f" % assigns(:total_hours)
assert_equal '2007-03-20'.to_date, assigns(:from)
@@ -327,7 +350,7 @@
assert_response :success
assert_template 'details'
assert_not_nil assigns(:entries)
- assert_equal 2, assigns(:entries).size
+ assert_equal 4, assigns(:entries).size
assert_not_nil assigns(:total_hours)
assert_equal 154.25, assigns(:total_hours)
# display all time by default
Index: test/test_helper.rb
===================================================================
--- test/test_helper.rb (revision 2641)
+++ test/test_helper.rb (working copy)
@@ -64,6 +64,21 @@
Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
end
+
+ def assert_error_on(object, field)
+ object.valid?
+ assert object.errors.on(field), "expected error on #{field} attribute"
+ end
+
+ def assert_no_error_on(object, field)
+ object.valid?
+ assert !object.errors.on(field), "expected no error on #{field} attribute"
+ end
+
+ def assert_no_errors(object, options = {})
+ object.valid? if options[:validate]
+ assert_equal [], object.errors.full_messages
+ end
def with_settings(options, &block)
saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
Index: test/unit/issue_test.rb
===================================================================
--- test/unit/issue_test.rb (revision 2641)
+++ test/unit/issue_test.rb (working copy)
@@ -234,6 +234,15 @@
assert_nil TimeEntry.find_by_issue_id(1)
end
+ def test_find_in_progress_success
+ assert_equal time_entries(:time_entry_in_progress),
+ issues(:issues_001).time_entry_in_progress(users(:users_001))
+ end
+
+ def test_find_in_progress_failure
+ assert_nil issues(:issues_001).time_entry_in_progress(users(:users_002))
+ end
+
def test_overdue
assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
assert !Issue.new(:due_date => Date.today).overdue?
Index: test/unit/time_entry_test.rb
===================================================================
--- test/unit/time_entry_test.rb (revision 2641)
+++ test/unit/time_entry_test.rb (working copy)
@@ -20,6 +20,10 @@
class TimeEntryTest < Test::Unit::TestCase
fixtures :issues, :projects, :users, :time_entries
+ def setup
+ User.current.language = 'en'
+ end
+
def test_hours_format
assertions = { "2" => 2.0,
"21.1" => 21.1,
@@ -44,4 +48,80 @@
assert_equal v, t.hours, "Converting #{k} failed:"
end
end
+
+ def test_start_time_must_be_set_if_hours_are_not_and_reverse
+ entry = TimeEntry.new
+ assert_error_on(entry, :start_time)
+ assert_error_on(entry, :hours)
+ end
+
+ def test_start_time_can_be_absent_if_hours_are_set_and_reverse
+ entry = TimeEntry.new :hours => 1
+ entry.valid?
+ assert_no_error_on(entry, :start_time)
+ entry = TimeEntry.new :start_time => Time.now
+ entry.valid?
+ assert_no_error_on(entry, :hours)
+ end
+
+ def successful_params
+ {:spent_on => '2008-07-13', :issue_id => 1, :user => users(:users_004),
+ :activity_id => Enumeration.get_values('ACTI').first}
+ end
+
+ def test_hours_not_calculated_if_set_explicitly
+ #I worked on this time to time during the day, and it was 1 hour in sum
+ entry = TimeEntry.new successful_params.merge(:hours => 1,
+ :start_time => '2008-07-13 10:56', :end_time => '2008-07-14 10:56')
+
+ entry.save!
+ assert_equal 1, entry.hours
+ end
+
+ {['10:56', '11:56'] => 1, ['10:56', '11:26'] => 0.5,
+ ['10:56', '10:57'] => 0.0167,
+ ['2008-07-13 23:50', '2008-07-14 00:20'] => 0.5}.each do |range, hours|
+
+ define_method "test_hours_calculated_#{range[0]}_to_#{range[1]}" do
+
+ #add default day if not specified
+ range = range.map {|time| time['-'] ? time : '2008-07-13 ' + time}
+
+ entry = TimeEntry.new successful_params.merge(:hours => nil,
+ :start_time => range[0], :end_time => range[1])
+ entry.save!
+ assert_in_delta hours, entry.hours, 0.0001
+ end
+ end
+
+ def assert_intersects(source, dest)
+ intersecting = time_entries(source).find_intersecting_entries
+
+ assert !intersecting.empty?,
+ "there should be intersecting entries for #{source.inspect}"
+
+ assert intersecting.map {|e| e.id}.
+ include?(time_entries(dest).id),
+ "#{source.inspect} should intersect with #{dest.inspect}"
+
+ intersecting
+ end
+
+ def test_find_intersecting_entries_for_incomplete
+ assert_intersects(:time_entry_in_progress, :intersecting_time_entry)
+ end
+
+ def test_find_intersecting_entries_for_complete_doesnt_find_itself
+ intersecting = assert_intersects(:intersecting_time_entry,
+ :time_entry_in_progress)
+
+ assert !intersecting.map {|e| e.id}.include?(
+ time_entries(:intersecting_time_entry)), 'time entry\'s ' +
+ 'intersecting entries shouldn\'t include itself'
+ end
+
+ def test_find_intersecting_entries_for_big
+ assert_intersects(:big_intersecting_time_entry, :time_entry_in_progress)
+ assert_intersects(:big_intersecting_time_entry, :intersecting_time_entry)
+ end
end
Index: vendor/plugins/test_utils/MIT-LICENSE
===================================================================
--- vendor/plugins/test_utils/MIT-LICENSE (revision 0)
+++ vendor/plugins/test_utils/MIT-LICENSE (revision 0)
@@ -0,0 +1,21 @@
+Copyright (C) 2008 Texuna Technologies
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
Index: vendor/plugins/test_utils/tasks/test_utils.rake
===================================================================
--- vendor/plugins/test_utils/tasks/test_utils.rake (revision 0)
+++ vendor/plugins/test_utils/tasks/test_utils.rake (revision 0)
@@ -0,0 +1,66 @@
+##
+# based on http://nubyonrails.com/articles/foscon-and-living-dangerously-with-rake
+# Run a single test (or group of tests started with given string) in Rails.
+#
+# rake blogs-list (or f_blogs-list)
+# => Runs test_list for BlogsController (functional test; use f_blogs-list to force it if unit test found)
+#
+# rake blog-create (or u_blog-create)
+# => Runs test_create for BlogTest (unit test; use u_blog-create to force it if functional test found))
+
+#test file will be matched in the order of this array items
+TEST_TYPES = [
+ ['u_', "unit/[file_name]_test.rb"],
+ ['f_', "functional/[file_name]_controller_test.rb"],
+ ['i_', "integration/[file_name]_test.rb"],
+ ['l_', "long/[file_name]_test.rb"],
+]
+
+rule "" do |t|
+ all_flags = TEST_TYPES.map { |item| item[0] }
+ if Regexp.new("(#{all_flags.join '|'}|)(.*)\\-([^.]+)$").match(t.name)
+ flag = $1
+ file_name = $2
+ test_name = $3
+
+ path_getter = lambda { |type_info| type_info[1].gsub '[file_name]', file_name}
+ file_path = nil
+ TEST_TYPES.each do |type_info|
+ my_file_path = path_getter.call(type_info)
+ if flag == type_info[0]
+ type_info[1].match /((.+)\/)/
+ type = $2
+ puts "forced #{type} test"
+ file_path = my_file_path
+ break
+ end
+ end
+
+ if file_path && !File.exist?("test/#{file_path}")
+ raise "No file found for #{file_path}"
+ end
+
+ if !file_path
+ TEST_TYPES.each do |type_info|
+ my_file_path = path_getter.call(type_info)
+ if File.exist? "test/#{my_file_path}"
+ puts "found #{my_file_path}"
+ file_path = my_file_path
+ break
+ end
+ end
+ end
+
+ if !file_path
+ raise "No file found for #{file_name}"
+ end
+
+ begin
+ sh "ruby -Ilib:test test/#{file_path} -n /^test_#{test_name}/"
+ rescue Exception => e
+ #no logger here, oops!
+ #log.debug "error executing tests: #{e.inspect}"
+ puts "error executing tests: #{e.inspect}"
+ end
+ end
+end
|