Merge pull request #7669 from betagouv/fix_hidden_required_in_conditionnel

fix(conditionnel): corrige les champs obligatoires mais cachés
This commit is contained in:
LeSim 2022-08-09 14:43:12 +02:00 committed by GitHub
commit 3eb440b9da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 49 deletions

View file

@ -34,7 +34,6 @@ class Champ < ApplicationRecord
delegate :libelle,
:type_champ,
:procedure,
:mandatory?,
:description,
:drop_down_list_options,
:drop_down_other?,
@ -92,8 +91,12 @@ class Champ < ApplicationRecord
@sections ||= dossier.sections_for(self)
end
def mandatory?
type_de_champ.mandatory? && visible?
end
def mandatory_blank_and_visible?
mandatory? && blank? && visible?
mandatory? && blank?
end
def blank?

View file

@ -39,7 +39,7 @@ class Champs::PieceJustificativeChamp < Champ
end
def mandatory_blank_and_visible?
mandatory? && !piece_justificative_file.attached? && visible?
mandatory? && !piece_justificative_file.attached?
end
def for_export

View file

@ -25,6 +25,6 @@ class Champs::SiretChamp < Champ
end
def mandatory_blank_and_visible?
mandatory? && Siret.new(siret: value).invalid? && visible?
mandatory? && Siret.new(siret: value).invalid?
end
end

View file

@ -33,7 +33,7 @@ class Champs::TitreIdentiteChamp < Champ
end
def mandatory_blank_and_visible?
mandatory? && !piece_justificative_file.attached? && visible?
mandatory? && !piece_justificative_file.attached?
end
def for_export

View file

@ -1,5 +1,5 @@
#{champ.libelle}
- if champ.mandatory?
- if champ.type_de_champ.mandatory?
%span.mandatory *
- if champ.updated_at.present? && seen_at.present?

View file

@ -7,7 +7,7 @@
.secondary{ class: champ.has_secondary_options_for_primary? ? '' : 'hidden' }
= form.label :secondary_value, for: "#{champ.input_id}-secondary" do
= champ.drop_down_secondary_libelle.presence || "Valeur secondaire dépendant de la première"
- if champ.mandatory?
- if champ.type_de_champ.mandatory?
%span.mandatory *
- if champ.drop_down_secondary_description.present?
.notice{ id: "#{champ.describedby_id}-secondary" }= string_to_html(champ.drop_down_secondary_description)

View file

@ -269,65 +269,104 @@ describe 'The user' do
context 'with condition' do
include Logic
let(:procedure) do
procedure = create(:procedure, :for_individual).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'age')
p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'permis de conduire')
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'tonnage')
p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'parking')
context 'with a required conditionnal champ' do
let(:procedure) do
procedure = create(:procedure, :published, :for_individual,
types_de_champ_public: [
{ type: :integer_number, libelle: 'age' },
{ type: :text, libelle: 'nom', mandatory: true }
])
age, nom = procedure.draft_revision.types_de_champ.all
nom.update(condition: greater_than_eq(champ_value(age.stable_id), constant(18)))
procedure
end
age, permis, tonnage, parking = procedure.draft_revision.types_de_champ.all
scenario 'submit a dossier with an hidden mandatory champ ', js: true do
log_in(user, procedure)
permis.update(condition: greater_than_eq(champ_value(age.stable_id), constant(18)))
tonnage.update(condition: ds_eq(champ_value(permis.stable_id), constant(true)))
parking.update(condition: less_than_eq(champ_value(tonnage.stable_id), constant(20)))
fill_individual
procedure.publish!
click_on 'Déposer le dossier'
expect(page).to have_current_path(merci_dossier_path(user_dossier))
end
procedure
scenario 'cannot submit a reveal dossier with a revealed mandatory champ ', js: true do
log_in(user, procedure)
fill_individual
fill_in('age', with: '18')
expect(page).to have_css('label', text: 'nom *', visible: :visible)
click_on 'Déposer le dossier'
expect(page).to have_current_path(brouillon_dossier_path(user_dossier))
end
end
scenario 'fill a dossier', js: true do
log_in(user, procedure)
context 'with a visibilite in cascade' do
let(:procedure) do
procedure = create(:procedure, :for_individual).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'age')
p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'permis de conduire')
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'tonnage')
p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'parking')
end
fill_individual
age, permis, tonnage, parking = procedure.draft_revision.types_de_champ.all
expect(page).to have_css('label', text: 'age', visible: true)
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
permis.update(condition: greater_than_eq(champ_value(age.stable_id), constant(18)))
tonnage.update(condition: ds_eq(champ_value(permis.stable_id), constant(true)))
parking.update(condition: less_than_eq(champ_value(tonnage.stable_id), constant(20)))
fill_in('age', with: '18')
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
procedure.publish!
choose('Oui')
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_css('label', text: 'tonnage', visible: true)
procedure
end
fill_in('tonnage', with: '1')
expect(page).to have_css('label', text: 'parking', visible: true)
scenario 'fill a dossier', js: true do
log_in(user, procedure)
# try to fill with invalid data
fill_in('tonnage', with: 'a')
expect(page).to have_no_css('label', text: 'parking', visible: true)
fill_individual
fill_in('age', with: '2')
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
expect(page).to have_css('label', text: 'age', visible: true)
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
click_on 'Déposer le dossier'
click_on 'Accéder à votre dossier'
click_on 'Modifier mon dossier'
fill_in('age', with: '18')
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
expect(page).to have_css('label', text: 'age', visible: true)
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
choose('Oui')
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_css('label', text: 'tonnage', visible: true)
fill_in('age', with: '18')
# the champ keeps their previous value so they are all displayed
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_css('label', text: 'tonnage', visible: true)
fill_in('tonnage', with: '1')
expect(page).to have_css('label', text: 'parking', visible: true)
# try to fill with invalid data
fill_in('tonnage', with: 'a')
expect(page).to have_no_css('label', text: 'parking', visible: true)
fill_in('age', with: '2')
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
click_on 'Déposer le dossier'
click_on 'Accéder à votre dossier'
click_on 'Modifier mon dossier'
expect(page).to have_css('label', text: 'age', visible: true)
expect(page).to have_no_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_no_css('label', text: 'tonnage', visible: true)
fill_in('age', with: '18')
# the champ keeps their previous value so they are all displayed
expect(page).to have_css('label', text: 'permis de conduire', visible: true)
expect(page).to have_css('label', text: 'tonnage', visible: true)
end
end
end