Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/zope-4.2.1' into zope-4.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Feb 12, 2020
2 parents 78514da + 993e2fe commit 48608c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 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)
33 changes: 24 additions & 9 deletions src/plone/app/testing/layers.py
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion src/plone/app/testing/layers.rst
Expand Up @@ -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()
Expand All @@ -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.
Expand Down

0 comments on commit 48608c6

Please sign in to comment.