summaryrefslogtreecommitdiff
path: root/test/models/helpers/node_actions_helper_test.rb
blob: 1b72ec9cdeb152f8a9c2b30e721006778a083d22 (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
146
require 'test_helper'

class NodeActionsHelperTest < ActionView::TestCase
  def setup
    @original_locale = I18n.locale
    I18n.locale = :en
  end

  def default_url_options
    { :locale => nil }
  end

  def icon(_name, **)
    '<svg class="stub-icon"></svg>'.html_safe
  end

  def teardown
    I18n.locale = @original_locale
  end

  def entry action, metadata = {}, node: nil, user: nil, page: nil
    NodeAction.create!(:node => node, :user => user, :page => page,
                        :action => action, :occurred_at => Time.now,
                        :metadata => { "username" => "quentin",
                                       "human_readable_node_name" => "Subject" }.merge(metadata))
  end

  test "publish renders the revision sentence, not the titles" do
    out = action_summary(entry("publish",
      { "via" => "draft", "title" => { "from" => "Old", "to" => "New" } }))

    assert_includes out, "quentin"
    assert_includes out, "new revision"
    assert_not_includes out, "Old"
  end

  test "first publish uses its own sentence" do
    out = action_summary(entry("publish",
      { "via" => "draft", "title" => { "from" => nil, "to" => "New" } }))

    assert_includes out, "for the first time"
  end

  test "rollback publishes get the rollback sentence" do
    out = action_summary(entry("publish",
      { "via" => "revision", "title" => { "from" => "Now", "to" => "Then" } }))

    assert_includes out, "rolled"
  end

  test "move renders the path pair" do
    out = action_summary(entry("move",
      { "path" => { "from" => "a/b", "to" => "a/c" } }))

    assert_includes out, "a/b"
    assert_includes out, "a/c"
  end

  test "unknown verbs degrade to a generic sentence, never an error" do
    out = action_summary(entry("frobnicate"))

    assert_includes out, "frobnicate"
    assert_includes out, "quentin"
  end

  test "dead references render as plain names from metadata, no links" do
    out = action_summary(entry("publish",
      { "title" => { "from" => "Old", "to" => "New" } }))

    assert_not_includes out, "<a "
    assert_includes out, "Subject"
  end

  test "metadata values are escaped" do
    out = action_summary(entry("publish",
      { "human_readable_node_name" => "<b>bold</b>" }))

    assert_not_includes out, "<b>"
  end

  test "live associations upgrade names to links" do
    out = action_summary(entry("publish",
      { "title" => { "from" => "Old", "to" => "New" } },
      :user => users(:quentin)))

    assert_includes out, "<a "
  end

  test "details are guarded off when nothing but an unchanged title is present" do
    unchanged = entry("publish", { "title" => { "from" => "Same", "to" => "Same" } })
    changed   = entry("publish", { "title" => { "from" => "Old",  "to" => "New" } })

    assert_not action_details?(unchanged)
    assert action_details?(changed)
  end

  test "first publish with a byline names the author" do
    out = action_summary(entry("publish",
      { "title" => { "from" => nil, "to" => "New" },
        "author" => { "from" => nil, "to" => "quentin" } }))

    assert_includes out, "author"
  end

  test "create entries with their flat title render and are guarded off details" do
    e = entry("create", { "title" => "Initial", "path" => "a/b" })

    assert_includes action_summary(e), "a/b"
    assert_not action_details?(e)
  end

  test "trash, restore, and destroy render their paths" do
    trash   = entry("trash",   { "path" => { "from" => "club/old", "to" => "trash/old" } })
    restore = entry("restore_from_trash", { "path" => { "from" => "trash/old", "to" => "club/new" } })
    doomed  = entry("destroy", { "path" => "trash/old" })

    assert_includes action_summary(trash),   "club/old"
    assert_includes action_summary(restore), "club/new"
    assert_includes action_summary(doomed),  "trash/old"
  end

  test "revision lifecycle badges cover create, publish, rollback, and inference" do
    created   = entry("create",  { "title" => "x", "path" => "a/b" })
    published = entry("publish", { "via" => "draft",    "title" => { "from" => nil, "to" => "x" } })
    restored  = entry("publish", { "via" => "revision", "title" => { "from" => "y", "to" => "x" } })
    restored.update!(:inferred_from => "from_page_revision")

    out = revision_lifecycle_badges([created, published, restored])

    assert_includes out, "created"
    assert_includes out, "published"
    assert_includes out, "restored"
    assert_includes out, "quentin"
    assert_includes out, "node_action_inferred"
    assert_includes out, "<span"
    assert_not_includes out, "&lt;span"

    assert_equal "", revision_lifecycle_badges(nil)
  end

  test "verb icons map known verbs, distinguish rollbacks, and fall back" do
    assert_includes verb_icon(entry("trash", { "path" => { "from" => "a", "to" => "t/a" } })), "node_action_icon--trash"
    assert_includes verb_icon(entry("publish", { "via" => "revision" })), "node_action_icon--history"
    assert_includes verb_icon(entry("frobnicate")), "node_action_icon--circle-dashed"
  end
end