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.

123 lines
4.8 KiB

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