2

背景:在我当前的项目中,我正在尝试使用 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 没有parent。这是我尝试使用 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>

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

4

2 回答 2

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 />在包中使用 -directive 时,您可以重新启用 z3c.autoinclude 为您的夹具setUpZope

    # Enable z3c.autoinclude
    configurationContext._features = set([
        feature for feature in configurationContext._features
        if feature != "disable-autoinclude"
    ])
于 2013-10-08T13:40:53.073 回答
2

我们找到了解决方案。

我们使用的自定义用户加法器是 SimpleItem,没有持久性。五.localsitemanager 试图获取实际上不可获取的东西的父级。将其更改为对象有效。

参考:https ://github.com/collective/collective.rcse/commit/f05d9e92bf4578ca82099eec714743903d181173

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

于 2013-10-08T15:09:07.287 回答