diff --git a/app/controllers/admin/people_controller.rb b/app/controllers/admin/people_controller.rb index 5678d7fbe6d..2907662432b 100644 --- a/app/controllers/admin/people_controller.rb +++ b/app/controllers/admin/people_controller.rb @@ -26,6 +26,11 @@ def edit; end def update if @person.update(person_params) + if @person.current_or_previous_prime_minister? + @person.historical_account.republish_to_publishing_api_async if @person.historical_account.present? + PresentPageToPublishingApiWorker.perform_async("PublishingApi::HistoricalAccountsIndexPresenter") + end + redirect_to [:admin, @person], notice: %("#{@person.name}" saved.) else render :edit diff --git a/test/functional/admin/people_controller_test.rb b/test/functional/admin/people_controller_test.rb index c27f70d6763..8898937c6b9 100644 --- a/test/functional/admin/people_controller_test.rb +++ b/test/functional/admin/people_controller_test.rb @@ -150,6 +150,51 @@ class Admin::PeopleControllerTest < ActionController::TestCase assert_equal "960x640_jpeg.jpg", person.reload.image.filename end + test "PUT :update does not republish the past prime ministers page if the person has not been the prime minister" do + person = create(:person) + + PresentPageToPublishingApi + .expects(:new) + .never + + Sidekiq::Testing.inline! do + put :update, params: { + id: person.id, + person: attributes_for(:person), + } + end + end + + test "PUT :update republishes the past prime ministers page & the persons historical account if the person is or has been the prime minister" do + login_as :gds_admin + historical_account = build(:historical_account) + person = create(:pm, historical_account:) + + service = mock + + PresentPageToPublishingApi + .expects(:new) + .once + .returns(service) + + service + .expects(:publish) + .once + .with(PublishingApi::HistoricalAccountsIndexPresenter) + + HistoricalAccount + .any_instance + .expects(:republish_to_publishing_api_async) + .once + + Sidekiq::Testing.inline! do + put :update, params: { + id: person.id, + person: attributes_for(:person), + } + end + end + test "should be able to destroy a destroyable person" do person = create(:person, forename: "Dave") delete :destroy, params: { id: person.id }