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.

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