diff --git a/app/assets/stylesheets/_user_research_recruitment_banner.scss b/app/assets/stylesheets/_user_research_recruitment_banner.scss new file mode 100644 index 000000000..e38d13f74 --- /dev/null +++ b/app/assets/stylesheets/_user_research_recruitment_banner.scss @@ -0,0 +1,22 @@ +.user-research-recruitment-banner { + background-color: $govuk-brand-colour; + @include govuk-responsive-padding(8, "top"); +} + +.user-research-recruitment-banner__divider { + border-bottom: 1px solid govuk-colour("white"); +} + +.user-research-recruitment-banner__title { + color: govuk-colour("white"); + @include govuk-responsive-margin(5, "bottom"); +} + +.user-research-recruitment-banner__intro { + color: govuk-colour("white"); +} + +.user-research-recruitment-banner__buttons { + @include govuk-responsive-padding(6, "bottom"); + align-items: center; +} diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 0c2e82fdb..5f596438c 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -1,4 +1,5 @@ @import "govuk_publishing_components/all_components"; +@import "user_research_recruitment_banner"; // TODO: move into component .gem-c-success-alert, diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index 70ea029f4..297c303f8 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -14,4 +14,11 @@ def index def signin_required @application = ::Doorkeeper::Application.find_by(id: session.delete(:signin_missing_for_application)) end + +private + + def show_user_research_recruitment_banner? + !cookies[:dismiss_user_research_recruitment_banner] && !current_user.user_research_recruitment_banner_hidden? + end + helper_method :show_user_research_recruitment_banner? end diff --git a/app/controllers/user_research_recruitment_controller.rb b/app/controllers/user_research_recruitment_controller.rb new file mode 100644 index 000000000..a34ac90e2 --- /dev/null +++ b/app/controllers/user_research_recruitment_controller.rb @@ -0,0 +1,17 @@ +class UserResearchRecruitmentController < ApplicationController + USER_RESEARCH_RECRUITMENT_FORM_URL = "https://docs.google.com/forms/d/1Bdu_GqOrSR4j6mbuzXkFTQg6FRktRMQc8Y-q879Mny8/viewform".freeze + + before_action :authenticate_user! + skip_after_action :verify_authorized + + def update + case params[:choice] + when "participate" + current_user.update!(user_research_recruitment_banner_hidden: true) + redirect_to USER_RESEARCH_RECRUITMENT_FORM_URL, allow_other_host: true + when "dismiss-banner" + cookies[:dismiss_user_research_recruitment_banner] = true + redirect_to root_path + end + end +end diff --git a/app/views/layouts/admin_layout.html.erb b/app/views/layouts/admin_layout.html.erb index 5f68cd388..e6ff12650 100644 --- a/app/views/layouts/admin_layout.html.erb +++ b/app/views/layouts/admin_layout.html.erb @@ -11,6 +11,8 @@ navigation_items: navigation_items, }%> + <%= yield(:user_research_recruitment_banner) %> +
<% if yield(:back_link).present? %> <%= render "govuk_publishing_components/components/back_link", href: yield(:back_link) %> diff --git a/app/views/root/index.html.erb b/app/views/root/index.html.erb index 5af694e18..4ef59d895 100644 --- a/app/views/root/index.html.erb +++ b/app/views/root/index.html.erb @@ -1,5 +1,30 @@ <% content_for :title, "Your applications" %> +<% if show_user_research_recruitment_banner? %> + <% content_for :user_research_recruitment_banner do %> +
+
+
+

Help us improve GOV.UK Publishing

+

We're holding research sessions to make Publishing work better.

