diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-21 17:34:13 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-21 17:34:13 +0200 |
| commit | 65da515e27939c90a66ec3b063b5e58943d3dcea (patch) | |
| tree | 16ede71f65cc48444561dbde6c9c95368b564eb9 /test | |
| parent | cc4659a308590be083065f19df8571efc345052a (diff) | |
Resolve preview's page-to-node lookup for every layer
pages_controller#preview loaded a page by id and rendered it
directly, assuming @page.node was always present. True for head and
draft, since both carry a real node_id: false for autosave, which
deliberately has node_id: nil (the mechanism that excludes it from
Node#pages and the revision count). A node with an autosave and no
draft underneath it: lock, start editing, never explicitly save a
draft: loads that autosave directly with no node at all, and any
view code assuming @page.node is real (headline image, credits,
gallery links) raises.
When @page has no node, resolve it by checking autosave_id, then
draft_id, then head_id on Node, and set @page.node in memory only:
the autosave's node_id: nil is never persisted differently. The
existing swap from draft to a fresher autosave, when both exist, is
unchanged.
Three tests: draft with a fresher autosave on top, autosave with no
draft underneath, and the ordinary head-only case with neither.
Diffstat (limited to 'test')
| -rw-r--r-- | test/controllers/pages_controller_test.rb | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb index 3879014..732869b 100644 --- a/test/controllers/pages_controller_test.rb +++ b/test/controllers/pages_controller_test.rb | |||
| @@ -1,5 +1,72 @@ | |||
| 1 | require 'test_helper' | 1 | require 'test_helper' |
| 2 | 2 | ||
| 3 | class PagesControllerTest < ActionController::TestCase | 3 | class PagesControllerTest < ActionController::TestCase |
| 4 | # will be removed anyway | 4 | test "preview shows the autosave when a draft and an autosave both exist" do |
| 5 | login_as :quentin | ||
| 6 | |||
| 7 | node = Node.root.children.create!(:slug => "preview_retest") | ||
| 8 | node.draft.update!(:title => "draft title") | ||
| 9 | node.publish_draft! | ||
| 10 | |||
| 11 | node.draft | ||
| 12 | node.lock_for_editing!(users(:quentin)) | ||
| 13 | node.autosave!({ :title => "draft title" }, users(:quentin)) | ||
| 14 | node.save_draft!(users(:quentin)) | ||
| 15 | node.autosave!({ :title => "autosave title" }, users(:quentin)) | ||
| 16 | |||
| 17 | get :preview, params: { :id => node.draft_id } | ||
| 18 | |||
| 19 | assert_response :success | ||
| 20 | assert_match "autosave title", response.body | ||
| 21 | end | ||
| 22 | |||
| 23 | test "preview renders a headlined image on the autosave without crashing" do | ||
| 24 | login_as :quentin | ||
| 25 | |||
| 26 | node = Node.root.children.create!(:slug => "preview_retest_with_image") | ||
| 27 | node.lock_for_editing!(users(:quentin)) | ||
| 28 | node.autosave!({ :title => "draft title" }, users(:quentin)) | ||
| 29 | node.save_draft!(users(:quentin)) | ||
| 30 | node.autosave!({ :title => "autosave title" }, users(:quentin)) | ||
| 31 | |||
| 32 | asset = Asset.create!(:name => "test", :upload_content_type => "image/png") | ||
| 33 | node.autosave.related_assets.create!(:asset_id => asset.id, :position => 1, :headline => true) | ||
| 34 | |||
| 35 | get :preview, params: { :id => node.draft_id } | ||
| 36 | |||
| 37 | assert_response :success | ||
| 38 | assert_match "autosave title", response.body | ||
| 39 | end | ||
| 40 | |||
| 41 | test "preview shows the autosave when no draft exists at all" do | ||
| 42 | login_as :quentin | ||
| 43 | |||
| 44 | node = Node.root.children.create!(:slug => "preview_retest_no_draft") | ||
| 45 | node.draft.destroy | ||
| 46 | node.update_column(:draft_id, nil) | ||
| 47 | |||
| 48 | node.lock_for_editing!(users(:quentin)) | ||
| 49 | node.autosave!({ :title => "autosave only title" }, users(:quentin)) | ||
| 50 | |||
| 51 | asset = Asset.create!(:name => "test", :upload_content_type => "image/png") | ||
| 52 | node.autosave.related_assets.create!(:asset_id => asset.id, :position => 1) | ||
| 53 | |||
| 54 | get :preview, params: { :id => node.autosave_id } | ||
| 55 | |||
| 56 | assert_response :success | ||
| 57 | assert_match "autosave only title", response.body | ||
| 58 | end | ||
| 59 | |||
| 60 | test "preview shows head normally when there is no draft or autosave" do | ||
| 61 | login_as :quentin | ||
| 62 | |||
| 63 | node = Node.root.children.create!(:slug => "preview_retest_head_only") | ||
| 64 | node.draft.update!(:title => "head title") | ||
| 65 | node.publish_draft! | ||
| 66 | |||
| 67 | get :preview, params: { :id => node.head_id } | ||
| 68 | |||
| 69 | assert_response :success | ||
| 70 | assert_match "head title", response.body | ||
| 71 | end | ||
| 5 | end | 72 | end |
