Skip to content

Commit

Permalink
Merge pull request #2303 from alphagov/add-user-research-recruitment-…
Browse files Browse the repository at this point in the history
…banner-v3

Add user research recruitment banner
  • Loading branch information
floehopper authored Aug 10, 2023
2 parents 33911bd + 9bbdb60 commit 38cc80f
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 1 deletion.
22 changes: 22 additions & 0 deletions app/assets/stylesheets/_user_research_recruitment_banner.scss
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import "govuk_publishing_components/all_components";
@import "user_research_recruitment_banner";

// TODO: move into component
.gem-c-success-alert,
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions app/controllers/user_research_recruitment_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/views/layouts/admin_layout.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
navigation_items: navigation_items,
}%>

<%= yield(:user_research_recruitment_banner) %>

<div class="govuk-width-container">
<% if yield(:back_link).present? %>
<%= render "govuk_publishing_components/components/back_link", href: yield(:back_link) %>
Expand Down
25 changes: 25 additions & 0 deletions app/views/root/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
<% content_for :title, "Your applications" %>

<% if show_user_research_recruitment_banner? %>
<% content_for :user_research_recruitment_banner do %>
<section class="user-research-recruitment-banner">
<div class="govuk-width-container">
<hr class="user-research-recruitment-banner__divider govuk-section-break govuk-section-break--l govuk-!-margin-top-0">
<h1 class="user-research-recruitment-banner__title govuk-heading-xl">Help us improve GOV.UK Publishing</h1>
<p class="user-research-recruitment-banner__intro govuk-body">We're holding research sessions to make Publishing work better.</p>
<%= form_tag user_research_recruitment_update_path, method: :put do %>
<div class="user-research-recruitment-banner__buttons govuk-button-group">
<button class="govuk-!-font-size-24 govuk-!-font-weight-bold govuk-button govuk-button--start govuk-button--inverse" type="submit" name="choice" value="participate" formtarget="_blank">
<span>Find out more</span>
<svg class="govuk-button__start-icon govuk-!-display-none-print" xmlns="http://www.w3.org/2000/svg" width="17.5" height="19" viewBox="0 0 33 40" focusable="false" aria-hidden="true">
<path fill="currentColor" d="M0 0h13l20 20-20 20H0l20-20z"></path>
</svg>
</button>
<button class="govuk-button govuk-button--inverse" type="submit" name="choice" value="dismiss-banner">
Hide this
</button>
</div>
<% end %>
</div>
</section>
<% end %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@
get "/signin-required" => "root#signin_required"

root to: "root#index"

put "/user-research-recruitment/update" => "user_research_recruitment#update"
end
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
58 changes: 58 additions & 0 deletions test/controllers/user_research_recruitment_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
94 changes: 94 additions & 0 deletions test/integration/user_research_recruitment_banner_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 38cc80f

Please sign in to comment.