diff --git a/.gitignore b/.gitignore index 937d8f7b..35dee02b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,15 @@ *.tmproj -config/database.yml -config/environment.rb -config/deploy.rb -log -tmp -db/data.yml -db/*.sqlite3 -nbproject -vendor/plugins/query_trace/ -db/schema.rb .dotest +/.emacs-project +config/database.yml +config/deploy.rb +config/environment.rb +db/*.sqlite3 +db/data.yml +db/schema.rb +log +nbproject public/javascripts/cache -public/stylesheets/cache \ No newline at end of file +public/stylesheets/cache +tmp +vendor/plugins/query_trace/ diff --git a/app/models/preference.rb b/app/models/preference.rb index d4530f3f..0a2f41ab 100644 --- a/app/models/preference.rb +++ b/app/models/preference.rb @@ -1,28 +1,37 @@ class Preference < ActiveRecord::Base belongs_to :user belongs_to :sms_context, :class_name => 'Context' - + def self.due_styles { :due_in_n_days => 0, :due_on => 1} end - + def self.day_number_to_name_map { 0 => "Sunday", - 1 => "Monday", - 2 => "Tuesday", - 3 => "Wednesday", - 4 => "Thursday", - 5 => "Friday", - 6 => "Saturday"} + 1 => "Monday", + 2 => "Tuesday", + 3 => "Wednesday", + 4 => "Thursday", + 5 => "Friday", + 6 => "Saturday"} end - + def hide_completed_actions? return show_number_completed == 0 end - + def parse_date(s) return nil if s.blank? - user.at_midnight(Date.strptime(s, date_format)) + date = nil + + if s.is_a?(Time) + date = s.to_date + elsif s.is_a?(String) + date = Date.strptime(s, date_format) + else + raise ArgumentError.new("Bad argument type:#{s.class}") + end + + user.at_midnight(date) end - -end \ No newline at end of file +end diff --git a/test/functional/todos_controller_test.rb b/test/functional/todos_controller_test.rb index 6b93857e..9bc355e1 100644 --- a/test/functional/todos_controller_test.rb +++ b/test/functional/todos_controller_test.rb @@ -6,7 +6,7 @@ class TodosController; def rescue_action(e) raise e end; end class TodosControllerTest < Test::Rails::TestCase fixtures :users, :preferences, :projects, :contexts, :todos, :tags, :taggings, :recurring_todos - + def setup @controller = TodosController.new @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new @@ -16,7 +16,7 @@ class TodosControllerTest < Test::Rails::TestCase get :index assert_redirected_to :controller => 'login', :action => 'login' end - + def test_not_done_counts login_as(:admin_user) get :index @@ -24,7 +24,7 @@ class TodosControllerTest < Test::Rails::TestCase assert_equal 3, assigns['context_not_done_counts'][contexts(:call).id] assert_equal 1, assigns['context_not_done_counts'][contexts(:lab).id] end - + def test_tag_is_retrieved_properly login_as(:admin_user) get :index @@ -33,7 +33,7 @@ class TodosControllerTest < Test::Rails::TestCase assert_equal 'foo', t.tags[0].name assert !t.starred? end - + def test_not_done_counts_after_hiding_project p = Project.find(1) p.hide! @@ -44,7 +44,7 @@ class TodosControllerTest < Test::Rails::TestCase assert_equal 2, assigns['context_not_done_counts'][contexts(:call).id] assert_equal nil, assigns['context_not_done_counts'][contexts(:lab).id] end - + def test_not_done_counts_after_hiding_and_unhiding_project p = Project.find(1) p.hide! @@ -57,29 +57,29 @@ class TodosControllerTest < Test::Rails::TestCase assert_equal 3, assigns['context_not_done_counts'][contexts(:call).id] assert_equal 1, assigns['context_not_done_counts'][contexts(:lab).id] end - + def test_deferred_count_for_project_source_view login_as(:admin_user) - xhr :post, :toggle_check, :id => 5, :_source_view => 'project' + xhr :post, :toggle_check, :id => 5, :_source_view => 'project' assert_equal 1, assigns['deferred_count'] - xhr :post, :toggle_check, :id => 15, :_source_view => 'project' + xhr :post, :toggle_check, :id => 15, :_source_view => 'project' assert_equal 0, assigns['deferred_count'] end - + def test_destroy_todo login_as(:admin_user) xhr :post, :destroy, :id => 1, :_source_view => 'todo' assert_rjs :page, "todo_1", :remove # #assert_rjs :replace_html, "badge-count", '9' end - + def test_create_todo assert_difference Todo, :count do login_as(:admin_user) put :create, :_source_view => 'todo', "context_name"=>"library", "project_name"=>"Build a working time machine", "todo"=>{"notes"=>"", "description"=>"Call Warren Buffet to find out how much he makes per day", "due"=>"30/11/2006"}, "tag_list"=>"foo bar" end end - + def test_create_todo_via_xml login_as(:admin_user) assert_difference Todo, :count do @@ -88,6 +88,26 @@ class TodosControllerTest < Test::Rails::TestCase end end + def test_create_todo_via_xml_show_from + login_as(:admin_user) + + assert_difference Todo, :count do + xml = "Call Warren Buffet to find out how much he makes per day#{projects(:timemachine).id}#{contexts(:agenda).id}#{1.week.from_now.xmlschema}" + + #p parse_xml_body(xml) + post :create, parse_xml_body(xml).update(:format => "xml") + assert_response :created + end + end + + def parse_xml_body(body) + env = { 'rack.input' => StringIO.new(body), + 'HTTP_X_POST_DATA_FORMAT' => 'xml', + 'CONTENT_LENGTH' => body.size.to_s } + ActionController::RackRequest.new(env).request_parameters + end + + def test_fail_to_create_todo_via_xml login_as(:admin_user) # #try to create with no context, which is not valid @@ -97,14 +117,14 @@ class TodosControllerTest < Test::Rails::TestCase assert_xml_select "error", "Context can't be blank" end end - + def test_create_deferred_todo original_todo_count = Todo.count login_as(:admin_user) put :create, :_source_view => 'todo', "context_name"=>"library", "project_name"=>"Build a working time machine", "todo"=>{"notes"=>"", "description"=>"Call Warren Buffet to find out how much he makes per day", "due"=>"30/11/2026", 'show_from' => '30/10/2026'}, "tag_list"=>"foo bar" assert_equal original_todo_count + 1, Todo.count end - + def test_update_todo_project t = Todo.find(1) login_as(:admin_user) @@ -112,7 +132,7 @@ class TodosControllerTest < Test::Rails::TestCase t = Todo.find(1) assert_equal 1, t.project_id end - + def test_update_todo_project_to_none t = Todo.find(1) login_as(:admin_user) @@ -120,7 +140,7 @@ class TodosControllerTest < Test::Rails::TestCase t = Todo.find(1) assert_nil t.project_id end - + def test_update_todo_to_deferred_is_reflected_in_badge_count login_as(:admin_user) get :index @@ -128,7 +148,7 @@ class TodosControllerTest < Test::Rails::TestCase xhr :post, :update, :id => 1, :_source_view => 'todo', "context_name"=>"library", "project_name"=>"Make more money than Billy Gates", "todo"=>{"id"=>"1", "notes"=>"", "description"=>"Call Warren Buffet to find out how much he makes per day", "due"=>"30/11/2006", "show_from"=>"30/11/2030"}, "tag_list"=>"foo bar" assert_equal 10, assigns['down_count'] end - + def test_update_todo t = Todo.find(1) login_as(:admin_user) @@ -148,7 +168,7 @@ class TodosControllerTest < Test::Rails::TestCase t.reload assert t.project.nil? end - + def test_update_todo_tags_to_none t = Todo.find(1) login_as(:admin_user) @@ -175,7 +195,7 @@ class TodosControllerTest < Test::Rails::TestCase assert_response :success assert_equal 3, @tagged end - + def test_rss_feed login_as(:admin_user) get :index, { :format => "rss" } @@ -214,7 +234,7 @@ class TodosControllerTest < Test::Rails::TestCase end end end - + def test_rss_feed_not_accessible_to_anonymous_user_without_token login_as nil get :index, { :format => "rss" } @@ -301,25 +321,25 @@ class TodosControllerTest < Test::Rails::TestCase assert !(/ /.match(@response.body)) # #puts @response.body end - + def test_mobile_index_uses_text_html_content_type login_as(:admin_user) get :index, { :format => "m" } assert_equal 'text/html', @response.content_type end - + def test_mobile_index_assigns_down_count login_as(:admin_user) get :index, { :format => "m" } assert_equal 11, assigns['down_count'] end - + def test_mobile_create_action_creates_a_new_todo login_as(:admin_user) post :create, {"format"=>"m", "todo"=>{"context_id"=>"2", "due(1i)"=>"2007", "due(2i)"=>"1", "due(3i)"=>"2", "show_from(1i)"=>"", "show_from(2i)"=>"", "show_from(3i)"=>"", - "project_id"=>"1", + "project_id"=>"1", "notes"=>"test notes", "description"=>"test_mobile_create_action", "state"=>"0"}} t = Todo.find_by_description("test_mobile_create_action") assert_not_nil t @@ -330,13 +350,13 @@ class TodosControllerTest < Test::Rails::TestCase assert_nil t.show_from assert_equal Date.new(2007,1,2), t.due.to_date end - + def test_mobile_create_action_redirects_to_mobile_home_page_when_successful login_as(:admin_user) post :create, {"format"=>"m", "todo"=>{"context_id"=>"2", "due(1i)"=>"2007", "due(2i)"=>"1", "due(3i)"=>"2", "show_from(1i)"=>"", "show_from(2i)"=>"", "show_from(3i)"=>"", - "project_id"=>"1", + "project_id"=>"1", "notes"=>"test notes", "description"=>"test_mobile_create_action", "state"=>"0"}} assert_redirected_to '/m' end @@ -346,7 +366,7 @@ class TodosControllerTest < Test::Rails::TestCase post :create, {"format"=>"m", "todo"=>{"context_id"=>"2", "due(1i)"=>"2007", "due(2i)"=>"1", "due(3i)"=>"2", "show_from(1i)"=>"", "show_from(2i)"=>"", "show_from(3i)"=>"", - "project_id"=>"1", + "project_id"=>"1", "notes"=>"test notes", "state"=>"0"}, "tag_list"=>"test, test2"} assert_template 'todos/new' end @@ -359,26 +379,26 @@ class TodosControllerTest < Test::Rails::TestCase def test_toggle_check_on_recurring_todo login_as(:admin_user) - + # link todo_1 and recurring_todo_1 recurring_todo_1 = RecurringTodo.find(1) todo_1 = Todo.find_by_recurring_todo_id(1) - + # mark todo_1 as complete by toggle_check - xhr :post, :toggle_check, :id => todo_1.id, :_source_view => 'todo' + xhr :post, :toggle_check, :id => todo_1.id, :_source_view => 'todo' todo_1.reload assert todo_1.completed? # check that there is only one active todo belonging to recurring_todo count = Todo.count(:all, :conditions => {:recurring_todo_id => recurring_todo_1.id, :state => 'active'}) assert_equal 1, count - + # check there is a new todo linked to the recurring pattern next_todo = Todo.find(:first, :conditions => {:recurring_todo_id => recurring_todo_1.id, :state => 'active'}) assert_equal "Call Bill Gates every day", next_todo.description # check that the new todo is not the same as todo_1 assert_not_equal todo_1.id, next_todo.id - + # change recurrence pattern to monthly and set show_from 2 days before due # date this forces the next todo to be put in the tickler recurring_todo_1.show_from_delta = 2 @@ -390,7 +410,7 @@ class TodosControllerTest < Test::Rails::TestCase recurring_todo_1.save # mark next_todo as complete by toggle_check - xhr :post, :toggle_check, :id => next_todo.id, :_source_view => 'todo' + xhr :post, :toggle_check, :id => next_todo.id, :_source_view => 'todo' next_todo.reload assert next_todo.completed? @@ -409,12 +429,12 @@ class TodosControllerTest < Test::Rails::TestCase def test_toggle_check_on_rec_todo_show_from_today login_as(:admin_user) - + # link todo_1 and recurring_todo_1 recurring_todo_1 = RecurringTodo.find(1) todo_1 = Todo.find_by_recurring_todo_id(1) today = Time.now.utc.at_midnight - + # change recurrence pattern to monthly and set show_from to today recurring_todo_1.target = 'show_from_date' recurring_todo_1.recurring_period = 'monthly' @@ -422,11 +442,11 @@ class TodosControllerTest < Test::Rails::TestCase recurring_todo_1.every_other1 = today.day recurring_todo_1.every_other2 = 1 recurring_todo_1.save - + # mark todo_1 as complete by toggle_check, this gets rid of todo_1 that was # not correctly created from the adjusted recurring pattern we defined # above. - xhr :post, :toggle_check, :id => todo_1.id, :_source_view => 'todo' + xhr :post, :toggle_check, :id => todo_1.id, :_source_view => 'todo' todo_1.reload assert todo_1.completed? @@ -436,14 +456,14 @@ class TodosControllerTest < Test::Rails::TestCase assert !new_todo.nil? # mark new_todo as complete by toggle_check - xhr :post, :toggle_check, :id => new_todo.id, :_source_view => 'todo' + xhr :post, :toggle_check, :id => new_todo.id, :_source_view => 'todo' new_todo.reload assert todo_1.completed? - + # locate the new todo in tickler new_todo = Todo.find(:first, :conditions => {:recurring_todo_id => recurring_todo_1.id, :state => 'deferred'}) assert !new_todo.nil? - + assert_equal "Call Bill Gates every day", new_todo.description # check that the new todo is not the same as todo_1 assert_not_equal todo_1.id, new_todo.id @@ -452,7 +472,7 @@ class TodosControllerTest < Test::Rails::TestCase assert !new_todo.show_from.nil? assert_equal Time.utc(today.year, today.month, today.day)+1.month, new_todo.show_from end - + def test_check_for_next_todo login_as :admin_user @@ -466,11 +486,11 @@ class TodosControllerTest < Test::Rails::TestCase @todo.due = Time.zone.now + 1.day @todo.save recurring_todo_1.every_other1 = @todo.due.day - recurring_todo_1.every_other2 = @todo.due.month + recurring_todo_1.every_other2 = @todo.due.month recurring_todo_1.save - + # mark todo complete - xhr :post, :toggle_check, :id => @todo.id, :_source_view => 'todo' + xhr :post, :toggle_check, :id => @todo.id, :_source_view => 'todo' @todo.reload assert @todo.completed? @@ -482,7 +502,7 @@ class TodosControllerTest < Test::Rails::TestCase next_todo = Todo.find(:first, :conditions => {:recurring_todo_id => recurring_todo_1.id, :state => 'deferred'}) assert !next_todo.nil? # check that the due date of the new todo is later than tomorrow - assert next_todo.due > @todo.due + assert next_todo.due > @todo.due end - + end