Skip to content

Commit

Permalink
Append edition_actions into history
Browse files Browse the repository at this point in the history
This updates the `edition_actions` helper to accept an array
of host_content_update events, filter by if they apply to
the edition, then cast as an actionable item.
  • Loading branch information
pezholio committed Jan 28, 2025
1 parent 335d62c commit 357fd32
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 4 deletions.
7 changes: 5 additions & 2 deletions app/helpers/action_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module ActionHelper
def edition_actions(edition)
edition.actions.reverse.delete_if do |a|
def edition_actions(edition, update_events)
actions = edition.actions.reject do |a|
[Action::IMPORTANT_NOTE, Action::IMPORTANT_NOTE_RESOLVED].include?(a.request_type)
end
update_actions = update_events.select { |e| e.is_for_edition?(edition) }.map(&:to_action)
actions.append(*update_actions)
actions.sort_by(&:created_at).reverse
end

def action_note?(action)
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_edition_history.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<div class="panel-collapse collapse<% if edition_counter == 0 %> in<% end %>" id="body<%= edition.version_number %>">
<ul class="panel-body list-unstyled remove-bottom-margin">
<% edition_actions(edition).each do |action| %>
<% edition_actions(edition, update_events).each do |action| %>
<li class="action-<%= action_class(action) %> add-bottom-margin add-left-margin">
<h3 class="h4">
<div class="add-label-margin normal">
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_history.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@
</p>

<div class="panel-group">
<%= render collection: @resource.history, partial: "/shared/edition_history", as: "edition" %>
<%= render collection: @resource.history, partial: "/shared/edition_history", as: "edition", locals: { update_events: @update_events } %>
</div>
</div>
143 changes: 143 additions & 0 deletions test/integration/host_content_update_history_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
require "legacy_integration_test_helper"

class HostContentUpdateHistoryTest < LegacyJavascriptIntegrationTest
setup do
setup_users
stub_linkables
stub_holidays_used_by_fact_check
end

context "Viewing content update history" do
setup do
user1 = {
"uid" => SecureRandom.uuid,
"name" => "User 1",
"email" => "[email protected]",
}

user2 = {
"uid" => SecureRandom.uuid,
"name" => "User 2",
"email" => "[email protected]",
}

@host_content_update_events = []

@out_of_scope_content_update_event = create_content_update_event(
updated_by_user_uid: user1["uid"],
)

some_time_passes
edition1 = FactoryBot.create(:answer_edition, slug: "test-slug")

some_time_passes
edition1.new_action(@author, Action::SEND_FACT_CHECK)

some_time_passes
edition1.new_action(@author, Action::RECEIVE_FACT_CHECK)

some_time_passes
edition1.new_action(@author, Action::PUBLISH)
edition1.state = "published"
edition1.save!

some_time_passes
@edition1_content_update_event = create_content_update_event(
updated_by_user_uid: user1["uid"],
)

some_time_passes
edition2 = edition1.build_clone
edition2.save!

some_time_passes
edition2.new_action(@author, Action::SEND_FACT_CHECK)

some_time_passes
edition2.new_action(@author, Action::RECEIVE_FACT_CHECK)

some_time_passes
edition2.new_action(@author, Action::PUBLISH)
edition1.state = "archived"
edition1.save!
edition2.state = "published"
edition2.save!

some_time_passes
@edition2_content_update_event = create_content_update_event(
updated_by_user_uid: user2["uid"],
)

some_time_passes
@edition2_content_update_event_1 = create_content_update_event(
updated_by_user_uid: user1["uid"],
)

some_time_passes
@edition3 = edition2.build_clone(GuideEdition)
@edition3.save!

@edition3.new_action(@author, Action::NEW_VERSION)

all_events = [
@out_of_scope_content_update_event,
@edition1_content_update_event,
@edition2_content_update_event,
@edition2_content_update_event_1,
]

stub_events_for_all_content_ids(events: all_events)
stub_users_from_signon_api([user1["uid"], user2["uid"]], [user1, user2])
end

should "show host content update actions" do
visit_edition @edition3
click_on "History and notes"

assert page.has_no_content?("Content block updated")
assert page.has_no_content?("Email address updated")

click_on "Edition 2"
within "#version2" do
within page.all(".action-content-block-updated")[0] do
assert page.has_content?(Time.zone.parse(@edition2_content_update_event_1["created_at"]).to_fs(:govuk_date))
assert page.has_content?("Content block updated by User 1")
assert page.has_content?("Email address updated")
end

within page.all(".action-content-block-updated")[1] do
assert page.has_content?(Time.zone.parse(@edition2_content_update_event["created_at"]).to_fs(:govuk_date))
assert page.has_content?("Content block updated by User 2")
assert page.has_content?("Email address updated")
end
end

click_on "Edition 1"
within "#version1" do
within ".action-content-block-updated" do
assert page.has_content?(Time.zone.parse(@edition1_content_update_event["created_at"]).to_fs(:govuk_date))
assert page.has_content?("Content block updated by User 1")
assert page.has_content?("Email address updated")
end
end
end
end

def some_time_passes
travel_to rand(1.hour..1.week).from_now
end

def create_content_update_event(updated_by_user_uid:)
{
"created_at" => Time.zone.now.to_s,
"payload" => {
"source_block" => {
"updated_by_user_uid" => updated_by_user_uid,
"content_id" => SecureRandom.uuid,
"title" => "Some content",
"document_type" => "content_block_email_address",
},
},
}
end
end
40 changes: 40 additions & 0 deletions test/unit/helpers/admin/action_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,44 @@ class ActionHelperTest < ActionView::TestCase
assert_match(/View in Content Block Manager/, note)
assert_match(/#{action.block_url}/, note)
end

test "#edition_actions includes update_events" do
edition = FactoryBot.create(:edition)

edition_actions = [
stub(:create_action, request_type: Action::CREATE, created_at: Time.zone.now - 1.week),
stub(:note_action, request_type: Action::NOTE, created_at: Time.zone.now - 2.days),
stub(:sent_fact_check_action, request_type: Action::SEND_FACT_CHECK, created_at: Time.zone.now - 3.days),
stub(:receive_fact_check_action, request_type: Action::RECEIVE_FACT_CHECK, created_at: Time.zone.now - 1.day),
stub(:important_note_action, request_type: Action::IMPORTANT_NOTE, created_at: Time.zone.now - 1.week),
stub(:important_note_resolved_action, request_type: Action::IMPORTANT_NOTE_RESOLVED, created_at: Time.zone.now - 1.week),
stub(:publish_action, request_type: Action::PUBLISH, created_at: Time.zone.now - 12.hours),
]

edition.stubs(:actions).returns(edition_actions)

update_events = [
stub("HostContentUpdateEvent", to_action: stub(:host_content_update_event_action, created_at: Time.zone.now - 2.hours)),
stub("HostContentUpdateEvent", to_action: stub(:host_content_update_event_action, created_at: Time.zone.now - 3.hours)),
stub("HostContentUpdateEvent", to_action: stub(:host_content_update_event_action, created_at: Time.zone.now)),
]

update_events[0].stubs(:is_for_edition?).with(edition).returns(true)
update_events[1].stubs(:is_for_edition?).with(edition).returns(true)
update_events[2].stubs(:is_for_edition?).with(edition).returns(false)

expected_actions = [
update_events[0].to_action,
update_events[1].to_action,
edition_actions[6],
edition_actions[3],
edition_actions[1],
edition_actions[2],
edition_actions[0],
]

result = edition_actions(edition, update_events)

assert_equal result, expected_actions
end
end

0 comments on commit 357fd32

Please sign in to comment.