From 7f27892a75fa5c8e2de6a4693c895147e2953098 Mon Sep 17 00:00:00 2001 From: pezholio Date: Thu, 19 Dec 2024 09:18:51 +0000 Subject: [PATCH] Add component for HostContentUpdateEvents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’ve also added a lightweight Cucumber test to ensure everything fits together as we expect. I had to make a slight change to one of the steps to ensure we’re always visiting the edit page for the latest edition --- .../views/_host-content-update-event.scss | 20 ++++++++++++ app/assets/stylesheets/application.scss | 1 + .../document_history_tab_component.rb | 2 ++ ...st_content_update_event_component.html.erb | 11 +++++++ .../host_content_update_event_component.rb | 23 +++++++++++++ ...dmin-history-content-block-updates.feature | 17 ++++++++++ .../content_block_update_steps.rb | 13 ++++++++ features/step_definitions/document_steps.rb | 12 ++++++- .../document_history_tab_component_test.rb | 13 ++++++++ ...ost_content_update_event_component_test.rb | 32 +++++++++++++++++++ 10 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/admin/views/_host-content-update-event.scss create mode 100644 app/components/admin/editions/host_content_update_event_component.html.erb create mode 100644 app/components/admin/editions/host_content_update_event_component.rb create mode 100644 features/admin-history-content-block-updates.feature create mode 100644 features/step_definitions/content_block_update_steps.rb create mode 100644 test/components/admin/editions/host_content_update_event_component_test.rb diff --git a/app/assets/stylesheets/admin/views/_host-content-update-event.scss b/app/assets/stylesheets/admin/views/_host-content-update-event.scss new file mode 100644 index 00000000000..15e8e392043 --- /dev/null +++ b/app/assets/stylesheets/admin/views/_host-content-update-event.scss @@ -0,0 +1,20 @@ +.app-view-editions-host-content-update-event-entry { + &__list-item { + margin-bottom: govuk-spacing(4); + } + + &__detail { + margin-top: govuk-spacing(0); + margin-bottom: govuk-spacing(0); + } + + &__heading { + margin-bottom: govuk-spacing(1); + } + + &__datetime { + margin-top: govuk-spacing(0); + margin-bottom: govuk-spacing(0); + color: $govuk-secondary-text-colour; + } +} diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 15a3d953592..f69dcfc37b6 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -34,6 +34,7 @@ $govuk-page-width: 1140px; @import "./admin/views/filter"; @import "./admin/views/govspeak-help"; @import "./admin/views/groups-index"; +@import "./admin/views/host-content-update-event"; @import "./admin/views/historical-accounts-index"; @import "./admin/views/organisations-index"; @import "./admin/views/organisations-edit"; diff --git a/app/components/admin/editions/document_history_tab_component.rb b/app/components/admin/editions/document_history_tab_component.rb index 033887c495c..283715292ed 100644 --- a/app/components/admin/editions/document_history_tab_component.rb +++ b/app/components/admin/editions/document_history_tab_component.rb @@ -26,6 +26,8 @@ def entries_on_previous_editions def render_entry(entry) if entry.is_a?(EditorialRemark) render(Admin::Editions::EditorialRemarkComponent.new(editorial_remark: entry)) + elsif entry.is_a?(HostContentUpdateEvent) + render(Admin::Editions::HostContentUpdateEventComponent.new(entry)) else render(Admin::Editions::AuditTrailEntryComponent.new(entry:, edition:)) end diff --git a/app/components/admin/editions/host_content_update_event_component.html.erb b/app/components/admin/editions/host_content_update_event_component.html.erb new file mode 100644 index 00000000000..3f85397a9ac --- /dev/null +++ b/app/components/admin/editions/host_content_update_event_component.html.erb @@ -0,0 +1,11 @@ +
+

Content Block Update

+ +

+ <%= activity %> +

