Plone.app.testing - rolemap from an other addon not loaded in Tests?

my.otheraddon contain a rolemap.xml with a role "Tester". I load this in the layer method setUpPloneSite - but the role from the package 'my.otheraddon' is not available. is this the wrong way in tests? or what is missing? does anybody know more?

# -*- coding: utf-8 -*-

# testing.py

from plone import api
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import IntegrationTesting
from plone.app.testing import PloneSandboxLayer
from plone.testing import zope


class MyAddonLayer(PloneSandboxLayer):

    defaultBases = (PLONE_APP_CONTENTTYPES_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        import plone.app.contenttypes
        import my.otheraddon
        import my.addon
        self.loadZCML(package=plone.app.contenttypes)
        self.loadZCML(package=my.otheraddon)
        self.loadZCML(package=my.addon)

    def setUpPloneSite(self, portal):
        applyProfile(portal, "plone.app.contenttypes:default")
        applyProfile(portal, "my.otheraddon:default")
        applyProfile(portal, "my.addon:default")


MY_ADDON_FIXTURE = MyAddonLayer()


MY_ADDON_INTEGRATION_TESTING = IntegrationTesting(
    bases=(MY_ADDON_FIXTURE,),
    name="MyAddonLayer:IntegrationTesting",
)
# -*- coding: utf-8 -*-
from my.addon.testing import MY_ADDON_INTEGRATION_TESTING  # noqa: E501

import unittest


class TestRolesAndPermissions(unittest.TestCase):
    """Test that permissions installed and configured."""

    layer = MY_ADDON_INTEGRATION_TESTING

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer["portal"]

    def test_roles(self):
        """Test roles."""
        # Fails! Why?
        # The Role 'Tester' is in the rolemap.xml        
        self.assertTrue('Tester' in portal.valid_roles())
        
       # print(portal.valid_roles()) --> ('Anonymous', 'Authenticated', 'Contributor', 'Editor', 'Manager', 'Member', 'Owner', 'Reader', 'Reviewer', 'Site Administrator')    

You need to load the GenericSetup profile of your package in the Layer:

from plone.app.testing import applyProfile
# ...
    def setUpPloneSite(self, portal):
        applyProfile(portal, "my.addon:default")

Also, you do not need to load the plone.app.contenttypes ZCML, this was already done by the layer used.
And: Do only load your own ZCML here and ensure your main configure ZCML explicitly loads the my.otheraddon ZCML. (Same for profiles: those should be loaded in your profiles metadata.xml )

1 Like

Plone Foundation Code of Conduct