From 1353cd86873f3a0e65f154d801ff0cf959632607 Mon Sep 17 00:00:00 2001 From: Florian Charlier Date: Thu, 9 Mar 2023 18:35:43 +0000 Subject: [PATCH] [FIX] survey: fix _compute_allowed_triggering_question_ids MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the method were called on several new questions from different surveys at the same time (can it happen?), allowed_triggering_question_ids would include questions from all surveys for each of them. A unit test of the method is also included. Introduced in b1d18562. Task-3226408 closes odoo/odoo#115930 X-original-commit: e506e8b049c61d5b772370792541b6875d5e5d72 Signed-off-by: Warnon Aurélien (awa) Signed-off-by: Florian Charlier (flch) --- addons/survey/models/survey_question.py | 3 +- addons/survey/tests/test_survey.py | 53 ++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/addons/survey/models/survey_question.py b/addons/survey/models/survey_question.py index b0bb39807ae..01f621907fa 100644 --- a/addons/survey/models/survey_question.py +++ b/addons/survey/models/survey_question.py @@ -311,7 +311,8 @@ class SurveyQuestion(models.Model): for question in conditional_questions: question_id = question._origin.id if not question_id: # New question - conditional_questions.allowed_triggering_question_ids = possible_trigger_questions + question.allowed_triggering_question_ids = possible_trigger_questions.filtered( + lambda q: q.survey_id.id == question.survey_id._origin.id) question.is_placed_before_trigger = False continue diff --git a/addons/survey/tests/test_survey.py b/addons/survey/tests/test_survey.py index de2fef2cd55..c072ba5b27a 100644 --- a/addons/survey/tests/test_survey.py +++ b/addons/survey/tests/test_survey.py @@ -3,13 +3,64 @@ from freezegun import freeze_time -from odoo import _, fields +from odoo import _, Command, fields from odoo.addons.survey.tests import common from odoo.tests.common import users class TestSurveyInternals(common.TestSurveyCommon): + @users('survey_manager') + def test_allowed_triggering_question_ids(self): + # Create 2 surveys, each with 3 questions, each with 2 suggested answers + survey_1, survey_2 = self.env['survey.survey'].create([ + {'title': 'Test Survey 1'}, + {'title': 'Test Survey 2'} + ]) + self.env['survey.question'].create([ + { + 'survey_id': survey_id, + 'title': f'Question {question_idx}', + 'question_type': 'simple_choice', + 'suggested_answer_ids': [ + Command.create({ + 'value': f'Answer {answer_idx}', + }) for answer_idx in range(2)], + } + for question_idx in range(3) + for survey_id in (survey_1 | survey_2).ids + ]) + survey_1_q_1, survey_1_q_2, _ = survey_1.question_ids + survey_2_q_1, survey_2_q_2, _ = survey_2.question_ids + + # Make sure they are already configured as conditionally triggered + survey_1_q_2.write({ + 'is_conditional': True, + 'triggering_question_id': survey_1_q_1, + 'triggering_answer_id': survey_1_q_1.suggested_answer_ids[0] + }) + survey_2_q_2.write({ + 'is_conditional': True, + 'triggering_question_id': survey_2_q_1, + 'triggering_answer_id': survey_2_q_1.suggested_answer_ids[0] + }) + + with self.subTest('Editing existing questions'): + # Only previous questions from the same survey + self.assertFalse(survey_1_q_2.allowed_triggering_question_ids & survey_2_q_2.allowed_triggering_question_ids) + self.assertEqual(survey_1_q_2.allowed_triggering_question_ids, survey_1_q_1) + self.assertEqual(survey_2_q_2.allowed_triggering_question_ids, survey_2_q_1) + + survey_1_new_question = self.env['survey.question'].new({'survey_id': survey_1, 'is_conditional': True}) + survey_2_new_question = self.env['survey.question'].new({'survey_id': survey_2, 'is_conditional': True}) + + with self.subTest('New questions'): + # New questions should be allowed to use any question with choices from the same survey + self.assertFalse(survey_1_new_question.allowed_triggering_question_ids + & survey_2_new_question.allowed_triggering_question_ids) + self.assertEqual(survey_1_new_question.allowed_triggering_question_ids.ids, survey_1.question_ids.ids) + self.assertEqual(survey_2_new_question.allowed_triggering_question_ids.ids, survey_2.question_ids.ids) + def test_answer_attempts_count(self): """ As 'attempts_number' and 'attempts_count' are computed using raw SQL queries, let us test the results. """