Skip to content

Commit

Permalink
Merge pull request #9389 from alphagov/allow-overriding-government-at…
Browse files Browse the repository at this point in the history
…tempt-3-part-2

Allow overriding government attempt 3 part 2
  • Loading branch information
ryanb-gds committed Aug 30, 2024
2 parents 3c35cc6 + 22c5e18 commit 36a8837
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 40 deletions.
31 changes: 31 additions & 0 deletions app/components/admin/editions/history_mode_form_controls.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="govuk-!-margin-bottom-8">
<%= render "govuk_publishing_components/components/input", {
name: "edition[political]",
type: "hidden",
value: "",
} %>
<%= render "govuk_publishing_components/components/checkboxes", {
name: "edition[political]",
id: "edition_political",
heading: "Political",
heading_size: "l",
no_hint_text: true,
items: [
{
label: "Associate with government of the time (currently set to #{@edition.government&.name}).",
value: "1",
checked: @edition.political,
conditional: renders_government_selector? ? (render "govuk_publishing_components/components/select", {
name: "edition[government_id]",
label: "Or, associate this document with a different government",
id: "edition_government_id",
options:,
}) : "",
},
],
} %>

<p class="govuk-body"><%= link_to "Read the history mode guidance", "https://www.gov.uk/guidance/how-to-publish-on-gov-uk/creating-and-updating-pages#history-mode", class: "govuk-link" %>
for more information as to what this means.</p>
</div>
32 changes: 32 additions & 0 deletions app/components/admin/editions/history_mode_form_controls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Admin::Editions::HistoryModeFormControls < ViewComponent::Base
def initialize(edition, current_user)
@edition = edition
@enforcer = Whitehall::Authority::Enforcer.new(current_user, edition)
end

def render?
@edition.document&.live? && @edition.can_be_marked_political? && @enforcer.can?(:mark_political)
end

def renders_government_selector?
Flipflop.override_government? && @enforcer.can?(:select_government_for_history_mode)
end

# noinspection RubyMismatchedArgumentType
def options
[
{
text: "Associate with default government",
value: "",
},
].tap do |options|
Government.find_each do |government|
options << {
text: government.name,
value: government.id,
selected: government.id == @edition.government_id,
}
end
end
end
end
24 changes: 1 addition & 23 deletions app/views/admin/editions/_standard_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,7 @@
<%= render "additional_significant_fields", form: form, edition: form.object %>
<% if edition.document&.live? && edition.can_be_marked_political? && can?(:mark_political, edition) %>
<div class="govuk-!-margin-bottom-8">
<%= form.hidden_field :political, value: "0" %>
<%= render "govuk_publishing_components/components/checkboxes", {
name: "edition[political]",
id: "edition_political",
heading: "Political",
heading_size: "l",
no_hint_text: true,
error: errors_for_input(edition.errors, :political),
items: [
{
label: "Associate with government of the time (currently set to #{edition.government&.name}).",
value: "1",
checked: edition.political,
},
],
} %>

<p class="govuk-body"><%= link_to "Read the history mode guidance", "https://www.gov.uk/guidance/how-to-publish-on-gov-uk/creating-and-updating-pages#history-mode", class: "govuk-link" %> for more information as to what this means.</p>
</div>
<% end %>
<%= render Admin::Editions::HistoryModeFormControls.new(@edition, current_user) %>
</div>

<%= render Admin::Editions::FirstPublishedAtComponent.new(
Expand Down
1 change: 1 addition & 0 deletions config/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
# description: "Take over the world."
feature :govspeak_visual_editor, description: "Enables a visual editor for Govspeak fields", default: false
feature :content_object_store, description: "Enables the object store for sharable content", default: Rails.env.development?
feature :override_government, description: "Enables GDS Editors and Admins to override the government associated with a document", default: false
end
4 changes: 3 additions & 1 deletion lib/whitehall/authority/rules/edition_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.actions
review_editorial_remark
review_fact_check
see
select_government_for_history_mode
unpublish
unwithdraw
update
Expand Down Expand Up @@ -171,7 +172,7 @@ def departmental_editor_can?(action)
can_publish?
when :force_publish
can_force_publish?
when :unpublish, :mark_political, :perform_administrative_tasks
when :unpublish, :mark_political, :perform_administrative_tasks, :select_government_for_history_mode
false
else
true
Expand Down Expand Up @@ -200,6 +201,7 @@ def departmental_writer_can?(action)
force_publish
reject
mark_political
select_government_for_history_mode
perform_administrative_tasks
]

