From c0c9250141c2fa64fed967b8b9ad9bada3694c3d Mon Sep 17 00:00:00 2001 From: erdgeist Date: Thu, 16 Jul 2026 02:40:03 +0200 Subject: Record the full lifecycle contract in NodeAction entries A contract comment above NodeAction.record! now specifies every verb's metadata shape. NodeAction.head_diff computes the publish diff between an outgoing head and its replacement -- default-locale title pair always, author/tags pairs and template/assets/abstract/ body flags only when changed, and a per-locale translation_diff with added/removed/changed status. It is a pure function of its two pages, shared by publish, rollback, and the future backfill, and reads translation rows directly so fallbacks never masquerade as content. publish entries carry via ("draft" or "revision"); restore_revision! is now transactional, takes the acting user, and logs through the same diff. Staged slug/parent changes applied at publish log a move entry with the path pair. Node creation logs a create entry with initial title and path. The draft-scoped translation_destroy writer is retired -- locale removal is recorded by the publish diff, where it becomes public fact. --- test/models/node_test.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) (limited to 'test/models/node_test.rb') diff --git a/test/models/node_test.rb b/test/models/node_test.rb index 022fff6..c8e49e5 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb @@ -633,6 +633,7 @@ class NodeTest < ActiveSupport::TestCase assert_equal node.head, action.page assert_equal @user1, action.user assert_equal "publish", action.action + assert_equal "draft", action.metadata["via"] end test "publish_draft! called with no user logs no actor, not a guessed one" do @@ -692,7 +693,7 @@ class NodeTest < ActiveSupport::TestCase assert_equal count_before, NodeAction.count end - test "publish_draft! records the title's from/to in metadata" do + test "publish_draft! records the title diff in metadata" do node = create_node_with_published_page Globalize.with_locale(:de) { node.head.update!(:title => "Original Title") } find_or_create_draft(node, @user1) @@ -701,7 +702,63 @@ class NodeTest < ActiveSupport::TestCase node.publish_draft!(@user1) action = NodeAction.last - assert_equal "Original Title", action.metadata["from"] - assert_equal "New Title", action.metadata["to"] + assert_equal "draft", action.metadata["via"] + assert_equal "Original Title", action.metadata.dig("title", "from") + assert_equal "New Title", action.metadata.dig("title", "to") + end + + test "publishing a staged slug change logs a move with the path pair" do + node = create_node_with_published_page + path_before = node.unique_name + node.staged_slug = "moved-#{node.slug}" + node.save! + publish_count_before = NodeAction.where(:action => "publish").count + + node.publish_draft!(@user1) + + node.reload + assert_not_equal path_before, node.unique_name + + action = NodeAction.where(:action => "move").last + assert_equal node, action.node + assert_equal @user1, action.user + assert_equal path_before, action.metadata.dig("path", "from") + assert_equal node.unique_name, action.metadata.dig("path", "to") + + # No draft was pending: path change alone must not fabricate a publish. + assert_equal publish_count_before, NodeAction.where(:action => "publish").count + end + + test "publishing a draft together with a staged move logs two entries" do + node = create_node_with_published_page + find_or_create_draft(node, @user1) + node.staged_slug = "relocated-#{node.slug}" + node.save! + + assert_difference "NodeAction.count", 2 do + node.publish_draft!(@user1) + end + + assert_equal %w[move publish], + NodeAction.order(:id).last(2).map(&:action).sort + end + + test "restore_revision! logs a publish via revision" do + node = create_node_with_published_page + Globalize.with_locale(:de) { node.head.update!(:title => "First") } + first_head = node.head + find_or_create_draft(node, @user1) + Globalize.with_locale(:de) { node.draft.update!(:title => "Second") } + node.publish_draft!(@user1) + + node.restore_revision!(first_head.revision, @user1) + + action = NodeAction.last + assert_equal "publish", action.action + assert_equal "revision", action.metadata["via"] + assert_equal first_head, action.page + assert_equal @user1, action.user + assert_equal "Second", action.metadata.dig("title", "from") + assert_equal "First", action.metadata.dig("title", "to") end end -- cgit v1.3