| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from dateutil.relativedelta import relativedelta |
| 4 |
from freezegun import freeze_time |
| 5 |
from markupsafe import Markup |
| 6 |
|
| 7 |
from odoo import Command, fields |
| 8 |
from odoo.addons.survey.tests import common |
| 9 |
from odoo.tests import tagged |
| 10 |
from odoo.tests.common import HttpCase |
| 11 |
|
| 12 |
|
| 13 |
@tagged('-at_install', 'post_install', 'functional') |
| 14 |
class TestCertificationFlow(common.TestSurveyCommon, HttpCase): |
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
super().setUpClass() |
| 18 |
cls.employee_emp = cls.env['hr.employee'].create({ |
| 19 |
'user_id': cls.user_emp.id, |
| 20 |
}) |
| 21 |
cls.certification = cls.env['survey.survey'].create({ |
| 22 |
'access_mode': 'public', |
| 23 |
'certification': True, |
| 24 |
'certification_validity_months': 3, |
| 25 |
'description': '<p>Description</p>', |
| 26 |
'questions_layout': 'page_per_question', |
| 27 |
'scoring_success_min': 85.0, |
| 28 |
'scoring_type': 'scoring_without_answers', |
| 29 |
'title': 'Test Certification', |
| 30 |
'users_login_required': True, |
| 31 |
}) |
| 32 |
cls.certification_q0 = cls._add_question( |
| 33 |
cls, None, 'Test question ...', 'simple_choice', |
| 34 |
sequence=2, |
| 35 |
constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=cls.certification.id, |
| 36 |
labels=[ |
| 37 |
{'value': 'Correct', 'is_correct': True, 'answer_score': 1.0}, |
| 38 |
{'value': 'Incorrect'}, |
| 39 |
]) |
| 40 |
|
| 41 |
@freeze_time('2024-03-20') |
| 42 |
def test_resume_line_creation(self): |
| 43 |
""" Check that the resume line is correctly created upon certification completion. |
| 44 |
|
| 45 |
As we test the method "survey.user_input._mark_done" which is called in sudo from |
| 46 |
the controller, the test is executed with the admin user. |
| 47 |
""" |
| 48 |
ResumeLine = self.env['hr.resume.line'] |
| 49 |
user_input_vals = { |
| 50 |
'survey_id': self.certification.id, |
| 51 |
'partner_id': self.user_emp.partner_id.id, |
| 52 |
'user_input_line_ids': [Command.create({ |
| 53 |
'question_id': self.certification_q0.id, |
| 54 |
'answer_type': 'suggestion', |
| 55 |
'suggested_answer_id': self.certification_q0.suggested_answer_ids.ids[0] |
| 56 |
})] |
| 57 |
} |
| 58 |
self.env['survey.user_input'].create(user_input_vals)._mark_done() |
| 59 |
resume_line = ResumeLine.search([('survey_id', '=', self.certification.id)], limit=1, order='id DESC') |
| 60 |
self.assertEqual(resume_line.employee_id, self.employee_emp) |
| 61 |
self.assertEqual(resume_line.name, self.certification.title) |
| 62 |
self.assertEqual(resume_line.description, Markup('<p>Description</p>')) |
| 63 |
self.assertEqual(resume_line.line_type_id, self.env.ref('hr_skills_survey.resume_type_certification')) |
| 64 |
self.assertEqual(resume_line.survey_id, self.certification) |
| 65 |
self.assertEqual(resume_line.date_start, fields.Date.today()) |
| 66 |
self.assertEqual(resume_line.date_end, fields.Date.today() + relativedelta(months=3)) |
| 67 |
# When redoing the same certification, the resume line is updated |
| 68 |
self.certification.description = False |
| 69 |
for (validity_months, expected_date_end) in ( |
| 70 |
(1, fields.Date.today() + relativedelta(months=1)), |
| 71 |
(6, fields.Date.today() + relativedelta(months=6)), |
| 72 |
(False, False), |
| 73 |
): |
| 74 |
self.certification.certification_validity_months = validity_months |
| 75 |
self.env['survey.user_input'].create(user_input_vals)._mark_done() |
| 76 |
resume_line = ResumeLine.search([('survey_id', '=', self.certification.id)], order='id DESC') |
| 77 |
self.assertEqual(len(resume_line), 1) |
| 78 |
self.assertEqual(resume_line.date_start, fields.Date.today()) |
| 79 |
self.assertEqual(resume_line.date_end, expected_date_end) |
| 80 |
self.assertEqual(resume_line.description, '') |
| 81 |
# Mark as done in batch 2 certifications for the same employee |
| 82 |
certification2 = self.certification.copy() |
| 83 |
self.certification.description = 'Description 1' |
| 84 |
certification2.description = 'Description 2' |
| 85 |
certification2.certification_validity_months = 9 |
| 86 |
self.env['survey.user_input'].create([ |
| 87 |
user_input_vals, |
| 88 |
{ |
| 89 |
'survey_id': certification2.id, |
| 90 |
'partner_id': self.user_emp.partner_id.id, |
| 91 |
'user_input_line_ids': [Command.create({ |
| 92 |
'question_id': certification2.question_ids[0].id, |
| 93 |
'answer_type': 'suggestion', |
| 94 |
'suggested_answer_id': certification2.question_ids[0].suggested_answer_ids.ids[0] |
| 95 |
})] |
| 96 |
} |
| 97 |
])._mark_done() |
| 98 |
cert_1_resume_line = ResumeLine.search([('survey_id', '=', self.certification.id)], order='id DESC') |
| 99 |
cert_2_resume_line = ResumeLine.search([('survey_id', '=', certification2.id)], order='id DESC') |
| 100 |
self.assertEqual(cert_1_resume_line.description, Markup('<p>Description 1</p>')) |
| 101 |
self.assertFalse(cert_1_resume_line.date_end) |
| 102 |
self.assertEqual(cert_2_resume_line.description, Markup('<p>Description 2</p>')) |
| 103 |
self.assertEqual(cert_2_resume_line.date_end, fields.Date.today() + relativedelta(months=9)) |