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
12 @controller = Admin::PageController.new
13 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new
17 @page_title = 'Just a Test'
24 assert_kind_of ResponseCache, @controller.cache
29 assert_response :success
30 assert_kind_of Page, assigns(:homepage)
33 def test_index__without_pages
36 assert_response :success
37 assert_nil assigns(:homepage)
40 def test_index__without_cookies
42 assert_response :success
43 assert_rendered_nodes_where { |page| [nil, 1].include?(page.parent_id) }
46 def test_index__with_empty_cookie
47 write_cookie('expanded_rows', '')
49 assert_response :success
50 assert '', cookies['expanded_rows']
51 assert_rendered_nodes_where { |page| [nil, 1].include?(page.parent_id) }
54 def test_index__with_cookie
55 write_cookie('expanded_rows', '1,5,9,10,11,12,52')
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) }
62 def test_index__with_mangled_cookie
63 write_cookie('expanded_rows', '1,5,:#*)&},9a,,,')
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?
72 @controller.config = {
73 'defaults.page.parts' => 'body, extended, summary',
74 'defaults.page.status' => 'published'
77 get :new, :parent_id => '1', :page => page_params
78 assert_response :success
79 assert_template 'admin/page/edit'
81 @page = assigns(:page)
82 assert_kind_of Page, @page
83 assert_nil @page.title
85 @expected_parent = Page.find(1)
86 assert_equal @expected_parent, @page.parent
88 assert_equal 3, @page.parts.size
89 assert_equal Status[:published], @page.status
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
95 @page = assigns(:page)
96 assert_equal 'test', @page.slug
97 assert_equal 'me', @page.breadcrumb
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]
105 @page = assigns(:page)
106 assert_equal 0, @page.parts.size
108 @page = get_test_page
109 assert_kind_of Page, @page
110 assert_equal '/page/', @cache.expired_path
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
118 def test_new__post_with_parts
119 post(:new, :parent_id => '1', :page => page_params,
121 '1' => part_params(:name => 'test-part-1'),
122 '2' => part_params(:name => 'test-part-2')
125 assert_redirected_to page_index_url
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
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
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)
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)
151 get :edit, :id => '1'
152 assert_response :success
153 assert_not_nil assigns(:meta)
154 assert_not_nil assigns(:buttons_partials)
158 get :edit, :id => '1', :page => page_params
159 assert_response :success
161 @page = assigns(:page)
162 assert_kind_of Page, @page
163 assert_equal 'Ruby Home Page', @page.title
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
176 # To-Do: Test what happens when an invalid page is submitted
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
189 # To-Do: Test what happens when an invalid page is submitted
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'))
196 assert_equal 2, @page.parts.size
198 post :edit, :id => @page.id, :page => page_params, :part => {'1' => part_params(:name => 'test-part-1', :content => 'changed')}
199 assert_response :redirect
201 @page = get_test_page
202 assert_equal 1, @page.parts.size
203 assert_equal 'changed', @page.parts.first.content
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]
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'
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
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}"}
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
249 @cache = @controller.cache = FakeResponseCache.new
251 assert_response :success
252 assert_match /Do.*?not.*?access/i, @response.body
253 assert_equal false, @cache.cleared?
255 def test_clear_cache__post
256 @cache = @controller.cache = FakeResponseCache.new
258 assert_redirected_to page_index_url
259 assert_match /cache.*clear/i, flash[:notice]
260 assert_equal true, @cache.cleared?
265 assert_response :success
266 assert_template 'admin/page/_part'
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
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"
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"
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}" }
300 unwanted.each do |page|
301 assert_no_tag :tag => 'tr', :attributes => {:id => "page-#{page.id}" }
305 def write_cookie(name, value)
306 @request.cookies[name] = CGI::Cookie.new(name, value)