Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / spec / controllers / admin / .svn / text-base / page_controller_spec.rb.svn-base
blob6f6d5a403e1efac6aa8cc396313ca2d1d50fa463
1 require File.dirname(__FILE__) + '/../../spec_helper'
3 describe Admin::PageController do
4   scenario :users, :pages
5   test_helper :pages, :page_parts, :caching
6   
7   integrate_views
8   
9   before :each do
10     login_as :existing
11   end
12   
13   it "should setup the response cache when it initializes" do
14     @controller.cache.should be_kind_of(ResponseCache)
15   end
16   
17   it "should allow you to view the index" do
18     get :index
19     response.should be_success
20     assigns(:homepage).should be_kind_of(Page) 
21   end
22   
23   it "should allow the index to render even with there are no pages" do
24     Page.destroy_all
25     get :index
26     response.should be_success
27     assigns(:homepage).should be_nil
28   end
29   
30   it "should show the tree partialy expanded on the index" do
31     get :index
32     response.should be_success
33     assert_rendered_nodes_where { |page| [nil, page_id(:home)].include?(page.parent_id) }
34   end
35   
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', '')
38     get :index
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) }
42   end
43   
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')
46   #   get :index
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) }
50   # end
51   
52   it "should index__with_mangled_cookie" #do
53   #   write_cookie('expanded_rows', '1,5,:#*)&},9a,,,')
54   #   get :index
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
59   # end
60   
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'
65     }
66     
67     get :new, :parent_id => page_id(:home), :page => page_params
68     response.should be_success
69     response.should render_template('admin/page/edit')
70     
71     page = assigns(:page)
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]
77   end
78   
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
82     
83     page = assigns(:page)
84     page.slug.should == 'test'
85     page.breadcrumb.should == 'me'
86   end
87   
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/) 
93     
94     page = assigns(:page)
95     page.parts.size.should == 0
96     
97     Page.find_by_title("New Page").should_not be_nil
98     @cache.expired_path.should == '/new-page/'
99   end
100   
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
106   end
107   
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"),
110       :part => {
111         '1' => part_params(:name => 'test-part-1'),
112         '2' => part_params(:name => 'test-part-2')
113       }
114     )
115     response.should redirect_to(page_index_url)
116     
117     page = Page.find_by_title("New Page")
118     page.should_not be_nil
119     
120     names = page.parts.collect { |part| part.name }.sort
121     names.should == ['test-part-1', 'test-part-2']
122   end
123   
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
131   end
132   
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))
137   end
138   
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)
144   end
145   
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)
151   end
152   
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
156     
157     page = assigns(:page)
158     page.should be_kind_of(Page)
159     page.title.should == 'Home'
160   end
161   
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
166     page = pages(:home)
167     page.title.should == "Updated Home Page"
168     @cache.expired_path.should == '/'
169     
170     # To-Do: Test what happens when an invalid page is submitted 
171   end
172   
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
177     page = pages(:first)
178     page.slug.should == 'monkey'
179     @cache.expired_path.should == '/first/'
180   end
181   
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')
186     end
187     page = pages(:new_page)
188     page.parts.size.should == 2
189     
190     post :edit, :id => page.id, :page => {}, :part => {'1' => part_params(:name => 'test-part-1', :content => 'changed')}
191     response.should be_redirect
192     
193     page = pages(:new_page) 
194     page.parts.size.should == 1
195     page.parts.first.content.should == 'changed'
196   end
197   
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/)
203   end
204   
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')
208     end
209     
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/)
213     
214     # changed value must not be saved
215     page_parts(:test_part_1).content.should == 'original-1'
216     
217     # but must be rendered to page
218     response.should have_tag('textarea', 'changed-1')
219   end  
220   
221   it "should prompt you when deleting a page" do
222     page = pages(:first)
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
227   end
228   
229   it "should show all children when prompting you during deletion regardless of sitemap expansion" do
230     page = pages(:home)
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}")
235     end
236   end
237   
238   it "should allow you to delete a page" do
239     page = pages(:first)
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 
244   end
245   
246   it "should prompt you when clearing the page cache" do
247     @cache = @controller.cache = FakeResponseCache.new
248     get :clear_cache
249     response.should be_success
250     @response.body.should match(/Do.*?not.*?access/i)
251     @cache.should_not be_cleared
252   end
253   
254   it "should allow you to clear the page cache" do
255     @cache = @controller.cache = FakeResponseCache.new
256     post :clear_cache
257     response.should redirect_to(page_index_url)
258     flash[:notice].should match(/cache.*clear/i)
259     @cache.should be_cleared
260   end
261   
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')
266   end
267   
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'
276   end
277   
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")
283   end
284   
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")
290   end
291   
292   protected
293   
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}")
298       end
299       unwanted.each do |page|
300         response.should_not have_tag("tr#page-#{page.id}")
301       end
302     end
303     
304     def write_cookie(name, value)
305       @request.cookies[name] = CGI::Cookie.new(name, value)
306     end