-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9389 from alphagov/allow-overriding-government-at…
…tempt-3-part-2 Allow overriding government attempt 3 part 2
- Loading branch information
Showing
7 changed files
with
132 additions
and
40 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
app/components/admin/editions/history_mode_form_controls.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
app/components/admin/editions/history_mode_form_controls.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/components/admin/editions/history_mode_form_controls_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters