decidim-forms 0.20.0 → 0.23.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/config/admin/decidim_forms_manifest.css +3 -0
  3. data/app/assets/config/admin/decidim_forms_manifest.js +1 -0
  4. data/app/assets/config/decidim_forms_manifest.css +1 -0
  5. data/app/assets/images/decidim/surveys/icon.svg +1 -19
  6. data/app/assets/javascripts/decidim/forms/admin/auto_select_options_from_url.component.js.es6 +40 -0
  7. data/app/assets/javascripts/decidim/forms/admin/collapsible_questions.js.es6 +13 -0
  8. data/app/assets/javascripts/decidim/forms/admin/forms.js.es6 +260 -16
  9. data/app/assets/javascripts/decidim/forms/admin/live_text_update.component.js.es6 +52 -0
  10. data/app/assets/javascripts/decidim/forms/autosortable_checkboxes.component.js.es6 +54 -34
  11. data/app/assets/javascripts/decidim/forms/display_conditions.component.js.es6 +204 -0
  12. data/app/assets/javascripts/decidim/forms/forms.js.es6 +49 -1
  13. data/app/assets/javascripts/decidim/forms/max_choices_alert.component.js.es6 +44 -0
  14. data/app/assets/stylesheets/decidim/forms/forms.scss +39 -0
  15. data/app/assets/stylesheets/decidim/forms/questionnaire-answers-pdf.scss +69 -0
  16. data/app/cells/decidim/forms/matrix_readonly/show.erb +5 -0
  17. data/app/cells/decidim/forms/matrix_readonly_cell.rb +12 -0
  18. data/app/cells/decidim/forms/question_readonly/show.erb +5 -1
  19. data/app/cells/decidim/forms/question_readonly_cell.rb +5 -0
  20. data/app/cells/decidim/forms/step_navigation/show.erb +35 -0
  21. data/app/cells/decidim/forms/step_navigation_cell.rb +46 -0
  22. data/app/commands/decidim/forms/admin/update_questionnaire.rb +33 -1
  23. data/app/commands/decidim/forms/answer_questionnaire.rb +2 -1
  24. data/app/controllers/decidim/forms/admin/concerns/has_questionnaire.rb +52 -2
  25. data/app/controllers/decidim/forms/admin/concerns/has_questionnaire_answers.rb +97 -0
  26. data/app/controllers/decidim/forms/concerns/has_questionnaire.rb +11 -2
  27. data/app/forms/decidim/forms/admin/display_condition_form.rb +100 -0
  28. data/app/forms/decidim/forms/admin/question_form.rb +21 -1
  29. data/app/forms/decidim/forms/admin/question_matrix_row_form.rb +26 -0
  30. data/app/forms/decidim/forms/answer_choice_form.rb +1 -0
  31. data/app/forms/decidim/forms/answer_form.rb +31 -2
  32. data/app/forms/decidim/forms/questionnaire_form.rb +25 -3
  33. data/app/helpers/decidim/forms/admin/application_helper.rb +37 -0
  34. data/app/helpers/decidim/forms/admin/concerns/has_questionnaire_answers_pagination_helper.rb +49 -0
  35. data/app/helpers/decidim/forms/admin/concerns/has_questionnaire_answers_url_helper.rb +40 -0
  36. data/app/helpers/decidim/forms/admin/questionnaire_answers_helper.rb +27 -0
  37. data/app/jobs/decidim/forms/export_questionnaire_answers_job.rb +19 -0
  38. data/app/models/decidim/forms/answer.rb +0 -3
  39. data/app/models/decidim/forms/answer_choice.rb +7 -0
  40. data/app/models/decidim/forms/answer_option.rb +14 -0
  41. data/app/models/decidim/forms/display_condition.rb +51 -0
  42. data/app/models/decidim/forms/question.rb +52 -2
  43. data/app/models/decidim/forms/question_matrix_row.rb +15 -0
  44. data/app/models/decidim/forms/questionnaire.rb +11 -1
  45. data/app/presenters/decidim/forms/admin/questionnaire_answer_presenter.rb +43 -0
  46. data/app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb +60 -0
  47. data/app/presenters/decidim/forms/answer_option_presenter.rb +20 -0
  48. data/app/presenters/decidim/forms/question_presenter.rb +16 -0
  49. data/app/queries/decidim/forms/questionnaire_participant.rb +35 -0
  50. data/app/queries/decidim/forms/questionnaire_participants.rb +43 -0
  51. data/app/types/decidim/forms/answer_option_type.rb +14 -0
  52. data/app/types/decidim/forms/question_type.rb +23 -0
  53. data/app/types/decidim/forms/questionnaire_type.rb +22 -0
  54. data/app/views/decidim/forms/admin/questionnaires/_answer_option_template.html.erb +1 -1
  55. data/app/views/decidim/forms/admin/questionnaires/_display_condition.html.erb +88 -0
  56. data/app/views/decidim/forms/admin/questionnaires/_display_condition_template.html.erb +7 -0
  57. data/app/views/decidim/forms/admin/questionnaires/_form.html.erb +67 -8
  58. data/app/views/decidim/forms/admin/questionnaires/_matrix_row.html.erb +34 -0
  59. data/app/views/decidim/forms/admin/questionnaires/_matrix_row_template.html.erb +7 -0
  60. data/app/views/decidim/forms/admin/questionnaires/_question.html.erb +45 -6
  61. data/app/views/decidim/forms/admin/questionnaires/_separator.html.erb +41 -0
  62. data/app/views/decidim/forms/admin/questionnaires/answers/export/_answer.html.erb +31 -0
  63. data/app/views/decidim/forms/admin/questionnaires/answers/export/pdf.html.erb +13 -0
  64. data/app/views/decidim/forms/admin/questionnaires/answers/index.html.erb +53 -0
  65. data/app/views/decidim/forms/admin/questionnaires/answers/show.html.erb +48 -0
  66. data/app/views/decidim/forms/admin/questionnaires/edit.html.erb +9 -5
  67. data/app/views/decidim/forms/questionnaires/_answer.html.erb +28 -99
  68. data/app/views/decidim/forms/questionnaires/answers/_long_answer.html.erb +1 -0
  69. data/app/views/decidim/forms/questionnaires/answers/_matrix_multiple.html.erb +43 -0
  70. data/app/views/decidim/forms/questionnaires/answers/_matrix_single.html.erb +43 -0
  71. data/app/views/decidim/forms/questionnaires/answers/_multiple_option.html.erb +23 -0
  72. data/app/views/decidim/forms/questionnaires/answers/_separator.html.erb +1 -0
  73. data/app/views/decidim/forms/questionnaires/answers/_short_answer.html.erb +1 -0
  74. data/app/views/decidim/forms/questionnaires/answers/_single_option.html.erb +30 -0
  75. data/app/views/decidim/forms/questionnaires/answers/_sorting.html.erb +23 -0
  76. data/app/views/decidim/forms/questionnaires/show.html.erb +78 -26
  77. data/app/views/layouts/decidim/forms/admin/questionnaires/questionnaire_answers.html.erb +12 -0
  78. data/config/initializers/wicked_pdf.rb +25 -0
  79. data/config/locales/am-ET.yml +1 -0
  80. data/config/locales/ar.yml +9 -4
  81. data/config/locales/bg-BG.yml +14 -0
  82. data/config/locales/bg.yml +14 -0
  83. data/config/locales/ca.yml +98 -8
  84. data/config/locales/cs.yml +89 -5
  85. data/config/locales/da-DK.yml +1 -0
  86. data/config/locales/da.yml +1 -0
  87. data/config/locales/de.yml +92 -4
  88. data/config/locales/el-GR.yml +1 -0
  89. data/config/locales/el.yml +118 -0
  90. data/config/locales/en.yml +89 -5
  91. data/config/locales/eo.yml +1 -0
  92. data/config/locales/es-MX.yml +94 -4
  93. data/config/locales/es-PY.yml +94 -4
  94. data/config/locales/es.yml +94 -4
  95. data/config/locales/et-EE.yml +1 -0
  96. data/config/locales/et.yml +1 -0
  97. data/config/locales/eu.yml +7 -4
  98. data/config/locales/fi-plain.yml +88 -4
  99. data/config/locales/fi.yml +88 -4
  100. data/config/locales/fr-CA.yml +172 -0
  101. data/config/locales/fr.yml +89 -5
  102. data/config/locales/ga-IE.yml +1 -0
  103. data/config/locales/gl.yml +7 -4
  104. data/config/locales/hr-HR.yml +1 -0
  105. data/config/locales/hr.yml +1 -0
  106. data/config/locales/hu.yml +11 -5
  107. data/config/locales/id-ID.yml +7 -4
  108. data/config/locales/is-IS.yml +1 -0
  109. data/config/locales/is.yml +1 -0
  110. data/config/locales/it.yml +89 -5
  111. data/config/locales/ja-JP.yml +170 -0
  112. data/config/locales/ja.yml +170 -0
  113. data/config/locales/ko-KR.yml +1 -0
  114. data/config/locales/ko.yml +1 -0
  115. data/config/locales/lt-LT.yml +1 -0
  116. data/config/locales/lt.yml +1 -0
  117. data/config/locales/lv.yml +118 -0
  118. data/config/locales/mt-MT.yml +1 -0
  119. data/config/locales/mt.yml +1 -0
  120. data/config/locales/nl.yml +92 -8
  121. data/config/locales/no.yml +98 -1
  122. data/config/locales/om-ET.yml +1 -0
  123. data/config/locales/pl.yml +113 -26
  124. data/config/locales/pt-BR.yml +8 -5
  125. data/config/locales/pt.yml +111 -24
  126. data/config/locales/ro-RO.yml +167 -0
  127. data/config/locales/ru.yml +4 -2
  128. data/config/locales/sk-SK.yml +88 -0
  129. data/config/locales/sk.yml +90 -0
  130. data/config/locales/sl.yml +12 -0
  131. data/config/locales/so-SO.yml +1 -0
  132. data/config/locales/sr-CS.yml +1 -0
  133. data/config/locales/sv.yml +88 -7
  134. data/config/locales/ti-ER.yml +1 -0
  135. data/config/locales/tr-TR.yml +7 -4
  136. data/config/locales/vi-VN.yml +1 -0
  137. data/config/locales/vi.yml +1 -0
  138. data/config/locales/zh-CN.yml +172 -0
  139. data/config/locales/zh-TW.yml +1 -0
  140. data/db/migrate/20200130194123_create_decidim_forms_display_conditions.rb +20 -0
  141. data/db/migrate/20200225123810_create_decidim_forms_question_matrix_rows.rb +11 -0
  142. data/db/migrate/20200304152939_add_matrix_row_id_to_decidim_forms_answer_choices.rb +11 -0
  143. data/lib/decidim/api/questionnaire_entity_interface.rb +18 -0
  144. data/lib/decidim/exporters/form_pdf.rb +33 -0
  145. data/lib/decidim/exporters/form_pdf_controller_helper.rb +11 -0
  146. data/lib/decidim/forms.rb +6 -0
  147. data/lib/decidim/forms/admin_engine.rb +1 -1
  148. data/lib/decidim/forms/api.rb +7 -0
  149. data/lib/decidim/forms/test.rb +6 -0
  150. data/lib/decidim/forms/test/factories.rb +60 -5
  151. data/lib/decidim/forms/test/shared_examples/has_questionnaire.rb +918 -60
  152. data/lib/decidim/forms/test/shared_examples/manage_questionnaire_answers.rb +108 -0
  153. data/lib/decidim/forms/test/shared_examples/manage_questionnaires.rb +33 -575
  154. data/lib/decidim/forms/test/shared_examples/manage_questionnaires/add_display_conditions.rb +179 -0
  155. data/lib/decidim/forms/test/shared_examples/manage_questionnaires/add_questions.rb +463 -0
  156. data/lib/decidim/forms/test/shared_examples/manage_questionnaires/update_display_conditions.rb +93 -0
  157. data/lib/decidim/forms/test/shared_examples/manage_questionnaires/update_questions.rb +461 -0
  158. data/lib/decidim/forms/user_answers_serializer.rb +21 -4
  159. data/lib/decidim/forms/version.rb +1 -1
  160. metadata +136 -11
