Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / base_test.rb
1 require 'abstract_unit'
2 require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
3
4 # Provide some controller to run the tests on.
5 module Submodule
6 class ContainedEmptyController < ActionController::Base
7 end
8 class ContainedNonEmptyController < ActionController::Base
9 def public_action
10 render :nothing => true
11 end
12
13 hide_action :hidden_action
14 def hidden_action
15 raise "Noooo!"
16 end
17
18 def another_hidden_action
19 end
20 hide_action :another_hidden_action
21 end
22 class SubclassedController < ContainedNonEmptyController
23 hide_action :public_action # Hiding it here should not affect the superclass.
24 end
25 end
26 class EmptyController < ActionController::Base
27 end
28 class NonEmptyController < ActionController::Base
29 def public_action
30 end
31
32 hide_action :hidden_action
33 def hidden_action
34 end
35 end
36
37 class MethodMissingController < ActionController::Base
38
39 hide_action :shouldnt_be_called
40 def shouldnt_be_called
41 raise "NO WAY!"
42 end
43
44 protected
45
46 def method_missing(selector)
47 render :text => selector.to_s
48 end
49
50 end
51
52 class DefaultUrlOptionsController < ActionController::Base
53 def default_url_options_action
54 end
55
56 def default_url_options(options = nil)
57 { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
58 end
59 end
60
61 class ControllerClassTests < Test::Unit::TestCase
62 def test_controller_path
63 assert_equal 'empty', EmptyController.controller_path
64 assert_equal EmptyController.controller_path, EmptyController.new.controller_path
65 assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
66 assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
67 end
68 def test_controller_name
69 assert_equal 'empty', EmptyController.controller_name
70 assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
71 end
72 end
73
74 class ControllerInstanceTests < Test::Unit::TestCase
75 def setup
76 @empty = EmptyController.new
77 @contained = Submodule::ContainedEmptyController.new
78 @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
79
80 @non_empty_controllers = [NonEmptyController.new,
81 Submodule::ContainedNonEmptyController.new]
82 end
83
84 def test_action_methods
85 @empty_controllers.each do |c|
86 hide_mocha_methods_from_controller(c)
87 assert_equal Set.new, c.__send__(:action_methods), "#{c.controller_path} should be empty!"
88 end
89 @non_empty_controllers.each do |c|
90 hide_mocha_methods_from_controller(c)
91 assert_equal Set.new(%w(public_action)), c.__send__(:action_methods), "#{c.controller_path} should not be empty!"
92 end
93 end
94
95 protected
96 # Mocha adds some public instance methods to Object that would be
97 # considered actions, so explicitly hide_action them.
98 def hide_mocha_methods_from_controller(controller)
99 mocha_methods = [
100 :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
101 :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
102 ]
103 controller.class.__send__(:hide_action, *mocha_methods)
104 end
105 end
106
107
108 class PerformActionTest < Test::Unit::TestCase
109 class MockLogger
110 attr_reader :logged
111
112 def initialize
113 @logged = []
114 end
115
116 def method_missing(method, *args)
117 @logged << args.first
118 end
119 end
120
121 def use_controller(controller_class)
122 @controller = controller_class.new
123
124 # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
125 # a more accurate simulation of what happens in "real life".
126 @controller.logger = Logger.new(nil)
127
128 @request = ActionController::TestRequest.new
129 @response = ActionController::TestResponse.new
130
131 @request.host = "www.nextangle.com"
132 end
133
134 def test_get_on_priv_should_show_selector
135 use_controller MethodMissingController
136 get :shouldnt_be_called
137 assert_response :success
138 assert_equal 'shouldnt_be_called', @response.body
139 end
140
141 def test_method_missing_is_not_an_action_name
142 use_controller MethodMissingController
143 assert ! @controller.__send__(:action_methods).include?('method_missing')
144
145 get :method_missing
146 assert_response :success
147 assert_equal 'method_missing', @response.body
148 end
149
150 def test_get_on_hidden_should_fail
151 use_controller NonEmptyController
152 get :hidden_action
153 assert_response 404
154
155 get :another_hidden_action
156 assert_response 404
157 end
158
159 def test_namespaced_action_should_log_module_name
160 use_controller Submodule::ContainedNonEmptyController
161 @controller.logger = MockLogger.new
162 get :public_action
163 assert_match /Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger.logged[1]
164 end
165 end
166
167 class DefaultUrlOptionsTest < Test::Unit::TestCase
168 def setup
169 @controller = DefaultUrlOptionsController.new
170
171 @request = ActionController::TestRequest.new
172 @response = ActionController::TestResponse.new
173
174 @request.host = 'www.example.com'
175 end
176
177 def test_default_url_options_are_used_if_set
178 ActionController::Routing::Routes.draw do |map|
179 map.default_url_options 'default_url_options', :controller => 'default_url_options'
180 map.connect ':controller/:action/:id'
181 end
182
183 get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
184
185 assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
186 assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
187 ensure
188 ActionController::Routing::Routes.load!
189 end
190 end
191
192 class EmptyUrlOptionsTest < Test::Unit::TestCase
193 def setup
194 @controller = NonEmptyController.new
195
196 @request = ActionController::TestRequest.new
197 @response = ActionController::TestResponse.new
198
199 @request.host = 'www.example.com'
200 end
201
202 def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
203 get :public_action
204 assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
205 end
206 end
207
208 class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase
209 def test_named_routes_still_work
210 ActionController::Routing::Routes.draw do |map|
211 map.resources :things
212 end
213 EmptyController.send :include, ActionController::UrlWriter
214
215 assert_equal '/things', EmptyController.new.send(:things_path)
216 ensure
217 ActionController::Routing::Routes.load!
218 end
219 end