diff --git a/app/models/question_glisser_deposer.rb b/app/models/question_glisser_deposer.rb index 3840a6a6f..04c605d7a 100644 --- a/app/models/question_glisser_deposer.rb +++ b/app/models/question_glisser_deposer.rb @@ -24,20 +24,16 @@ def base_json end def audio_fields - fields = { 'audio_url' => audio_principal } - if transcription_intitule&.ecrit.blank? && transcription_intitule&.audio_url - fields['intitule_audio'] = transcription_intitule&.audio_url + fields = {} + if transcription_intitule&.complete? + fields['audio_url'] = transcription_intitule.audio_url + elsif transcription_modalite_reponse&.complete? + fields['audio_url'] = transcription_modalite_reponse&.audio_url + fields['intitule_url'] = transcription_intitule&.audio_url end fields end - def audio_principal - return unless transcription_intitule.complete? || transcription_modalite_reponse.complete? - return transcription_intitule.audio_url if transcription_intitule.complete? - - transcription_modalite_reponse.audio_url - end - def reponses_fields reponses_non_classees = reponses.to_a.shuffle.map do |reponse| illustration_url = cdn_for(reponse.illustration) if reponse.illustration.attached? diff --git a/spec/models/question_glisser_deposer_spec.rb b/spec/models/question_glisser_deposer_spec.rb index 1d4d8dfff..8021ed5a2 100644 --- a/spec/models/question_glisser_deposer_spec.rb +++ b/spec/models/question_glisser_deposer_spec.rb @@ -5,38 +5,76 @@ describe QuestionGlisserDeposer, type: :model do it { is_expected.to have_many(:reponses).with_foreign_key(:question_id) } - describe '#as_json' do - let(:question) do - create(:question_glisser_deposer, - illustration: Rack::Test::UploadedFile.new( - Rails.root.join('spec/support/programme_tele.png') - )) - end - let!(:modalite) do - create(:transcription, :avec_audio, question_id: question.id, - categorie: :modalite_reponse) - end - let!(:reponse1) { create(:choix, :avec_illustration, :bon, question_id: question.id) } - let!(:reponse2) { create(:choix, :avec_illustration, :bon, question_id: question.id) } - let(:json) { question.as_json } + let(:question) do + create(:question_glisser_deposer, + illustration: Rack::Test::UploadedFile.new( + Rails.root.join('spec/support/programme_tele.png') + )) + end + let!(:reponse1) { create(:choix, :avec_illustration, :bon, question_id: question.id) } + let!(:reponse2) { create(:choix, :avec_illustration, :bon, question_id: question.id) } + let!(:modalite) do + create(:transcription, :avec_audio, question_id: question.id, + categorie: :modalite_reponse) + end + let(:json) { question.as_json } + describe '#as_json' do it 'serialise les champs' do - intitule = create(:transcription, :avec_audio, question_id: question.id, - ecrit: 'Mon Intitulé') expect(json.keys).to match_array(%w[id intitule audio_url nom_technique description illustration modalite_reponse type - reponsesNonClassees]) + reponsesNonClassees intitule_url]) expect(json['type']).to eql('glisser-deposer-billets') - expect(json['intitule']).to eql('Mon Intitulé') expect(json['modalite_reponse']).to eql(modalite.ecrit) expect(json['illustration']).to eql(Rails.application.routes.url_helpers.url_for( question.illustration )) - expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for( - intitule.audio - )) expect(json['reponsesNonClassees'].size).to eql(2) expect(json['reponsesNonClassees'].first['illustration']).to_not be(nil) end end + + describe 'les audios' do + context 'quand il y a une modalité de réponse compléte' do + context 'avec un intitulé complet' do + let!(:intitule) do + create(:transcription, :avec_audio, question_id: question.id, + ecrit: 'Mon Intitulé') + end + it do + expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for( + intitule.audio + )) + expect(json['intitule']).to eql('Mon Intitulé') + expect(json['intitule_url']).to eql(nil) + end + end + + context 'sans intitulé' do + it do + expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for( + modalite.audio + )) + expect(json['intitule']).to eql(nil) + expect(json['intitule_url']).to eql(nil) + end + end + + context 'avec un intitulé incomplet' do + let!(:intitule) do + create(:transcription, :avec_audio, question_id: question.id, + ecrit: nil) + end + it do + expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for( + modalite.audio + )) + expect(json['intitule']).to eql(nil) + expect(json['intitule_url']).to eql(Rails.application.routes.url_helpers.url_for( + intitule.audio + )) + end + end + end + end end