<commit_before># frozen_string_literal: true

require "spec_helper"

module Decidim
  module Surveys
    module Admin
      describe UpdateSurvey do
        let(:current_organization) { create(:organization) }
        let(:participatory_process) { create(:participatory_process, organization: current_organization) }
        let(:component) { create(:component, manifest_name: "surveys", participatory_space: participatory_process) }
        let(:survey) { create(:survey, component: component) }
        let(:published_at) { nil }
        let(:form_params) do
          {
            "title" => {
              "en" => "Title",
              "ca" => "Title",
              "es" => "Title"
            },
            "tos" => {
              "en" => "<p>TOS</p>",
              "ca" => "<p>TOS</p>",
              "es" => "<p>TOS</p>"
            },
            "description" => {
              "en" => "<p>Content</p>",
              "ca" => "<p>Contingut</p>",
              "es" => "<p>Contenido</p>"
            },
            "questions" => {
              "0" => {
                "body" => {
                  "en" => "First question",
                  "ca" => "Primera pregunta",
                  "es" => "Primera pregunta"
                },
                "position" => "0",
                "question_type" => "short_answer",
                "answer_options" => {}
              },
              "1" => {
                "body" => {
                  "en" => "Second question",
                  "ca" => "Segona pregunta",
                  "es" => "Segunda pregunta"
                },
                "description" => { "en" => "Description" },
                "position" => "1",
                "mandatory" => "1",
                "question_type" => "long_answer",
                "answer_options" => {}
              },
              "2" => {
                "body" => {
                  "en" => "Third question",
                  "ca" => "Tercera pregunta",
                  "es" => "Tercera pregunta"
                },
                "position" => "2",
                "question_type" => "single_option",
                "answer_options" => {
                  "0" => {
                    "body" => {
                      "en" => "First answer",
                      "ca" => "Primera resposta",
                      "es" => "Primera respuesta"
                    }
                  },
                  "1" => {
                    "body" => {
                      "en" => "Second answer",
                      "ca" => "Segona resposta",
                      "es" => "Segunda respuesta"
                    }
                  }
                }
              }
            },
            "published_at" => published_at
          }
        end
        let(:form) do
          SurveyForm.from_params(
            survey: form_params
          ).with_context(
            current_organization: current_organization
          )
        end
        let(:command) { described_class.new(form, survey) }

        describe "when the form is invalid" do
          before do
            expect(form).to receive(:invalid?).and_return(true)
          end

          it "broadcasts invalid" do
            expect { command.call }.to broadcast(:invalid)
          end

          it "doesn't update the survey" do
            expect(survey).not_to receive(:update!)
            command.call
          end
        end

        describe "when the form is valid" do
          it "broadcasts ok" do
            expect { command.call }.to broadcast(:ok)
          end

          it "updates the survey" do
            command.call
            survey.reload

            expect(survey.description["en"]).to eq("<p>Content</p>")
            expect(survey.questions.length).to eq(3)

            survey.questions.each_with_index do |question, idx|
              expect(question.body["en"]).to eq(form_params["questions"][idx.to_s]["body"]["en"])
            end

            expect(survey.questions[1]).to be_mandatory
            expect(survey.questions[1].description["en"]).to eq(form_params["questions"][1]["description"]["en"])
            expect(survey.questions[1].question_type).to eq("long_answer")
            expect(survey.questions[2].answer_options[1]["body"]["en"]).to eq(form_params["questions"]["2"]["answer_options"]["1"]["body"]["en"])
          end
        end

        describe "when the survey has an existing question" do
          let!(:survey_question) { create(:survey_question, survey: survey) }

          context "and the question should be removed" do
            let(:form_params) do<commit_msg>Fix bad hash key access (#3075)<commit_after># frozen_string_literal: true

require "spec_helper"

module Decidim
  module Surveys
    module Admin
      describe UpdateSurvey do
        let(:current_organization) { create(:organization) }
        let(:participatory_process) { create(:participatory_process, organization: current_organization) }
        let(:component) { create(:component, manifest_name: "surveys", participatory_space: participatory_process) }
        let(:survey) { create(:survey, component: component) }
        let(:published_at) { nil }
        let(:form_params) do
          {
            "title" => {
              "en" => "Title",
              "ca" => "Title",
              "es" => "Title"
            },
            "tos" => {
              "en" => "<p>TOS</p>",
              "ca" => "<p>TOS</p>",
              "es" => "<p>TOS</p>"
            },
            "description" => {
              "en" => "<p>Content</p>",
              "ca" => "<p>Contingut</p>",
              "es" => "<p>Contenido</p>"
            },
            "questions" => {
              "0" => {
                "body" => {
                  "en" => "First question",
                  "ca" => "Primera pregunta",
                  "es" => "Primera pregunta"
                },
                "position" => "0",
                "question_type" => "short_answer",
                "answer_options" => {}
              },
              "1" => {
                "body" => {
                  "en" => "Second question",
                  "ca" => "Segona pregunta",
                  "es" => "Segunda pregunta"
                },
                "description" => { "en" => "Description" },
                "position" => "1",
                "mandatory" => "1",
                "question_type" => "long_answer",
                "answer_options" => {}
              },
              "2" => {
                "body" => {
                  "en" => "Third question",
                  "ca" => "Tercera pregunta",
                  "es" => "Tercera pregunta"
                },
                "position" => "2",
                "question_type" => "single_option",
                "answer_options" => {
                  "0" => {
                    "body" => {
                      "en" => "First answer",
                      "ca" => "Primera resposta",
                      "es" => "Primera respuesta"
                    }
                  },
                  "1" => {
                    "body" => {
                      "en" => "Second answer",
                      "ca" => "Segona resposta",
                      "es" => "Segunda respuesta"
                    }
                  }
                }
              }
            },
            "published_at" => published_at
          }
        end
        let(:form) do
          SurveyForm.from_params(
            survey: form_params
          ).with_context(
            current_organization: current_organization
          )
        end
        let(:command) { described_class.new(form, survey) }

        describe "when the form is invalid" do
          before do
            expect(form).to receive(:invalid?).and_return(true)
          end

          it "broadcasts invalid" do
            expect { command.call }.to broadcast(:invalid)
          end

          it "doesn't update the survey" do
            expect(survey).not_to receive(:update!)
            command.call
          end
        end

        describe "when the form is valid" do
          it "broadcasts ok" do
            expect { command.call }.to broadcast(:ok)
          end

          it "updates the survey" do
            command.call
            survey.reload

            expect(survey.description["en"]).to eq("<p>Content</p>")
            expect(survey.questions.length).to eq(3)

            survey.questions.each_with_index do |question, idx|
              expect(question.body["en"]).to eq(form_params["questions"][idx.to_s]["body"]["en"])
            end

            expect(survey.questions[1]).to be_mandatory
            expect(survey.questions[1].description["en"]).to eq(form_params["questions"]["1"]["description"]["en"])
            expect(survey.questions[1].question_type).to eq("long_answer")
            expect(survey.questions[2].answer_options[1]["body"]["en"]).to eq(form_params["questions"]["2"]["answer_options"]["1"]["body"]["en"])
          end
        end

        describe "when the survey has an existing question" do
          let!(:survey_question) { create(:survey_question, survey: survey) }

          context "and the question should be removed" do
            let(:form_params) do
{'fraction_non_alphanumeric': 0.14108910891089108, 'fraction_numerical': 0.004304778303917348, 'mean_word_length': 1.3372736418511066, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4046604', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2469, 'n_words': 494}