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.

139 lines
5.0 KiB

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