From 5b7b6e664b42a9d0080b6139d4bb769fbada4b38 Mon Sep 17 00:00:00 2001 From: ale-rt Date: Sun, 9 Feb 2020 12:44:01 +0100 Subject: [PATCH 1/2] MockMailHostLayer: clean up the registry on test tear down --- news/62.bugfix | 2 +- src/plone/app/testing/layers.py | 33 +++++++++++++++++++++++--------- src/plone/app/testing/layers.rst | 5 ++++- 3 files changed, 29 insertions(+), 11 deletions(-) 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 8304321..3603707 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. From 993e2fefbe58ea8585d5b01989c700b525d2a150 Mon Sep 17 00:00:00 2001 From: "Jens W. Klein" Date: Tue, 11 Feb 2020 17:25:09 +0100 Subject: [PATCH 2/2] fixes for Zope 4.2.1 --- src/plone/app/testing/helpers.rst | 8 ++++---- src/plone/app/testing/layers.rst | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/plone/app/testing/helpers.rst b/src/plone/app/testing/helpers.rst index b4742ec..7f1eac5 100644 --- a/src/plone/app/testing/helpers.rst +++ b/src/plone/app/testing/helpers.rst @@ -237,7 +237,7 @@ We'll now tear down just the ``HELPER_DEMOS_INTEGRATION_TESTING`` layer. At this point, we should still have a Plone site, but none of the persistent or component architecture changes from our layer. - >>> runner.tear_down_unneeded(options, [l for l in setupLayers if l not in (HELPER_DEMOS_INTEGRATION_TESTING, HELPER_DEMOS_FIXTURE,)], setupLayers) + >>> runner.tear_down_unneeded(options, [l for l in setupLayers if l not in (HELPER_DEMOS_INTEGRATION_TESTING, HELPER_DEMOS_FIXTURE,)], setupLayers, []) Tear down plone.app.testing.layers.HelperDemos:Integration in ... seconds. Tear down...HelperDemos in ... seconds. @@ -254,7 +254,7 @@ component architecture changes from our layer. Let's tear down the rest of the layers too. - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.PloneFixture in ... seconds. Tear down plone.testing.zope.Startup in ... seconds. Tear down plone.testing.zca.LayerCleanup in ... seconds. @@ -397,7 +397,7 @@ We'll now tear down just the ``MY_INTEGRATION_TESTING`` layer. At this point, we should still have a Plone site, but none of the changes from our layer. - >>> runner.tear_down_unneeded(options, [l for l in setupLayers if l not in (MY_INTEGRATION_TESTING, MY_FIXTURE)], setupLayers) + >>> runner.tear_down_unneeded(options, [l for l in setupLayers if l not in (MY_INTEGRATION_TESTING, MY_FIXTURE)], setupLayers, []) Tear down plone.app.testing.layers.MyLayer:Integration in ... seconds. >>> queryUtility(Interface, name="dummy1") is None @@ -436,7 +436,7 @@ layer. Let's tear down the rest of the layers too. - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.PloneFixture in ... seconds. Tear down plone.testing.zope.Startup in ... seconds. Tear down plone.testing.zca.LayerCleanup in ... seconds. diff --git a/src/plone/app/testing/layers.rst b/src/plone/app/testing/layers.rst index 3603707..3989062 100644 --- a/src/plone/app/testing/layers.rst +++ b/src/plone/app/testing/layers.rst @@ -98,7 +98,7 @@ There is no default workflow or content: Layer tear-down resets the environment. - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.PloneFixture in ... seconds. Tear down plone.testing.zope.Startup in ... seconds. Tear down plone.testing.zca.LayerCleanup in ... seconds. @@ -186,7 +186,7 @@ And the component site has been reset: Layer tear-down resets the environment. - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.Plone:Integration in ... seconds. Tear down plone.app.testing.layers.PloneFixture in ... seconds. Tear down plone.testing.zope.Startup in ... seconds. @@ -266,7 +266,7 @@ Along with the rest of the state: Layer tear-down resets the environment. - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.Plone:Functional in ... seconds. Tear down plone.app.testing.layers.PloneFixture in ... seconds. Tear down plone.testing.zope.Startup in ... seconds. @@ -367,7 +367,7 @@ Test tear-down does nothing beyond what the base layers do. When the server is torn down, the ZServer thread is stopped. - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.Plone:WSGIServer in ... seconds. Tear down plone.testing.zope.WSGIServer in ... seconds. Tear down plone.app.testing.layers.PloneFixture in ... seconds. @@ -472,7 +472,7 @@ and the registry is cleaned up: >>> registry["plone.email_from_address"] >>> registry["plone.email_from_name"] - >>> runner.tear_down_unneeded(options, [], setupLayers) + >>> runner.tear_down_unneeded(options, [], setupLayers, []) Tear down plone.app.testing.layers.MockMailHostLayer in ... seconds. Tear down plone.app.testing.layers.PloneFixture in ... seconds. Tear down plone.testing.zope.Startup in ... seconds.