Skip to content

Commit

Permalink
Merge pull request #8958 from alphagov/1057-add-a-user-interface-for-…
Browse files Browse the repository at this point in the history
…whitehalls-republish-tasks

Add a user interface for republishing past prime ministers page
  • Loading branch information
brucebolt authored Apr 18, 2024
2 parents 78145cc + c02d553 commit 8157394
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/controllers/admin/republishing_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Admin::RepublishingController < Admin::BaseController
before_action :enforce_permissions!

def index
@republishable_documents = republishable_documents
end

def republish_past_prime_ministers_index
PresentPageToPublishingApiWorker.perform_async("PublishingApi::HistoricalAccountsIndexPresenter")
flash[:notice] = "'Past Prime Ministers' page has been scheduled for republishing"
redirect_to(admin_republishing_index_path)
end

def confirm
republishable_document = republishable_documents.find { |document| document[:slug] == params[:document_slug] }

return render "admin/errors/not_found", status: :not_found unless republishable_document

@document_title = republishable_document[:title]
@republishing_path = republishable_document[:republishing_path]
end

private

def enforce_permissions!
enforce_permission!(:administer, :republish_documents)
end

def republishable_documents
historical_accounts_index_presenter = PublishingApi::HistoricalAccountsIndexPresenter.new

[{
title: historical_accounts_index_presenter.content[:title],
public_path: historical_accounts_index_presenter.base_path,
republishing_path: admin_republishing_republish_past_prime_ministers_path,
slug: historical_accounts_index_presenter.base_path.split("/").last,
}]
end
end
18 changes: 18 additions & 0 deletions app/views/admin/republishing/confirm.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% content_for :page_title, "Republish '#{@document_title}'" %>
<% content_for :title, "Are you sure you want to republish '#{@document_title}'?" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row">
<section class="govuk-grid-column-two-thirds">
<p class="govuk-body govuk-!-margin-bottom-7">
This will schedule the page to be republished.
</p>
<%= form_with(url: @republishing_path, method: :post, data: {
module: "prevent-multiple-form-submissions",
}) do
render("govuk_publishing_components/components/button", {
text: "Confirm republishing",
})
end %>
</section>
</div>
46 changes: 46 additions & 0 deletions app/views/admin/republishing/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<% content_for :page_title, "Republish documents" %>
<% content_for :title, "Republish documents" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row">
<section class="govuk-grid-column-two-thirds">
<p class="govuk-body">
Sometimes it may be necessary to republish content to the Publishing API. This will refresh the content on the website.
</p>

<p class="govuk-body">
For example, if we make an update to govspeak and a publishing application pre-renders that content prior to its submission to Publishing API, that would require us to re-render and save new HTML for content.
</p>

<p class="govuk-body">
The following actions will allow you to schedule the republishing of content that was originally published in this application.
Any linked editions will also be republished through dependency resolution.
Try to pick the document type most focused to the scope of what you need to republish to avoid unnecessary server load.
</p>

<%= render "govuk_publishing_components/components/table", {
head: [
{
text: "Document",
},
{
text: "Action",
},
],
rows: @republishable_documents.map do |document|
[
{
text: link_to(document[:title], Plek.website_root + document[:public_path], class:"govuk-link"),
},
{
text: link_to(sanitize("Republish #{tag.span('\'' + document[:title] + '\' page', class: 'govuk-visually-hidden')}"),
admin_confirm_republishing_path(document[:slug]),
id: "republish-past-prime-ministers-index",
class: "govuk-link",
),
},
]
end,
} %>
</section>
</div>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })

resources :users, only: %i[index show edit update]

get "republishing" => "republishing#index", as: :republishing_index
get "republishing/:document_slug/confirm" => "republishing#confirm", as: :confirm_republishing
post "republishing/republish-past-prime-ministers" => "republishing#republish_past_prime_ministers_index"

resources :documents, only: [] do
resources :review_reminders, only: %i[new create edit update]
end
Expand Down
13 changes: 13 additions & 0 deletions features/republishing-documents.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Republishing published documents
As an editor
I want to be able to republish published documents
So that they reflect changes to their dependencies when this doesn't happen automatically

Background:
Given I am a GDS admin

Scenario: Republish the "Past prime ministers" page
Given a published publication "Past prime ministers" exists
And the "Past prime ministers" page can be republished
When I request a republish of the "Past prime ministers" page
Then I can see the "Past prime ministers" page has been scheduled for republishing
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Given(/^the "Past prime ministers" page can be republished$/) do
create(:ministerial_role, name: "Prime Minister", cabinet_member: true)
end

When(/^I request a republish of the "Past prime ministers" page$/) do
visit admin_republishing_index_path
find("#republish-past-prime-ministers-index").click
click_button("Confirm republishing")
end

Then(/^I can see the "Past prime ministers" page has been scheduled for republishing/) do
expect(page).to have_selector(".gem-c-success-alert", text: "'Past Prime Ministers' page has been scheduled for republishing")
end
4 changes: 4 additions & 0 deletions lib/whitehall/authority/rules/miscellaneous_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def can?(action)
end
end

def can_for_republish_documents?(_action)
actor.gds_admin?
end

def can_for_emergency_banner?(_action)
actor.gds_admin?
end
Expand Down
60 changes: 60 additions & 0 deletions test/functional/admin/republishing_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require "test_helper"

class Admin::RepublishingControllerTest < ActionController::TestCase
setup do
login_as :gds_admin
create(:ministerial_role, name: "Prime Minister", cabinet_member: true)
end

should_be_an_admin_controller

view_test "GDS Admin users should be able to acess the GET :index and see links to republishable documents" do
get :index

assert_select ".govuk-table__cell:nth-child(1) a[href='https://www.test.gov.uk/government/history/past-prime-ministers']", text: "Past Prime Ministers"
assert_select ".govuk-table__cell:nth-child(2) a[href='/government/admin/republishing/past-prime-ministers/confirm']", text: "Republish 'Past Prime Ministers' page"
assert_response :ok
end

test "Non-GDS Admin users should not be able to access the GET :index" do
login_as :writer

get :index
assert_response :forbidden
end

test "GDS Admin users should be able to access GET :confirm with a republishable document slug" do
get :confirm, params: { document_slug: "past-prime-ministers" }
assert_response :ok
end

test "GDS Admin users should see a 404 page when trying to republish a document with an unregistered document slug" do
get :confirm, params: { document_slug: "not-republishable" }
assert_response :not_found
end

test "Non-GDS Admin users should not be able to access GET :confirm" do
login_as :writer

get :confirm, params: { document_slug: "past-prime-ministers" }
assert_response :forbidden
end

test "GDS Admin users should be able to trigger the PresentPageToPublishingWorker job with the HistoricalAccountsIndexPresenter" do
PresentPageToPublishingApiWorker.expects(:perform_async).with("PublishingApi::HistoricalAccountsIndexPresenter").once

post :republish_past_prime_ministers_index

assert_redirected_to admin_republishing_index_path
assert_equal "'Past Prime Ministers' page has been scheduled for republishing", flash[:notice]
end

test "Non-GDS Admin users should not be able to republish the page" do
PresentPageToPublishingApiWorker.expects(:perform_async).with("PublishingApi::HistoricalAccountsIndexPresenter").never

login_as :writer

post :republish_past_prime_ministers_index
assert_response :forbidden
end
end

0 comments on commit 8157394

Please sign in to comment.