aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/flash_test.rb
blob: c448f36cb3d0b1dc09c373867c2f7e05ee2adb58 (plain) (blame)
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
require 'abstract_unit'

class FlashTest < ActionController::TestCase
  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"]
      flash.keep
      render :inline => "hello"
    end
    
    def use_flash_and_update_it
      flash.update("this" => "hello again")
      @flash_copy = {}.update flash
      render :inline => "hello"
    end

    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

    def rescue_action(e)
      raise unless ActionView::MissingTemplate === e
    end

    # methods for test_sweep_after_halted_filter_chain
    before_filter :halt_and_redir, :only => "filter_halting_action"

    def std_action
      @flash_copy = {}.update(flash)
      render :nothing => true
    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
  end

  tests TestController

  def test_flash
    get :set_flash

    get :use_flash
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "hello", assigns["flashy"]

    get :use_flash
    assert_nil assigns["flash_copy"]["that"], "On second flash"
  end

  def test_keep_flash
    get :set_flash
    
    get :use_flash_and_keep_it
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "hello", assigns["flashy"]

    get :use_flash
    assert_equal "hello", assigns["flash_copy"]["that"], "On second flash"

    get :use_flash
    assert_nil assigns["flash_copy"]["that"], "On third flash"
  end
  
  def test_flash_now
    get :set_flash_now
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "bar"  , assigns["flash_copy"]["foo"]
    assert_equal "hello", assigns["flashy"]

    get :attempt_to_use_flash_now
    assert_nil assigns["flash_copy"]["that"]
    assert_nil assigns["flash_copy"]["foo"]
    assert_nil assigns["flashy"]
  end 
  
  def test_update_flash
    get :set_flash
    get :use_flash_and_update_it
    assert_equal "hello",       assigns["flash_copy"]["that"]
    assert_equal "hello again", assigns["flash_copy"]["this"]
    get :use_flash
    assert_nil                  assigns["flash_copy"]["that"], "On second flash"
    assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash"
  end

  def test_flash_after_reset_session
    get :use_flash_after_reset_session
    assert_equal "hello",    assigns["flashy_that"]
    assert_equal "good-bye", assigns["flashy_this"]
    assert_nil   assigns["flashy_that_reset"]
  end 

  def test_does_not_set_the_session_if_the_flash_is_empty
    get :std_action
    assert_nil session["flash"]
  end

  def test_sweep_after_halted_filter_chain
    get :std_action
    assert_nil assigns["flash_copy"]["foo"]
    get :filter_halting_action
    assert_equal "bar", assigns["flash_copy"]["foo"]
    get :std_action # follow redirection
    assert_equal "bar", assigns["flash_copy"]["foo"]
    get :std_action
    assert_nil assigns["flash_copy"]["foo"]
  end

  def test_keep_and_discard_return_values
    flash = ActionController::Flash::FlashHash.new
    flash.update(:foo => :foo_indeed, :bar => :bar_indeed)

    assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed
    assert_nil flash.discard(:unknown) # non existant key passed
    assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard()) # nothing passed
    assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed      

    assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed
    assert_nil flash.keep(:unknown) # non existant key passed
    assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep()) # nothing passed
    assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed     
  end
end