Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 19966 Bytes
1 | require File.dirname(__FILE__) + '/../spec_helper' |
2 | |
3 | class PageSpecTestPage < Page |
4 | description 'this is just a test page' |
5 | |
6 | def headers |
7 | { |
8 | 'cool' => 'beans', |
9 | 'request' => @request.inspect[20..30], |
10 | 'response' => @response.inspect[20..31] |
11 | } |
12 | end |
13 | |
14 | tag 'test1' do |
15 | 'Hello world!' |
16 | end |
17 | |
18 | tag 'test2' do |
19 | 'Another test.' |
20 | end |
21 | end |
22 | |
23 | describe Page, 'validations' do |
24 | dataset :pages |
25 | test_helper :validations |
26 | |
27 | before :each do |
28 | @page = @model = Page.new(page_params) |
29 | end |
30 | |
31 | it 'should validate length of' do |
32 | { |
33 | :title => 255, |
34 | :slug => 100, |
35 | :breadcrumb => 160 |
36 | }.each do |field, max| |
37 | assert_invalid field, ('this must not be longer than %d characters' % max), 'x' * (max + 1) |
38 | assert_valid field, 'x' * max |
39 | end |
40 | end |
41 | |
42 | it 'should validate presence of' do |
43 | [:title, :slug, :breadcrumb].each do |field| |
44 | assert_invalid field, 'this must not be blank', '', ' ', nil |
45 | end |
46 | end |
47 | |
48 | it 'should validate format of' do |
49 | @page.parent = pages(:home) |
50 | assert_valid :slug, 'abc', 'abcd-efg', 'abcd_efg', 'abc.html', '/', '123' |
51 | assert_invalid :slug, 'this does not match the expected format', 'abcd efg', ' abcd', 'abcd/efg' |
52 | end |
53 | |
54 | it 'should validate numericality of' do |
55 | assert_invalid :status_id, 'this must not be blank', '', nil |
56 | [:id, :status_id, :parent_id].each do |field| |
57 | assert_valid field, '1', '2' |
58 | assert_invalid field, 'this must be a number', 'abcd', '1,2', '1.3' |
59 | end |
60 | end |
61 | |
62 | it 'should validate uniqueness of' do |
63 | @page.parent = pages(:parent) |
64 | assert_invalid :slug, 'this slug is already in use by a sibling of this page', 'child', 'child-2', 'child-3' |
65 | assert_valid :slug, 'child-4' |
66 | end |
67 | |
68 | it 'should allow mass assignment for class name' do |
69 | @page.attributes = { :class_name => 'ArchivePage' } |
70 | assert_valid @page |
71 | @page.class_name.should == 'ArchivePage' |
72 | end |
73 | |
74 | it 'should not be valid when class name is not a descendant of page' do |
75 | @page.class_name = 'Object' |
76 | @page.valid?.should == false |
77 | assert_not_nil @page.errors.on(:class_name) |
78 | @page.errors.on(:class_name).should == 'must be set to a valid descendant of Page' |
79 | end |
80 | |
81 | it 'should not be valid when class name is not a descendant of page and it is set through mass assignment' do |
82 | @page.attributes = {:class_name => 'Object' } |
83 | @page.valid?.should == false |
84 | assert_not_nil @page.errors.on(:class_name) |
85 | @page.errors.on(:class_name).should == 'must be set to a valid descendant of Page' |
86 | end |
87 | |
88 | it 'should be valid when class name is page or empty or nil' do |
89 | [nil, '', 'Page'].each do |value| |
90 | @page = ArchivePage.new(page_params) |
91 | @page.class_name = value |
92 | assert_valid @page |
93 | @page.class_name.should == value |
94 | end |
95 | end |
96 | end |
97 | |
98 | describe Page, "behaviors" do |
99 | it 'should include' do |
100 | Page.included_modules.should include(StandardTags) |
101 | Page.included_modules.should include(Annotatable) |
102 | end |
103 | end |
104 | |
105 | describe Page, "layout" do |
106 | dataset :pages_with_layouts |
107 | |
108 | it 'should be accessible' do |
109 | @page = pages(:first) |
110 | @page.layout_id.should == layout_id(:main) |
111 | @page.layout.name.should == "Main" |
112 | end |
113 | |
114 | it 'should be inherited' do |
115 | @page = pages(:inherited_layout) |
116 | @page.layout_id.should == nil |
117 | @page.layout.should == @page.parent.layout |
118 | end |
119 | end |
120 | |
121 | describe Page do |
122 | dataset :pages |
123 | |
124 | before :each do |
125 | @page = pages(:first) |
126 | end |
127 | |
128 | it 'should have parts' do |
129 | @page.parts.count.should == 1 |
130 | pages(:home).parts.count.should == 4 |
131 | end |
132 | |
133 | it 'should destroy dependant parts' do |
134 | @page.parts.create(page_part_params(:name => 'test', :page_id => nil)) |
135 | @page.parts.find_by_name('test').should be_kind_of(PagePart) |
136 | |
137 | id = @page.id |
138 | @page.destroy |
139 | PagePart.find_by_page_id_and_name(id, 'test').should be_nil |
140 | end |
141 | |
142 | it 'should allow access to parts by name with a string' do |
143 | part = @page.part('body') |
144 | part.name.should == 'body' |
145 | end |
146 | |
147 | it 'should allow access to parts by name with a symbol' do |
148 | part = @page.part(:body) |
149 | part.name.should == 'body' |
150 | end |
151 | |
152 | it 'should allow access to parts by name when page is unsaved' do |
153 | part = PagePart.new(:content => "test", :name => "test") |
154 | @page.parts << part |
155 | @page.part('test').should == part |
156 | @page.part(:test).should == part |
157 | end |
158 | |
159 | it 'should allow access to parts by name created with the build method when page is unsaved' do |
160 | @page.parts.build(:content => "test", :name => "test") |
161 | @page.part('test').content.should == "test" |
162 | @page.part(:test).content.should == "test" |
163 | end |
164 | |
165 | it "should accept new page parts as an array of PageParts" do |
166 | @page.parts = [PagePart.new(:name => 'body', :content => 'Hello, world!')] |
167 | @page.parts.size.should == 1 |
168 | @page.parts.first.should be_kind_of(PagePart) |
169 | @page.parts.first.name.should == 'body' |
170 | @page.parts.first.content.should == 'Hello, world!' |
171 | end |
172 | |
173 | it "should dirty the page object when only changing parts" do |
174 | lambda do |
175 | @page.parts = [PagePart.new(:name => 'body', :content => 'Hello, world!')] |
176 | @page.changed.should_not be_empty |
177 | end |
178 | end |
179 | |
180 | # invalid, as published_at is set when form is submitted |
181 | |
182 | it 'should set published_at when published' do |
183 | @page = Page.new(page_params(:status_id => '1', :published_at => nil)) |
184 | @page.published_at.should be_nil |
185 | |
186 | @page.status_id = Status[:published].id |
187 | @page.save |
188 | @page.published_at.should_not be_nil |
189 | @page.published_at.utc.day.should == Time.now.utc.day |
190 | end |
191 | |
192 | it 'should not update published_at when already published' do |
193 | @page = Page.new(page_params(:status_id => Status[:published].id)) |
194 | @page.published_at.should be_kind_of(Time) |
195 | |
196 | expected = @page.published_at |
197 | @page.save |
198 | @page.published_at.should == expected |
199 | end |
200 | |
201 | it 'should answer true when published' do |
202 | @page.status = Status[:published] |
203 | @page.published?.should == true |
204 | end |
205 | |
206 | it 'should answer false when not published' do |
207 | @page.status = Status[:draft] |
208 | @page.published?.should be_false |
209 | end |
210 | |
211 | it 'should answer false when not published but scheduled' do |
212 | @page.status = Status[:scheduled] |
213 | @page.published?.should be_false |
214 | end |
215 | |
216 | it 'should change its status to scheduled when publishing in the future' do |
217 | @page = Page.new(page_params(:status_id => '100', :published_at => '2020-1-1')) |
218 | @page.save |
219 | @page.status_id.should == 90 |
220 | end |
221 | |
222 | it 'should change its status to draft when set to draft' do |
223 | @page = pages(:scheduled) |
224 | @page.status_id = Status[:draft].id |
225 | @page.save |
226 | @page.status_id.should == 1 |
227 | end |
228 | |
229 | it 'should be published status when published_at is in the past' do |
230 | #current time 01-29-2010 |
231 | scheduled_time = '2010-1-1' |
232 | @page = Page.new(page_params(:status_id => Status[:scheduled].id, :published_at => scheduled_time)) |
233 | @page.save |
234 | @page.status_id.should == Status[:published].id |
235 | end |
236 | |
237 | it "should answer the page's url" do |
238 | @page = pages(:parent) |
239 | @page.url.should == '/parent/' |
240 | @page.children.first.url.should == '/parent/child/' |
241 | |
242 | grandchild = pages(:grandchild) |
243 | grandchild.url.should == '/parent/child/grandchild/' |
244 | end |
245 | |
246 | it 'should allow you to calculate a child url from the parent' do |
247 | @page = pages(:parent) |
248 | child = pages(:child) |
249 | @page.child_url(child).should == '/parent/child/' |
250 | end |
251 | |
252 | it 'should have status' do |
253 | @page = pages(:home) |
254 | @page.status.should == Status[:published] |
255 | end |
256 | |
257 | it 'should allow you to set the status' do |
258 | @page = pages(:home) |
259 | draft = Status[:draft] |
260 | @page.status = draft |
261 | @page.status.should == draft |
262 | @page.status_id.should == draft.id |
263 | end |
264 | |
265 | it 'should respond to cache? with true (by default)' do |
266 | @page.cache?.should == true |
267 | end |
268 | |
269 | it 'should respond to virtual? with false (by default)' do |
270 | @page.virtual?.should == false |
271 | end |
272 | |
273 | it 'should allow you to tell if a part exists based on a string or symbol' do |
274 | @page = pages(:home) |
275 | @page.has_part?(:body).should == true |
276 | @page.has_part?('sidebar').should == true |
277 | @page.has_part?(:obviously_false_part_name).should == false |
278 | end |
279 | |
280 | it 'should allow you to tell if a part is inherited' do |
281 | @page = pages(:child) |
282 | @page.has_part?(:sidebar).should == false |
283 | @page.inherits_part?(:sidebar).should == true |
284 | |
285 | @page = pages(:home) |
286 | @page.has_part?(:sidebar).should == true |
287 | @page.inherits_part?(:sidebar).should == false |
288 | end |
289 | |
290 | it 'should allow you to tell if a part exists or is inherited' do |
291 | @page = pages(:child) |
292 | @page.has_part?(:sidebar).should == false |
293 | @page.has_or_inherits_part?(:sidebar).should == true |
294 | @page.has_or_inherits_part?(:obviously_false_part_name).should == false |
295 | |
296 | @page = pages(:home) |
297 | @page.has_part?(:sidebar).should == true |
298 | @page.has_or_inherits_part?(:sidebar).should == true |
299 | @page.has_or_inherits_part?(:obviously_false_part_name).should == false |
300 | end |
301 | |
302 | it 'should support optimistic locking' do |
303 | p1, p2 = Page.find(page_id(:first)), Page.find(page_id(:first)) |
304 | p1.update_attributes!(:breadcrumb => "foo") |
305 | lambda { p2.update_attributes!(:breadcrumb => "blah") }.should raise_error(ActiveRecord::StaleObjectError) |
306 | end |
307 | end |
308 | |
309 | describe Page, "before save filter" do |
310 | dataset :home_page |
311 | |
312 | before :each do |
313 | Page.create(page_params(:title =>"Month Index", :class_name => "ArchiveMonthIndexPage")) |
314 | @page = Page.find_by_title("Month Index") |
315 | end |
316 | |
317 | it 'should set the class name correctly' do |
318 | @page.should be_kind_of(ArchiveMonthIndexPage) |
319 | end |
320 | |
321 | it 'should set the virtual bit correctly' do |
322 | @page.virtual?.should == true |
323 | @page.virtual.should == true |
324 | end |
325 | |
326 | it 'should update virtual based on new class name' do |
327 | # turn a regular page into a virtual page |
328 | @page.class_name = "ArchiveMonthIndexPage" |
329 | @page.save.should == true |
330 | @page.virtual?.should == true |
331 | @page.send(:read_attribute, :virtual).should == true |
332 | |
333 | # turn a virtual page into a non-virtual one |
334 | ["", nil, "Page", "EnvDumpPage"].each do |value| |
335 | @page = ArchiveYearIndexPage.create(page_params) |
336 | @page.class_name = value |
337 | @page.save.should == true |
338 | @page = Page.find @page.id |
339 | @page.should be_instance_of(Page.descendant_class(value)) |
340 | @page.virtual?.should == false |
341 | @page.send(:read_attribute, :virtual).should == false |
342 | end |
343 | end |
344 | end |
345 | |
346 | describe Page, "rendering" do |
347 | dataset :pages, :markup_pages, :snippets, :layouts |
348 | test_helper :render |
349 | |
350 | before :each do |
351 | @page = pages(:home) |
352 | end |
353 | |
354 | it 'should render' do |
355 | @page.render.should == 'Hello world!' |
356 | end |
357 | |
358 | it 'should render with a filter' do |
359 | pages(:textile).render.should == '<p>Some <strong>Textile</strong> content.</p>' |
360 | end |
361 | |
362 | it 'should render with tags' do |
363 | pages(:radius).render.should == "Radius body." |
364 | end |
365 | |
366 | it 'should render with a layout' do |
367 | @page.update_attribute(:layout_id, layout_id(:main)) |
368 | @page.render.should == "<html>\n <head>\n <title>Home</title>\n </head>\n <body>\n Hello world!\n </body>\n</html>\n" |
369 | end |
370 | |
371 | it 'should render a part' do |
372 | @page.render_part(:body).should == "Hello world!" |
373 | end |
374 | |
375 | it "should render blank when given a non-existent part" do |
376 | @page.render_part(:empty).should == '' |
377 | end |
378 | |
379 | it 'should render a snippet' do |
380 | @page.render_snippet(snippets(:first)).should == 'test' |
381 | end |
382 | |
383 | it 'should render a snippet with a filter' do |
384 | @page.render_snippet(snippets(:markdown)).should match(%r{<p><strong>markdown</strong></p>}) |
385 | end |
386 | |
387 | it 'should render a snippet with a tag' do |
388 | @page.render_snippet(snippets(:radius)).should == 'Home' |
389 | end |
390 | |
391 | it 'should render custom pages with tags' do |
392 | create_page "Test Page", :body => "<r:test1 /> <r:test2 />", :class_name => "PageSpecTestPage" |
393 | pages(:test_page).should render_as('Hello world! Another test. body.') |
394 | end |
395 | end |
396 | |
397 | describe Page, "#find_by_url" do |
398 | dataset :pages, :file_not_found |
399 | |
400 | before :each do |
401 | @page = pages(:home) |
402 | end |
403 | |
404 | it 'should allow you to find the home page' do |
405 | @page.find_by_url('/').should == @page |
406 | end |
407 | |
408 | it 'should allow you to find deeply nested pages' do |
409 | @page.find_by_url('/parent/child/grandchild/great-grandchild/').should == pages(:great_grandchild) |
410 | end |
411 | |
412 | it 'should not allow you to find virtual pages' do |
413 | @page.find_by_url('/virtual/').should == pages(:file_not_found) |
414 | end |
415 | |
416 | it 'should find the FileNotFoundPage when a page does not exist' do |
417 | @page.find_by_url('/nothing-doing/').should == pages(:file_not_found) |
418 | end |
419 | |
420 | it 'should find a draft FileNotFoundPage in dev mode' do |
421 | @page.find_by_url('/drafts/no-page-here', false).should == pages(:lonely_draft_file_not_found) |
422 | end |
423 | |
424 | it 'should not find a draft FileNotFoundPage in live mode' do |
425 | @page.find_by_url('/drafts/no-page-here').should_not == pages(:lonely_draft_file_not_found) |
426 | end |
427 | |
428 | it 'should find a custom file not found page' do |
429 | @page.find_by_url('/gallery/nothing-doing').should == pages(:no_picture) |
430 | end |
431 | |
432 | it 'should not find draft pages in live mode' do |
433 | @page.find_by_url('/draft/').should == pages(:file_not_found) |
434 | end |
435 | |
436 | it 'should find draft pages in dev mode' do |
437 | @page.find_by_url('/draft/', false).should == pages(:draft) |
438 | end |
439 | |
440 | it "should use the top-most published 404 page by default" do |
441 | @page.find_by_url('/foo').should == pages(:file_not_found) |
442 | @page.find_by_url('/foo/bar').should == pages(:file_not_found) |
443 | end |
444 | end |
445 | |
446 | describe Page, "class" do |
447 | it 'should have a description' do |
448 | PageSpecTestPage.description.should == 'this is just a test page' |
449 | end |
450 | |
451 | it 'should have a display name' do |
452 | Page.display_name.should == "Page" |
453 | |
454 | PageSpecTestPage.display_name.should == "Page Spec Test" |
455 | |
456 | PageSpecTestPage.display_name = "New Name" |
457 | PageSpecTestPage.display_name.should == "New Name" |
458 | |
459 | FileNotFoundPage.display_name.should == "File Not Found" |
460 | end |
461 | |
462 | it 'should list decendants' do |
463 | descendants = Page.descendants |
464 | assert_kind_of Array, descendants |
465 | assert_match /PageSpecTestPage/, descendants.inspect |
466 | end |
467 | |
468 | it 'should allow initialization with empty defaults' do |
469 | @page = Page.new_with_defaults({}) |
470 | @page.parts.size.should == 0 |
471 | end |
472 | |
473 | it 'should allow initialization with default page parts' do |
474 | @page = Page.new_with_defaults({ 'defaults.page.parts' => 'a, b, c'}) |
475 | @page.parts.size.should == 3 |
476 | @page.parts.first.name.should == 'a' |
477 | @page.parts.last.name.should == 'c' |
478 | end |
479 | |
480 | it 'should allow initialization with default page status' do |
481 | @page = Page.new_with_defaults({ 'defaults.page.status' => 'published' }) |
482 | @page.status.should == Status[:published] |
483 | end |
484 | |
485 | it 'should allow initialization with default filter' do |
486 | @page = Page.new_with_defaults({ 'defaults.page.filter' => 'Textile', 'defaults.page.parts' => 'a, b, c' }) |
487 | @page.parts.each do |part| |
488 | part.filter_id.should == 'Textile' |
489 | end |
490 | end |
491 | |
492 | it "should expose default page parts" do |
493 | override = PagePart.new(:name => 'override') |
494 | Page.stub!(:default_page_parts).and_return([override]) |
495 | @page = Page.new_with_defaults({}) |
496 | @page.parts.should eql([override]) |
497 | end |
498 | |
499 | it 'should allow you to get the class name of a descendant class with a string' do |
500 | ["", nil, "Page"].each do |value| |
501 | Page.descendant_class(value).should == Page |
502 | end |
503 | Page.descendant_class("PageSpecTestPage").should == PageSpecTestPage |
504 | end |
505 | |
506 | it 'should allow you to determine if a string is a valid descendant class name' do |
507 | ["", nil, "Page", "PageSpecTestPage"].each do |value| |
508 | Page.is_descendant_class_name?(value).should == true |
509 | end |
510 | Page.is_descendant_class_name?("InvalidPage").should == false |
511 | end |
512 | |
513 | end |
514 | |
515 | describe Page, "loading subclasses before bootstrap" do |
516 | before :each do |
517 | Page.connection.should_receive(:tables).and_return([]) |
518 | end |
519 | |
520 | it "should not attempt to search for missing subclasses" do |
521 | Page.connection.should_not_receive(:select_values).with("SELECT DISTINCT class_name FROM pages WHERE class_name <> '' AND class_name IS NOT NULL") |
522 | Page.load_subclasses |
523 | end |
524 | end |
525 | |
526 | describe Page, "loading subclasses when upgrading from 0.5.x where class_name column is not present" do |
527 | before :each do |
528 | column_names = Page.column_names - ["class_name"] |
529 | Page.should_receive(:column_names).and_return(column_names) |
530 | end |
531 | |
532 | it "should not attempt to search for missing subclasses" do |
533 | Page.connection.should_not_receive(:select_values).with("SELECT DISTINCT class_name FROM pages WHERE class_name <> '' AND class_name IS NOT NULL") |
534 | Page.load_subclasses |
535 | end |
536 | end |
537 | |
538 | describe Page, 'loading subclasses after bootstrap' do |
539 | it "should find subclasses in extensions" do |
540 | defined?(ArchivePage).should_not be_nil |
541 | end |
542 | |
543 | it "should not adjust the display name of subclasses found in extensions" do |
544 | ArchivePage.display_name.should_not match(/not installed/) |
545 | end |
546 | end |
547 | |
548 | describe Page, "class which is applied to a page but not defined" do |
549 | dataset :pages |
550 | |
551 | before :each do |
552 | Object.send(:const_set, :ClassNotDefinedPage, Class.new(Page){ def self.missing?; false end }) |
553 | create_page "Class Not Defined", :class_name => "ClassNotDefinedPage" |
554 | Object.send(:remove_const, :ClassNotDefinedPage) |
555 | Page.load_subclasses |
556 | end |
557 | |
558 | it 'should be created dynamically as a new subclass of Page' do |
559 | Object.const_defined?("ClassNotDefinedPage").should == true |
560 | end |
561 | |
562 | it 'should indicate that it wasn\'t defined' do |
563 | ClassNotDefinedPage.missing?.should == true |
564 | end |
565 | |
566 | it "should adjust the display name to indicate that the page type is not installed" do |
567 | ClassNotDefinedPage.display_name.should match(/not installed/) |
568 | end |
569 | |
570 | after :each do |
571 | Object.send(:remove_const, :ClassNotDefinedPage) |
572 | end |
573 | end |
574 | |
575 | describe Page, "class find_by_url" do |
576 | dataset :pages, :file_not_found |
577 | |
578 | it 'should find the home page' do |
579 | Page.find_by_url('/').should == pages(:home) |
580 | end |
581 | |
582 | it 'should find children' do |
583 | Page.find_by_url('/parent/child/').should == pages(:child) |
584 | end |
585 | |
586 | it 'should not find draft pages in live mode' do |
587 | Page.find_by_url('/draft/').should == pages(:file_not_found) |
588 | Page.find_by_url('/draft/', false).should == pages(:draft) |
589 | end |
590 | |
591 | it 'should raise an exception when root page is missing' do |
592 | pages(:home).destroy |
593 | Page.find_by_parent_id().should be_nil |
594 | lambda { Page.find_by_url "/" }.should raise_error(Page::MissingRootPageError, 'Database missing root page') |
595 | end |
596 | end |
597 | |
598 | describe Page, "processing" do |
599 | dataset :pages_with_layouts |
600 | |
601 | before :all do |
602 | @request = ActionController::TestRequest.new :url => '/page/' |
603 | @response = ActionController::TestResponse.new |
604 | @page = pages(:home) |
605 | end |
606 | |
607 | it 'should set response body' do |
608 | @page.process(@request, @response) |
609 | @response.body.should match(/Hello world!/) |
610 | end |
611 | |
612 | it 'should set headers and pass request and response' do |
613 | create_page "Test Page", :class_name => "PageSpecTestPage" |
614 | @page = pages(:test_page) |
615 | @page.process(@request, @response) |
616 | @response.headers['cool'].should == 'beans' |
617 | @response.headers['request'].should == 'TestRequest' |
618 | @response.headers['response'].should == 'TestResponse' |
619 | end |
620 | |
621 | it 'should set content type based on layout' do |
622 | @page = pages(:utf8) |
623 | @page.process(@request, @response) |
624 | @response.should be_success |
625 | @response.headers['Content-Type'].should == 'text/html;charset=utf8' |
626 | end |
627 | |
628 | it "should copy custom headers into the response" do |
629 | @page.stub!(:headers).and_return({"X-Extra-Header" => "This is my header"}) |
630 | @page.process(@request, @response) |
631 | @response.header['X-Extra-Header'].should == "This is my header" |
632 | end |
633 | |
634 | it "should set a 200 status code by default" do |
635 | @page.process(@request, @response) |
636 | @response.response_code.should == 200 |
637 | end |
638 | |
639 | it "should set the response code to the result of the response_code method on the page" do |
640 | @page.stub!(:response_code).and_return(404) |
641 | @page.process(@request, @response) |
642 | @response.response_code.should == 404 |
643 | end |
644 | end |