You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # Copyright 2017 David Vidal <david.vidal@tecnativa.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo.tests.common import at_install, post_install, SavepointCase
  6. @at_install(True)
  7. @post_install(True)
  8. class TestMassMailingEvent(SavepointCase):
  9. @classmethod
  10. def setUpClass(cls):
  11. super(TestMassMailingEvent, cls).setUpClass()
  12. cls.event = cls.env['event.event'].create({
  13. 'name': 'Test event',
  14. 'date_begin': '2017-06-24 8:00:00',
  15. 'date_end': '2017-06-30 18:00:00',
  16. })
  17. cls.registration = cls.env['event.registration'].create({
  18. 'event_id': cls.event.id,
  19. 'email': 'partner_a@example.org',
  20. })
  21. cls.states_all = cls.env['event.registration.state'].search([])
  22. cls.state_confirmed = cls.env['event.registration.state'].search([
  23. ('code', '=', 'open')
  24. ])
  25. cls.contact_list = cls.env['mail.mass_mailing.list'].create({
  26. 'name': 'Test list',
  27. })
  28. cls.contact_a = cls.env['mail.mass_mailing.contact'].create({
  29. 'list_id': cls.contact_list.id,
  30. 'name': 'Test contact A',
  31. 'email': 'partner_a@example.org',
  32. })
  33. cls.contact_b = cls.env['mail.mass_mailing.contact'].create({
  34. 'list_id': cls.contact_list.id,
  35. 'name': 'Test contact B',
  36. 'email': 'partner_b@example.org',
  37. })
  38. cls.partner_a = cls.env['res.partner'].create({
  39. 'name': 'Test partner A',
  40. 'email': 'partner_a@example.org',
  41. })
  42. cls.partner_b = cls.env['res.partner'].create({
  43. 'name': 'Test partner B',
  44. 'email': 'partner_b@example.org',
  45. })
  46. def test_mailing_contact(self):
  47. domain = [
  48. ('list_id', 'in', [self.contact_list.id]),
  49. ('opt_out', '=', False),
  50. ]
  51. mass_mailing = self.env['mail.mass_mailing'].create({
  52. 'name': 'Test subject',
  53. 'email_from': 'from@example.com',
  54. 'mailing_model': 'mail.mass_mailing.contact',
  55. 'mailing_domain': str(domain),
  56. 'contact_list_ids': [(6, 0, [self.contact_list.id])],
  57. 'body_html': '<p>Test email body</p>',
  58. 'reply_to_mode': 'email',
  59. })
  60. mail_contact = self.env['mail.mass_mailing.contact'].with_context(
  61. exclude_mass_mailing=mass_mailing.id)
  62. self.assertEqual(
  63. [self.contact_a.id, self.contact_b.id],
  64. mass_mailing.get_recipients())
  65. self.assertEqual(2, mail_contact.search_count(domain))
  66. mass_mailing.write({
  67. 'event_id': self.event.id,
  68. 'exclude_event_state_ids': [(6, 0, self.states_all.ids)],
  69. })
  70. self.assertEqual(
  71. [self.contact_b.id],
  72. mass_mailing.get_recipients())
  73. self.assertEqual(1, mail_contact.search_count(domain))
  74. self.registration.state = 'draft'
  75. mass_mailing.write({
  76. 'exclude_event_state_ids': [(6, 0, self.state_confirmed.ids)],
  77. })
  78. self.assertEqual(
  79. [self.contact_a.id, self.contact_b.id],
  80. mass_mailing.get_recipients())
  81. self.assertEqual(2, mail_contact.search_count(domain))
  82. def test_mailing_partner(self):
  83. domain = [
  84. ('id', 'in', [self.partner_a.id, self.partner_b.id]),
  85. ('opt_out', '=', False),
  86. ]
  87. domain_reg = [
  88. ('event_id', '=', self.event.id),
  89. ]
  90. mass_mailing = self.env['mail.mass_mailing'].create({
  91. 'name': 'Test subject',
  92. 'email_from': 'from@example.com',
  93. 'mailing_model': 'res.partner',
  94. 'mailing_domain': str(domain),
  95. 'body_html': '<p>Test email body</p>',
  96. 'reply_to_mode': 'email',
  97. })
  98. mail_partner = self.env['res.partner'].with_context(
  99. exclude_mass_mailing=mass_mailing.id)
  100. mail_registration = self.env['event.registration'].with_context(
  101. exclude_mass_mailing=mass_mailing.id)
  102. self.assertEqual(
  103. [self.partner_a.id, self.partner_b.id],
  104. mass_mailing.get_recipients())
  105. self.assertEqual(2, mail_partner.search_count(domain))
  106. mass_mailing.write({
  107. 'event_id': self.event.id,
  108. 'exclude_event_state_ids': [(6, 0, self.states_all.ids)],
  109. })
  110. self.assertEqual(
  111. [self.partner_b.id],
  112. mass_mailing.get_recipients())
  113. self.assertEqual(1, mail_partner.search_count(domain))
  114. self.assertEqual(0, mail_registration.search_count(domain_reg))
  115. self.registration.state = 'draft'
  116. mass_mailing.write({
  117. 'exclude_event_state_ids': [(6, 0, self.state_confirmed.ids)],
  118. })
  119. self.assertEqual(
  120. [self.partner_a.id, self.partner_b.id],
  121. mass_mailing.get_recipients())
  122. self.assertEqual(2, mail_partner.search_count(domain))
  123. self.assertEqual(1, mail_registration.search_count(domain_reg))