summaryrefslogtreecommitdiff
path: root/test/models/node_action_test.rb
blob: 4672456a2628d279029befc3c7f3d83e8e0e624c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 "first publish carries the byline when the page has one" do
    diff = NodeAction.head_diff(nil, build_page(:user => users(:quentin)))

    assert_equal({ "from" => nil, "to" => users(:quentin).login }, diff[:author])
  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 "asset delta 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)
    assert diff[:assets].present?
    assert_nil NodeAction.head_diff(old_page, old_page)[:assets]
  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

  test "record! passes provenance and historical timestamps through" do
    long_ago = 2.years.ago
    node = Node.root.children.create!(:slug => "provenance_backfill_test")
    action = NodeAction.record!(:node => node, :action => "publish",
                                 :occurred_at => long_ago,
                                 :inferred_from => "from_page_revision")

    assert_equal "from_page_revision", action.inferred_from
    assert_in_delta long_ago.to_f, action.occurred_at.to_f, 1.0
  end


  test "record! with participants: writes one action_participant per subject" do
    n1, n2 = Node.root.children.create!(:slug => "participant_a"), Node.root.children.create!(:slug => "participant_b")
    action = NodeAction.record!(:participants => [n1, n2], :action => "trash", :user => users(:quentin))
    assert_equal [n1, n2], action.action_participants.map(&:subject)
  end

  test "record! with node: alone still writes exactly one participant" do
    node = Node.root.children.create!(:slug => "participant_sugar")
    action = NodeAction.record!(:node => node, :action => "create", :user => users(:quentin))
    assert_equal [node], action.action_participants.map(&:subject)
  end

  test "record! refuses an empty participant set" do
    assert_raises(ArgumentError) { NodeAction.record!(:action => "create", :user => users(:quentin)) }
  end
end