@@ -0,0 +1 @@
1
+ zh-TW:
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateDecidimFormsDisplayConditions < ActiveRecord::Migration[5.1]
4
+ def up
5
+ create_table :decidim_forms_display_conditions do |t|
6
+ t.bigint :decidim_question_id, index: { name: "decidim_forms_display_condition_question" }, null: false
7
+ t.bigint :decidim_condition_question_id, index: { name: "decidim_forms_display_condition_condition_question" }, null: false
8
+ t.bigint :decidim_answer_option_id, index: { name: "decidim_forms_display_condition_answer_option" }
9
+ t.integer :condition_type, default: 0, null: false
10
+ t.jsonb :condition_value
11
+ t.boolean :mandatory, default: false
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+
17
+ def down
18
+ drop_table :decidim_forms_display_conditions
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateDecidimFormsQuestionMatrixRows < ActiveRecord::Migration[5.2]
4
+ def change
5
+ create_table :decidim_forms_question_matrix_rows do |t|
6
+ t.references :decidim_question, index: { name: "index_decidim_forms_question_matrix_questionnaire_id" }
7
+ t.integer :position, index: true
8
+ t.jsonb :body
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddMatrixRowIdToDecidimFormsAnswerChoices < ActiveRecord::Migration[5.2]
4
+ class AnswerChoice < ApplicationRecord
5
+ self.table_name = :decidim_forms_answer_choices
6
+ end
7
+
8
+ def change
9
+ add_column :decidim_forms_answer_choices, :decidim_question_matrix_row_id, :integer
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Forms
5
+ # This interface should be implemented by any Type that can be linked to a questionnaire
6
+ # The only requirement is to have an ID and the Type name be the class.name + Type
7
+ QuestionnaireEntityInterface = GraphQL::InterfaceType.define do
8
+ name "QuestionnaireEntityInterface"
9
+ description "An interface that can be used in objects with questionnaires"
10
+
11
+ field :id, !types.ID, "ID of this entity"
12
+
13
+ resolve_type ->(obj, _ctx) {
14
+ "#{obj.class.name}Type".constantize
15
+ }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "wicked_pdf"
4
+
5
+ module Decidim
6
+ module Exporters
7
+ # Inherits from abstract PDF exporter. This class is used to set
8
+ # the parameters used to create a PDF when exporting Survey Answers.
9
+ #
10
+ class FormPDF < PDF
11
+ # i18n-tasks-use t('decidim.admin.exports.formats.FormPDF')
12
+
13
+ def controller
14
+ @controller ||= FormPDFControllerHelper.new
15
+ end
16
+
17
+ def template
18
+ "decidim/forms/admin/questionnaires/answers/export/pdf.html.erb"
19
+ end
20
+
21
+ def layout
22
+ "decidim/forms/admin/questionnaires/questionnaire_answers.html.erb"
23
+ end
24
+
25
+ def locals
26
+ {
27
+ questionnaire: collection.first.first.questionnaire,
28
+ collection: collection.map { |answer| Decidim::Forms::Admin::QuestionnaireParticipantPresenter.new(participant: answer.first) }
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Exporters
5
+ # A dummy controller to render views while exporting questionnaires
6
+ class FormPDFControllerHelper < ActionController::Base
7
+ helper Decidim::TranslationsHelper
8
+ helper Decidim::Forms::Admin::QuestionnaireAnswersHelper
9
+ end
10
+ end
11
+ end
@@ -3,6 +3,7 @@
3
3
  require "decidim/forms/admin"
4
4
  require "decidim/forms/engine"
5
5
  require "decidim/forms/admin_engine"
6
+ require "decidim/forms/api"
6
7
 
7
8
  module Decidim
8
9
  # This namespace holds the logic of the `Forms`.
@@ -10,4 +11,9 @@ module Decidim
10
11
  autoload :UserAnswersSerializer, "decidim/forms/user_answers_serializer"
11
12
  autoload :DataPortabilityUserAnswersSerializer, "decidim/forms/data_portability_user_answers_serializer"
12
13
  end
14
+
15
+ module Exporters
16
+ autoload :FormPDF, "decidim/exporters/form_pdf"
17
+ autoload :FormPDFControllerHelper, "decidim/exporters/form_pdf_controller_helper"
18
+ end
13
19
  end
@@ -10,7 +10,7 @@ module Decidim
10
10
  paths["lib/tasks"] = nil
11
11
 
12
12
  initializer "decidim_forms.admin_assets" do |app|
13
- app.config.assets.precompile += %w(admin/decidim_forms_manifest.js)
13
+ app.config.assets.precompile += %w(admin/decidim_forms_manifest.js admin/decidim_forms_manifest.css)
14
14
  end
15
15
 
16
16
  def load_seed
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Forms
5
+ autoload :QuestionnaireEntityInterface, "decidim/api/questionnaire_entity_interface"
6
+ end
7
+ end
@@ -2,3 +2,9 @@
2
2
 
3
3
  require "decidim/forms/test/shared_examples/has_questionnaire"
4
4
  require "decidim/forms/test/shared_examples/manage_questionnaires"
5
+ require "decidim/forms/test/shared_examples/manage_questionnaire_answers"
6
+
7
+ if defined? Decidim::Templates
8
+ require "decidim/templates/test/shared_examples/uses_questionnaire_templates"
9
+ require "decidim/templates/test/factories"
10
+ end
@@ -4,7 +4,7 @@ require "decidim/core/test/factories"
4
4
  require "decidim/participatory_processes/test/factories"
5
5
 
6
6
  FactoryBot.define do
7
- factory :questionnaire, class: Decidim::Forms::Questionnaire do
7
+ factory :questionnaire, class: "Decidim::Forms::Questionnaire" do
8
8
  title { generate_localized_title }
9
9
  description do
10
10
  Decidim::Faker::Localized.wrapped("<p>", "</p>") do
@@ -23,14 +23,26 @@ FactoryBot.define do
23
23
  qs
24
24
  end
25
25
  end
26
+
27
+ trait :empty do
28
+ title { {} }
29
+ description { {} }
30
+ tos { {} }
31
+ end
26
32
  end
27
33
 
28
- factory :questionnaire_question, class: Decidim::Forms::Question do
34
+ factory :questionnaire_question, class: "Decidim::Forms::Question" do
29
35
  transient do
30
36
  options { [] }
37
+ rows { [] }
31
38
  end
32
39
 
33
40
  body { generate_localized_title }
41
+ description do
42
+ Decidim::Faker::Localized.wrapped("<p>", "</p>") do
43
+ generate_localized_title
44
+ end
45
+ end
34
46
  mandatory { false }
35
47
  position { 0 }
36
48
  question_type { Decidim::Forms::Question::TYPES.first }
@@ -45,6 +57,14 @@ FactoryBot.define do
45
57
  )