+ + +
diff --git a/app/components/admin/editions/host_content_update_event_component.rb b/app/components/admin/editions/host_content_update_event_component.rb new file mode 100644 index 00000000000..f224b858300 --- /dev/null +++ b/app/components/admin/editions/host_content_update_event_component.rb @@ -0,0 +1,23 @@ +class Admin::Editions::HostContentUpdateEventComponent < ViewComponent::Base + include ApplicationHelper + + def initialize(event) + @event = event + end + +private + + attr_reader :event + + def activity + "#{event.content_title.strip} updated" + end + + def time + absolute_time(event.created_at) + end + + def actor + event.author ? linked_author(event.author, class: "govuk-link") : "User (removed)" + end +end diff --git a/features/admin-history-content-block-updates.feature b/features/admin-history-content-block-updates.feature new file mode 100644 index 00000000000..a3808cd478e --- /dev/null +++ b/features/admin-history-content-block-updates.feature @@ -0,0 +1,17 @@ +Feature: Showing content block updates in history + + Background: + Given I am an editor + And a published news article "Stubble to be Outlawed" exists + And the document has been updated by a change to the content block "Some email address" + + Scenario: Content block update exists for current edition + When I am on the edit page for news article "Stubble to be Outlawed" + And I click the "History" tab + Then I should see an entry for the content block "Some email address" on the current edition + + Scenario: Content block update exists for a previous edition + When I force publish a new edition of the news article "Stubble to be Outlawed" + And I am on the edit page for news article "Stubble to be Outlawed" + And I click the "History" tab + Then I should see an entry for the content block "Some email address" on the previous edition diff --git a/features/step_definitions/content_block_update_steps.rb b/features/step_definitions/content_block_update_steps.rb new file mode 100644 index 00000000000..662775d0146 --- /dev/null +++ b/features/step_definitions/content_block_update_steps.rb @@ -0,0 +1,13 @@ +And(/^the document has been updated by a change to the content block "([^"]*)"$/) do |content_block_title| + host_content_update_event = build(:host_content_update_event, content_title: content_block_title) + HostContentUpdateEvent.expects(:all_for_date_window).at_least_once.returns([host_content_update_event]) +end + +Then(/^I should see an entry for the content block "([^"]*)" on the (current|previous) edition$/) do |content_block, current_or_previous| + selector = current_or_previous == "current" ? ".app-view-editions__current-edition-entries" : ".app-view-editions__previous-edition-entries" + + within selector do + assert_text "Content Block Update" + assert_text "#{content_block} updated" + end +end diff --git a/features/step_definitions/document_steps.rb b/features/step_definitions/document_steps.rb index 46bbf4b6642..9e006a89b3e 100644 --- a/features/step_definitions/document_steps.rb +++ b/features/step_definitions/document_steps.rb @@ -92,6 +92,16 @@ publish end +When("I force publish a new edition of {edition}") do |edition| + stub_publishing_api_links_with_taxons(edition.content_id, %w[a-taxon-content-id]) + visit_edition_admin edition.title + click_button "Create new edition" + fill_in_change_note_if_required + apply_to_all_nations_if_required + click_button "Save and go to document summary" + publish(force: true) +end + When("I force publish {edition}") do |edition| stub_publishing_api_links_with_taxons(edition.content_id, %w[a-taxon-content-id]) visit_edition_admin edition.title, :draft @@ -139,7 +149,7 @@ When(/^I am on the edit page for (.*?) "(.*?)"$/) do |document_type, title| document_type = document_type.tr(" ", "_") - document = document_type.classify.constantize.find_by(title:) + document = document_type.classify.constantize.where(title:).last visit send("edit_admin_#{document_type}_path", document) end diff --git a/test/components/admin/editions/document_history_tab_component_test.rb b/test/components/admin/editions/document_history_tab_component_test.rb index 7cb766cc04d..7aa70aec28a 100644 --- a/test/components/admin/editions/document_history_tab_component_test.rb +++ b/test/components/admin/editions/document_history_tab_component_test.rb @@ -54,17 +54,22 @@ class Admin::Editions::DocumentHistoryTabComponentTest < ViewComponent::TestCase assert_selector ".app-view-editions__newer-edition-entries h3", text: "On newer editions" assert_selector ".app-view-editions__newer-edition-entries div.app-view-editions-audit-trail-entry__list-item", count: 2 assert_selector ".app-view-editions__newer-edition-entries div.app-view-editions-editorial-remark__list-item", count: 1 + assert_selector ".app-view-editions__newer-edition-entries div.app-view-editions-host-content-update-event-entry__list-item", count: 0 assert_selector ".app-view-editions__current-edition-entries h3", text: "On this edition" assert_selector ".app-view-editions__current-edition-entries div.app-view-editions-audit-trail-entry__list-item", count: 4 assert_selector ".app-view-editions__current-edition-entries div.app-view-editions-editorial-remark__list-item", count: 1 + assert_selector ".app-view-editions__current-edition-entries div.app-view-editions-host-content-update-event-entry__list-item", count: 1 assert_selector ".app-view-editions__previous-edition-entries h3", text: "On previous editions" assert_selector ".app-view-editions__previous-edition-entries div.app-view-editions-audit-trail-entry__list-item", count: 2 assert_selector ".app-view-editions__previous-edition-entries div.app-view-editions-editorial-remark__list-item", count: 0 + assert_selector ".app-view-editions__previous-edition-entries div.app-view-editions-host-content-update-event-entry__list-item", count: 2 end def seed_document_event_history + @events = [] + acting_as(@user) do @document = create(:document) @first_edition = create(:draft_edition, document: @document, major_change_published_at: Time.zone.now) @@ -96,6 +101,10 @@ def seed_document_event_history @first_edition.publish! end + some_time_passes + @events << build(:host_content_update_event, created_at: Time.zone.now) + some_time_passes + @events << build(:host_content_update_event, created_at: Time.zone.now) some_time_passes acting_as(@user) do @@ -108,6 +117,8 @@ def seed_document_event_history @second_edition.publish! end + some_time_passes + @events << build(:host_content_update_event, created_at: Time.zone.now) some_time_passes acting_as(@user2) do @@ -117,6 +128,8 @@ def seed_document_event_history some_time_passes create(:editorial_remark, edition: @third_edition, author: @user, body: "Drafted to include newer changes.") end + + HostContentUpdateEvent.stubs(:all_for_date_window).returns(@events) end def some_time_passes diff --git a/test/components/admin/editions/host_content_update_event_component_test.rb b/test/components/admin/editions/host_content_update_event_component_test.rb new file mode 100644 index 00000000000..7379fd9684d --- /dev/null +++ b/test/components/admin/editions/host_content_update_event_component_test.rb @@ -0,0 +1,32 @@ +require "test_helper" + +class Admin::Editions::HostContentUpdateEventComponentTest < ViewComponent::TestCase + extend Minitest::Spec::DSL + include Rails.application.routes.url_helpers + + let(:created_at) { Time.zone.local(2020, 1, 1, 11, 11) } + let(:content_title) { "Some content" } + let(:user) { build_stubbed(:user) } + + let(:host_content_update_event) do + build(:host_content_update_event, content_title:, created_at:, author: user) + end + + it "constructs output based on the entry when an actor is present" do + render_inline(Admin::Editions::HostContentUpdateEventComponent.new(host_content_update_event)) + + assert_equal page.find("h4").text, "Content Block Update" + assert_equal page.all("p")[0].text.strip, "#{content_title} updated" + assert_equal page.all("p")[1].text.strip, "1 January 2020 11:11am by #{user.name}" + end + + describe "when an actor is not present" do + let(:user) { nil } + + it "shows removed user when an actor is not present" do + render_inline(Admin::Editions::HostContentUpdateEventComponent.new(host_content_update_event)) + + assert_equal page.all("p")[1].text.strip, "1 January 2020 11:11am by User (removed)" + end + end +end