diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 3d26cdd0..5fe6f26a 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -9,12 +9,12 @@ require "redcloth" require 'date' require 'time' -# Commented the following line because of #744. It prevented rake db:migrate to +# Commented the following line because of #744. It prevented rake db:migrate to # run because this tag went looking for the taggings table that did not exist -# when you feshly create a new database -# Old comment: We need this in development mode, or you get 'method missing' errors +# when you feshly create a new database Old comment: We need this in development +# mode, or you get 'method missing' errors # -# Tag +# Tag class ApplicationController < ActionController::Base @@ -160,7 +160,34 @@ class ApplicationController < ActionController::Base response.content_type = 'text/html' end end - + + def create_todo_from_recurring_todo(rt, date=nil) + # create todo and initialize with data from recurring_todo rt + todo = current_user.todos.build( { :description => rt.description, :notes => rt.notes, :project_id => rt.project_id, :context_id => rt.context_id}) + + # set dates + todo.recurring_todo_id = rt.id + todo.due = rt.get_due_date(date) + # make sure that show_from is not in the past + show_from_date = rt.get_show_from_date(date) + todo.show_from = show_from_date < Time.now.utc ? nil : show_from_date + + saved = todo.save + if saved + todo.tag_with(rt.tag_list, current_user) + todo.tags.reload + end + + # increate number of occurences created from recurring todo + rt.inc_occurences + + # mark recurring todo complete if there are no next actions left + checkdate = todo.due.nil? ? todo.show_from : todo.due + rt.toggle_completion! unless rt.has_next_todo(checkdate) + + return saved ? todo : nil + end + protected def admin_login_required @@ -192,7 +219,7 @@ class ApplicationController < ActionController::Base def openid_enabled? self.class.openid_enabled? end - + private def parse_date_per_user_prefs( s ) @@ -231,29 +258,5 @@ class ApplicationController < ActionController::Base def set_time_zone Time.zone = current_user.prefs.time_zone if logged_in? end - - def create_todo_from_recurring_todo(rt, date=nil) - # create todo and initialize with data from recurring_todo rt - todo = current_user.todos.build( { :description => rt.description, :notes => rt.notes, :project_id => rt.project_id, :context_id => rt.context_id}) - - # set dates - todo.due = rt.get_due_date(date) - todo.show_from = rt.get_show_from_date(date) - todo.recurring_todo_id = rt.id - saved = todo.save - if saved - todo.tag_with(rt.tag_list, current_user) - todo.tags.reload - end - - # increate number of occurences created from recurring todo - rt.inc_occurences - - # mark recurring todo complete if there are no next actions left - checkdate = todo.due.nil? ? todo.show_from : todo.due - rt.toggle_completion! unless rt.has_next_todo(checkdate) - - return saved ? todo : nil - end end diff --git a/lib/authenticated_test_helper.rb b/lib/authenticated_test_helper.rb index 2f743aa0..9137cf37 100644 --- a/lib/authenticated_test_helper.rb +++ b/lib/authenticated_test_helper.rb @@ -1,113 +1,113 @@ -module AuthenticatedTestHelper - # Sets the current user in the session from the user fixtures. - def login_as(user) - @request.session['user_id'] = user ? users(user).id : nil - end - - def content_type(type) - @request.env['Content-Type'] = type - end - - def accept(accept) - @request.env["HTTP_ACCEPT"] = accept - end - - def authorize_as(user) - if user - @request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{users(user).login}:test")}" - accept 'application/xml' - content_type 'application/xml' - else - @request.env["HTTP_AUTHORIZATION"] = nil - accept nil - content_type nil - end - end - - # http://project.ioni.st/post/217#post-217 - # - # def test_new_publication - # assert_difference(Publication, :count) do - # post :create, :publication => {...} - # # ... - # end - # end - # - def assert_difference(object, method = nil, difference = 1) - initial_value = object.send(method) - yield - assert_equal initial_value + difference, object.send(method), "#{object}##{method}" - end - - def assert_no_difference(object, method, &block) - assert_difference object, method, 0, &block - end - - # Assert the block redirects to the login - # - # assert_requires_login(:bob) { |c| c.get :edit, :id => 1 } - # - def assert_requires_login(login = nil) - yield HttpLoginProxy.new(self, login) - end - - def assert_http_authentication_required(login = nil) - yield XmlLoginProxy.new(self, login) - end - - def reset!(*instance_vars) - instance_vars = [:controller, :request, :response] unless instance_vars.any? - instance_vars.collect! { |v| "@#{v}".to_sym } - instance_vars.each do |var| - instance_variable_set(var, instance_variable_get(var).class.new) - end - end -end - -class BaseLoginProxy - attr_reader :controller - attr_reader :options - def initialize(controller, login) - @controller = controller - @login = login - end - - private - def authenticated - raise NotImplementedError - end - - def check - raise NotImplementedError - end - - def method_missing(method, *args) - @controller.reset! - authenticate - @controller.send(method, *args) - check - end -end - -class HttpLoginProxy < BaseLoginProxy - protected - def authenticate - @controller.login_as @login if @login - end - - def check - @controller.assert_redirected_to :controller => 'account', :action => 'login' - end -end - -class XmlLoginProxy < BaseLoginProxy - protected - def authenticate - @controller.accept 'application/xml' - @controller.authorize_as @login if @login - end - - def check - @controller.assert_response 401 - end +module AuthenticatedTestHelper + # Sets the current user in the session from the user fixtures. + def login_as(user) + @request.session['user_id'] = user ? users(user).id : nil + end + + def content_type(type) + @request.env['Content-Type'] = type + end + + def accept(accept) + @request.env["HTTP_ACCEPT"] = accept + end + + def authorize_as(user) + if user + @request.env["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{users(user).login}:test")}" + accept 'application/xml' + content_type 'application/xml' + else + @request.env["HTTP_AUTHORIZATION"] = nil + accept nil + content_type nil + end + end + + # http://project.ioni.st/post/217#post-217 + # + # def test_new_publication + # assert_difference(Publication, :count) do + # post :create, :publication => {...} + # # ... + # end + # end + # + def assert_difference(object, method = nil, difference = 1) + initial_value = object.send(method) + yield + assert_equal initial_value + difference, object.send(method), "#{object}##{method}" + end + + def assert_no_difference(object, method, &block) + assert_difference object, method, 0, &block + end + + # Assert the block redirects to the login + # + # assert_requires_login(:bob) { |c| c.get :edit, :id => 1 } + # + def assert_requires_login(login = nil) + yield HttpLoginProxy.new(self, login) + end + + def assert_http_authentication_required(login = nil) + yield XmlLoginProxy.new(self, login) + end + + def reset!(*instance_vars) + instance_vars = [:controller, :request, :response] unless instance_vars.any? + instance_vars.collect! { |v| "@#{v}".to_sym } + instance_vars.each do |var| + instance_variable_set(var, instance_variable_get(var).class.new) + end + end +end + +class BaseLoginProxy + attr_reader :controller + attr_reader :options + def initialize(controller, login) + @controller = controller + @login = login + end + + private + def authenticated + raise NotImplementedError + end + + def check + raise NotImplementedError + end + + def method_missing(method, *args) + @controller.reset! + authenticate + @controller.send(method, *args) + check + end +end + +class HttpLoginProxy < BaseLoginProxy + protected + def authenticate + @controller.login_as @login if @login + end + + def check + @controller.assert_redirected_to :controller => 'account', :action => 'login' + end +end + +class XmlLoginProxy < BaseLoginProxy + protected + def authenticate + @controller.accept 'application/xml' + @controller.authorize_as @login if @login + end + + def check + @controller.assert_response 401 + end end \ No newline at end of file diff --git a/test/functional/recurring_todos_controller_test.rb b/test/functional/recurring_todos_controller_test.rb index 1d0d174e..6ba70d75 100644 --- a/test/functional/recurring_todos_controller_test.rb +++ b/test/functional/recurring_todos_controller_test.rb @@ -18,5 +18,5 @@ class RecurringTodosControllerTest < ActionController::TestCase xhr :post, :destroy, :id => 1, :_source_view => 'todo' assert_rjs :page, "recurring_todo_1", :remove end - + end diff --git a/test/unit/recurring_todo_test.rb b/test/unit/recurring_todo_test.rb index 068bb35c..f3fccbbd 100644 --- a/test/unit/recurring_todo_test.rb +++ b/test/unit/recurring_todo_test.rb @@ -174,12 +174,14 @@ class RecurringTodoTest < Test::Rails::TestCase def test_yearly_pattern # beginning of same year due_date = @yearly.get_due_date(Time.utc(2008,2,10)) # feb 10th - assert_equal @sunday, due_date # june 8th + assert_equal @sunday, due_date # june 8th + # same month, previous date due_date = @yearly.get_due_date(@saturday) # june 7th show_from_date = @yearly.get_show_from_date(@saturday) # june 7th assert_equal @sunday, due_date # june 8th assert_equal @sunday-5.days, show_from_date + # same month, day after due_date = @yearly.get_due_date(@monday) # june 9th assert_equal Time.utc(2009,6,8), due_date # june 8th next year @@ -197,7 +199,7 @@ class RecurringTodoTest < Test::Rails::TestCase due_date = @yearly.get_due_date(Time.utc(2008,6,12)) # june 7th assert_equal Time.utc(2009,6,10), due_date # june 10th - # test handling of nil + # test handling of nil due_date1 = @yearly.get_due_date(nil) due_date2 = @yearly.get_due_date(Time.now.utc + 1.day) assert_equal due_date1, due_date2 @@ -207,7 +209,7 @@ class RecurringTodoTest < Test::Rails::TestCase due_date = @yearly.get_due_date(nil) assert_equal due_date.year, this_year+2 end - + def test_toggle_completion t = @yearly assert_equal :active, t.current_state