/
page_spec.rb
527 lines (425 loc) · 15.4 KB
/
page_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
require File.dirname(__FILE__) + '/../spec_helper'
class PageSpecTestPage < Page
description 'this is just a test page'
def headers
{
'cool' => 'beans',
'request' => @request.inspect[20..30],
'response' => @response.inspect[20..31]
}
end
tag 'test1' do
'Hello world!'
end
tag 'test2' do
'Another test.'
end
end
describe Page, 'validations' do
scenario :pages
test_helper :validations
before :each do
@page = @model = Page.new(page_params)
end
it 'should validate length of' do
{
:title => 255,
:slug => 100,
:breadcrumb => 160
}.each do |field, max|
assert_invalid field, ('%d-character limit' % max), 'x' * (max + 1)
assert_valid field, 'x' * max
end
end
it 'should validate presence of' do
[:title, :slug, :breadcrumb].each do |field|
assert_invalid field, 'required', '', ' ', nil
end
end
it 'should validate format of' do
@page.parent = pages(:home)
assert_valid :slug, 'abc', 'abcd-efg', 'abcd_efg', 'abc.html', '/', '123'
assert_invalid :slug, 'invalid format', 'abcd efg', ' abcd', 'abcd/efg'
end
it 'should validate numericality of' do
assert_invalid :status_id, 'required', '', nil
[:id, :status_id, :parent_id].each do |field|
assert_valid field, '1', '2'
assert_invalid field, 'must be a number', 'abcd', '1,2', '1.3'
end
end
it 'should validate uniqueness of' do
@page.parent = pages(:parent)
assert_invalid :slug, 'slug already in use for child of parent', 'child', 'child-2', 'child-3'
assert_valid :slug, 'child-4'
end
it 'should allow mass assignment for class name' do
@page.attributes = { :class_name => 'ArchivePage' }
assert_valid @page
@page.class_name.should == 'ArchivePage'
end
it 'should not be valid when class name is not a descendant of page' do
@page.class_name = 'Object'
@page.valid?.should == false
assert_not_nil @page.errors.on(:class_name)
@page.errors.on(:class_name).should == 'must be set to a valid descendant of Page'
end
it 'should not be valid when class name is not a descendant of page and it is set through mass assignment' do
@page.attributes = {:class_name => 'Object' }
@page.valid?.should == false
assert_not_nil @page.errors.on(:class_name)
@page.errors.on(:class_name).should == 'must be set to a valid descendant of Page'
end
it 'should be valid when class name is page or empty or nil' do
[nil, '', 'Page'].each do |value|
@page = ArchivePage.new(page_params)
@page.class_name = value
assert_valid @page
@page.class_name.should == value
end
end
end
describe Page, "behaviors" do
it 'should include' do
Page.included_modules.should include(StandardTags)
Page.included_modules.should include(Annotatable)
end
end
describe Page, "layout" do
scenario :pages_with_layouts
it 'should be accessible' do
@page = pages(:first)
@page.layout_id.should == layout_id(:main)
@page.layout.name.should == "Main"
end
it 'should be inherited' do
@page = pages(:inherited_layout)
@page.layout_id.should == nil
@page.layout.should == @page.parent.layout
end
end
describe Page do
scenario :pages
before :each do
@page = pages(:first)
end
it 'should have parts' do
@page.parts.count.should == 1
pages(:home).parts.count.should == 4
end
it 'should destroy dependant parts' do
@page.parts.create(page_part_params(:name => 'test', :page_id => nil))
assert_kind_of PagePart, @page.parts.find_by_name('test')
id = @page.id
@page.destroy
assert_nil PagePart.find_by_page_id_and_name(id, 'test')
end
it 'should allow access to parts by name with a string' do
part = @page.part('body')
part.name.should == 'body'
end
it 'should allow access to parts by name with a symbol' do
part = @page.part(:body)
part.name.should == 'body'
end
it 'should allow access to parts by name when page is unsaved' do
part = PagePart.new(:content => "test", :name => "test")
@page.parts << part
@page.part('test').should == part
@page.part(:test).should == part
end
it 'should allow access to parts by name created with the build method when page is unsaved' do
@page.parts.build(:content => "test", :name => "test")
@page.part('test').content.should == "test"
@page.part(:test).content.should == "test"
end
it 'should set published_at when published' do
@page = Page.new(page_params(:status_id => '1', :published_at => nil))
assert_nil @page.published_at
@page.status_id = Status[:published].id
@page.save
assert_not_nil @page.published_at
@page.published_at.day.should == Time.now.day
end
it 'should not update published_at when already published' do
@page = Page.new(page_params(:status_id => Status[:published].id))
@page.published_at.should be_kind_of(Time)
expected = @page.published_at
@page.save
@page.published_at.should == expected
end
it 'should answer true when published' do
@page.status = Status[:published]
@page.published?.should == true
end
it 'should answer false when not published' do
@page.status = Status[:draft]
@page.published?.should be_false
end
it "should answer the page's url" do
@page = pages(:parent)
@page.url.should == '/parent/'
@page.children.first.url.should == '/parent/child/'
grandchild = pages(:grandchild)
grandchild.url.should == '/parent/child/grandchild/'
end
it 'should allow you to calculate a child url from the parent' do
@page = pages(:parent)
child = pages(:child)
@page.child_url(child).should == '/parent/child/'
end
it 'should return the appropriate status code in headers' do
@page.headers.should == { 'Status' => ActionController::Base::DEFAULT_RENDER_STATUS_CODE }
end
it 'should have status' do
@page = pages(:home)
@page.status.should == Status[:published]
end
it 'should allow you to set the status' do
@page = pages(:home)
draft = Status[:draft]
@page.status = draft
@page.status.should == draft
@page.status_id.should == draft.id
end
it 'should respond to cache? with true (by default)' do
@page.cache?.should == true
end
it 'should respond to virtual? with false (by default)' do
@page.virtual?.should == false
end
it 'should allow you to tell if a part exists based on a string or symbol' do
@page = pages(:home)
@page.has_part?(:body).should == true
@page.has_part?('sidebar').should == true
@page.has_part?(:obviously_false_part_name).should == false
end
it 'should allow you to tell if a part is inherited' do
@page = pages(:child)
@page.has_part?(:sidebar).should == false
@page.inherits_part?(:sidebar).should == true
@page = pages(:home)
@page.has_part?(:sidebar).should == true
@page.inherits_part?(:sidebar).should == false
end
it 'should allow you to tell if a part exists or is inherited' do
@page = pages(:child)
@page.has_part?(:sidebar).should == false
@page.has_or_inherits_part?(:sidebar).should == true
@page.has_or_inherits_part?(:obviously_false_part_name).should == false
@page = pages(:home)
@page.has_part?(:sidebar).should == true
@page.has_or_inherits_part?(:sidebar).should == true
@page.has_or_inherits_part?(:obviously_false_part_name).should == false
end
it 'should support optimistic locking' do
p1, p2 = pages(:first), pages(:first)
p1.save!
lambda { p2.save! }.should raise_error(ActiveRecord::StaleObjectError)
end
end
describe Page, "before save filter" do
scenario :home_page
before :each do
Page.create(page_params(:title =>"Month Index", :class_name => "ArchiveMonthIndexPage"))
@page = Page.find_by_title("Month Index")
end
it 'should set the class name correctly' do
@page.should be_kind_of(ArchiveMonthIndexPage)
end
it 'should set the virtual bit correctly' do
@page.virtual?.should == true
@page.virtual.should == true
end
it 'should update virtual based on new class name' do
# turn a regular page into a virtual page
@page.class_name = "ArchiveMonthIndexPage"
@page.save.should == true
@page.virtual?.should == true
@page.send(:read_attribute, :virtual).should == true
# turn a virtual page into a non-virtual one
["", nil, "Page", "EnvDumpPage"].each do |value|
@page = ArchiveYearIndexPage.create(page_params)
@page.class_name = value
@page.save.should == true
@page = Page.find @page.id
@page.should be_instance_of(Page.descendant_class(value))
@page.virtual?.should == false
@page.send(:read_attribute, :virtual).should == false
end
end
end
describe Page, "rendering" do
scenario :pages, :markup_pages, :snippets, :layouts
test_helper :render
before :each do
@page = pages(:home)
end
it 'should render' do
@page.render.should == 'Hello world!'
end
it 'should render with a filter' do
pages(:textile).render.should == '<p>Some <strong>Textile</strong> content.</p>'
end
it 'should render with tags' do
pages(:radius).render.should == "Radius body."
end
it 'should render with a layout' do
@page.update_attribute(:layout_id, layout_id(:main))
@page.render.should == "<html>\n <head>\n <title>Home</title>\n </head>\n <body>\n Hello world!\n </body>\n</html>\n"
end
it 'should render a part' do
@page.render_part(:body).should == "Hello world!"
end
it "should render blank when given a non-existent part" do
@page.render_part(:empty).should == ''
end
it 'should render a snippet' do
assert_snippet_renders :first, 'test'
end
it 'should render a snippet with a filter' do
assert_snippet_renders :markdown, '<p><strong>markdown</strong></p>'
end
it 'should render a snippet with a tag' do
assert_snippet_renders :radius, 'Home'
end
it 'should render custom pages with tags' do
create_page "Test Page", :body => "<r:test1 /> <r:test2 />", :class_name => "PageSpecTestPage"
pages(:test_page).should render_as('Hello world! Another test. body.')
end
end
describe Page, "#find_by_url" do
scenario :pages, :file_not_found
before :each do
@page = pages(:home)
end
it 'should allow you to find the home page' do
@page.find_by_url('/') .should == @page
end
it 'should allow you to find deeply nested pages' do
@page.find_by_url('/parent/child/grandchild/great-grandchild/').should == pages(:great_grandchild)
end
it 'should not allow you to find virtual pages' do
@page.find_by_url('/virtual/').should == pages(:file_not_found)
end
it 'should find the FileNotFoundPage when a page does not exist' do
@page.find_by_url('/nothing-doing/').should == pages(:file_not_found)
end
it 'should find a custom file not found page' do
@page.find_by_url('/gallery/nothing-doing/').should == pages(:no_picture)
end
it 'should not find draft pages in live mode' do
@page.find_by_url('/draft/').should == pages(:file_not_found)
end
it 'should find draft pages in dev mode' do
@page.find_by_url('/draft/', false).should == pages(:draft)
end
end
describe Page, "class" do
it 'should have a description' do
PageSpecTestPage.description.should == 'this is just a test page'
end
it 'should have a display name' do
Page.display_name.should == "Page"
PageSpecTestPage.display_name.should == "Page Spec Test"
PageSpecTestPage.display_name = "New Name"
PageSpecTestPage.display_name.should == "New Name"
FileNotFoundPage.display_name.should == "File Not Found"
end
it 'should list decendants' do
descendants = Page.descendants
assert_kind_of Array, descendants
assert_match /PageSpecTestPage/, descendants.inspect
end
it 'should allow initialization with empty defaults' do
@page = Page.new_with_defaults({})
@page.parts.size.should == 0
end
it 'should allow initialization with default page parts' do
@page = Page.new_with_defaults({ 'defaults.page.parts' => 'a, b, c'})
@page.parts.size.should == 3
@page.parts.first.name.should == 'a'
@page.parts.last.name.should == 'c'
end
it 'should allow initialization with default page status' do
@page = Page.new_with_defaults({ 'defaults.page.status' => 'published' })
@page.status.should == Status[:published]
end
it 'should allow initialization with default filter' do
@page = Page.new_with_defaults({ 'defaults.page.filter' => 'Textile', 'defaults.page.parts' => 'a, b, c' })
@page.parts.each do |part|
part.filter_id.should == 'Textile'
end
end
it 'should allow you to get the class name of a descendant class with a string' do
["", nil, "Page"].each do |value|
Page.descendant_class(value).should == Page
end
Page.descendant_class("PageSpecTestPage").should == PageSpecTestPage
end
it 'should allow you to determine if a string is a valid descendant class name' do
["", nil, "Page", "PageSpecTestPage"].each do |value|
Page.is_descendant_class_name?(value).should == true
end
Page.is_descendant_class_name?("InvalidPage").should == false
end
end
describe Page, "class which is applied to a page but not defined" do
scenario :pages
before :each do
eval(%Q{class ClassNotDefined < Page; def self.missing?; false end end}, TOPLEVEL_BINDING)
create_page "Class Not Defined", :class_name => "ClassNotDefined"
Object.send(:remove_const, :ClassNotDefined)
Page.load_subclasses
end
it 'should be created dynamically as a new subclass of Page' do
Object.const_defined?("ClassNotDefined").should == true
end
it 'should indicate that it wasn\'t defined' do
ClassNotDefined.missing?.should == true
end
end
describe Page, "class find_by_url" do
scenario :pages, :file_not_found
it 'should find the home page' do
Page.find_by_url('/').should == pages(:home)
end
it 'should find children' do
Page.find_by_url('/parent/child/').should == pages(:child)
end
it 'should not find draft pages in live mode' do
Page.find_by_url('/draft/').should == pages(:file_not_found)
Page.find_by_url('/draft/', false).should == pages(:draft)
end
it 'should raise an exception when root page is missing' do
pages(:home).destroy
Page.find_by_parent_id().should be_nil
lambda { Page.find_by_url "/" }.should raise_error(Page::MissingRootPageError, 'Database missing root page')
end
end
describe Page, "processing" do
scenario :pages_with_layouts
before :all do
@request = ActionController::TestRequest.new :url => '/page/'
@response = ActionController::TestResponse.new
end
it 'should set response body' do
@page = pages(:home)
@page.process(@request, @response)
@response.body.should match(/Hello world!/)
end
it 'should set headers and pass request and response' do
create_page "Test Page", :class_name => "PageSpecTestPage"
@page = pages(:test_page)
@page.process(@request, @response)
@response.headers['cool'].should == 'beans'
@response.headers['request'].should == 'TestRequest'
@response.headers['response'].should == 'TestResponse'
end
it 'should set content type based on layout' do
@page = pages(:utf8)
@page.process(@request, @response)
assert_response :success
@response.headers['Content-Type'].should == 'text/html;charset=utf8'
end
end