Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / test / functional / admin / .svn / text-base / page_controller_test.rb.svn-base
blobce3f4cf075db22601ccf5847834364f96c4c46e0
1 require File.dirname(__FILE__) + '/../../test_helper'
2 require 'admin/page_controller'
4 # Re-raise errors caught by the controller.
5 class Admin::PageController; def rescue_action(e) raise e end; end
7 class Admin::PageControllerTest < Test::Unit::TestCase
8   fixtures :users, :pages
9   test_helper :pages, :page_parts, :caching, :login
10   
11   def setup
12     @controller = Admin::PageController.new
13     @request    = ActionController::TestRequest.new
14     @response   = ActionController::TestResponse.new
15     login_as :existing
16     
17     @page_title = 'Just a Test'
18     
19     destroy_test_page
20     destroy_test_part
21   end
22   
23   def test_initialize
24     assert_kind_of ResponseCache, @controller.cache
25   end
27   def test_index
28     get :index
29     assert_response :success
30     assert_kind_of Page, assigns(:homepage)
31   end
32   
33   def test_index__without_pages
34     Page.destroy_all
35     get :index
36     assert_response :success
37     assert_nil assigns(:homepage)
38   end
40   def test_index__without_cookies
41     get :index
42     assert_response :success
43     assert_rendered_nodes_where { |page| [nil, 1].include?(page.parent_id) }
44   end
46   def test_index__with_empty_cookie
47     write_cookie('expanded_rows', '')
48     get :index
49     assert_response :success
50     assert '', cookies['expanded_rows']
51     assert_rendered_nodes_where { |page| [nil, 1].include?(page.parent_id) }
52   end
54   def test_index__with_cookie
55     write_cookie('expanded_rows', '1,5,9,10,11,12,52')
56     get :index
57     assert_response :success
58     assert '1,5,9,10,11,12,52', cookies['expanded_rows']
59     assert_rendered_nodes_where { |page| [nil, 1, 5, 9, 52, 10, 11, 12].include?(page.parent_id) }
60   end
61   
62   def test_index__with_mangled_cookie
63     write_cookie('expanded_rows', '1,5,:#*)&},9a,,,')
64     get :index
65     assert_response :success
66     assert '1,5,:#*)&},9a,,,', cookies['expanded_rows']
67     assert_rendered_nodes_where { |page| [nil, 1, 5].include?(page.parent_id) }
68     assert !assigns(:homepage).nil?
69   end
70   
71   def test_new
72     @controller.config = {
73       'defaults.page.parts' => 'body, extended, summary',
74       'defaults.page.status' => 'published'
75     }
76     
77     get :new, :parent_id => '1', :page => page_params
78     assert_response :success
79     assert_template 'admin/page/edit'
80     
81     @page = assigns(:page)
82     assert_kind_of Page, @page
83     assert_nil @page.title
84     
85     @expected_parent = Page.find(1)
86     assert_equal @expected_parent, @page.parent
87     
88     assert_equal 3, @page.parts.size
89     assert_equal Status[:published], @page.status
90   end
91   def test_new__with_slug_and_breadcrumb
92     get :new, :parent_id => '1', :page => page_params, :slug => 'test', :breadcrumb => 'me'
93     assert_response :success
94     
95     @page = assigns(:page)
96     assert_equal 'test', @page.slug
97     assert_equal 'me', @page.breadcrumb
98   end
99   def test_new__post
100     @cache = @controller.cache = FakeResponseCache.new
101     post :new, :parent_id => '1', :page => page_params
102     assert_redirected_to page_index_url
103     assert_match /saved/, flash[:notice]
104     
105     @page = assigns(:page)
106     assert_equal 0, @page.parts.size
107     
108     @page = get_test_page
109     assert_kind_of Page, @page
110     assert_equal '/page/', @cache.expired_path
111   end
112   def test_new__post_with_invalid_page
113     post :new, :parent_id => '1', :page => page_params(:status_id => 'abc')
114     assert_response :success
115     assert_match /error/, flash[:error]
116     assert_nil get_test_page
117   end
118   def test_new__post_with_parts
119     post(:new, :parent_id => '1', :page => page_params,
120       :part => {
121         '1' => part_params(:name => 'test-part-1'),
122         '2' => part_params(:name => 'test-part-2')
123       }
124     )
125     assert_redirected_to page_index_url
126     
127     @page = get_test_page
128     assert_kind_of Page, @page
129     names = @page.parts.collect { |part| part.name }.sort
130     assert_equal ['test-part-1', 'test-part-2'], names
131   end
132   def test_new__post_with_invalid_part
133     @part_name = 'extra long ' * 25
134     post :new, :parent_id => '1', :page => page_params, :part => { '1' => part_params(:name => @part_name)}
135     assert_response :success # not redirected
136     assert_match /error/, flash[:error]
137     assert_nil get_test_page
138     assert_nil get_test_part
139   end
140   def test_new__save_and_continue_editing
141     post :new, :parent_id => '1', :page => page_params, :continue => 'Save and Continue Editing'
142     @page = get_test_page
143     assert_redirected_to page_edit_url(:id => @page.id)
144   end
145   def test_new_edit__meta_and_buttons_partials_initialized
146     get :new, :parent_id => '1'
147     assert_response :success
148     assert_not_nil assigns(:meta)
149     assert_not_nil assigns(:buttons_partials)
150    
151     get :edit, :id => '1'
152     assert_response :success
153     assert_not_nil assigns(:meta)
154     assert_not_nil assigns(:buttons_partials)
155   end
156   
157   def test_edit
158     get :edit, :id => '1', :page => page_params
159     assert_response :success
160     
161     @page = assigns(:page)
162     assert_kind_of Page, @page
163     assert_equal 'Ruby Home Page', @page.title
164   end  
165   def test_edit__post
166     @cache = @controller.cache = FakeResponseCache.new
167     @page = create_test_page
168     post :edit, :id => @page.id, :page => page_params(:status_id => '1')
169     assert_response :redirect
170     assert_equal 1, get_test_page.status.id
172     @page = get_test_page
173     assert_kind_of Page, @page
174     assert_equal '/page/', @cache.expired_path
175     
176     # To-Do: Test what happens when an invalid page is submitted 
177   end
178   def test_edit__post_with_slug_change
179     @cache = @controller.cache = FakeResponseCache.new
180     @page = create_test_page
181     post :edit, :id => @page.id, :page => page_params(:slug => 'monkey', :status_id => '1')
182     assert_response :redirect
183     assert_equal 1, get_test_page.status.id
185     @page = get_test_page
186     assert_kind_of Page, @page
187     assert_equal '/page/', @cache.expired_path
188     
189     # To-Do: Test what happens when an invalid page is submitted 
190   end
191   def test_edit__post_with_parts
192     @page = create_test_page(:no_part => true)
193     @page.parts.create(part_params(:name => 'test-part-1'))
194     @page.parts.create(part_params(:name => 'test-part-2'))
195     
196     assert_equal 2, @page.parts.size
197     
198     post :edit, :id => @page.id, :page => page_params, :part => {'1' => part_params(:name => 'test-part-1', :content => 'changed')}
199     assert_response :redirect
200     
201     @page = get_test_page 
202     assert_equal 1, @page.parts.size
203     assert_equal 'changed', @page.parts.first.content
204   end
206   def test_edit__post_with_optimistic_locking
207     @page = create_test_page
208     post :edit, :id => @page.id, :page => page_params(:status_id => '1', :lock_version => '12')
209     assert_response :success # not redirected
210     assert_match /has been modified/, flash[:error]
211   end
212   
213   def test_edit__post_with_parts_and_optimistic_locking
214     @page = create_test_page
215     @page.parts.create(part_params(:name => 'test-part-1', :content => 'original-1'))
216     post :edit, :id => @page.id, :page => page_params(:status_id => '1', :lock_version => '12'), :part => {'1' => part_params(:name => 'test-part-1', :content => 'changed-1')}
217     assert_response :success # not redirected
218     assert_match /has been modified/, flash[:error]
219     #changed value must not be saved
220     assert_equal 'original-1', @page.parts.find_by_name('test-part-1').content
221     #but must be rendered to page
222     assert_tag :tag => 'textarea', :content => 'changed-1' 
223   end  
224   
225   def test_remove
226     @page = create_test_page
227     get :remove, :id => @page.id 
228     assert_response :success
229     assert_equal @page, assigns(:page)
230     assert_not_nil get_test_page
231   end
232   def test_remove_should_show_all_children_regardless_of_sitemap_expansion
233     @page = pages(:homepage)
234     get :remove, :id => @page.id
235     assert_response :success
236     Page.find(:all).each do |page|
237       assert_tag "tr", :attributes => {:id => "page-#{page.id}"}
238     end
239   end
240   def test_remove__post
241     @page = create_test_page
242     post :remove, :id => @page.id
243     assert_redirected_to page_index_url
244     assert_match /removed/, flash[:notice]
245     assert_nil get_test_page
246   end
247   
248   def test_clear_cache
249     @cache = @controller.cache = FakeResponseCache.new
250     get :clear_cache
251     assert_response :success
252     assert_match /Do.*?not.*?access/i, @response.body
253     assert_equal false, @cache.cleared?
254   end
255   def test_clear_cache__post
256     @cache = @controller.cache = FakeResponseCache.new
257     post :clear_cache
258     assert_redirected_to page_index_url
259     assert_match /cache.*clear/i, flash[:notice]
260     assert_equal true, @cache.cleared?
261   end
262   
263   def test_add_part
264     get :add_part
265     assert_response :success
266     assert_template 'admin/page/_part'
267   end
268   
269   def test_children
270     get :children, :id => '1', :level => '1'
271     assert_response :success
272     assert_equal pages(:homepage), assigns(:parent)
273     assert_equal 1, assigns(:level)
274     assert ! %r{<head>}.match(@response.body)
275     assert_equal 'text/html', @response.content_type
276     assert_equal 'utf-8', @response.charset
277   end
278   
279   def test_tag_reference
280     xml_http_request :get, :tag_reference, :class_name => "Page"
281     assert_response :success
282     assert_equal "Page", assigns(:class_name)
283     assert_template "tag_reference"
284   end
286   def test_filter_reference
287     xml_http_request :get, :filter_reference, :filter_name => "Textile"
288     assert_response :success
289     assert_equal "Textile", assigns(:filter_name)
290     assert_template "filter_reference"
291   end
292   
293   protected
294   
295     def assert_rendered_nodes_where(&block)
296       wanted, unwanted = Page.find(:all).partition(&block)
297       wanted.each do |page|
298         assert_tag :tag => 'tr', :attributes => {:id => "page-#{page.id}" }
299       end
300       unwanted.each do |page|
301         assert_no_tag :tag => 'tr', :attributes => {:id => "page-#{page.id}" }
302       end
303     end
304     
305     def write_cookie(name, value)
306       @request.cookies[name] = CGI::Cookie.new(name, value)
307     end