如何使用Products.membrane插件设置plone测试

时间:2013-10-08 09:02:30

标签: plone

上下文:在我目前的项目中,我正在尝试使用plone.app.testing设置测试环境。 该代码可在github上找到:https://github.com/collective/collective.rcse

我正在尝试在设置中添加一些成员,但它引发了以下异常:

Traceback (most recent call last):
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.testing-3.9.7-py2.7.egg/zope/testing/testrunner/runner.py", line 366, in run_layer
    setup_layer(options, layer, setup_layers)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.testing-3.9.7-py2.7.egg/zope/testing/testrunner/runner.py", line 628, in setup_layer
    setup_layer(options, base, setup_layers)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.testing-3.9.7-py2.7.egg/zope/testing/testrunner/runner.py", line 633, in setup_layer
    layer.setUp()
  File "/Uses/toutpt/myproject/buildout-cache/eggs/plone.app.testing-4.2.2-py2.7.egg/plone/app/testing/helpers.py", line 343, in setUp
    self.setUpPloneSite(portal)
  File "/Uses/toutpt/myproject/src/collective.rcse/collective/rcse/testing.py", line 71, in setUpPloneSite
    self.create_user(portal, "simplemember1")
  File "/Uses/toutpt/myproject/src/collective.rcse/collective/rcse/testing.py", line 82, in create_user
    regtool.addMember(username, username)
  File "<string>", line 10, in addMember
  File "/Uses/toutpt/myproject/buildout-cache/eggs/plone.protect-2.0.2-py2.7.egg/plone/protect/utils.py", line 46, in _curried
    return callable(*args, **kw)
  File "<string>", line 10, in addMember
  File "/Uses/toutpt/myproject/buildout-cache/eggs/AccessControl-3.0.8-py2.7-macosx-10.6-x86_64.egg/AccessControl/requestmethod.py", line 70, in _curried
    return callable(*args, **kw)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.CMFCore-2.2.7-py2.7.egg/Products/CMFCore/RegistrationTool.py", line 160, in addMember
    membership.addMember(id, password, roles, domains, properties)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.PlonePAS-4.1.1-py2.7.egg/Products/PlonePAS/tools/membership.py", line 136, in addMember
    acl_users._doAddUser(id, password, roles, domains)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.PlonePAS-4.1.1-py2.7.egg/Products/PlonePAS/pas.py", line 42, in _doAddUser
    retval = _old_doAddUser(self, login, password, roles, domains)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.PluggableAuthService-1.10.0-py2.7.egg/Products/PluggableAuthService/PluggableAuthService.py", line 1004, in _doAddUser
    if useradder.doAddUser( login, password ):
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.membrane-2.1.9-py2.7.egg/Products/membrane/plugins/usermanager.py", line 283, in doAddUser
    adder = getCurrentUserAdder(self)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/Products.membrane-2.1.9-py2.7.egg/Products/membrane/utils.py", line 46, in getCurrentUserAdder
    name, adder = adders.next()
  File "/Uses/toutpt/myproject/buildout-cache/eggs/zope.component-3.9.5-py2.7.egg/zope/component/registry.py", line 172, in getUtilitiesFor
    for name, utility in self.utilities.lookupAll((), interface):
  File "/Uses/toutpt/myproject/buildout-cache/eggs/five.localsitemanager-2.0.5-py2.7.egg/five/localsitemanager/registry.py", line 77, in _uncached_lookupAll
    tmp_result[k] = _wrap(v, registry)
  File "/Uses/toutpt/myproject/buildout-cache/eggs/five.localsitemanager-2.0.5-py2.7.egg/five/localsitemanager/registry.py", line 143, in _wrap
    registry_site = registry_site.__parent__
AttributeError: 'BaseGlobalComponents' object has no attribute '__parent__'

调用此跟踪显示膜以添加成员但在尝试查找IUserAdder组件时,组件注册表会在以下代码中引发异常:

def _wrap(comp, registry):
    """Return an aq wrapped component with the site as the parent but
    only if the comp has an aq wrapper to begin with.
    """

    # If component is stored as a ComponentPathWrapper, we traverse to
    # the component using the stored path:
    if isinstance(comp, ComponentPathWrapper):
        comp = getSite().unrestrictedTraverse(comp.path)
        if IAcquirer.providedBy(comp):
            return _rewrap(comp)
        else:
            return comp

    # BBB: The primary reason for doing this sort of wrapping of
    # returned utilities is to support CMF tool-like functionality where
    # a tool expects its aq_parent to be the portal object. New code
    # (ie new utilities) should not rely on this predictability to
    # get the portal object and should search out an alternate means
    # (possibly retrieve the ISiteRoot utility). Although in most
    # cases getting at the portal object shouldn't be the required pattern
    # but instead looking up required functionality via other (possibly
    # local) components.

    if registry.__bases__ and IAcquirer.providedBy(comp):
        current_site = getSite()
        registry_site = Acquisition.aq_base(registry.__parent__)
        if not ISite.providedBy(registry_site):
            registry_site = registry_site.__parent__

        ...

在测试期间,registry_site没有父级。以下是我尝试使用PDB的内容:

(Pdb) registry_site
<BaseGlobalComponents test-stack-3>
(Pdb) ISite.providedBy(registry_site)
False
(Pdb) registry
<zope.component.globalregistry.GlobalAdapterRegistry object at 0x105499990>
(Pdb) getSite()
<PloneSite at /plone>
(Pdb) Acquisition.aq_base(registry.__parent__)
<BaseGlobalComponents test-stack-3>
(Pdb) registry.__bases__
(<zope.component.globalregistry.GlobalAdapterRegistry object at 0x1049238d0>,)
(Pdb) registry
<zope.component.globalregistry.GlobalAdapterRegistry object at 0x105499990>

因为它只在测试期间发生,这意味着我必须在测试设置中添加一些内容。

2 个答案:

答案 0 :(得分:3)

这应该是我们如何设置的:

def setUpZope(self, app, configurationContext):
    import collective.indexing
    import Products.membrane

    self.loadZCML(package=collective.indexing)
    self.loadZCML(package=Products.membrane)

    z2.installProduct(app, 'collective.indexing')
    z2.installProduct(app, 'Products.membrane')

    # + your dexterity.membrane product setup

def setUpPloneSite(self, portal):
    from zope.publisher.browser import TestRequest
    from zope.globalrequest import setRequest
    request = TestRequest()
    setRequest(request)

    # + your dexterity.membrane product policy
    # + create (and reindex) content (with dexterity.membrane)

    import transaction
    transaction commit()

然而,当其他所有方法都失败了,并且您正在尝试进行功能测试,而您在包中使用<includeDependencies /> - 指令时,可以在{{1}中为您的装置重新启用z3c.autoinclude与...:

setUpZope

答案 1 :(得分:2)

我们找到了解决方案。

我们使用的自定义用户加法器是SimpleItem而不是持久性的。 five.localsitemanager试图获得一些实际上无法获得的东西。将其更改为对象有效。

cf:https://github.com/collective/collective.rcse/commit/f05d9e92bf4578ca82099eec714743903d181173

不知道为什么它在测试之外工作。