diff --git a/app/components/admin/settings/cookies_consent_tab_component.html.erb b/app/components/admin/settings/cookies_consent_tab_component.html.erb index 8c5cf80b2f8f..3c6a34768313 100644 --- a/app/components/admin/settings/cookies_consent_tab_component.html.erb +++ b/app/components/admin/settings/cookies_consent_tab_component.html.erb @@ -2,6 +2,7 @@ <%= render Admin::Settings::TableComponent.new(setting_name: "feature") do %> <%= render Admin::Settings::RowComponent.new("feature.cookies_consent", type: :feature, tab: tab) %> + <%= render Admin::Settings::RowComponent.new("cookies_consent.admin_test_mode", type: :feature, tab: tab) %> <%= render Admin::Settings::RowComponent.new("cookies_consent.more_info_link", type: :text, tab: tab) %> <%= render Admin::Settings::RowComponent.new("cookies_consent.version_name", type: :text, tab: tab) %> <% end %> diff --git a/app/components/layout/cookies_consent/banner_component.rb b/app/components/layout/cookies_consent/banner_component.rb index b9889c58c026..c88260c5d2b1 100644 --- a/app/components/layout/cookies_consent/banner_component.rb +++ b/app/components/layout/cookies_consent/banner_component.rb @@ -1,5 +1,6 @@ class Layout::CookiesConsent::BannerComponent < Layout::CookiesConsent::BaseComponent delegate :cookies, to: :helpers + delegate :current_user, to: :helpers def render? super && missing_cookies_setup? diff --git a/app/components/layout/cookies_consent/base_component.rb b/app/components/layout/cookies_consent/base_component.rb index f896eb27e73d..eeae2bb9016a 100644 --- a/app/components/layout/cookies_consent/base_component.rb +++ b/app/components/layout/cookies_consent/base_component.rb @@ -1,6 +1,8 @@ class Layout::CookiesConsent::BaseComponent < ApplicationComponent + delegate :current_user, to: :helpers + def render? - feature?(:cookies_consent) + feature?(:cookies_consent) && (public_setup? || testing_setup?) end private @@ -8,4 +10,12 @@ def render? def version_name Setting["cookies_consent.version_name"] end + + def public_setup? + !feature?("cookies_consent.admin_test_mode") + end + + def testing_setup? + current_user&.administrator? && !public_setup? + end end diff --git a/app/models/setting.rb b/app/models/setting.rb index dc48ce5251bd..64639a252e43 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -185,7 +185,8 @@ def defaults "sdg.process.legislation": true, "cookies_consent.more_info_link": "", "cookies_consent.setup_page": false, - "cookies_consent.version_name": "v1" + "cookies_consent.version_name": "v1", + "cookies_consent.admin_test_mode": false } end diff --git a/config/locales/en/settings.yml b/config/locales/en/settings.yml index 4e8e0b65ab59..5cb7fcff9df1 100644 --- a/config/locales/en/settings.yml +++ b/config/locales/en/settings.yml @@ -231,6 +231,8 @@ en: legislation: Related SDG in collaborative legislation legislation_description: Allow collaborative legislation to be linked to Sustainable Development Goals cookies_consent: + admin_test_mode: Enable testing mode + admin_test_mode_description: This feature will hide the cookie consent interface for all users except administrators when enabled. It allows you to test your settings before publishing. Turn off this feature once you have successfully tested it. more_info_link: Link to cookies policy more_info_link_description: Link shown in the consent banner to the cookie policy page. setup_page: Enable third party cookies setup page diff --git a/config/locales/es/settings.yml b/config/locales/es/settings.yml index 7723adaa0af8..41f1a77d43d2 100644 --- a/config/locales/es/settings.yml +++ b/config/locales/es/settings.yml @@ -231,6 +231,8 @@ es: legislation: Alineamiento ODS en legislación colaborativa legislation_description: Permitir alineamiento de los Objetivos de Desarrollo Sostenible en legislación colaborativa cookies_consent: + admin_test_mode: Activar el modo de pruebas + admin_test_mode_description: Esta función ocultará la interfaz de consentimiento de cookies para todos los usuarios excepto los administradores cuando esté activada. Le permite probar su configuración antes de publicarla. Desactive esta función una vez que la haya probado correctamente. more_info_link: Enlace a la política de cookies more_info_link_description: Enlace que se muestra en el banner de consentimiento a la página de política de cookies setup_page: Activar la página de configuración de consentimiento de cookies de terceros diff --git a/spec/components/layout/cookies_consent/banner_component_spec.rb b/spec/components/layout/cookies_consent/banner_component_spec.rb index c0fdfac08646..4bc74b8dcba2 100644 --- a/spec/components/layout/cookies_consent/banner_component_spec.rb +++ b/spec/components/layout/cookies_consent/banner_component_spec.rb @@ -62,4 +62,30 @@ expect(page).not_to have_button("Reject") end + + context "when admin testing mode is enabled" do + before { Setting["cookies_consent.admin_test_mode"] = true } + + it "shows the cookie banner to administrators" do + sign_in(create(:administrator).user) + + render_inline Layout::CookiesConsent::BannerComponent.new + + expect(page).to have_css(".cookies-consent-banner") + end + + it "does not show the cookie banner to common users" do + sign_in(create(:user)) + + render_inline Layout::CookiesConsent::BannerComponent.new + + expect(page).not_to have_css(".cookies-consent-banner") + end + + it "does not show the cookie banner to anonymous users" do + render_inline Layout::CookiesConsent::BannerComponent.new + + expect(page).not_to have_css(".cookies-consent-banner") + end + end end diff --git a/spec/components/layout/cookies_consent/setup_component_spec.rb b/spec/components/layout/cookies_consent/setup_component_spec.rb new file mode 100644 index 000000000000..95f21c6496ce --- /dev/null +++ b/spec/components/layout/cookies_consent/setup_component_spec.rb @@ -0,0 +1,48 @@ +require "rails_helper" + +describe Layout::CookiesConsent::SetupComponent do + before do + Setting["feature.cookies_consent"] = true + Setting["cookies_consent.setup_page"] = true + end + + it "is rendered when the cookies consent and the cookies setup features are enabled" do + render_inline Layout::CookiesConsent::SetupComponent.new + + expect(page).to have_css(".cookies-consent-setup") + end + + it "is not rendered when the cookies consent feature is disabled" do + Setting["feature.cookies_consent"] = false + + render_inline Layout::CookiesConsent::SetupComponent.new + + expect(page).not_to have_css(".cookies-consent-setup") + end + + it "is not rendered when the cookies setup feature is disabled" do + Setting["cookies_consent.setup_page"] = false + + render_inline Layout::CookiesConsent::SetupComponent.new + + expect(page).not_to have_css(".cookies-consent-setup") + end + + it "is rendered when the admin test mode is enabled and the current_user is an administrator" do + Setting["cookies_consent.admin_test_mode"] = true + sign_in(create(:administrator).user) + + render_inline Layout::CookiesConsent::SetupComponent.new + + expect(page).to have_css(".cookies-consent-setup") + end + + it "is not rendered when the admin testing mode is enabled and the current_user is not an administrator" do + Setting["cookies_consent.admin_test_mode"] = true + sign_in(create(:user)) + + render_inline Layout::CookiesConsent::SetupComponent.new + + expect(page).not_to have_css(".cookies-consent-setup") + end +end diff --git a/spec/system/cookies_consent_spec.rb b/spec/system/cookies_consent_spec.rb index 831c62871528..4b145141abe0 100644 --- a/spec/system/cookies_consent_spec.rb +++ b/spec/system/cookies_consent_spec.rb @@ -230,4 +230,28 @@ end end end + + describe "when admin test mode is enabled" do + before { Setting["cookies_consent.admin_test_mode"] = true } + + scenario "shows the cookie banner when an administrator user is logged in" do + login_as(create(:administrator).user) + + visit root_path + + expect(page).to have_css(".cookies-consent-banner") + end + + scenario "does not show the cookie banner for when the user is not an administrator" do + visit root_path + + expect(page).not_to have_css(".cookies-consent-banner") + + login_as(create(:user)) + + visit root_path + + expect(page).not_to have_css(".cookies-consent-banner") + end + end end