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.

138 lines
5.0 KiB

  1. # Copyright 2017 Tecnativa - Jairo Llopis
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from mock import patch
  4. from odoo.exceptions import ValidationError
  5. from odoo.tests import common
  6. @common.at_install(False)
  7. @common.post_install(True)
  8. class DynamicListCase(common.SavepointCase):
  9. @classmethod
  10. def setUpClass(cls):
  11. super(DynamicListCase, cls).setUpClass()
  12. cls.tag = cls.env["res.partner.category"].create({
  13. "name": "testing tag",
  14. })
  15. cls.partners = cls.env["res.partner"]
  16. for number in range(5):
  17. cls.partners |= cls.partners.create({
  18. "name": "partner %d" % number,
  19. "category_id": [(4, cls.tag.id, False)],
  20. "email": "%d@example.com" % number,
  21. })
  22. cls.list = cls.env["mail.mass_mailing.list"].create({
  23. "name": "test list",
  24. "dynamic": True,
  25. "sync_domain": repr([("category_id", "in", cls.tag.ids)]),
  26. })
  27. cls.mail = cls.env["mail.mass_mailing"].create({
  28. "name": "test mass mailing",
  29. "contact_list_ids": [(4, cls.list.id, False)],
  30. })
  31. cls.mail._onchange_model_and_list()
  32. def test_list_sync(self):
  33. """List is synced correctly."""
  34. Contact = self.env["mail.mass_mailing.contact"]
  35. # Partner 0 is not categorized
  36. self.partners[0].category_id = False
  37. # Partner 1 has no email
  38. self.partners[1].email = False
  39. # Set list as unsynced
  40. self.list.dynamic = False
  41. # Create contact for partner 0 in unsynced list
  42. contact0 = Contact.create({
  43. "list_ids": [(4, self.list.id)],
  44. "partner_id": self.partners[0].id,
  45. })
  46. self.assertEqual(self.list.contact_nbr, 1)
  47. # Set list as add-synced
  48. self.list.dynamic = True
  49. self.list.action_sync()
  50. self.assertEqual(self.list.contact_nbr, 4)
  51. self.assertTrue(contact0.exists())
  52. # Set list as full-synced
  53. self.list.sync_method = "full"
  54. Contact.search([
  55. ("list_ids", "in", self.list.id),
  56. ("partner_id", "=", self.partners[2].id),
  57. ]).unlink()
  58. self.list.action_sync()
  59. self.assertEqual(self.list.contact_nbr, 3)
  60. self.assertFalse(contact0.exists())
  61. # Cannot add or edit contacts in fully synced lists
  62. with self.assertRaises(ValidationError):
  63. Contact.create({
  64. "list_ids": [(4, self.list.id)],
  65. "partner_id": self.partners[0].id,
  66. })
  67. contact1 = Contact.search([
  68. ("list_ids", "in", self.list.id),
  69. ], limit=1)
  70. with self.assertRaises(ValidationError):
  71. contact1.name = "other"
  72. with self.assertRaises(ValidationError):
  73. contact1.email = "other@example.com"
  74. with self.assertRaises(ValidationError):
  75. contact1.partner_id = self.partners[0]
  76. # Unset dynamic list
  77. self.list.dynamic = False
  78. # Now the contact is created without exception
  79. Contact.create({
  80. "list_ids": [(4, self.list.id)],
  81. "email": "test@example.com",
  82. })
  83. # Contacts can now be changed
  84. contact1.name = "other"
  85. def test_sync_when_sending_mail(self):
  86. """Check that list in synced when sending a mass mailing."""
  87. self.list.action_sync()
  88. self.assertEqual(self.list.contact_nbr, 5)
  89. # Create a new partner
  90. self.partners.create({
  91. "name": "extra partner",
  92. "category_id": [(4, self.tag.id, False)],
  93. "email": "extra@example.com",
  94. })
  95. # Mock sending low level method, because an auto-commit happens there
  96. with patch("odoo.addons.mail.models.mail_mail.MailMail.send") as s:
  97. self.mail.send_mail()
  98. self.assertEqual(1, s.call_count)
  99. self.assertEqual(6, self.list.contact_nbr)
  100. def test_load_filter(self):
  101. domain = "[('id', '=', 1)]"
  102. ir_filter = self.env['ir.filters'].create({
  103. 'name': 'Test filter',
  104. 'model_id': 'res.partner',
  105. 'domain': domain,
  106. })
  107. wizard = self.env['mail.mass_mailing.load.filter'].with_context(
  108. active_id=self.list.id,
  109. ).create({
  110. 'filter_id': ir_filter.id,
  111. })
  112. wizard.load_filter()
  113. self.assertEqual(self.list.sync_domain, domain)
  114. def test_change_partner(self):
  115. self.list.sync_method = 'full'
  116. self.list.action_sync()
  117. # This shouldn't fail
  118. self.partners[:1].write({
  119. 'email': 'test_mass_mailing_list_dynamic@example.org',
  120. })
  121. def test_is_synced(self):
  122. self.list.dynamic = False
  123. self.list._onchange_dynamic()
  124. # It shouldn't change when list is reversed to normal
  125. self.assertTrue(self.list.is_synced)
  126. self.list.dynamic = True
  127. self.list._onchange_dynamic()
  128. self.assertFalse(self.list.is_synced)
  129. self.list.action_sync()
  130. self.assertTrue(self.list.is_synced)