46
58
  end
47
59
  end
60
+
61
+ if question.matrix_rows.empty?
62
+ evaluator.rows.each do |row|
63
+ question.matrix_rows.build(
64
+ body: row["body"]
65
+ )
66
+ end
67
+ end
48
68
  end
49
69
 
50
70
  trait :with_answer_options do
@@ -52,23 +72,58 @@ FactoryBot.define do
52
72
  Array.new(3).collect { build(:answer_option) }
53
73
  end
54
74
  end
75
+
76
+ trait :conditioned do
77
+ display_conditions do
78
+ Array.new(3).collect { build(:display_condition) }
79
+ end
80
+ end
81
+
82
+ trait :separator do
83
+ question_type { :separator }
84
+ end
55
85
  end
56
86
 
57
- factory :answer, class: Decidim::Forms::Answer do
87
+ factory :answer, class: "Decidim::Forms::Answer" do
58
88
  body { "hola" }
59
89
  questionnaire
60
90
  question { create(:questionnaire_question, questionnaire: questionnaire) }
61
91
  user { create(:user, organization: questionnaire.questionnaire_for.organization) }
92
+ session_token { Digest::MD5.hexdigest(user.id.to_s) }
62
93
  end
63
94
 
64
- factory :answer_option, class: Decidim::Forms::AnswerOption do
95
+ factory :answer_option, class: "Decidim::Forms::AnswerOption" do
65
96
  question { create(:questionnaire_question) }
66
97
  body { generate_localized_title }
67
98
  free_text { false }
68
99
  end
69
100
 
70
- factory :answer_choice, class: Decidim::Forms::AnswerChoice do
101
+ factory :answer_choice, class: "Decidim::Forms::AnswerChoice" do
71
102
  answer
72
103
  answer_option { create(:answer_option, question: answer.question) }
104
+ matrix_row { create(:question_matrix_row, question: answer.question) }
105
+ end
106
+
107
+ factory :question_matrix_row, class: "Decidim::Forms::QuestionMatrixRow" do
108
+ question { create(:questionnaire_question) }
109
+ body { generate_localized_title }
110
+ position { 0 }
111
+ end
112
+
113
+ factory :display_condition, class: "Decidim::Forms::DisplayCondition" do
114
+ condition_question { create(:questionnaire_question) }
115
+ question { create(:questionnaire_question, position: 1) }
116
+ condition_type { :answered }
117
+ mandatory { true }
118
+
119
+ trait :equal do
120
+ condition_type { :equal }
121
+ answer_option { create(:answer_option, question: condition_question) }
122
+ end
123
+
124
+ trait :match do
125
+ condition_type { :match }
126
+ condition_value { generate_localized_title }
127
+ end
73
128
  end
74
129
  end
@@ -35,7 +35,9 @@ shared_examples_for "has questionnaire" do
35
35
 
36
36
  check "questionnaire_tos_agreement"
37
37
 
38
- accept_confirm { click_button "Submit" }
38
+ accept_confirm do
39
+ click_button "Submit"
40
+ end
39
41
 
40
42
  within ".success.flash" do
41
43
  expect(page).to have_content("successfully")
@@ -47,6 +49,73 @@ shared_examples_for "has questionnaire" do
47
49
  expect(page).to have_no_i18n_content(question.body)
48
50
  end
49
51
 
52
+ context "with multiple steps" do
53
+ let!(:separator) { create(:questionnaire_question, questionnaire: questionnaire, position: 1, question_type: :separator) }
54
+ let!(:question2) { create(:questionnaire_question, questionnaire: questionnaire, position: 2) }
55
+
56
+ before do
57
+ visit questionnaire_public_path
58
+ end
59
+
60
+ it "allows answering the first questionnaire" do
61
+ expect(page).to have_content("STEP 1 OF 2")
62
+
63
+ within ".answer-questionnaire__submit" do
64
+ expect(page).to have_no_content("Back")
65
+ end
66
+
67
+ answer_first_questionnaire
68
+
69
+ expect(page).to have_no_selector(".success.flash")
70
+ end
71
+
72
+ it "allows revisiting previously-answered questionnaires with my answers" do
73
+ answer_first_questionnaire
74
+
75
+ click_link "Back"
76
+
77
+ expect(page).to have_content("STEP 1 OF 2")
78
+ expect(page).to have_field("questionnaire_responses_0", with: "My first answer")
79
+ end
80
+
81
+ it "finishes the submission when answering the last questionnaire" do
82
+ answer_first_questionnaire
83
+
84
+ check "questionnaire_tos_agreement"
85
+ accept_confirm { click_button "Submit" }
86
+
87
+ within ".success.flash" do
88
+ expect(page).to have_content("successfully")
89
+ end
90
+
91
+ visit questionnaire_public_path
92
+
93
+ expect(page).to have_content("You have already answered this form.")
94
+ end
95
+
96
+ def answer_first_questionnaire
97
+ expect(page).to have_no_selector("#questionnaire_tos_agreement")
98
+
99
+ fill_in question.body["en"], with: "My first answer"
100
+ within ".answer-questionnaire__submit" do
101
+ click_link "Continue"
102
+ end
103
+ expect(page).to have_content("STEP 2 OF 2")
104
+ end
105
+ end
106
+
107
+ it "requires confirmation when exiting mid-answering" do
108
+ visit questionnaire_public_path
109
+
110
+ fill_in question.body["en"], with: "My first answer"
111
+
112
+ dismiss_page_unload do
113
+ page.find(".logo-wrapper a").click
114
+ end
115
+
116
+ expect(page).to have_current_path questionnaire_public_path
117
+ end
118
+
50
119
  context "when the questionnaire has already been answered by someone else" do
