Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Corrige l'import XLS pour les questions #1873

Merged
merged 7 commits into from
Feb 13, 2025
22 changes: 15 additions & 7 deletions app/admin/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

controller do
def import_xls
return if params[:file_xls].blank?
return invalide_format_xls unless ImportExport::ImportXls.fichier_xls?(params[:file_xls])
return if fichier_import.blank?
return invalide_format_xls unless ImportExport::ImportXls.fichier_xls?(fichier_import)

initialise_import
flash[:success] = I18n.t('.layouts.succes.import_question')
Expand All @@ -29,14 +29,22 @@ def import_xls

private

def fichier_import
params[:question][:file_xls]
end

def type_question
params[:question][:type]
end

def initialise_import
ImportExport::Questions::ImportExportDonnees.new(type: params[:type])
.importe_donnees(params[:file_xls])
ImportExport::Questions::ImportExportDonnees.new(type: type_question)
.importe_donnees(fichier_import)
end

def erreur_import(error)
flash[:error] = error.message
redirect_to admin_import_xls_path(type: params[:type])
redirect_to admin_import_xls_path(type: type_question)
end

private
Expand All @@ -51,12 +59,12 @@ def redirection_apres_import
QuestionClicDansTexte::QUESTION_TYPE => admin_questions_clic_dans_texte_path
}

redirection_paths[params[:type]] || admin_questions_path
redirection_paths[type_question] || admin_questions_path
end

def invalide_format_xls
flash[:error] = I18n.t('.layouts.erreurs.import_question.format_invalide', format: 'XLS')
redirect_to admin_import_xls_path(type: params[:type])
redirect_to admin_import_xls_path(type: type_question)
end
end
end
3 changes: 2 additions & 1 deletion app/models/import_export/questions/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def remplis_champs_saisie
@onglet.set_valeur(@ligne, 8, @question.suffix_reponse)
@onglet.set_valeur(@ligne, 9, @question.reponse_placeholder)
@onglet.set_valeur(@ligne, 10, @question.type_saisie)
@onglet.set_valeur(@ligne, 11, @question.texte_a_trous)
@question.reponses.each_with_index { |reponse, index| ajoute_saisies(reponse, index) }
end

Expand All @@ -98,7 +99,7 @@ def ajoute_choix(choix, index)
def ajoute_saisies(reponse, index)
columns = %w[intitule nom_technique type_choix]
columns.each_with_index do |col, i|
colonne = 11 + (index * columns.size) + i
colonne = 12 + (index * columns.size) + i
etienneCharignon marked this conversation as resolved.
Show resolved Hide resolved
@onglet.set_valeur(0, colonne, "reponse_#{index + 1}_#{col}")
@onglet.set_valeur(@ligne, colonne, reponse.send(col))
end
Expand Down
22 changes: 22 additions & 0 deletions spec/features/admin/import_xls_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'Import XLS', type: :feature do
let!(:compte) { create :compte_superadmin }

before { connecte(compte) }

it 'importe un fichier XLS de type QuestionGlisserDeposer' do
visit admin_import_xls_path(type: 'QuestionGlisserDeposer')

