2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class FlashTest < ActionController::TestCase
|
2007-01-22 14:43:50 +01:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
def set_flash
|
|
|
|
flash["that"] = "hello"
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_flash_now
|
|
|
|
flash.now["that"] = "hello"
|
|
|
|
flash.now["foo"] ||= "bar"
|
|
|
|
flash.now["foo"] ||= "err"
|
|
|
|
@flashy = flash.now["that"]
|
|
|
|
@flash_copy = {}.update flash
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_to_use_flash_now
|
|
|
|
@flash_copy = {}.update flash
|
|
|
|
@flashy = flash["that"]
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_flash
|
|
|
|
@flash_copy = {}.update flash
|
|
|
|
@flashy = flash["that"]
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_flash_and_keep_it
|
|
|
|
@flash_copy = {}.update flash
|
|
|
|
@flashy = flash["that"]
|
2007-12-21 08:48:59 +01:00
|
|
|
flash.keep
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_flash_and_update_it
|
|
|
|
flash.update("this" => "hello again")
|
|
|
|
@flash_copy = {}.update flash
|
2007-01-22 14:43:50 +01:00
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def use_flash_after_reset_session
|
|
|
|
flash["that"] = "hello"
|
|
|
|
@flashy_that = flash["that"]
|
|
|
|
reset_session
|
|
|
|
@flashy_that_reset = flash["that"]
|
|
|
|
flash["this"] = "good-bye"
|
|
|
|
@flashy_this = flash["this"]
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def rescue_action(e)
|
2008-05-18 06:22:34 +02:00
|
|
|
raise unless ActionView::MissingTemplate === e
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
# methods for test_sweep_after_halted_filter_chain
|
|
|
|
before_filter :halt_and_redir, :only => "filter_halting_action"
|
|
|
|
|
|
|
|
def std_action
|
|
|
|
@flash_copy = {}.update(flash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_halting_action
|
|
|
|
@flash_copy = {}.update(flash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def halt_and_redir
|
|
|
|
flash["foo"] = "bar"
|
|
|
|
redirect_to :action => "std_action"
|
|
|
|
@flash_copy = {}.update(flash)
|
|
|
|
end
|
2010-05-25 19:45:45 +02:00
|
|
|
|
|
|
|
def redirect_with_alert
|
|
|
|
redirect_to '/nowhere', :alert => "Beware the nowheres!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_with_notice
|
|
|
|
redirect_to '/somewhere', :notice => "Good luck in the somewheres!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_with_other_flashes
|
|
|
|
redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
tests TestController
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
def test_flash
|
|
|
|
get :set_flash
|
|
|
|
|
|
|
|
get :use_flash
|
|
|
|
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "hello", @response.template.assigns["flashy"]
|
|
|
|
|
|
|
|
get :use_flash
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_keep_flash
|
|
|
|
get :set_flash
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
get :use_flash_and_keep_it
|
2007-01-22 14:43:50 +01:00
|
|
|
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "hello", @response.template.assigns["flashy"]
|
|
|
|
|
|
|
|
get :use_flash
|
|
|
|
assert_equal "hello", @response.template.assigns["flash_copy"]["that"], "On second flash"
|
|
|
|
|
|
|
|
get :use_flash
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["that"], "On third flash"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_flash_now
|
|
|
|
get :set_flash_now
|
|
|
|
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "bar" , @response.template.assigns["flash_copy"]["foo"]
|
|
|
|
assert_equal "hello", @response.template.assigns["flashy"]
|
|
|
|
|
|
|
|
get :attempt_to_use_flash_now
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["that"]
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["foo"]
|
|
|
|
assert_nil @response.template.assigns["flashy"]
|
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_update_flash
|
|
|
|
get :set_flash
|
|
|
|
get :use_flash_and_update_it
|
|
|
|
assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "hello again", @response.template.assigns["flash_copy"]["this"]
|
|
|
|
get :use_flash
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
|
|
|
|
assert_equal "hello again", @response.template.assigns["flash_copy"]["this"], "On second flash"
|
|
|
|
end
|
2009-08-04 17:16:03 +02:00
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def test_flash_after_reset_session
|
|
|
|
get :use_flash_after_reset_session
|
|
|
|
assert_equal "hello", @response.template.assigns["flashy_that"]
|
|
|
|
assert_equal "good-bye", @response.template.assigns["flashy_this"]
|
|
|
|
assert_nil @response.template.assigns["flashy_that_reset"]
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
def test_sweep_after_halted_filter_chain
|
|
|
|
get :std_action
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["foo"]
|
|
|
|
get :filter_halting_action
|
|
|
|
assert_equal "bar", @response.template.assigns["flash_copy"]["foo"]
|
|
|
|
get :std_action # follow redirection
|
|
|
|
assert_equal "bar", @response.template.assigns["flash_copy"]["foo"]
|
|
|
|
get :std_action
|
|
|
|
assert_nil @response.template.assigns["flash_copy"]["foo"]
|
|
|
|
end
|
2009-08-04 17:16:03 +02:00
|
|
|
|
|
|
|
def test_does_not_set_the_session_if_the_flash_is_empty
|
|
|
|
get :std_action
|
|
|
|
assert_nil session["flash"]
|
|
|
|
end
|
2010-05-25 19:45:45 +02:00
|
|
|
|
|
|
|
def test_redirect_to_with_alert
|
|
|
|
get :redirect_with_alert
|
|
|
|
assert_equal "Beware the nowheres!", @controller.send(:flash)[:alert]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_redirect_to_with_notice
|
|
|
|
get :redirect_with_notice
|
|
|
|
assert_equal "Good luck in the somewheres!", @controller.send(:flash)[:notice]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_redirect_to_with_other_flashes
|
|
|
|
get :redirect_with_other_flashes
|
|
|
|
assert_equal "Horses!", @controller.send(:flash)[:joyride]
|
|
|
|
end
|
|
|
|
end
|