1 require File.dirname(__FILE__) + '/../../spec_helper'
3 describe Admin::PageController do
4 scenario :users, :pages
5 test_helper :pages, :page_parts, :caching
13 it "should setup the response cache when it initializes" do
14 @controller.cache.should be_kind_of(ResponseCache)
17 it "should allow you to view the index" do
19 response.should be_success
20 assigns(:homepage).should be_kind_of(Page)
23 it "should allow the index to render even with there are no pages" do
26 response.should be_success
27 assigns(:homepage).should be_nil
30 it "should show the tree partialy expanded on the index" do
32 response.should be_success
33 assert_rendered_nodes_where { |page| [nil, page_id(:home)].include?(page.parent_id) }
36 it "should show the tree partialy expanded on the index even when the expanded_rows cookie is empty" do
37 write_cookie('expanded_rows', '')
39 response.should be_success
40 cookies['expanded_rows'].should be_nil
41 assert_rendered_nodes_where { |page| [nil, page_id(:home)].include?(page.parent_id) }
44 it "should show the tree partially expanded on the index according to the expanded_rows cookie" #do
45 # write_cookie('expanded_rows', '1,5,9,10,11,12,52')
47 # response.should be_success
48 # cookies['expanded_rows'].should == '1,5,9,10,11,12,52'
49 # assert_rendered_nodes_where { |page| [nil, 1, 5, 9, 52, 10, 11, 12].include?(page.parent_id) }
52 it "should index__with_mangled_cookie" #do
53 # write_cookie('expanded_rows', '1,5,:#*)&},9a,,,')
55 # response.should be_success
56 # cookies['expanded_rows'].should == '1,5,:#*)&},9a,,,'
57 # assert_rendered_nodes_where { |page| [nil, 1, 5].include?(page.parent_id) }
58 # assigns(:homepage).should_not be_nil
61 it "should allow you to enter information for a new page" do
62 @controller.config = {
63 'defaults.page.parts' => 'body, extended, summary',
64 'defaults.page.status' => 'published'
67 get :new, :parent_id => page_id(:home), :page => page_params
68 response.should be_success
69 response.should render_template('admin/page/edit')
72 page.should be_kind_of(Page)
73 page.title.should be_nil
74 page.parent.should == pages(:home)
75 page.parts.size.should == 3
76 page.status.should == Status[:published]
79 it "should allow you to enter information for a new page and set the slug and breadcrumb" do
80 get :new, :parent_id => page_id(:home), :page => page_params, :slug => 'test', :breadcrumb => 'me'
81 response.should be_success
84 page.slug.should == 'test'
85 page.breadcrumb.should == 'me'
88 it "should allow you to create a new page" do
89 @cache = @controller.cache = FakeResponseCache.new
90 post :new, :parent_id => page_id(:home), :page => page_params(:title => "New Page")
91 response.should redirect_to(page_index_url)
92 flash[:notice].should match(/saved/)
95 page.parts.size.should == 0
97 Page.find_by_title("New Page").should_not be_nil
98 @cache.expired_path.should == '/new-page/'
101 it "should show errors when you try and create a new page with invalid data" do
102 post :new, :parent_id => page_id(:home), :page => page_params(:title => "New Page", :status_id => 'abc')
103 response.should be_success
104 flash[:error].should match(/error/)
105 Page.find_by_title("New Page").should be_nil
108 it "should allow you to create a new page with page parts" do
109 post(:new, :parent_id => page_id(:home), :page => page_params(:title => "New Page"),
111 '1' => part_params(:name => 'test-part-1'),
112 '2' => part_params(:name => 'test-part-2')
115 response.should redirect_to(page_index_url)
117 page = Page.find_by_title("New Page")
118 page.should_not be_nil
120 names = page.parts.collect { |part| part.name }.sort
121 names.should == ['test-part-1', 'test-part-2']
124 it "should not allow you to create a new page with an invalid part" do
125 @part_name = 'extra long ' * 25
126 post :new, :parent_id => page_id(:home), :page => page_params(:title => "New Page"), :part => { '1' => part_params(:name => @part_name)}
127 response.should be_success # not redirected
128 flash[:error].should match(/error/)
129 Page.find_by_title("New Page").should be_nil
130 PagePart.find_by_name(@part_name).should be_nil
133 it "should allow you to save and continue editing" do
134 post :new, :parent_id => page_id(:home), :page => page_params(:title => "New Page"), :continue => 'Save and Continue Editing'
135 page = Page.find_by_title("New Page")
136 response.should redirect_to(page_edit_url(:id => page.id))
139 it "should initialize meta and buttons_partials in new action" do
140 get :new, :parent_id => page_id(:home)
141 response.should be_success
142 assigns(:meta).should be_kind_of(Array)
143 assigns(:buttons_partials).should be_kind_of(Array)
146 it "should initialize meta and buttons_partials in edit action" do
147 get :edit, :id => page_id(:home)
148 response.should be_success
149 assigns(:meta).should be_kind_of(Array)
150 assigns(:buttons_partials).should be_kind_of(Array)
153 it "should allow you to edit a page" do
154 get :edit, :id => page_id(:home), :page => page_params
155 response.should be_success
157 page = assigns(:page)
158 page.should be_kind_of(Page)
159 page.title.should == 'Home'
162 it "should allow you to save changes to a page" do
163 @cache = @controller.cache = FakeResponseCache.new
164 post :edit, :id => page_id(:home), :page => { :title => "Updated Home Page" }
165 response.should be_redirect
167 page.title.should == "Updated Home Page"
168 @cache.expired_path.should == '/'
170 # To-Do: Test what happens when an invalid page is submitted
173 it "should expire the cache correctly when you change a page's slug" do
174 @cache = @controller.cache = FakeResponseCache.new
175 post :edit, :id => page_id(:first), :page => { :slug => 'monkey' }
176 response.should be_redirect
178 page.slug.should == 'monkey'
179 @cache.expired_path.should == '/first/'
182 it "should allow you to save changes to a page and its parts" do
183 create_page("New Page") do
184 create_page_part('test-part-1')
185 create_page_part('test-part-2')
187 page = pages(:new_page)
188 page.parts.size.should == 2
190 post :edit, :id => page.id, :page => {}, :part => {'1' => part_params(:name => 'test-part-1', :content => 'changed')}
191 response.should be_redirect
193 page = pages(:new_page)
194 page.parts.size.should == 1
195 page.parts.first.content.should == 'changed'
198 it "should allow you to edit pages with optimistic locking" do
199 create_page("New Page")
200 post :edit, :id => page_id(:new_page), :page => page_params(:status_id => '1', :lock_version => '12')
201 response.should be_success # not redirected
202 flash[:error].should match(/has been modified/)
205 it "should allow you to edit pages and parts with optimistic locking" do
206 create_page("New Page") do
207 create_page_part('test-part-1', :content => 'original-1')
210 post :edit, :id => page_id(:new_page), :page => page_params(:status_id => '1', :lock_version => '12'), :part => {'1' => part_params(:name => 'test-part-1', :content => 'changed-1')}
211 response.should be_success # not redirected
212 flash[:error].should match(/has been modified/)
214 # changed value must not be saved
215 page_parts(:test_part_1).content.should == 'original-1'
217 # but must be rendered to page
218 response.should have_tag('textarea', 'changed-1')
221 it "should prompt you when deleting a page" do
223 get :remove, :id => page.id
224 response.should be_success
225 assigns(:page).should == page
226 Page.find(page.id).should_not be_nil
229 it "should show all children when prompting you during deletion regardless of sitemap expansion" do
231 get :remove, :id => page.id
232 response.should be_success
233 Page.find(:all).each do |page|
234 @response.should have_tag("tr#page-#{page.id}")
238 it "should allow you to delete a page" do
240 post :remove, :id => page.id
241 response.should redirect_to(page_index_url)
242 flash[:notice].should match(/removed/)
243 Page.find_by_id(page.id).should be_nil
246 it "should prompt you when clearing the page cache" do
247 @cache = @controller.cache = FakeResponseCache.new
249 response.should be_success
250 @response.body.should match(/Do.*?not.*?access/i)
251 @cache.should_not be_cleared
254 it "should allow you to clear the page cache" do
255 @cache = @controller.cache = FakeResponseCache.new
257 response.should redirect_to(page_index_url)
258 flash[:notice].should match(/cache.*clear/i)
259 @cache.should be_cleared
262 it "should use the _part template when adding a part with AJAX" do
263 xml_http_request :get, :add_part
264 response.should be_success
265 response.should render_template('admin/page/_part')
268 it "should render the appropriate children when branch of the site map is expanded via AJAX" do
269 xml_http_request :get, :children, :id => page_id(:home), :level => '1'
270 response.should be_success
271 assigns(:parent).should == pages(:home)
272 assigns(:level).should == 1
273 response.body.should_not have_text('<head>')
274 response.content_type.should == 'text/html'
275 response.charset.should == 'utf-8'
278 it "should render the appropriate tag reference when requested via AJAX" do
279 xml_http_request :get, :tag_reference, :class_name => "Page"
280 response.should be_success
281 assigns(:class_name).should == "Page"
282 response.should render_template("tag_reference")
285 it "should render the appropriate filter reference when requested via AJAX" do
286 xml_http_request :get, :filter_reference, :filter_name => "Textile"
287 response.should be_success
288 assigns(:filter_name).should == "Textile"
289 response.should render_template("filter_reference")
294 def assert_rendered_nodes_where(&block)
295 wanted, unwanted = Page.find(:all).partition(&block)
296 wanted.each do |page|
297 response.should have_tag("tr#page-#{page.id}")
299 unwanted.each do |page|
300 response.should_not have_tag("tr#page-#{page.id}")
304 def write_cookie(name, value)
305 @request.cookies[name] = CGI::Cookie.new(name, value)