stub_request(:get, %r{^https://stockagepreprod\.eva\.beta\.gouv\.fr(/.*)?$})
.to_return(status: 200, body: '', headers: {})

chemin_fichier = Rails.root.join('spec/support/import_question_glisser_deposer.xls').to_s
attach_file('question[file_xls]', chemin_fichier)
click_button 'Importer le fichier'

expect(page).to have_http_status(200)
end
end
72 changes: 36 additions & 36 deletions spec/models/import_export/questions/export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
let(:spreadsheet) { response_service.export.workbook }
let(:worksheet) { spreadsheet.worksheet(0) }

let(:headers_xls) { worksheet.row(0).map { |header| header.parameterize.underscore.to_sym } }

describe 'pour une question clic' do
let(:question) do
create(:question_clic_dans_image, description: 'Ceci est une description',
Expand All @@ -34,16 +36,11 @@

it 'gĂ©nĂ©re un fichier xls avec les entĂȘtes sur chaque colonnes' do
expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[0]).to eq('Libelle')
expect(worksheet.row(0)[1]).to eq('Nom technique')
expect(worksheet.row(0)[2]).to eq('Illustration')
expect(worksheet.row(0)[3]).to eq('Intitule ecrit')
expect(worksheet.row(0)[4]).to eq('Intitule audio')
expect(worksheet.row(0)[5]).to eq('Consigne ecrit')
expect(worksheet.row(0)[6]).to eq('Consigne audio')
expect(worksheet.row(0)[7]).to eq('Description')
expect(worksheet.row(0)[8]).to eq('Zone cliquable')
expect(worksheet.row(0)[9]).to eq('Image au clic')

headers_attendus = ImportExport::Questions::ImportExportDonnees::HEADERS_ATTENDUS[
QuestionClicDansImage::QUESTION_TYPE
]
expect(headers_xls).to eq headers_attendus
end

it 'génére un fichier xls avec les détails de la question' do
Expand Down Expand Up @@ -71,15 +68,16 @@

it 'gĂ©nĂ©re un fichier xls avec les entĂȘtes spĂ©cifiques' do
expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[8]).to eq('Zone depot')
expect(worksheet.row(0)[9]).to eq('reponse_1_nom_technique')
expect(worksheet.row(0)[10]).to eq('reponse_1_position_client')
expect(worksheet.row(0)[11]).to eq('reponse_1_type_choix')
expect(worksheet.row(0)[12]).to eq('reponse_1_illustration_url')
expect(worksheet.row(0)[13]).to eq('reponse_2_nom_technique')
expect(worksheet.row(0)[14]).to eq('reponse_2_position_client')
expect(worksheet.row(0)[15]).to eq('reponse_2_type_choix')
expect(worksheet.row(0)[16]).to eq('reponse_2_illustration_url')

headers_attendus = ImportExport::Questions::ImportExportDonnees::HEADERS_ATTENDUS[
QuestionGlisserDeposer::QUESTION_TYPE
]
headers_attendus += %i[reponse_1_nom_technique reponse_1_position_client reponse_1_type_choix
reponse_1_illustration_url reponse_2_nom_technique
reponse_2_position_client reponse_2_type_choix
reponse_2_illustration_url]

expect(headers_xls).to eq headers_attendus
end

it 'génére un fichier xls avec les détails de la question' do
Expand All @@ -106,21 +104,23 @@

it 'gĂ©nĂ©re un fichier xls avec les entĂȘtes spĂ©cifiques' do
expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[8]).to eq('Suffix reponse')
expect(worksheet.row(0)[9]).to eq('Reponse placeholder')
expect(worksheet.row(0)[10]).to eq('Type saisie')
expect(worksheet.row(0)[11]).to eq('reponse_1_intitule')
expect(worksheet.row(0)[12]).to eq('reponse_1_nom_technique')
expect(worksheet.row(0)[13]).to eq('reponse_1_type_choix')

headers_attendus = ImportExport::Questions::ImportExportDonnees::HEADERS_ATTENDUS[
QuestionSaisie::QUESTION_TYPE
]
headers_attendus += %i[reponse_1_intitule reponse_1_nom_technique reponse_1_type_choix]

expect(headers_xls).to eq headers_attendus
end

it 'génére un fichier xls avec les détails de la question' do
ligne = worksheet.row(1)
expect(ligne[8]).to eq(question.suffix_reponse)
expect(ligne[9]).to eq(question.reponse_placeholder)
expect(ligne[10]).to eq(question.type_saisie)
expect(ligne[11]).to eq(question.reponses.last.intitule)
expect(ligne[12]).to eq(question.reponses.last.nom_technique)
expect(ligne[11]).to eq(question.texte_a_trous)
expect(ligne[12]).to eq(question.reponses.last.intitule)
expect(ligne[13]).to eq(question.reponses.last.nom_technique)
end
end

Expand All @@ -134,15 +134,15 @@

it 'gĂ©nĂ©re un fichier xls avec les entĂȘtes spĂ©cifiques' do
expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[8]).to eq('Type qcm')
expect(worksheet.row(0)[9]).to eq('choix_1_intitule')
expect(worksheet.row(0)[10]).to eq('choix_1_nom_technique')
expect(worksheet.row(0)[11]).to eq('choix_1_type_choix')
expect(worksheet.row(0)[12]).to eq('choix_1_audio_url')
expect(worksheet.row(0)[13]).to eq('choix_2_intitule')
expect(worksheet.row(0)[14]).to eq('choix_2_nom_technique')
expect(worksheet.row(0)[15]).to eq('choix_2_type_choix')
expect(worksheet.row(0)[16]).to eq('choix_2_audio_url')

headers_attendus = ImportExport::Questions::ImportExportDonnees::HEADERS_ATTENDUS[
QuestionQcm::QUESTION_TYPE
]
headers_attendus += %i[choix_1_intitule choix_1_nom_technique choix_1_type_choix
choix_1_audio_url choix_2_intitule choix_2_nom_technique
choix_2_type_choix choix_2_audio_url]

expect(headers_xls).to eq headers_attendus
end

it 'génére un fichier xls avec les détails de la question' do
Expand Down
Binary file not shown.