From d56155231814633e04f856d22646fea24ef97011 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 15 Jul 2026 01:41:34 +0200 Subject: Add NodeAction: an append-only log of who did what to a node node_id/page_id/user_id are lookup and ordering only -- all three nullify on delete, so an entry outlives its actor and its subject. Everything that must survive those deletions lives in a mandatory metadata jsonb written once at creation: the actor's username, the node's human-readable name (pinned to the default locale), and action-specific extras such as publish's title from/to. NodeAction.record! is the single constructor, so every entry gets the same baseline metadata without each call site re-implementing it. occurred_at is one field for live and backfilled entries alike; inferred_from distinguishes them -- nil means witnessed at the moment it happened, populated names how a backfilled entry was estimated. Instrumented so far: publish (crediting the actual publisher, threaded through from the controller -- previously nobody had the act of publishing recorded anywhere), revert's discard_autosave and destroy_draft branches, and translation destroy. publish_draft! now runs in a transaction so the promotion and its log entry land together. The remaining verbs follow once this mechanism has proven itself. --- .../page_translations_controller_test.rb | 15 ++++ test/models/node_test.rb | 83 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) (limited to 'test') diff --git a/test/controllers/page_translations_controller_test.rb b/test/controllers/page_translations_controller_test.rb index 8fa732f..82f8bce 100644 --- a/test/controllers/page_translations_controller_test.rb +++ b/test/controllers/page_translations_controller_test.rb @@ -82,4 +82,19 @@ class PageTranslationsControllerTest < ActionController::TestCase assert_equal page_count_before, node.pages.count assert_equal "in progress", Globalize.with_locale(:en) { node.autosave.title } end + + test "destroy logs a translation_destroy NodeAction" do + login_as :quentin + node = Node.root.children.create!(:slug => "translation_destroy_log_test") + Globalize.with_locale(:en) { node.draft.update!(:title => "English title") } + + delete :destroy, params: { :node_id => node.id, :translation_locale => "en" } + + action = NodeAction.last + assert_equal node, action.node + assert_equal "en", action.locale + assert_equal "translation_destroy", action.action + assert_equal users(:quentin), action.user + assert_redirected_to node_path(node) + end end diff --git a/test/models/node_test.rb b/test/models/node_test.rb index 959387d..022fff6 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb @@ -621,4 +621,87 @@ class NodeTest < ActiveSupport::TestCase assert_equal "Edited directly on autosave", Globalize.with_locale(:en) { autosave.title } assert_equal "v2", Globalize.with_locale(:de) { autosave.title } end + + test "publish_draft! logs a NodeAction crediting the actual publisher" do + node = Node.root.children.create!(:slug => "publish_log_test") + node.draft.update!(:title => "Version one") + + node.publish_draft!(@user1) + + action = NodeAction.last + assert_equal node, action.node + assert_equal node.head, action.page + assert_equal @user1, action.user + assert_equal "publish", action.action + end + + test "publish_draft! called with no user logs no actor, not a guessed one" do + node = Node.root.children.create!(:slug => "publish_log_no_user_test") + node.draft.update!(:title => "Version one") + + node.publish_draft! + + action = NodeAction.last + assert_nil action.user + assert_nil action.metadata["username"] + end + + test "publish_draft! with nothing pending creates no NodeAction" do + node = Node.root.children.create!(:slug => "publish_log_noop_test") + node.publish_draft! + count_before = NodeAction.count + + result = node.publish_draft! + + assert_nil result + assert_equal count_before, NodeAction.count + end + + test "revert! logs discard_autosave for an in-progress autosave" do + node = create_node_with_published_page + node.lock_for_editing!(@user1) + node.autosave!({:title => "in progress"}, @user1) + + node.revert!(@user1) + + action = NodeAction.last + assert_equal node, action.node + assert_equal @user1, action.user + assert_equal "discard_autosave", action.action + end + + test "revert! logs destroy_draft for a draft with a head behind it" do + node = create_node_with_published_page + find_or_create_draft(node, @user1) + + node.revert!(@user1) + + action = NodeAction.last + assert_equal node, action.node + assert_equal @user1, action.user + assert_equal "destroy_draft", action.action + end + + test "revert! with nothing to revert logs nothing" do + node = create_node_with_published_page + node.lock_for_editing!(@user1) + count_before = NodeAction.count + + node.revert!(@user1) + + assert_equal count_before, NodeAction.count + end + + test "publish_draft! records the title's from/to 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) + Globalize.with_locale(:de) { node.draft.update!(:title => "New Title") } + + node.publish_draft!(@user1) + + action = NodeAction.last + assert_equal "Original Title", action.metadata["from"] + assert_equal "New Title", action.metadata["to"] + end end -- cgit v1.3