我有一个基于 ATFolder 的内容类型:
ConceptSheetFolderSchema = folder.ATFolderSchema.copy()
ConceptSheetFolderSchema['title'].widget.label = _(u"Title")
ConceptSheetFolderSchema['title'].widget.description = _(u"")
ConceptSheetFolderSchema['title'].storage = atapi.AnnotationStorage()
ConceptSheetFolderSchema['description'].widget.label = _(u"Description")
ConceptSheetFolderSchema['description'].widget.description = _("")
ConceptSheetFolderSchema['description'].storage = atapi.AnnotationStorage()
finalizeATCTSchema(ConceptSheetFolderSchema, folderish=True, moveDiscussion=False)
class ConceptSheetFolder(folder.ATFolder):
"""
This is the central container for concept sheets in the site
"""
implements(IConceptSheetFolder)
portal_type = "Concept Sheet Folder"
_at_rename_after_creation = True
schema = ConceptSheetFolderSchema
title = atapi.ATFieldProperty('title')
description = atapi.ATFieldProperty('description')
atapi.registerType(ConceptSheetFolder, PROJECTNAME)
我可以通过 Plone 界面添加一个 ConceptSheetFolder 没有问题,但是我无法让这个基本测试工作:
class TestContent(unittest.TestCase):
layer = PROJECT_CONCEPTSHEETS_INTEGRATION_TESTING
def test_hierarchy(self):
portal = self.layer['portal']
# Ensure that we can create the various content types without error
setRoles(portal, TEST_USER_ID, ('Manager',))
portal.invokeFactory('Concept Sheet Folder', 'csf1', title=u"Concept Sheet folder")
portal['csf1'].invokeFactory('project.ConceptSheet', 'cs1', title=u"ConceptSheet")
portal['csf1']['cs1'].invokeFactory('project.ConceptMilestone', 'cs1', title=u"Approved")`
我得到一个错误
未授权:无法创建概念表文件夹
,当我尝试此测试时。我用 Google 搜索了一下,发现 this Nabble post ,引导我查看 Plone/CMFCore/TestTools.py 中的 isConstructionAllowed()。使用 pdb,我发现 ._queryFactoryMethod() 在此上下文中运行时返回“无”。
所以看起来这种类型的 FactoryTool 不工作,至少在测试中不工作。我在正常的 GenericSetup 位置(types.xml、Concept_Sheet_Folder.xml、factorytool.xml)进行了测试,我不知道还有什么可能导致这个问题。有什么想法吗?
额外的问题:为什么这在 Plone 界面中有效,但在测试中却无效?
编辑(2011 年 12 月 13 日):这是我的 Concept_Sheet_Folder.xml
<?xml version="1.0"?>
<object name="Concept Sheet Folder"
meta_type="Factory-based Type Information with dynamic views"
i18n:domain="iedea.conceptsheets" xmlns:i18n="http://xml.zope.org/namespaces/i18n">
<property name="title" i18n:translate="">Concept Sheet Folder</property>
<property name="description"
i18n:translate="">A folder which can contain concept sheets.</property>
<property name="content_icon">++resource++conceptsheetfolder_icon.gif</property>
<property name="content_meta_type">Concept Sheet Folder</property>
<property name="product">iedea.conceptsheets</property>
<property name="factory">addConceptSheetFolder</property>
<property name="immediate_view">atct_edit</property>
<property name="global_allow">True</property>
<property name="filter_content_types">True</property>
<property name="allowed_content_types">
<element value="Concept Sheet" />
</property>
<property name="allow_discussion">False</property>
<property name="default_view">view</property>
<property name="view_methods">
<element value="view"/>
</property>
<alias from="(Default)" to="(dynamic view)"/>
<alias from="edit" to="atct_edit"/>
<alias from="sharing" to="@@sharing"/>
<alias from="view" to="(selected layout)"/>
<action title="View" action_id="view" category="object" condition_expr=""
url_expr="string:${folder_url}/" visible="True">
<permission value="View"/>
</action>
<action title="Edit" action_id="edit" category="object" condition_expr=""
url_expr="string:${object_url}/edit" visible="True">
<permission value="Modify portal content"/>
</action>
</object>
最佳答案
我自己也遇到过这个问题。问题是当您尝试创建原型(prototype)时,您的原型(prototype)工厂尚未正确注册。
这就是 _queryFactoryMethod() 返回 None 的原因,正如您所发现的。
根据您是使用 Products.ZopeTestCase 还是更新的 plone.app.testing 作为您的测试框架,解决方案略有不同。
但是,在这两种情况下,您都需要确保已安装定义您尝试创建(通过 invokeFactory)的原型(prototype) (ConceptSheetFolder) 的附加产品。
使用 Products.ZopeTestCase 时:
如果您正在使用 Products.ZopeTestCase(和 Products.PloneTestCase),您需要调用
- Products.ZopeTestCase.installProduct
您需要确保您的 installProduct 调用不会被推迟到您的测试被调用之后。
在 Plone 4 中,这意味着您的 installProduct 调用不应在 @onsetup 装饰函数中(尽管这在 Plone 3 中仍然有效)。
这个邮件列表讨论可能会进一步澄清问题:
使用 plone.app.testing 时:
如果您正在使用 plone.app.testing,您应该调用:
- plone.testing.z2.installProduct
这应该在您从 PloneSandboxLayer 覆盖的 setUpZope 方法中完成。
有关详细信息,请阅读 plone.app.testing.helpers.py 中 setUpZope 下的说明(第 257 行) https://github.com/plone/plone.app.testing/blob/2ef789f8173c695179b043fd4634e0bdb6567511/plone/app/testing/helpers.py
关于plone - Plone测试中出现未经授权的错误,尽管可以通过网络添加内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8296005/