test_onboarding.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.onboarding.tests.common import TestOnboardingCommon
  4. class TestOnboarding(TestOnboardingCommon):
  5. def test_onboarding_completion_global(self):
  6. # Completing onboarding as company_1
  7. self.assertEqual(self.env.company, self.company_1)
  8. self.assertDictEqual(
  9. self.onboarding_1.current_progress_id._get_and_update_onboarding_state(),
  10. {self.onboarding_1_step_1.id: 'not_done', self.onboarding_1_step_2.id: 'not_done'})
  11. self.onboarding_1_step_1.action_set_just_done()
  12. # Test completed step state consolidation from `just_done` to `done`
  13. self.assertDictEqual(
  14. self.onboarding_1.current_progress_id._get_and_update_onboarding_state(),
  15. {self.onboarding_1_step_1.id: 'just_done', self.onboarding_1_step_2.id: 'not_done'})
  16. self.assertDictEqual(
  17. self.onboarding_1.current_progress_id._get_and_update_onboarding_state(),
  18. {self.onboarding_1_step_1.id: 'done', self.onboarding_1_step_2.id: 'not_done'})
  19. self.assert_step_is_done(self.onboarding_1_step_1, self.company_2)
  20. self.assert_onboarding_is_not_done(self.onboarding_1, self.company_2)
  21. self.onboarding_1_step_2.action_set_just_done()
  22. self.assert_step_is_done(self.onboarding_1_step_2, self.company_2)
  23. self.assert_onboarding_is_done(self.onboarding_1, self.company_2)
  24. # Once onboarding is done, a key 'onboarding_state' is added to the rendering values
  25. self.assertDictEqual(
  26. self.onboarding_1.current_progress_id._get_and_update_onboarding_state(),
  27. {self.onboarding_1_step_1.id: 'done', self.onboarding_1_step_2.id: 'just_done', 'onboarding_state': 'just_done'})
  28. # Consolidate values
  29. self.assertDictEqual(
  30. self.onboarding_1.current_progress_id._get_and_update_onboarding_state(),
  31. {self.onboarding_1_step_1.id: 'done', self.onboarding_1_step_2.id: 'done', 'onboarding_state': 'done'})
  32. self.onboarding_1.current_progress_id.action_close()
  33. self.assertTrue(self.onboarding_1.current_progress_id.is_onboarding_closed)
  34. # Adding new step resets onboarding state to 'not_done' even if closed
  35. onboarding_1_step_3 = self.env['onboarding.onboarding.step'].create({
  36. 'title': 'Test Onboarding 1 - Step 3',
  37. 'onboarding_id': self.onboarding_1.id,
  38. 'panel_step_open_action_name': 'action_fake_open_onboarding_step',
  39. })
  40. self.assert_step_is_not_done(onboarding_1_step_3)
  41. self.assert_onboarding_is_not_done(self.onboarding_1)
  42. # Completing it sets onboarding state to done again
  43. onboarding_1_step_3.action_set_just_done()
  44. self.assert_onboarding_is_done(self.onboarding_1)
  45. # If a company is added, onboarding is 'done'
  46. company_3 = self.env.company.create({
  47. 'currency_id': self.env.ref('base.EUR').id,
  48. 'name': 'Another Test Company',
  49. })
  50. self.assert_onboarding_is_done(self.onboarding_1.with_company(company_3))
  51. # Adding new step resets onboarding state to 'not_done'
  52. self.env['onboarding.onboarding.step'].create({
  53. 'title': 'Test Onboarding 1 - Step 4',
  54. 'onboarding_id': self.onboarding_1.id,
  55. 'panel_step_open_action_name': 'action_fake_open_onboarding_step',
  56. })
  57. # Closing the panel still allows to track if all steps are completed
  58. self.onboarding_1.action_close()
  59. self.assertTrue(self.onboarding_1.current_progress_id.is_onboarding_closed)
  60. self.assert_onboarding_is_not_done(self.onboarding_1)
  61. def test_onboarding_completion_per_company(self):
  62. """Checks the behavior of onboarding and step states in multi-company setting:
  63. the onboarding state has to track the completion of each of its steps, global and
  64. per-company, to determine if whether it is completed.
  65. """
  66. # Completing onboarding as company_1
  67. self.assertEqual(self.env.company, self.company_1)
  68. # Updating onboarding to per-company
  69. self.onboarding_1.is_per_company = True
  70. # Required after progress reset (simulate role of controller)
  71. self.onboarding_1._search_or_create_progress()
  72. self.onboarding_1_step_1.action_set_just_done()
  73. self.assert_step_is_done(self.onboarding_1_step_1)
  74. self.onboarding_1_step_2.action_set_just_done()
  75. self.assert_onboarding_is_done(self.onboarding_1)
  76. # Completing onboarding as existing company_2
  77. self.activate_company(self.company_2)
  78. # First access from company_2
  79. self.onboarding_1._search_or_create_progress()
  80. # Blank state for company 2
  81. self.assert_step_is_not_done(self.onboarding_1_step_1)
  82. self.assert_onboarding_is_not_done(self.onboarding_1)
  83. # But no change for company 1
  84. self.assert_step_is_done(self.onboarding_1_step_1.with_company(self.company_1))
  85. self.assert_onboarding_is_done(self.onboarding_1.with_company(self.company_1))
  86. self.onboarding_1_step_1.action_set_just_done()
  87. self.assert_step_is_done(self.onboarding_1_step_1)
  88. self.assert_onboarding_is_not_done(self.onboarding_1)
  89. self.onboarding_1_step_2.with_company(self.company_2).action_set_just_done()
  90. self.assert_step_is_done(self.onboarding_1_step_2)
  91. self.assert_onboarding_is_done(self.onboarding_1)
  92. # is_onboarding_closed status is also company-independent
  93. self.onboarding_1.action_close()
  94. self.assertTrue(self.onboarding_1.current_progress_id.is_onboarding_closed)
  95. self.assertFalse(self.onboarding_1.with_company(self.company_1).current_progress_id.is_onboarding_closed)
  96. def test_onboarding_to_company_change(self):
  97. """Checks that changing onboarding to per-company resets completions states.
  98. """
  99. # Completing onboarding as company_1
  100. self.assertEqual(self.env.company, self.company_1)
  101. self.onboarding_1_step_1.action_set_just_done()
  102. self.onboarding_1_step_2.action_set_just_done()
  103. self.assert_onboarding_is_done(self.onboarding_1)
  104. # Updating onboarding to per-company
  105. self.onboarding_1.is_per_company = True
  106. # Required after progress reset (simulate role of controller)
  107. self.onboarding_1._search_or_create_progress()
  108. self.assert_onboarding_is_not_done(self.onboarding_1)
  109. def test_no_crash_on_multiple_progress_records(self):
  110. existing_progress = self.env['onboarding.progress'].search([
  111. ('onboarding_id', '=', self.onboarding_1.id), ('company_id', '=', False)
  112. ])
  113. self.assertEqual(len(existing_progress), 1)
  114. extra_progress = self.env['onboarding.progress'].create({
  115. 'onboarding_id': self.onboarding_1.id,
  116. 'company_id': False
  117. })
  118. self.env['onboarding.progress.step'].create([{
  119. 'step_id': self.onboarding_1_step_1.id,
  120. 'progress_id': progress.id
  121. } for progress in (existing_progress, extra_progress)
  122. ])
  123. nb_progress = self.env['onboarding.progress'].search([
  124. ('onboarding_id', '=', self.onboarding_1.id), ('company_id', '=', False)], count=True)
  125. nb_progress_steps = self.env['onboarding.progress.step'].search([
  126. ('step_id', '=', self.onboarding_1_step_1.id)], count=True)
  127. # Even though multiple onboarding progress (& steps) records exist
  128. self.assertEqual(nb_progress, 2)
  129. self.assertEqual(nb_progress_steps, 2)
  130. # no error is raised, and so we can interact
  131. _ = self.onboarding_1_step_1.current_progress_step_id
  132. self.onboarding_1_step_1.action_set_just_done()
  133. # Same with onboarding progress
  134. _ = self.onboarding_1.current_progress_id
  135. self.onboarding_1.action_close()