Skip to content

Commit 86832ca

Browse files
MuriloDalRiChrisBAshton
authored andcommitted
Fix draft locale deletion bug in Publishing API
When changing the primary locale of a draft (e.g. from English to French), the draft for the previous locale was not being discarded in Publishing API, leaving orphaned drafts. This commit: - Captures dirty attributes in `EditionService` before reload. - Passes the previous primary locale to `PublishingApiPusher`. - Upates `PublishingApiPusher` to discard the previous locale draft during `update_draft` event if the primary locale has changed. - Adds regression tests in `DraftLocaleSwitchTest`. Jira: https://gov-uk.atlassian.net/browse/WHIT-2842
1 parent 2332184 commit 86832ca

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

app/services/edition_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ def notify!
5151
end
5252

5353
def update_publishing_api!
54+
options_for_publishing_api_pusher = options
55+
if edition.saved_change_to_primary_locale?
56+
options_for_publishing_api_pusher[:primary_locale_before_last_save] = edition.primary_locale_before_last_save
57+
end
58+
5459
ServiceListeners::PublishingApiPusher
5560
.new(edition.reload)
56-
.push(event: verb, options:)
61+
.push(event: verb, options: options_for_publishing_api_pusher)
5762
end
5863

5964
def prepare_edition

app/services/service_listeners/publishing_api_pusher.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ def push(event:, options: {})
3737
end
3838

3939
handle_associated_documents(event)
40+
handle_locale_change(options[:primary_locale_before_last_save]) if options.key?(:primary_locale_before_last_save)
4041
end
4142

4243
private
4344

45+
def handle_locale_change(previous_locale)
46+
api.discard_translation_async(edition, locale: previous_locale)
47+
end
48+
4449
def handle_associated_documents(event)
4550
if edition.respond_to?(:associated_documents) || edition.respond_to?(:deleted_associated_documents)
4651
PublishingApiAssociatedDocuments.process(edition, event)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "test_helper"
2+
3+
class DraftLocaleSwitchTest < ActiveSupport::TestCase
4+
test "updates Publishing API when changing primary locale of a draft" do
5+
edition = create(:draft_document_collection, title: "English Title", primary_locale: "en")
6+
edition.primary_locale = "fr"
7+
edition.save!
8+
9+
Whitehall::PublishingApi.expects(:patch_links).at_least_once
10+
11+
Whitehall::PublishingApi.expects(:save_draft_translation).with(
12+
anything,
13+
:fr,
14+
nil,
15+
bulk_publishing: false,
16+
)
17+
18+
Whitehall::PublishingApi.expects(:discard_translation_async).with(
19+
anything,
20+
locale: "en",
21+
)
22+
23+
DraftEditionUpdater.new(edition, current_user: create(:user)).perform!
24+
end
25+
26+
test "does not discard translation when primary locale is unchanged" do
27+
edition = create(:draft_document_collection, title: "English Title", primary_locale: "en")
28+
edition.title = "New Title"
29+
edition.primary_locale = "en"
30+
edition.save!
31+
32+
Whitehall::PublishingApi.expects(:patch_links).at_least_once
33+
Whitehall::PublishingApi.expects(:save_draft_translation).with(
34+
anything,
35+
:en,
36+
nil,
37+
bulk_publishing: false,
38+
)
39+
40+
Whitehall::PublishingApi.expects(:discard_translation_async).never
41+
42+
DraftEditionUpdater.new(edition, current_user: create(:user)).perform!
43+
end
44+
end

0 commit comments

Comments
 (0)