Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / test / functional / .svn / text-base / site_controller_test.rb.svn-base
blob083c75ddc9333125ecd620811c58951068d531ef
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'site_controller'
4 # Re-raise errors caught by the controller.
5 class SiteController; def rescue_action(e) raise e end; end
7 class SiteControllerTest < Test::Unit::TestCase
8   fixtures :pages, :page_parts
9   test_helper :pages
10   
11   def setup
12     @controller = SiteController.new
13     @request    = ActionController::TestRequest.new
14     @response   = ActionController::TestResponse.new
15     @cache      = @controller.cache
16     @cache.perform_caching = false
17     @cache.clear
18   end
20   def test_initialize
21     assert_equal Radiant::Config, @controller.config
22     assert_kind_of ResponseCache, @cache
23   end
25   def test_show_page__home_page
26     get :show_page, :url => ''
27     assert_response :success
28     assert_equal 'This is the body portion of the Ruby home page.', @response.body
29   end
30   
31   def test_show_page__one_level_deep
32     get :show_page, :url => 'documentation/'
33     assert_response :success
34     assert_equal 'This is the documentation section.', @response.body
35   end
37   def test_show_page__two_levels_deep
38     get :show_page, :url => 'documentation/books/'
39     assert_response :success
40     assert_equal 'This is the books page.', @response.body
41   end
42   
43   def test_show_page__not_found
44     get :show_page, :url => 'asdf'
45     assert_response :missing
46     assert_template 'site/not_found'
47   end
48   
49   def test_show_page__missing_root_redirects_to_admin
50     pages(:homepage).destroy
51     get :show_page, :url => '/'
52     assert_redirected_to welcome_url
53   end
54   
55   def test_show_page__radius_integration
56     get :show_page, :url => 'radius'
57     assert_response :success
58     assert_equal "<h1>Radius Test Page</h1>\n\n\n\t<ul>\n\t<li>Radius Test Child 1</li>\n\t\t<li>Radius Test Child 2</li>\n\t\t<li>Radius Test Child 3</li>\n\t</ul>", @response.body
59   end
60   
61   def test_show_page__not_published
62     ['draft', 'hidden'].each do |url|
63       get :show_page, :url => url
64       assert_response :missing, "for URL: #{url}"
65       assert_template 'site/not_found', "for URL: #{url}"
66     end
67     
68     #validates the custom 404 page is rendered
69     get :show_page, :url => "/gallery/gallery_draft/"
70     assert_response :missing
71     assert_template nil
72   end
73   
74   def test_show_page__on_dev_site
75     @request.host = 'dev.site.com'
76     ['draft', 'hidden'].each do |url|
77       get :show_page, :url => url
78       assert_response :success, "url: #{url}"
79     end
80   end
81   
82   def test_show_page__on_dev_site_in_conf
83     @controller.config = { 'dev.host' => 'mysite.com'  }
84     
85     @request.host = 'mysite.com'
86     ['draft', 'hidden'].each do |url|
87       get :show_page, :url => url
88       assert_response :success, "url: #{url}"
89     end
90     
91     @request.host = 'dev.site.com' # should function like live site because of config
92     ['draft', 'hidden'].each do |url|
93       get :show_page, :url => url
94       assert_response :missing, "url: #{url}"
95     end
96   end
97   
98   def test_show_page__does_not_have_no_cache_header
99     get :show_page, :url => '/'
100     assert_equal false, @response.headers.keys.include?('Cache-Control')
101   end
102   
103   class TestPage
104     def request
105       @request
106     end
107     def response
108       @response
109     end
110     def process(request, response)
111       @request, @response = request, response
112       response.headers['Status'] = '200 OK'
113     end
114     def cache?
115       true
116     end
117   end
118   
119   def test_show_page__page_processed
120     class << @controller
121       def find_page(url)
122         SiteControllerTest::TestPage.new
123       end
124     end
125     get :show_page, :url => 'really/just/a/test'
126     assert_response :success
127     page = assigns(:page)
128     assert_same @request, page.request
129     assert_same @response, page.response
130   end
131   
132   def test_show_page__cached
133     @controller.cache.perform_caching = true
134     @cache.clear
135     get :show_page, :url => 'documentation'
136     assert_response :success
137     assert File.exists?(cache_file('documentation'))
138   end
139   
140   def test_show_page__no_cache
141     @controller.cache.perform_caching = true
142     @cache.clear
143     get :show_page, :url => 'no-cache'
144     assert_response :success
145     assert !File.exists?(cache_file('no-cache'))
146   end
147   
148   def test_show_page__no_cache_on_dev_site
149     @controller.cache.perform_caching = true
150     @request.host = 'dev.site.com'
151     @cache.clear
152     get :show_page, :url => 'documentation'
153     assert_response :success
154     assert !File.exists?(cache_file('documentation'))
155   end
156   
157   def test_show_page__no_cache_on_dev_site__cached
158     @controller.cache.perform_caching = true
159     @request.host = 'dev.site.com'
160     @cache.cache_response('documentation', response(:body => 'expired body'))
161     get :show_page, :url => 'documentation'
162     assert_response :success
163     assert_equal 'This is the documentation section.', @response.body
164   end
165   
166   def test_show_page__no_cache_if_post 
167     @controller.cache.perform_caching = true 
168     @cache.clear 
169     post :show_page, :url => 'documentation' 
170     assert_response :success 
171     assert !File.exists?(cache_file('documentation')) 
172   end 
174   def test_show_page__no_cache_if_post__cached 
175     @controller.cache.perform_caching = true 
176     @request.host = 'dev.site.com' 
177     @cache.cache_response('documentation', response(:body => 'expired body')) 
178     post :show_page, :url => 'documentation' 
179     assert_response :success 
180     assert_equal 'This is the documentation section.', @response.body 
181   end 
182   
183   
184   def test_show_page__no_pages
185     Page.destroy_all
186     get :show_page, :url => '/'
187     assert_redirected_to welcome_url
188   end
189   
190   private
191   
192     def cache_file(path)
193       "#{@cache.directory}/#{path}.yml"
194     end
195     
196     def response(options = {})
197       r = ActionController::TestResponse.new
198       options.each do |k, v|
199         r.send("#{k}=", v)
200       end
201       r
202     end