Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Members folder in plone.app.testing?

How can I add Members folder for my functional tests in plone.app.testing so that it is findable as in real site?

Have have set member area creation flag in my product installation step which I'm testing.

membership.memberareaCreationFlag = 1 

I need to get this test working:

class TestMemberFolder(unittest.TestCase):          
    layer = MY_FUNCTIONAL_TESTING         

    def setUp(self):                                
        portal = self.portal = self.layer['portal'] 

    def test_members_folder(self):

        membership = getToolByName(self.portal, 'portal_membership')  
        membership.addMember("basicuser", "secret", ["Member"], [])
        transaction.commit()

        login(self.portal, "basicuser")                               

        # This works just fine, because it was set by my product
        self.assertEquals(membership.memberareaCreationFlag, 1,       
            "memberareaCreationFlag must be 1 when it is enabled")    


        members_folder = membership.getMembersFolder()                
        # But this fails
        self.assertIsNotNone(members_folder)     
        # Also we should have the user folder here
        self.assertTrue(members_folder.hasObject('basicuser'))

I specifically need Member folder functionality. Just a folder owned by the test user does not cut it.

Also I tried creating new user with acl_users.userFolderAddUser, but that does not help neighter.

The memberareaCreationFlag works just fine in live Plone site.

like image 605
Epeli Avatar asked Oct 11 '22 15:10

Epeli


1 Answers

I finally figured it out.

At first membership.memberareaCreationFlag = 1 is not enough for enabling member folders.

It must be enabled with SecurityControlPanelAdapter in plone.app.controlpanel.security

from plone.app.controlpanel.security import ISecuritySchema
# Fetch the adapter
security_adapter =  ISecuritySchema(portal)
security_adapter.set_enable_user_folders(True)

Also the Functional testing fixture does not create the member folder automatically, but is possible to install it manually in your fixture class

class YourPloneFixture(PloneSandboxLayer):
    defaultBases = (PLONE_FIXTURE,)

    def setUpZope(self, app, configurationContext):

        # Required by Products.CMFPlone:plone-content
        z2.installProduct(app, 'Products.PythonScripts')

    def setUpPloneSite(self, portal):

        # Installs all the Plone stuff. Workflows etc.
        self.applyProfile(portal, 'Products.CMFPlone:plone')

        # Install portal content. Including the Members folder!
        self.applyProfile(portal, 'Products.CMFPlone:plone-content')

Finally as Member folders are created uppon user login, but the login helper function in plone.app.testing seem to be too low level for this. Login with zope.testbrowser seems to do the trick

browser = Browser(self.layer['app'])
browser.open(self.portal.absolute_url() + '/login_form')
browser.getControl(name='__ac_name').value = TEST_USER_NAME
browser.getControl(name='__ac_password').value = TEST_USER_PASSWOR
browser.getControl(name='submit').click()

Phew.

like image 168
Epeli Avatar answered Oct 14 '22 05:10

Epeli