esamatti
esamatti

Reputation: 18944

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.

Upvotes: 5

Views: 574

Answers (3)

esamatti
esamatti

Reputation: 18944

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.

Upvotes: 4

user2665694
user2665694

Reputation:

Something like

  def setUpPloneSite(self, portal):
        # Install into Plone site using portal_setup
        quickInstallProduct(portal, 'Products.DataGridField')
        quickInstallProduct(portal, 'Products.ATVocabularyManager')
        quickInstallProduct(portal, 'Products.MasterSelectWidget')
        if HAVE_LP:
            quickInstallProduct(portal, 'Products.LinguaPlone')
        applyProfile(portal, 'vs.org:default')
        portal.acl_users.userFolderAddUser('god', 'dummy', ['Manager'], []) 
        setRoles(portal, 'god', ['Manager'])
        login(portal, 'god')

is perfectly working for us.

Upvotes: 0

Matthew Wilkes
Matthew Wilkes

Reputation: 1088

self.assert_ isn't a testing method, use something like self.assertTrue, or self.assertIsNotNone.

To add members folders just turn on member folder creation and add a new user.

Upvotes: 0

Related Questions