Expand Down
62 changes: 62 additions & 0 deletions test/components/admin/editions/history_mode_form_controls_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "test_helper"

class Admin::Editions::HistoryModeFormControlsTest < ViewComponent::TestCase
setup do
@test_strategy = Flipflop::FeatureSet.current.test!
end

test "hides the government selector for GDS editor users when the feature flag is disabled" do
@test_strategy.switch!(:override_government, false)
government = create(:current_government)
published_edition = create(:published_news_article, government_id: government.id)
new_draft = create(:news_article, document: published_edition.document, government_id: government.id)
render_inline(Admin::Editions::HistoryModeFormControls.new(new_draft, create(:gds_editor)))
assert_selector "select#edition_government_id", count: 0
end

test "conditionally displays the government selector for gds editor users" do
@test_strategy.switch!(:override_government, true)
previous_government = create(:previous_government)
governments = [create(:current_government), previous_government]
published_edition = create(:published_news_article, government_id: previous_government.id)
new_draft = create(:news_article, document: published_edition.document, government_id: previous_government.id)
render_inline(Admin::Editions::HistoryModeFormControls.new(new_draft, create(:gds_editor)))
assert_selector "#edition_political-0-conditional-0 select#edition_government_id"
assert_selector "option[value='']"
governments.each do |government|
assert_selector "option[value='#{government.id}']"
end
assert_selector "option[value='#{previous_government.id}'][selected='selected']"
end

test "displays the political checkbox for managing editor users " do
@test_strategy.switch!(:override_government, true)
published_edition = create(:published_news_article)
new_draft = create(:news_article, document: published_edition.document)
render_inline(Admin::Editions::HistoryModeFormControls.new(new_draft, create(:managing_editor)))
assert_selector "#edition_political"
end

test "doesn't display the government selector for managing editor users " do
@test_strategy.switch!(:override_government, true)
published_edition = create(:published_news_article)
new_draft = create(:news_article, document: published_edition.document)
render_inline(Admin::Editions::HistoryModeFormControls.new(new_draft, create(:managing_editor)))
assert_selector "select#edition_government_id", count: 0
end

test "doesn't display the political checkbox for writer users " do
@test_strategy.switch!(:override_government, true)
published_edition = create(:published_news_article)
new_draft = create(:news_article, document: published_edition.document)
render_inline(Admin::Editions::HistoryModeFormControls.new(new_draft, create(:writer)))
assert_selector "#edition_political", count: 0
end

test "doesn't display the political checkbox on creation" do
@test_strategy.switch!(:override_government, true)
new_draft = create(:news_article)
render_inline(Admin::Editions::HistoryModeFormControls.new(new_draft, create(:managing_editor)))
assert_selector "#edition_political", count: 0
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,12 @@ class Admin::GenericEditionsController::PolticalDocumentsTest < ActionController
assert_equal previous_government, new_draft.reload.government
end

view_test "displays the political checkbox for privileged users " do
create(:current_government)
view_test "displays the history mode form controls for privileged users " do
login_as :managing_editor
published_edition = create(:published_news_article, first_published_at: 2.days.ago)
new_draft = create(:news_article, document: published_edition.document, first_published_at: 2.days.ago)
get :edit, params: { id: new_draft }
assert_select "#edition_political"
end

view_test "doesn't display the political checkbox for non-privileged users " do
published_edition = create(:published_news_article)
new_draft = create(:news_article, document: published_edition.document)
get :edit, params: { id: new_draft }
assert_select "#edition_political", count: 0
end

view_test "doesn't display the political checkbox on creation" do
login_as :managing_editor
get :new
assert_select "#edition_political", count: 0
assert_select "#edition_political"
end

def edit_historic_document
Expand Down

0 comments on commit 36a8837

Please sign in to comment.