diff --git a/news/62.bugfix b/news/62.bugfix index 4a8304d..ec5183e 100644 --- a/news/62.bugfix +++ b/news/62.bugfix @@ -1 +1 @@ -Properly configure the mail sender setting the appropriate registry records (Fixes #62) +MockMailHostLayer configures the mail sender setting the appropriate registry records (Fixes #62) diff --git a/src/plone/app/testing/layers.py b/src/plone/app/testing/layers.py index b6d8a96..a250b21 100644 --- a/src/plone/app/testing/layers.py +++ b/src/plone/app/testing/layers.py @@ -388,28 +388,43 @@ def testSetUp(self): with zope.zopeApp() as app: portal = app[PLONE_SITE_ID] registry = getUtility(IRegistry, context=portal) + if not registry["plone.email_from_address"]: + portal._original_email_address = registry["plone.email_from_address"] # noqa: E501 registry["plone.email_from_address"] = "noreply@example.com" + if not registry["plone.email_from_name"]: + portal._original_email_name = registry["plone.email_from_name"] registry["plone.email_from_name"] = u"Plone site" + portal._original_MailHost = portal.MailHost portal.MailHost = mailhost = MockMailHost('MailHost') + sm = getSiteManager(context=portal) sm.unregisterUtility(provided=IMailHost) sm.registerUtility(mailhost, provided=IMailHost) def testTearDown(self): + with zope.zopeApp() as app: portal = app[PLONE_SITE_ID] - _o_mailhost = getattr(portal, '_original_MailHost', None) - if _o_mailhost: - portal.MailHost = portal._original_MailHost - sm = getSiteManager(context=portal) - sm.unregisterUtility(provided=IMailHost) - sm.registerUtility( - aq_base(portal._original_MailHost), - provided=IMailHost - ) + registry = getUtility(IRegistry, context=portal) + + portal.MailHost = portal._original_MailHost + + sm = getSiteManager(context=portal) + sm.unregisterUtility(provided=IMailHost) + sm.registerUtility(aq_base(portal.MailHost), provided=IMailHost) + + if hasattr(portal, "_original_email_name"): + registry["plone.email_from_name"] = portal._original_email_name + delattr(portal, "_original_email_name") + + if hasattr(portal, "_original_email_address"): + registry["plone.email_from_address"] = portal._original_email_address # noqa: E501 + delattr(portal, "_original_email_address") + + delattr(portal, "_original_MailHost") MOCK_MAILHOST_FIXTURE = MockMailHostLayer() diff --git a/src/plone/app/testing/layers.rst b/src/plone/app/testing/layers.rst index 602931a..3989062 100644 --- a/src/plone/app/testing/layers.rst +++ b/src/plone/app/testing/layers.rst @@ -457,7 +457,8 @@ The list can be reset: ... portal.MailHost.messages [] -When the test is torn down the original MaiHost is restored: +When the test is torn down the original MaiHost is restored +and the registry is cleaned up: >>> layers.MOCK_MAILHOST_FIXTURE.testTearDown() >>> zope.STARTUP.testTearDown() @@ -469,6 +470,8 @@ When the test is torn down the original MaiHost is restored: ... AttributeError: 'RequestContainer' object has no attribute 'messages' + >>> registry["plone.email_from_address"] + >>> registry["plone.email_from_name"] >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.MockMailHostLayer in ... seconds. Tear down plone.app.testing.layers.PloneFixture in ... seconds.