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_action_test.rb | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 test/models/node_action_test.rb (limited to 'test/models/node_action_test.rb') diff --git a/test/models/node_action_test.rb b/test/models/node_action_test.rb new file mode 100644 index 0000000..24d8245 --- /dev/null +++ b/test/models/node_action_test.rb @@ -0,0 +1,112 @@ +require "test_helper" + +class NodeActionTest < ActiveSupport::TestCase + + # Standalone pages, no node -- same pattern autosave relies on. + # Default-locale attributes are written pinned, never ambient. + def build_page en: nil, **attrs + page = Globalize.with_locale(I18n.default_locale) do + Page.create!({ :title => "Titel" }.merge(attrs)) + end + page.translations.create!({ :locale => "en" }.merge(en)) if en + page.reload + end + + test "first publish yields exactly the title pair, from nil" do + diff = NodeAction.head_diff(nil, build_page(:title => "Erstausgabe")) + + assert_equal({ :title => { "from" => nil, "to" => "Erstausgabe" } }, diff) + end + + test "title pair is always present, even when unchanged" do + diff = NodeAction.head_diff(build_page, build_page) + + assert_equal "Titel", diff[:title]["from"] + assert_equal "Titel", diff[:title]["to"] + end + + test "author pair present only when the byline changed" do + u1, u2 = users(:quentin), users(:aaron) + + assert_nil NodeAction.head_diff(build_page(:user => u1), build_page(:user => u1))[:author] + + changed = NodeAction.head_diff(build_page(:user => u1), build_page(:user => u2)) + assert_equal({ "from" => u1.login, "to" => u2.login }, changed[:author]) + end + + test "tags pair present only when the tag set changed" do + old_page = build_page; old_page.tag_list = "alpha, beta"; old_page.save! + new_page = build_page; new_page.tag_list = "beta, gamma"; new_page.save! + + diff = NodeAction.head_diff(old_page.reload, new_page.reload) + assert_equal %w[alpha beta], diff[:tags]["from"] + assert_equal %w[beta gamma], diff[:tags]["to"] + + same = NodeAction.head_diff(old_page, old_page) + assert_nil same[:tags] + end + + test "template_changed flag only when true" do + old_page = build_page(:template_name => "standard_template") + + assert NodeAction.head_diff(old_page, build_page(:template_name => "update"))[:template_changed] + assert_nil NodeAction.head_diff(old_page, build_page(:template_name => "standard_template"))[:template_changed] + end + + test "assets_changed flag when the attached set differs" do + asset = Asset.create!(:name => "diff probe", + :upload_file_name => "test_image.png", + :upload_content_type => "image/png", + :upload_file_size => 49854, + :upload_updated_at => Time.current) + old_page, new_page = build_page, build_page + new_page.related_assets.create!(:asset_id => asset.id, :position => 1) + + + diff = NodeAction.head_diff(old_page, new_page.reload) + assert diff[:assets_changed] + assert_nil NodeAction.head_diff(old_page, old_page)[:assets_changed] + end + + test "default-locale abstract and body changes become flags, only when true" do + diff = NodeAction.head_diff(build_page(:abstract => "a"), build_page(:abstract => "b")) + + assert diff[:abstract_changed] + assert_nil diff[:body_changed] + end + + test "added locale carries status and the new title" do + diff = NodeAction.head_diff(build_page, build_page(en: { :title => "English" })) + + entry = diff[:translation_diff]["en"] + assert_equal "added", entry["status"] + assert_equal({ "from" => nil, "to" => "English" }, entry["title"]) + end + + test "removed locale keeps the vanished title" do + diff = NodeAction.head_diff(build_page(en: { :title => "English" }), build_page) + + entry = diff[:translation_diff]["en"] + assert_equal "removed", entry["status"] + assert_equal "English", entry["title"]["from"] + assert_nil entry["title"]["to"] + end + + test "changed locale carries only what actually differs" do + old_page = build_page(en: { :title => "Same", :body => "old" }) + new_page = build_page(en: { :title => "Same", :body => "new" }) + + entry = NodeAction.head_diff(old_page, new_page)[:translation_diff]["en"] + assert_equal "changed", entry["status"] + assert_nil entry["title"] + assert entry["body_changed"] + assert_nil entry["abstract_changed"] + end + + test "untouched locales are absent; identical pages yield no translation_diff at all" do + old_page = build_page(en: { :title => "Same" }) + new_page = build_page(en: { :title => "Same" }) + + assert_nil NodeAction.head_diff(old_page, new_page)[:translation_diff] + end +end -- cgit v1.3