51
120
  let!(:question) do
52
121
  create(
@@ -188,7 +257,7 @@ shared_examples_for "has questionnaire" do
188
257
  end
189
258
 
190
259
  context "when a question has a rich text description" do
191
- let!(:question) { create(:questionnaire_question, questionnaire: questionnaire, position: 0, description: "<b>This question is important</b>") }
260
+ let!(:question) { create(:questionnaire_question, questionnaire: questionnaire, position: 0, description: { en: "<b>This question is important</b>" }) }
192
261
 
193
262
  it "properly interprets HTML descriptions" do
194
263
  visit questionnaire_public_path
@@ -205,6 +274,7 @@ shared_examples_for "has questionnaire" do
205
274
  :questionnaire_question,
206
275
  questionnaire: questionnaire,
207
276
  question_type: question_type,
277
+ position: 1,
208
278
  options: [
209
279
  { "body" => answer_option_bodies[0] },
210
280
  { "body" => answer_option_bodies[1] },
@@ -219,6 +289,7 @@ shared_examples_for "has questionnaire" do
219
289
  questionnaire: questionnaire,
220
290
  question_type: "multiple_option",
221
291
  max_choices: 2,
292
+ position: 2,
222
293
  options: [
223
294
  { "body" => Decidim::Faker::Localized.sentence },
224
295
  { "body" => Decidim::Faker::Localized.sentence },
@@ -237,16 +308,16 @@ shared_examples_for "has questionnaire" do
237
308
  it "renders them as radio buttons with attached text fields disabled by default" do
238
309
  expect(page).to have_selector(".radio-button-collection input[type=radio]", count: 3)
239
310
 
240
- expect(page).to have_field("questionnaire_answers_0_choices_2_custom_body", disabled: true, count: 1)
311
+ expect(page).to have_field("questionnaire_responses_0_choices_2_custom_body", disabled: true, count: 1)
241
312
 
242
313
  choose answer_option_bodies[2]["en"]
243
314
 
244
- expect(page).to have_field("questionnaire_answers_0_choices_2_custom_body", disabled: false, count: 1)
315
+ expect(page).to have_field("questionnaire_responses_0_choices_2_custom_body", disabled: false, count: 1)
245
316
  end
246
317
 
247
318
  it "saves the free text in a separate field if submission correct" do
248
319
  choose answer_option_bodies[2]["en"]
249
- fill_in "questionnaire_answers_0_choices_2_custom_body", with: "Cacatua"
320
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "Cacatua"
250
321
 
251
322
  check "questionnaire_tos_agreement"
252
323
  accept_confirm { click_button "Submit" }
@@ -264,7 +335,7 @@ shared_examples_for "has questionnaire" do
264
335
  check other_question.answer_options.third.body["en"]
265
336
 
266
337
  choose answer_option_bodies[2]["en"]
267
- fill_in "questionnaire_answers_0_choices_2_custom_body", with: "Cacatua"
338
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "Cacatua"
268
339
 
269
340
  check "questionnaire_tos_agreement"
270
341
  accept_confirm { click_button "Submit" }
@@ -273,7 +344,7 @@ shared_examples_for "has questionnaire" do
273
344
  expect(page).to have_content("There was a problem answering")
274
345
  end
275
346
 
276
- expect(page).to have_field("questionnaire_answers_0_choices_2_custom_body", with: "Cacatua")
347
+ expect(page).to have_field("questionnaire_responses_0_choices_2_custom_body", with: "Cacatua")
277
348
  end
278
349
  end
279
350
 
@@ -283,16 +354,16 @@ shared_examples_for "has questionnaire" do
283
354
  it "renders them as check boxes with attached text fields disabled by default" do
284
355
  expect(page.first(".check-box-collection")).to have_selector("input[type=checkbox]", count: 3)
285
356
 
286
- expect(page).to have_field("questionnaire_answers_0_choices_2_custom_body", disabled: true, count: 1)
357
+ expect(page).to have_field("questionnaire_responses_0_choices_2_custom_body", disabled: true, count: 1)
287
358
 
288
359
  check answer_option_bodies[2]["en"]
289
360
 
290
- expect(page).to have_field("questionnaire_answers_0_choices_2_custom_body", disabled: false, count: 1)
361
+ expect(page).to have_field("questionnaire_responses_0_choices_2_custom_body", disabled: false, count: 1)
291
362
  end
292
363
 
293
364
  it "saves the free text in a separate field if submission correct" do
294
365
  check answer_option_bodies[2]["en"]
295
- fill_in "questionnaire_answers_0_choices_2_custom_body", with: "Cacatua"
366
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "Cacatua"
296
367
 
297
368
  check "questionnaire_tos_agreement"
298
369
  accept_confirm { click_button "Submit" }
@@ -305,12 +376,12 @@ shared_examples_for "has questionnaire" do
305
376
  end
306
377
 
307
378
  it "preserves the previous custom body if submission not correct" do
308
- check "questionnaire_answers_1_choices_0_body"
309
- check "questionnaire_answers_1_choices_1_body"
310
- check "questionnaire_answers_1_choices_2_body"
379
+ check "questionnaire_responses_1_choices_0_body"
380
+ check "questionnaire_responses_1_choices_1_body"
381
+ check "questionnaire_responses_1_choices_2_body"
311
382
 
312
383
  check answer_option_bodies[2]["en"]
313
- fill_in "questionnaire_answers_0_choices_2_custom_body", with: "Cacatua"
384
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "Cacatua"
314
385
 
315
386
  check "questionnaire_tos_agreement"
316
387
  accept_confirm { click_button "Submit" }
@@ -319,7 +390,7 @@ shared_examples_for "has questionnaire" do
319
390
  expect(page).to have_content("There was a problem answering")
320
391
  end
321
392
 
322
- expect(page).to have_field("questionnaire_answers_0_choices_2_custom_body", with: "Cacatua")
393
+ expect(page).to have_field("questionnaire_responses_0_choices_2_custom_body", with: "Cacatua")
323
394
  end
324
395
  end
325
396
  end
@@ -330,7 +401,7 @@ shared_examples_for "has questionnaire" do
330
401
  it "renders the answer as a textarea" do
331
402
  visit questionnaire_public_path
332
403
 
333
- expect(page).to have_selector("textarea#questionnaire_answers_0")
404
+ expect(page).to have_selector("textarea#questionnaire_responses_0")
334
405
  end
335
406
  end
336
407
 
@@ -340,7 +411,7 @@ shared_examples_for "has questionnaire" do
340
411
  it "renders the answer as a text field" do
341
412
  visit questionnaire_public_path
342
413
 
343
- expect(page).to have_selector("input[type=text]#questionnaire_answers_0")
414
+ expect(page).to have_selector("input[type=text]#questionnaire_responses_0")
344
415
  end
345
416
  end
346
417
 
@@ -409,6 +480,8 @@ shared_examples_for "has questionnaire" do
409
480
  check answer_options[1]["body"][:en]
410
481
  check answer_options[2]["body"][:en]
411
482
 
483
+ expect(page).to have_content("too many choices")
484
+
412
485
  check "questionnaire_tos_agreement"
413
486
 
414
487
  accept_confirm { click_button "Submit" }
@@ -429,17 +502,46 @@ shared_examples_for "has questionnaire" do
429
502
  end
430
503
  end
431
504
 
432
- context "when question type is multiple option" do
433
- let(:answer_options) { Array.new(2) { { "body" => Decidim::Faker::Localized.sentence } } }
434
- let!(:question) { create(:questionnaire_question, questionnaire: questionnaire, question_type: "multiple_option", options: answer_options) }
505
+ context "when question type is sorting" do
506
+ let!(:question) do
507
+ create(
508
+ :questionnaire_question,
509
+ questionnaire: questionnaire,
510
+ question_type: "sorting",
511
+ options: [
512
+ { "body" => { "en" => "chocolate" } },
513
+ { "body" => { "en" => "like" } },
514
+ { "body" => { "en" => "We" } },
515
+ { "body" => { "en" => "dark" } },
516
+ { "body" => { "en" => "all" } }
517
+ ]
518
+ )
519
+ end
435
520
 
436
- it "renders the question answers as a collection of radio buttons" do
521
+ it "renders the question answers as a collection of check boxes sortable on click" do
437
522
  visit questionnaire_public_path
438
523
 
439
- expect(page).to have_selector(".check-box-collection input[type=checkbox]", count: 2)
524
+ expect(page).to have_selector(".sortable-check-box-collection input[type=checkbox]", count: 5)
440
525
 
441
- check answer_options[0]["body"][:en]
442
- check answer_options[1]["body"][:en]
526
+ expect(page).to have_content("chocolate\nlike\nWe\ndark\nall")
527
+
528
+ check "We"
529
+ check "all"
530
+ check "like"
531
+ check "dark"
532
+ check "chocolate"
533
+
534
+ expect(page).to have_content("1. We\n2. all\n3. like\n4. dark\n5. chocolate")
535
+ end
536
+
537
+ it "properly saves valid sortings" do
538
+ visit questionnaire_public_path
539
+
540
+ check "We"
541
+ check "all"
542
+ check "like"
543
+ check "dark"
544
+ check "chocolate"
443
545
 
444
546
  check "questionnaire_tos_agreement"
445
547
 
@@ -449,53 +551,175 @@ shared_examples_for "has questionnaire" do
449
551
  expect(page).to have_content("successfully")
450
552
  end
451
553
 
554
+ expect(Decidim::Forms::Answer.first.choices.pluck(:position, :body)).to eq(
555
+ [[0, "We"], [1, "all"], [2, "like"], [3, "dark"], [4, "chocolate"]]
556
+ )
557
+ end
558
+
559
+ it "displays errors on incomplete sortings" do
452
560
  visit questionnaire_public_path
453
561
 
454
- expect(page).to have_content("You have already answered this form.")
455
- expect(page).to have_no_i18n_content(question.body)
562
+ check "We"
563
+
564
+ accept_confirm { click_button "Submit" }
565
+
566
+ within ".alert.flash" do
567
+ expect(page).to have_content("problem")
568
+ end
569
+
570
+ expect(page).to have_content("are not complete")
571
+ end
572
+
573
+ it "displays maintains sorting order if errors" do
574
+ visit questionnaire_public_path
575
+
576
+ check "We"
577
+ check "dark"
578
+ check "chocolate"
579
+
580
+ accept_confirm { click_button "Submit" }
581
+
582
+ within ".alert.flash" do
583
+ expect(page).to have_content("problem")
584
+ end
585
+
586
+ # Check the next round to ensure a re-submission conserves status
587
+ expect(page).to have_content("are not complete")
588
+ expect(page).to have_content("1. We\n2. dark\n3. chocolate\nlike\nall")
589
+
590
+ checkboxes = page.all("input[type=checkbox]")
591
+
592
+ checkboxes[0].uncheck
593
+ check "We"
594
+ check "all"
595
+
596
+ accept_confirm { click_button "Submit" }
597
+
598
+ within ".alert.flash" do
599
+ expect(page).to have_content("problem")
600
+ end
601
+
602
+ expect(page).to have_content("are not complete")
603
+ expect(page).to have_content("1. dark\n2. chocolate\n3. We\n4. all\nlike")
456
604
  end
457
605
  end
458
606
 
459
- context "when question type is sorting" do
607
+ context "when question type is matrix_single" do
608
+ let(:matrix_rows) { Array.new(2) { { "body" => Decidim::Faker::Localized.sentence } } }
609
+ let(:answer_options) { Array.new(2) { { "body" => Decidim::Faker::Localized.sentence } } }
610
+ let(:mandatory) { false }
611
+
460
612
  let!(:question) do
461
613
  create(
462
614
  :questionnaire_question,
463
615
  questionnaire: questionnaire,
464
- question_type: "sorting",
465
- options: [
466
- { "body" => "idiotas" },
467
- { "body" => "trates" },
468
- { "body" => "No" },
469
- { "body" => "por" },
470
- { "body" => "nos" }
471
- ]
616
+ question_type: "matrix_single",
617
+ rows: matrix_rows,
618
+ options: answer_options,
619
+ mandatory: mandatory
472
620
  )
473
621
  end
474
622
 
475
- it "renders the question answers as a collection of check boxes sortable on click" do
623
+ it "renders the question answers as a collection of radio buttons" do
476
624
  visit questionnaire_public_path
477
625
 
478
- expect(page).to have_selector(".sortable-check-box-collection input[type=checkbox]", count: 5)
626
+ expect(page).to have_selector(".radio-button-collection input[type=radio]", count: 4)
627
+
628
+ expect(page).to have_content(matrix_rows.map { |row| row["body"]["en"] }.join("\n"))
629
+ expect(page).to have_content(answer_options.map { |option| option["body"]["en"] }.join(" "))
479
630
 
480
- expect(page).to have_content("idiotas\ntrates\nNo\npor\nnos")
631
+ radio_buttons = page.all(".radio-button-collection input[type=radio]")
481
632
 
482
- check "No"
483
- check "nos"
484
- check "trates"
485
- check "por"
486
- check "idiotas"
633
+ choose radio_buttons.first[:id]
634
+ choose radio_buttons.last[:id]
487
635
 
488
- expect(page).to have_content("1. No\n2. nos\n3. trates\n4. por\n5. idiotas")
636
+ check "questionnaire_tos_agreement"
637
+
638
+ accept_confirm { click_button "Submit" }
639
+
640
+ within ".success.flash" do
641
+ expect(page).to have_content("successfully")
642
+ end
643
+
644
+ visit questionnaire_public_path
645
+
646
+ expect(page).to have_content("You have already answered this form.")
647
+ expect(page).to have_no_i18n_content(question.body)
648
+
649
+ first_choice, last_choice = Decidim::Forms::Answer.last.choices.pluck(:decidim_answer_option_id, :decidim_question_matrix_row_id)
650
+
651
+ expect(first_choice).to eq([question.answer_options.first.id, question.matrix_rows.first.id])
652
+ expect(last_choice).to eq([question.answer_options.last.id, question.matrix_rows.last.id])
489
653
  end
490
654
 
491
- it "properly saves valid sortings" do
655
+ it "preserves the chosen answers if submission not correct" do
656
+ visit questionnaire_public_path
657
+
658
+ radio_buttons = page.all(".radio-button-collection input[type=radio]")
659
+ choose radio_buttons[1][:id]
660
+
661
+ accept_confirm { click_button "Submit" }
662
+
663
+ within ".alert.flash" do
664
+ expect(page).to have_content("There was a problem answering")
665
+ end
666
+
667
+ radio_buttons = page.all(".radio-button-collection input[type=radio]")
668
+ expect(radio_buttons.map { |b| b[:checked] }).to eq([nil, "true", nil, nil])
669
+ end
670
+
671
+ context "when the question is mandatory and the answer is not complete" do
672
+ let!(:mandatory) { true }
673
+
674
+ it "shows an error if the question is mandatory and the answer is not complete" do
675
+ visit questionnaire_public_path
676
+
677
+ radio_buttons = page.all(".radio-button-collection input[type=radio]")
678
+ choose radio_buttons[0][:id]
679
+
680
+ check "questionnaire_tos_agreement"
681
+ accept_confirm { click_button "Submit" }
682
+
683
+ within ".alert.flash" do
684
+ expect(page).to have_content("There was a problem answering")
685
+ end
686
+
687
+ expect(page).to have_content("Choices are not complete")
688
+ end
689
+ end
690
+ end
691
+
692
+ context "when question type is matrix_multiple" do
693
+ let(:matrix_rows) { Array.new(2) { { "body" => Decidim::Faker::Localized.sentence } } }
694
+ let(:answer_options) { Array.new(3) { { "body" => Decidim::Faker::Localized.sentence } } }
695
+ let(:max_choices) { nil }
696
+ let(:mandatory) { false }
697
+
698
+ let!(:question) do
699
+ create(
700
+ :questionnaire_question,
701
+ questionnaire: questionnaire,
702
+ question_type: "matrix_multiple",
703
+ rows: matrix_rows,
704
+ options: answer_options,
705
+ max_choices: max_choices,
706
+ mandatory: mandatory
707
+ )
708
+ end
709
+
710
+ it "renders the question answers as a collection of check boxes" do
492
711
  visit questionnaire_public_path
493
712
 
494
- check "No"
495
- check "nos"
496
- check "trates"
497
- check "por"
498
- check "idiotas"
713
+ expect(page).to have_selector(".check-box-collection input[type=checkbox]", count: 6)
714
+
715
+ expect(page).to have_content(matrix_rows.map { |row| row["body"]["en"] }.join("\n"))
716
+ expect(page).to have_content(answer_options.map { |option| option["body"]["en"] }.join(" "))
717
+
718
+ checkboxes = page.all(".check-box-collection input[type=checkbox]")
719
+
720
+ check checkboxes[0][:id]
721
+ check checkboxes[1][:id]
722
+ check checkboxes[3][:id]
499
723
 
500
724
  check "questionnaire_tos_agreement"
501
725
 
@@ -505,24 +729,658 @@ shared_examples_for "has questionnaire" do
505
729
  expect(page).to have_content("successfully")
506
730
  end
507
731
 
508
- expect(Decidim::Forms::Answer.first.choices.pluck(:position, :body)).to eq(
509
- [[0, "No"], [1, "nos"], [2, "trates"], [3, "por"], [4, "idiotas"]]
510
- )
732
+ visit questionnaire_public_path
733
+
734
+ expect(page).to have_content("You have already answered this form.")
735
+ expect(page).to have_no_i18n_content(question.body)
736
+
737
+ first_choice, second_choice, third_choice = Decidim::Forms::Answer.last.choices.pluck(:decidim_answer_option_id, :decidim_question_matrix_row_id)
738
+
739
+ expect(first_choice).to eq([question.answer_options.first.id, question.matrix_rows.first.id])
740
+ expect(second_choice).to eq([question.answer_options.second.id, question.matrix_rows.first.id])
741
+ expect(third_choice).to eq([question.answer_options.first.id, question.matrix_rows.last.id])
511
742
  end
512
743
 
513
- it "displays errors on incomplete sortings" do
514
- visit questionnaire_public_path
744
+ context "when the question hax max_choices defined" do
745
+ let!(:max_choices) { 2 }
515
746
 
516
- check "No"
747
+ it "respects the max number of choices" do
748
+ visit questionnaire_public_path
517
749
 
518
- accept_confirm { click_button "Submit" }
750
+ expect(page).to have_content("Max choices: 2")
519
751
 
520
- within ".alert.flash" do
521
- expect(page).to have_content("problem")
752
+ checkboxes = page.all(".check-box-collection input[type=checkbox]")
753
+
754
+ check checkboxes[0][:id]
755
+ check checkboxes[1][:id]
756
+ check checkboxes[2][:id]
757
+
758
+ expect(page).to have_content("too many choices")
759
+
760
+ check checkboxes[3][:id]
761
+ check checkboxes[4][:id]
762
+
763
+ expect(page).to have_content("too many choices")
764
+
765
+ check checkboxes[5][:id]
766
+
767
+ uncheck checkboxes[0][:id]
768
+
769
+ expect(page).to have_content("too many choices")
770
+
771
+ check "questionnaire_tos_agreement"
772
+
773
+ accept_confirm { click_button "Submit" }
774
+
775
+ within ".alert.flash" do
776
+ expect(page).to have_content("There was a problem answering")
777
+ end
778
+
779
+ expect(page).to have_content("are too many")
780
+
781
+ checkboxes = page.all(".check-box-collection input[type=checkbox]")
782
+
783
+ uncheck checkboxes[5][:id]
784
+
785
+ accept_confirm { click_button "Submit" }
786
+
787
+ within ".success.flash" do
788
+ expect(page).to have_content("successfully")
789
+ end
522
790
  end
791
+ end
523
792
 
524
- expect(page).to have_content("are not complete")
793
+ context "when the question is mandatory and the answer is not complete" do
794
+ let!(:mandatory) { true }
795
+
796
+ it "shows an error" do
797
+ visit questionnaire_public_path
798
+
799
+ checkboxes = page.all(".check-box-collection input[type=checkbox]")
800
+ check checkboxes[0][:id]
801
+
802
+ check "questionnaire_tos_agreement"
803
+ accept_confirm { click_button "Submit" }
804
+
805
+ within ".alert.flash" do
806
+ expect(page).to have_content("There was a problem answering")
807
+ end
808
+
809
+ expect(page).to have_content("Choices are not complete")
810
+ end
811
+ end
812
+
813
+ context "when the submission is not correct" do
814
+ let!(:max_choices) { 2 }
815
+
816
+ it "preserves the chosen answers" do
817
+ visit questionnaire_public_path
818
+
819
+ checkboxes = page.all(".check-box-collection input[type=checkbox]")
820
+ check checkboxes[0][:id]
821
+ check checkboxes[1][:id]
822
+ check checkboxes[2][:id]
823
+ check checkboxes[5][:id]
824
+
825
+ check "questionnaire_tos_agreement"
826
+ accept_confirm { click_button "Submit" }
827
+
828
+ within ".alert.flash" do
829
+ expect(page).to have_content("There was a problem answering")
830
+ end
831
+
832
+ checkboxes = page.all(".check-box-collection input[type=checkbox]")
833
+ expect(checkboxes.map { |c| c[:checked] }).to eq(["true", "true", "true", nil, nil, "true"])
834
+ end
525
835
  end
526
836
  end
837
+
838
+ describe "display conditions" do
839
+ let(:answer_options) do
840
+ 3.times.to_a.map do |x|
841
+ {
842
+ "body" => Decidim::Faker::Localized.sentence,
843
+ "free_text" => x == 2
844
+ }
845
+ end
846
+ end
847
+ let(:condition_question_options) { [] }
848
+ let!(:question) { create(:questionnaire_question, questionnaire: questionnaire, position: 2) }
849
+ let!(:conditioned_question_id) { "#questionnaire_responses_1" }
850
+ let!(:condition_question) do
851
+ create(:questionnaire_question,
852
+ questionnaire: questionnaire,
853
+ question_type: condition_question_type,
854
+ position: 1,
855
+ options: condition_question_options)
856
+ end
857
+
858
+ context "when a question has a display condition" do
859
+ context "when condition is of type 'answered'" do
860
+ let!(:display_condition) do
861
+ create(:display_condition,
862
+ condition_type: "answered",
863
+ question: question,
864
+ condition_question: condition_question)
865
+ end
866
+
867
+ before do
868
+ visit questionnaire_public_path
869
+ end
870
+
871
+ context "when the condition_question type is short answer" do
872
+ let!(:condition_question_type) { "short_answer" }
873
+
874
+ it "shows the question only if the condition is fulfilled" do
875
+ expect_question_to_be_visible(false)
876
+
877
+ fill_in "questionnaire_responses_0", with: "Cacatua"
878
+ change_focus
879
+
880
+ expect_question_to_be_visible(true)
881
+
882
+ fill_in "questionnaire_responses_0", with: ""
883
+ change_focus
884
+
885
+ expect_question_to_be_visible(false)
886
+ end
887
+ end
888
+
889
+ context "when the condition_question type is long answer" do
890
+ let!(:condition_question_type) { "long_answer" }
891
+
892
+ it "shows the question only if the condition is fulfilled" do
893
+ expect_question_to_be_visible(false)
894
+
895
+ fill_in "questionnaire_responses_0", with: "Cacatua"
896
+ change_focus
897
+
898
+ expect_question_to_be_visible(true)
899
+
900
+ fill_in "questionnaire_responses_0", with: ""
901
+ change_focus
902
+
903
+ expect_question_to_be_visible(false)
904
+ end
905
+ end
906
+
907
+ context "when the condition_question type is single option" do
908
+ let!(:condition_question_type) { "single_option" }
909
+ let!(:condition_question_options) { answer_options }
910
+
911
+ it "shows the question only if the condition is fulfilled" do
912
+ expect_question_to_be_visible(false)
913
+
914
+ choose condition_question.answer_options.first.body["en"]
915
+
916
+ expect_question_to_be_visible(true)
917
+
918
+ choose condition_question.answer_options.second.body["en"]
919
+
920
+ expect_question_to_be_visible(false)
921
+ end
922
+ end
923
+
924
+ context "when the condition_question type is multiple option" do
925
+ let!(:condition_question_type) { "multiple_option" }
926
+ let!(:condition_question_options) { answer_options }
927
+
928
+ it "shows the question only if the condition is fulfilled" do
929
+ expect_question_to_be_visible(false)
930
+
931
+ check condition_question.answer_options.first.body["en"]
932
+
933
+ expect_question_to_be_visible(true)
934
+
935
+ uncheck condition_question.answer_options.first.body["en"]
936
+
937
+ expect_question_to_be_visible(false)
938
+
939
+ check condition_question.answer_options.second.body["en"]
940
+
941
+ expect_question_to_be_visible(false)
942
+
943
+ check condition_question.answer_options.first.body["en"]
944
+
945
+ expect_question_to_be_visible(true)
946
+ end
947
+ end
948
+ end
949
+
950
+ context "when a question has a display condition of type 'not_answered'" do
951
+ let!(:display_condition) do
952
+ create(:display_condition,
953
+ condition_type: "not_answered",
954
+ question: question,
955
+ condition_question: condition_question)
956
+ end
957
+
958
+ before do
959
+ visit questionnaire_public_path
960
+ end
961
+
962
+ context "when the condition_question type is short answer" do
963
+ let!(:condition_question_type) { "short_answer" }
964
+
965
+ it "shows the question only if the condition is fulfilled" do
966
+ expect_question_to_be_visible(true)
967
+
968
+ fill_in "questionnaire_responses_0", with: "Cacatua"
969
+ change_focus
970
+
971
+ expect_question_to_be_visible(false)
972
+
973
+ fill_in "questionnaire_responses_0", with: ""
974
+ change_focus
975
+
976
+ expect_question_to_be_visible(true)
977
+ end
978
+ end
979
+
980
+ context "when the condition_question type is long answer" do
981
+ let!(:condition_question_type) { "long_answer" }
982
+
983
+ it "shows the question only if the condition is fulfilled" do
984
+ expect_question_to_be_visible(true)
985
+
986
+ fill_in "questionnaire_responses_0", with: "Cacatua"
987
+ change_focus
988
+
989
+ expect_question_to_be_visible(false)
990
+
991
+ fill_in "questionnaire_responses_0", with: ""
992
+ change_focus
993
+
994
+ expect_question_to_be_visible(true)
995
+ end
996
+ end
997
+
998
+ context "when the condition_question type is single option" do
999
+ let!(:condition_question_type) { "single_option" }
1000
+ let!(:condition_question_options) { answer_options }
1001
+
1002
+ it "shows the question only if the condition is fulfilled" do
1003
+ expect_question_to_be_visible(true)
1004
+
1005
+ choose condition_question.answer_options.first.body["en"]
1006
+
1007
+ expect_question_to_be_visible(false)
1008
+ end
1009
+ end
1010
+
1011
+ context "when the condition_question type is multiple option" do
1012
+ let!(:condition_question_type) { "multiple_option" }
1013
+ let!(:condition_question_options) { answer_options }
1014
+
1015
+ it "shows the question only if the condition is fulfilled" do
1016
+ expect_question_to_be_visible(true)
1017
+
1018
+ check condition_question.answer_options.first.body["en"]
1019
+
1020
+ expect_question_to_be_visible(false)
1021
+
1022
+ uncheck condition_question.answer_options.first.body["en"]
1023
+
1024
+ expect_question_to_be_visible(true)
1025
+ end
1026
+ end
1027
+ end
1028
+
1029
+ context "when a question has a display condition of type 'equal'" do
1030
+ let!(:display_condition) do
1031
+ create(:display_condition,
1032
+ condition_type: "equal",
1033
+ question: question,
1034
+ condition_question: condition_question,
1035
+ answer_option: condition_question.answer_options.first)
1036
+ end
1037
+
1038
+ before do
1039
+ visit questionnaire_public_path
1040
+ end
1041
+
1042
+ context "when the condition_question type is single option" do
1043
+ let!(:condition_question_type) { "single_option" }
1044
+ let!(:condition_question_options) { answer_options }
1045
+
1046
+ it "shows the question only if the condition is fulfilled" do
1047
+ expect_question_to_be_visible(false)
1048
+
1049
+ choose condition_question.answer_options.first.body["en"]
1050
+
1051
+ expect_question_to_be_visible(true)
1052
+
1053
+ choose condition_question.answer_options.second.body["en"]
1054
+
1055
+ expect_question_to_be_visible(false)
1056
+ end
1057
+ end
1058
+
1059
+ context "when the condition_question type is multiple option" do
1060
+ let!(:condition_question_type) { "multiple_option" }
1061
+ let!(:condition_question_options) { answer_options }
1062
+
1063
+ it "shows the question only if the condition is fulfilled" do
1064
+ expect_question_to_be_visible(false)
1065
+
1066
+ check condition_question.answer_options.first.body["en"]
1067
+
1068
+ expect_question_to_be_visible(true)
1069
+
1070
+ uncheck condition_question.answer_options.first.body["en"]
1071
+
1072
+ expect_question_to_be_visible(false)
1073
+
1074
+ check condition_question.answer_options.second.body["en"]
1075
+
1076
+ expect_question_to_be_visible(false)
1077
+
1078
+ check condition_question.answer_options.first.body["en"]
1079
+
1080
+ expect_question_to_be_visible(true)
1081
+ end
1082
+ end
1083
+ end
1084
+
1085
+ context "when a question has a display condition of type 'not_equal'" do
1086
+ let!(:display_condition) do
1087
+ create(:display_condition,
1088
+ condition_type: "not_equal",
1089
+ question: question,
1090
+ condition_question: condition_question,
1091
+ answer_option: condition_question.answer_options.first)
1092
+ end
1093
+
1094
+ before do
1095
+ visit questionnaire_public_path
1096
+ end
1097
+
1098
+ context "when the condition_question type is single option" do
1099
+ let!(:condition_question_type) { "single_option" }
1100
+ let!(:condition_question_options) { answer_options }
1101
+
1102
+ it "shows the question only if the condition is fulfilled" do
1103
+ expect_question_to_be_visible(false)
1104
+
1105
+ choose condition_question.answer_options.second.body["en"]
1106
+
1107
+ expect_question_to_be_visible(true)
1108
+
1109
+ choose condition_question.answer_options.first.body["en"]
1110
+
1111
+ expect_question_to_be_visible(false)
1112
+ end
1113
+ end
1114
+
1115
+ context "when the condition_question type is multiple option" do
1116
+ let!(:condition_question_type) { "multiple_option" }
1117
+ let!(:condition_question_options) { answer_options }
1118
+
1119
+ it "shows the question only if the condition is fulfilled" do
1120
+ expect_question_to_be_visible(false)
1121
+
1122
+ check condition_question.answer_options.second.body["en"]
1123
+
1124
+ expect_question_to_be_visible(true)
1125
+
1126
+ uncheck condition_question.answer_options.second.body["en"]
1127
+
1128
+ expect_question_to_be_visible(false)
1129
+
1130
+ check condition_question.answer_options.first.body["en"]
1131
+
1132
+ expect_question_to_be_visible(false)
1133
+
1134
+ check condition_question.answer_options.second.body["en"]
1135
+
1136
+ expect_question_to_be_visible(true)
1137
+ end
1138
+ end
1139
+ end
1140
+
1141
+ context "when a question has a display condition of type 'match'" do
1142
+ let!(:condition_value) { { en: "something" } }
1143
+ let!(:display_condition) do
1144
+ create(:display_condition,
1145
+ condition_type: "match",
1146
+ question: question,
1147
+ condition_question: condition_question,
1148
+ condition_value: condition_value)
1149
+ end
1150
+
1151
+ before do
1152
+ visit questionnaire_public_path
1153
+ end
1154
+
1155
+ context "when the condition_question type is short answer" do
1156
+ let!(:condition_question_type) { "short_answer" }
1157
+
1158
+ it "shows the question only if the condition is fulfilled" do
1159
+ expect_question_to_be_visible(false)
1160
+
1161
+ fill_in "questionnaire_responses_0", with: "Aren't we all expecting #{condition_value[:en]}?"
1162
+ change_focus
1163
+
1164
+ expect_question_to_be_visible(true)
1165
+
1166
+ fill_in "questionnaire_responses_0", with: "Now upcase #{condition_value[:en].upcase}!"
1167
+ change_focus
1168
+
1169
+ expect_question_to_be_visible(true)
1170
+
1171
+ fill_in "questionnaire_responses_0", with: "Cacatua"
1172
+ change_focus
1173
+
1174
+ expect_question_to_be_visible(false)
1175
+ end
1176
+ end
1177
+
1178
+ context "when the condition_question type is long answer" do
1179
+ let!(:condition_question_type) { "long_answer" }
1180
+
1181
+ it "shows the question only if the condition is fulfilled" do
1182
+ expect_question_to_be_visible(false)
1183
+
1184
+ fill_in "questionnaire_responses_0", with: "Aren't we all expecting #{condition_value[:en]}?"
1185
+ change_focus
1186
+
1187
+ expect_question_to_be_visible(true)
1188
+
1189
+ fill_in "questionnaire_responses_0", with: "Now upcase #{condition_value[:en].upcase}!"
1190
+ change_focus
1191
+
1192
+ expect_question_to_be_visible(true)
1193
+
1194
+ fill_in "questionnaire_responses_0", with: "Cacatua"
1195
+ change_focus
1196
+
1197
+ expect_question_to_be_visible(false)
1198
+ end
1199
+ end
1200
+
1201
+ context "when the condition_question type is single option" do
1202
+ let!(:condition_question_type) { "single_option" }
1203
+ let!(:condition_question_options) { answer_options }
1204
+ let!(:condition_value) { { en: condition_question.answer_options.first.body["en"].split.second.upcase } }
1205
+
1206
+ it "shows the question only if the condition is fulfilled" do
1207
+ expect_question_to_be_visible(false)
1208
+
1209
+ choose condition_question.answer_options.first.body["en"]
1210
+
1211
+ expect_question_to_be_visible(true)
1212
+ end
1213
+ end
1214
+
1215
+ context "when the condition_question type is single option with free text" do
1216
+ let!(:condition_question_type) { "single_option" }
1217
+ let!(:condition_question_options) { answer_options }
1218
+ let!(:condition_value) { { en: "forty two" } }
1219
+
1220
+ it "shows the question only if the condition is fulfilled" do
1221
+ expect_question_to_be_visible(false)
1222
+
1223
+ choose condition_question.answer_options.third.body["en"]
1224
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "The answer is #{condition_value[:en]}"
1225
+ change_focus
1226
+
1227
+ expect_question_to_be_visible(true)
1228
+
1229
+ choose condition_question.answer_options.first.body["en"]
1230
+ expect_question_to_be_visible(false)
1231
+
1232
+ choose condition_question.answer_options.third.body["en"]
1233
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "oh no not 42 again"
1234
+ change_focus
1235
+
1236
+ expect_question_to_be_visible(false)
1237
+ end
1238
+ end
1239
+
1240
+ context "when the condition_question type is multiple option" do
1241
+ let!(:condition_question_type) { "multiple_option" }
1242
+ let!(:condition_question_options) { answer_options }
1243
+ let!(:condition_value) { { en: "forty two" } }
1244
+
1245
+ it "shows the question only if the condition is fulfilled" do
1246
+ expect_question_to_be_visible(false)
1247
+
1248
+ check condition_question.answer_options.third.body["en"]
1249
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "The answer is #{condition_value[:en]}"
1250
+ change_focus
1251
+
1252
+ expect_question_to_be_visible(true)
1253
+
1254
+ check condition_question.answer_options.first.body["en"]
1255
+ expect_question_to_be_visible(true)
1256
+
1257
+ uncheck condition_question.answer_options.third.body["en"]
1258
+ expect_question_to_be_visible(false)
1259
+
1260
+ check condition_question.answer_options.third.body["en"]
1261
+ fill_in "questionnaire_responses_0_choices_2_custom_body", with: "oh no not 42 again"
1262
+ change_focus
1263
+
1264
+ expect_question_to_be_visible(false)
1265
+ end
1266
+ end
1267
+ end
1268
+ end
1269
+
1270
+ context "when a question has multiple display conditions" do
1271
+ before do
1272
+ visit questionnaire_public_path
1273
+ end
1274
+
1275
+ context "when all conditions are mandatory" do
1276
+ let!(:condition_question_type) { "single_option" }
1277
+ let!(:condition_question_options) { answer_options }
1278
+ let!(:display_conditions) do
1279
+ [
1280
+ create(:display_condition,
1281
+ condition_type: "answered",
1282
+ question: question,
1283
+ condition_question: condition_question,
1284
+ mandatory: true),
1285
+ create(:display_condition,
1286
+ condition_type: "not_equal",
1287
+ question: question,
1288
+ condition_question: condition_question,
1289
+ mandatory: true,
1290
+ answer_option: condition_question.answer_options.second)
1291
+ ]
1292
+ end
1293
+
1294
+ it "is displayed only if all conditions are fulfilled" do
1295
+ expect_question_to_be_visible(false)
1296
+
1297
+ choose condition_question.answer_options.second.body["en"]
1298
+
1299
+ expect_question_to_be_visible(false)
1300
+
1301
+ choose condition_question.answer_options.first.body["en"]
1302
+
1303
+ expect_question_to_be_visible(true)
1304
+ end
1305
+ end
1306
+
1307
+ context "when all conditions are non-mandatory" do
1308
+ let!(:condition_question_type) { "multiple_option" }
1309
+ let!(:condition_question_options) { answer_options }
1310
+ let!(:display_conditions) do
1311
+ [
1312
+ create(:display_condition,
1313
+ condition_type: "equal",
1314
+ question: question,
1315
+ condition_question: condition_question,
1316
+ mandatory: false,
1317
+ answer_option: condition_question.answer_options.first),
1318
+ create(:display_condition,
1319
+ condition_type: "not_equal",
1320
+ question: question,
1321
+ condition_question: condition_question,
1322
+ mandatory: false,
1323
+ answer_option: condition_question.answer_options.third)
1324
+ ]
1325
+ end
1326
+
1327
+ it "is displayed if any of the conditions is fulfilled" do
1328
+ expect_question_to_be_visible(false)
1329
+
1330
+ check condition_question.answer_options.first.body["en"]
1331
+
1332
+ expect_question_to_be_visible(true)
1333
+
1334
+ uncheck condition_question.answer_options.first.body["en"]
1335
+ check condition_question.answer_options.second.body["en"]
1336
+
1337
+ expect_question_to_be_visible(true)
1338
+
1339
+ check condition_question.answer_options.first.body["en"]
1340
+
1341
+ expect_question_to_be_visible(true)
1342
+ end
1343
+ end
1344
+
1345
+ context "when a mandatory question has conditions that have not been fulfilled" do
1346
+ let!(:condition_question_type) { "short_answer" }
1347
+ let!(:question) { create(:questionnaire_question, questionnaire: questionnaire, position: 2, mandatory: true) }
1348
+ let!(:display_conditions) do
1349
+ [
1350
+ create(:display_condition,
1351
+ condition_type: "match",
1352
+ question: question,
1353
+ condition_question: condition_question,
1354
+ condition_value: { en: "hey", es: "ey", ca: "ei" },
1355
+ mandatory: true)
1356
+ ]
1357
+ end
1358
+
1359
+ it "doesn't throw error" do
1360
+ visit questionnaire_public_path
1361
+
1362
+ fill_in condition_question.body["en"], with: "My first answer"
1363
+
1364
+ check "questionnaire_tos_agreement"
1365
+
1366
+ accept_confirm { click_button "Submit" }
1367
+
1368
+ within ".success.flash" do
1369
+ expect(page).to have_content("successfully")
1370
+ end
1371
+ end
1372
+ end
1373
+ end
1374
+ end
1375
+
1376
+ private
1377
+
1378
+ def expect_question_to_be_visible(visible)
1379
+ expect(page).to have_css(conditioned_question_id, visible: visible)
1380
+ end
1381
+
1382
+ def change_focus
1383
+ check "questionnaire_tos_agreement"
1384
+ end
527
1385
  end
528
1386
  end