From bea0b3d38754383ae5ebfd818156df1c6a3fd742 Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Thu, 12 Sep 2024 14:55:01 +0200 Subject: [PATCH] You can pass a distribution_name to factory.addPloneSite. We then pass all other arguments and keyword arguments to the `plone.distribution` site api. --- Products/CMFPlone/factory.py | 26 +++++++++++++++++++++++++ Products/CMFPlone/tests/configure.zcml | 12 ++++++++++++ Products/CMFPlone/tests/test_factory.py | 25 ++++++++++++++++++++++++ news/3961.feature.1 | 3 +++ 4 files changed, 66 insertions(+) create mode 100644 news/3961.feature.1 diff --git a/Products/CMFPlone/factory.py b/Products/CMFPlone/factory.py index ef8aa065bd..4b652b0e5c 100644 --- a/Products/CMFPlone/factory.py +++ b/Products/CMFPlone/factory.py @@ -138,8 +138,34 @@ def addPloneSite( setup_content=None, default_language="en", portal_timezone="UTC", + distribution_name=None, + **kwargs, ): """Add a PloneSite to the context.""" + if distribution_name: + from plone.distribution.api import site as site_api + + # Pass all arguments and keyword arguments in the answers, + # But the 'distribution_name' is not needed there. + answers = { + "site_id": site_id, + "title": title, + "description": description, + "profile_id": profile_id, + "snapshot": snapshot, + "content_profile_id": content_profile_id, + "extension_ids": extension_ids, + "setup_content": setup_content, + "default_language": default_language, + "portal_timezone": portal_timezone, + } + answers.update(kwargs) + site = site_api._create_site( + context=context, distribution_name=distribution_name, answers=answers + ) + setSite(site) + return site + if content_profile_id is not None: warnings.warn( "addPloneSite ignores the content_profile_id keyword argument " diff --git a/Products/CMFPlone/tests/configure.zcml b/Products/CMFPlone/tests/configure.zcml index bd6ca7c2db..1f730fd5c1 100644 --- a/Products/CMFPlone/tests/configure.zcml +++ b/Products/CMFPlone/tests/configure.zcml @@ -1,6 +1,8 @@ @@ -11,4 +13,14 @@ permission="zope2.View" /> + + + + + diff --git a/Products/CMFPlone/tests/test_factory.py b/Products/CMFPlone/tests/test_factory.py index bcbf875394..79e441a9ba 100644 --- a/Products/CMFPlone/tests/test_factory.py +++ b/Products/CMFPlone/tests/test_factory.py @@ -1,3 +1,4 @@ +from importlib.metadata import distribution from plone.dexterity.interfaces import IDexterityFTI from plone.registry.interfaces import IRegistry from Products.CMFPlone.factory import addPloneSite @@ -9,6 +10,13 @@ import unittest +try: + distribution("plone.distribution") + HAS_DISTRIBUTION = True +except PackageNotFoundError: + HAS_DISTRIBUTION = False + + class TestFactoryPloneSite(unittest.TestCase): layer = PRODUCTS_CMFPLONE_INTEGRATION_TESTING @@ -67,3 +75,20 @@ def test_site_creation_title_is_set_in_registry(self): ploneSite = addPloneSite(self.app, "ploneFoo", title="Foo") registry = getUtility(IRegistry, context=ploneSite) self.assertEqual(registry["plone.site_title"], "Foo") + + @unittest.skipIf( + not HAS_DISTRIBUTION, + "Passing a distribution_name needs plone.distribution.", + ) + def test_site_creation_distribution(self): + """Create a Plone Site using a distribution""" + ploneSite = addPloneSite( + self.app, + "ploneFoo", + title="Foo", + distribution_name="testdistro", + default_language="nl", + ) + self.assertEqual(ploneSite.getId(), "ploneFoo") + self.assertEqual(ploneSite.title, "Foo") + self.assertEqual(ploneSite.Language(), "nl") diff --git a/news/3961.feature.1 b/news/3961.feature.1 new file mode 100644 index 0000000000..da7c79fa52 --- /dev/null +++ b/news/3961.feature.1 @@ -0,0 +1,3 @@ +You can pass a `distribution_name` to `factory.addPloneSite`. +We then pass all other arguments and keyword arguments to the `plone.distribution` site api. +[maurits]