diff --git a/decidim-forms/lib/decidim/forms/user_answers_serializer.rb b/decidim-forms/lib/decidim/forms/user_answers_serializer.rb index a5d167514f42e..f569b59886094 100644 --- a/decidim-forms/lib/decidim/forms/user_answers_serializer.rb +++ b/decidim-forms/lib/decidim/forms/user_answers_serializer.rb @@ -42,7 +42,7 @@ def questions_hash questionnaire_id = @answers.first&.decidim_questionnaire_id return {} unless questionnaire_id - questions = Decidim::Forms::Question.where(decidim_questionnaire_id: questionnaire_id) + questions = Decidim::Forms::Question.where(decidim_questionnaire_id: questionnaire_id).order(:position) return {} if questions.none? questions.each.inject({}) do |serialized, question| diff --git a/decidim-forms/spec/serializers/decidim/forms/user_answers_serializer_spec.rb b/decidim-forms/spec/serializers/decidim/forms/user_answers_serializer_spec.rb index cc3b00849e219..ac7f03526841a 100644 --- a/decidim-forms/spec/serializers/decidim/forms/user_answers_serializer_spec.rb +++ b/decidim-forms/spec/serializers/decidim/forms/user_answers_serializer_spec.rb @@ -219,6 +219,13 @@ def memory_usage end end end + + describe "questions_hash" do + it "generates a hash of questions ordered by position" do + questions.shuffle! + expect(subject.instance_eval { questions_hash }.keys.map { |key| key[0].to_i }.uniq).to eq(questions.sort_by(&:position).map { |question| question.position + 1 }) + end + end end end end