+ <%= form_tag user_research_recruitment_update_path, method: :put do %> +
+ + +
+ <% end %> +
+
+ <% end %> +<% end %> +
diff --git a/config/routes.rb b/config/routes.rb index 7103862cb..97a0b5e86 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -77,4 +77,6 @@ get "/signin-required" => "root#signin_required" root to: "root#index" + + put "/user-research-recruitment/update" => "user_research_recruitment#update" end diff --git a/db/migrate/20230804094159_add_user_research_recruitment_banner_hidden_to_users.rb b/db/migrate/20230804094159_add_user_research_recruitment_banner_hidden_to_users.rb new file mode 100644 index 000000000..fa15027a5 --- /dev/null +++ b/db/migrate/20230804094159_add_user_research_recruitment_banner_hidden_to_users.rb @@ -0,0 +1,5 @@ +class AddUserResearchRecruitmentBannerHiddenToUsers < ActiveRecord::Migration[7.0] + def change + add_column :users, :user_research_recruitment_banner_hidden, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index fd8029a12..23ce30460 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_08_02_095323) do +ActiveRecord::Schema[7.0].define(version: 2023_08_04_094159) do create_table "batch_invitation_application_permissions", id: :integer, charset: "utf8mb3", force: :cascade do |t| t.integer "batch_invitation_id", null: false t.integer "supported_permission_id", null: false @@ -209,6 +209,7 @@ t.boolean "require_2sv", default: false, null: false t.string "reason_for_2sv_exemption" t.date "expiry_date_for_2sv_exemption" + t.boolean "user_research_recruitment_banner_hidden", default: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["invitation_token"], name: "index_users_on_invitation_token" t.index ["invited_by_id"], name: "index_users_on_invited_by_id" diff --git a/test/controllers/user_research_recruitment_controller_test.rb b/test/controllers/user_research_recruitment_controller_test.rb new file mode 100644 index 000000000..91d7e7a3b --- /dev/null +++ b/test/controllers/user_research_recruitment_controller_test.rb @@ -0,0 +1,58 @@ +require "test_helper" + +class UserResearchRecruitmentControllerTest < ActionController::TestCase + attr_reader :choice + + context "#update" do + should "require users to be signed in" do + put :update + + assert_redirected_to new_user_session_path + end + + context "when user clicks the button to dismiss the banner" do + setup do + @choice = "dismiss-banner" + end + + should "set session cookie" do + sign_in create(:user) + + put :update, params: { choice: } + + assert cookies[:dismiss_user_research_recruitment_banner] + end + + should "redirect to root path" do + sign_in create(:user) + + put :update, params: { choice: } + + assert_redirected_to root_path + end + end + + context "when user clicks the button to participate in user research" do + setup do + @choice = "participate" + end + + should "set user_research_recruitment_banner_hidden to true for the current_user" do + user = create(:user) + sign_in user + + put :update, params: { choice: } + + assert user.user_research_recruitment_banner_hidden? + end + + should "redirect to the Google Form" do + sign_in create(:user) + + put :update, params: { choice: } + + assert_redirected_to UserResearchRecruitmentController::USER_RESEARCH_RECRUITMENT_FORM_URL + end + end + end +end diff --git a/test/integration/user_research_recruitment_banner_test.rb b/test/integration/user_research_recruitment_banner_test.rb new file mode 100644 index 000000000..f8559c626 --- /dev/null +++ b/test/integration/user_research_recruitment_banner_test.rb @@ -0,0 +1,94 @@ +require "test_helper" + +class UserResearchRecruitmentBannerTest < ActionDispatch::IntegrationTest + should "not display the banner on the login page" do + visit new_user_session_path + + assert_not has_content?(user_research_recruitment_banner_title) + end + + should "display the banner on the dashboard" do + user = create(:user, name: "user-name", email: "user@example.com") + visit new_user_session_path + signin_with(user) + + assert has_content?(user_research_recruitment_banner_title) + assert has_css?("form[action='#{user_research_recruitment_update_path}']", text: "Find out more") + end + + should "not display the banner on any page other than the dashboard" do + user = create(:user, name: "user-name", email: "user@example.com") + visit new_user_session_path + signin_with(user) + + click_on "Change your email or password" + + assert_not has_content?(user_research_recruitment_banner_title) + end + + should "hide the banner until the next session" do + user = create(:user, name: "user-name", email: "user@example.com") + + using_session("Session 1") do + visit new_user_session_path + signin_with(user) + + assert has_content?(user_research_recruitment_banner_title) + + within ".user-research-recruitment-banner" do + click_on "Hide this" + end + + visit root_path + + assert_not has_content?(user_research_recruitment_banner_title) + end + + using_session("Session 2") do + visit new_user_session_path + signin_with(user) + + assert has_content?(user_research_recruitment_banner_title) + end + end + + should "hide the banner permanently if the user clicks the button to participate in user research" do + user = create(:user, name: "user-name", email: "user@example.com") + + using_session("Session 1") do + visit new_user_session_path + signin_with(user) + + assert has_content?(user_research_recruitment_banner_title) + + within ".user-research-recruitment-banner" do + allowing_request_to_user_research_recruitment_google_form do + click_on "Find out more" + end + end + + visit root_path + + assert_not has_content?(user_research_recruitment_banner_title) + end + + using_session("Session 2") do + visit new_user_session_path + signin_with(user) + + assert_not has_content?(user_research_recruitment_banner_title) + end + end + +private + + def user_research_recruitment_banner_title + "Help us improve GOV.UK Publishing" + end + + def allowing_request_to_user_research_recruitment_google_form + yield + rescue ActionController::RoutingError + raise unless current_url == UserResearchRecruitmentController::USER_RESEARCH_RECRUITMENT_FORM_URL + end +end