1 require File.dirname(__FILE__) + '/../test_helper'
3 class PageTest < Test::Unit::TestCase
4 fixtures :users, :pages, :page_parts, :snippets, :layouts
5 test_helper :pages, :page_parts, :validations, :render
8 @page_title = 'Page Title'
11 @part_name = 'test-part'
14 @page = @model = Page.new(VALID_PAGE_PARAMS)
16 @request = ActionController::TestRequest.new :url => '/page/'
17 @response = ActionController::TestResponse.new
21 assert Page.included_modules.include?(StandardTags)
24 def test_validates_length_of
29 }.each do |field, max|
30 assert_invalid field, ('%d-character limit' % max), 'x' * (max + 1)
31 assert_valid field, 'x' * max
35 def test_validates_presence_of
36 [:title, :slug, :breadcrumb].each do |field|
37 assert_invalid field, 'required', '', ' ', nil
41 def test_validates_format_of
42 @page.parent = pages(:homepage)
43 assert_valid :slug, 'abc', 'abcd-efg', 'abcd_efg', 'abc.html', '/', '123'
44 assert_invalid :slug, 'invalid format', 'abcd efg', ' abcd', 'abcd/efg'
47 def test_validates_numericality_of
48 assert_invalid :status_id, 'required', '', nil
49 [:id, :status_id, :parent_id].each do |field|
50 assert_valid field, '1', '2'
51 assert_invalid field, 'must be a number', 'abcd', '1,2', '1.3'
55 def test_validates_uniqueness_of
56 @page.parent = pages(:radius)
57 assert_invalid :slug, 'slug already in use for child of parent', 'child-1', 'child-2', 'child-3'
58 assert_valid :slug, 'child-4'
61 def test_included_modules
62 assert Page.included_modules.include?(Annotatable), 'Annotatable is not included'
66 @page = pages(:page_with_layout)
67 assert_equal 1, @page.layout_id
68 assert_kind_of Layout, @page.layout
72 @homepage = pages(:homepage)
73 assert_equal(4, @homepage.parts.count)
75 @documentation = pages(:documentation)
76 assert_equal(1, @documentation.parts.count)
79 def test_destroy__parts_dependant
80 @page = create_test_page
81 @page.parts.create(part_params(:name => @part_name, :page_id => nil))
82 assert_kind_of PagePart, @page.parts.find_by_name(@part_name)
86 assert_nil PagePart.find_by_page_id_and_name(id, @part_name)
90 part = pages(:radius).part('body')
91 assert_equal 'body', part.name
93 def test_part__lookup_by_symbol
94 part = pages(:radius).part(:body)
95 assert_equal 'body', part.name
97 def test_part__when_page_is_unsaved
98 part = PagePart.new(:content => "test", :name => "test")
100 assert_equal part, @page.part('test')
101 assert_equal part, @page.part(:test)
103 def test_part__when_parts_are_unsaved
104 @page = pages(:radius)
105 @page.parts.build(:content => "test", :name => "test")
106 assert_equal "test", @page.part('test').content
107 assert_equal "test", @page.part(:test).content
110 def test_published_at
111 @page = create_test_page(:status_id => '1')
112 assert_nil @page.published_at
114 @page.status_id = Status[:published].id
116 assert_not_nil @page.published_at
117 assert_equal Time.now.day, @page.published_at.day
119 def test_published_at__not_updated_on_save_because_already_published
120 @page = create_test_page(:status_id => Status[:published].id)
121 assert_kind_of Time, @page.published_at
123 expected = @page.published_at
125 assert_equal expected, @page.published_at
129 @page.status = Status[:published]
130 assert_equal true, @page.published?
132 def test_published__not_published
133 @page.status = Status[:draft]
134 assert_equal false, @page.published?
138 @page = pages(:parent)
139 assert_equal '/parent/', @page.url
140 assert_equal '/parent/child/', @page.children.first.url
142 grandchild = pages(:grandchild)
143 assert_equal '/parent/child/grandchild/', grandchild.url
147 @page = pages(:parent)
148 child = pages(:child)
149 assert_equal '/parent/child/', @page.child_url(child)
152 def test_find_by_url_1
153 @page = pages(:homepage)
154 assert_equal @page, @page.find_by_url('/')
156 def test_find_by_url_2
157 @page = pages(:homepage)
158 expected = pages(:great_grandchild)
159 found = @page.find_by_url('/parent/child/grandchild/great-grandchild/')
160 assert_equal expected, found
162 def test_find_by_url__when_virtual
163 @page = pages(:homepage)
164 found = @page.find_by_url('/assorted/virtual/')
165 assert_equal nil, found
167 def test_find_by_url__when_not_found_and_missing_page_defined
168 @page = pages(:homepage)
169 found = @page.find_by_url('/gallery/asdf/')
170 assert_instance_of FileNotFoundPage, found
172 def test_find_by_url__when_not_found_and_custom_missing_page_defined
173 @page = pages(:homepage)
174 found = @page.find_by_url('/custom_404/asdf/')
175 assert_instance_of CustomFileNotFoundPage, found
177 def test_find_by_url__when_not_found_on_live
178 @page = pages(:homepage)
179 found = @page.find_by_url('/gallery/gallery_draft/')
180 assert_instance_of FileNotFoundPage, found
182 def test_find_by_url__when_not_found_on_dev
183 @page = pages(:homepage)
184 expected = pages(:gallery_draft)
185 found = @page.find_by_url('/gallery/gallery_draft/', false)
186 assert_equal expected, found
189 def test_find_by_url_class_method
190 @root = pages(:homepage)
191 assert_equal @root, Page.find_by_url('/')
193 @page = pages(:books)
194 assert_equal @page, Page.find_by_url('/documentation/books/')
196 @root = pages(:homepage)
197 assert_equal 'File Not Found', Page.find_by_url('/gallery/gallery_draft/').title
198 assert_equal 'Gallery Draft', Page.find_by_url('/gallery/gallery_draft/', false).title
200 def test_find_by_url_class_method__raises_exception_when_root_missing
201 pages(:homepage).destroy
202 assert_nil Page.find_by_parent_id(nil)
203 e = assert_raises(Page::MissingRootPageError) { Page.find_by_url "/" }
204 assert_equal 'Database missing root page', e.message
208 expected = { 'Status' => ActionController::Base::DEFAULT_RENDER_STATUS_CODE }
209 assert_equal expected, @page.headers
213 expected = 'This is the body portion of the Ruby home page.'
214 assert_page_renders :homepage, expected
216 def test_render__with_filter
217 expected = '<p>Some <strong>Textile</strong> content.</p>'
218 assert_page_renders :textile, expected
220 def test_render__with_tags
221 expected = "<h1>Radius Test Page</h1>\n\n\n\t<ul>\n\t<li>Radius Test Child 1</li>\n\t\t<li>Radius Test Child 2</li>\n\t\t<li>Radius Test Child 3</li>\n\t</ul>"
222 assert_page_renders :radius, expected
224 def test_render__with_layout
225 expected = "<html>\n <head>\n <title>Page With Layout</title>\n </head>\n <body>\n Page With Layout\n </body>\n</html>\n"
226 assert_page_renders :page_with_layout, expected
229 def test_render_snippet
230 assert_snippet_renders :first, 'test'
232 def test_render_snippet_with_filter
233 assert_snippet_renders :markdown, '<p><strong>markdown</strong></p>'
235 def test_render_snippet_with_tag
236 assert_snippet_renders :snippet_with_tag, 'New Page'
240 @page = pages(:textile)
241 @page.process(@request, @response)
242 assert_match %r{Some <strong>Textile</strong> content.}, @response.body
244 def test_process_with_headers
245 @page = pages(:test_page)
246 @page.process(@request, @response)
247 assert_equal 'beans', @response.headers['cool']
248 assert_equal 'TestRequest', @response.headers['request']
249 assert_equal 'TestResponse', @response.headers['response']
251 def test_process__page_with_content_type_set_on_layot
252 @page = pages(:page_with_content_type_set_on_layout)
253 @page.process(@request, @response)
254 assert_response :success
255 assert_equal 'text/html;charset=utf8', @response.headers['Content-Type']
259 @page = pages(:homepage)
260 assert_equal Status[:published], @page.status
264 @page = pages(:homepage)
265 draft = Status[:draft]
267 assert_equal draft, @page.status
268 assert_equal draft.id, @page.status_id
272 assert_equal true, @page.cache?
275 def test_layout__inherited
276 @page = pages(:child_of_page_with_layout)
277 assert_equal nil, @page.layout_id
278 assert_equal @page.parent.layout, @page.layout
282 assert_equal false, @page.virtual?
286 @page = create_test_page(:class_name => "ArchiveMonthIndexPage")
287 assert_kind_of ArchiveMonthIndexPage, @page
288 assert_equal true, @page.virtual?
289 assert_equal true, @page.virtual
293 assert_equal 'this is just a test page', TestPage.description
296 def test_defined_tags
297 assert_page_renders :test_page, 'Hello world! Another test.'
299 def test_defined_tags_are_unique_for_each_behavior
300 assert_render_error %r{undefined tag `test1'}, '<r:test1 />'
304 @page = pages(:test_page)
305 assert_equal "Hello world! Another test.", @page.render_part(:body)
308 def test_display_name_class_method
309 assert_equal "Page", Page.display_name
311 assert_equal "Test", TestPage.display_name
313 TestPage.display_name = "New Name"
314 assert_equal "New Name", TestPage.display_name
316 assert_equal "File Not Found", FileNotFoundPage.display_name
319 def test_decendants_class_method
320 descendants = Page.descendants
321 assert_kind_of Array, descendants
322 assert_match /TestPage/, descendants.inspect
325 def test_mass_assignment_for_class_name
327 @page.attributes = { :class_name => 'ArchivePage' }
329 assert_equal 'ArchivePage', @page.class_name
332 def test_mass_assignment_class_name_must_be_set_to_a_valid_descendant
334 @page.attributes = {:class_name => 'Object' }
336 assert_not_nil @page.errors.on(:class_name)
337 assert_equal @page.errors.on(:class_name), 'must be set to a valid descendant of Page'
340 def test_class_name_must_be_a_valid_descendant
342 @page.class_name = 'Object'
344 assert_not_nil @page.errors.on(:class_name)
345 assert_equal @page.errors.on(:class_name), 'must be set to a valid descendant of Page'
348 def test_class_name_can_be_set_to_page_or_empty_or_nil
349 [nil, '', 'Page'].each do |value|
350 @page = ArchivePage.new
351 @page.class_name = value
353 assert_equal value, @page.class_name
357 def test_new_with_defaults_class_method__nil_config
358 @page = Page.new_with_defaults({})
359 assert_equal 0, @page.parts.size
362 def test_new_with_defaults_class_method__default_parts
363 @page = Page.new_with_defaults({ 'defaults.page.parts' => 'a, b, c'})
364 assert_equal 3, @page.parts.size
365 assert_equal 'a', @page.parts.first.name
366 assert_equal 'c', @page.parts.last.name
369 def test_new_with_defaults_class_method__default_status
370 @page = Page.new_with_defaults({ 'defaults.page.status' => 'published' })
371 assert_equal Status[:published], @page.status
374 def test_descendant_class_class_method
375 ["", nil, "Page"].each do |value|
376 assert_equal Page.descendant_class(value), Page
378 assert_equal Page.descendant_class("TestPage"), TestPage
379 assert_equal Page.descendant_class("NoCachePage"), NoCachePage
380 assert_equal Page.descendant_class("ArchivePage"), ArchivePage
383 def test_is_descendant_class_name_class_method
384 ["", nil, "Page", "TestPage", "NoCachePage", "ArchivePage"].each do |value|
385 assert Page.is_descendant_class_name?(value)
387 assert !Page.is_descendant_class_name?("InvalidPage")
390 def test_optimistic_locking
394 assert_raises(ActiveRecord::StaleObjectError) {
399 def test_should_update_virtual_based_on_new_class_name
400 # turn a regular page into a virtual page
401 @page.class_name = "VirtualPage"
402 @page.slug = "virtual1"
404 assert @page.virtual?
405 assert @page.send(:read_attribute, :virtual)
407 @page = VirtualPage.create(page_params)
408 # turn a virtual page into a non-virtual one
409 ["", nil, "Page", "EnvDumpPage"].each do |value|
410 @page.class_name = value
411 assert_nothing_raised { @page.save! }
412 @page = Page.find(@page.id)
413 assert_instance_of Page.descendant_class(value), @page
414 assert !@page.virtual?
415 assert !@page.send(:read_attribute, :virtual)
420 @page = pages(:homepage)
421 assert(@page.has_part?(:body))
422 assert(@page.has_part?('extended'))
423 assert(!@page.has_part?(:obviously_false_part_name))
425 def test_has_or_inherits_part
426 @page = pages(:child)
427 assert(!@page.has_part?(:sidebar))
428 assert(@page.has_or_inherits_part?(:sidebar))
429 assert(!@page.has_or_inherits_part?(:obviously_false_part_name))
431 @page = pages(:homepage)
432 assert(@page.has_part?(:sidebar))
433 assert(@page.has_or_inherits_part?(:sidebar))
434 assert(!@page.has_or_inherits_part?(:obviously_false_part_name))
436 def test_inherits_part
437 @page = pages(:child)
438 assert(!@page.has_part?(:sidebar))
439 assert(@page.inherits_part?(:sidebar))
441 @page = pages(:homepage)
442 assert(@page.has_part?(:sidebar))
443 assert(!@page.inherits_part?(:sidebar))
445 @page = pages(:radius)
446 assert(@page.has_part?(:extended))
447 assert(!@page.inherits_part?(:extended))