Back to index

plone3  3.1.7
test_schemata.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 """
00026 """
00027 
00028 import operator
00029 
00030 from Products.Archetypes.tests.attestcase import ATTestCase
00031 from Products.Archetypes.atapi import *
00032 from Products.Archetypes.config import PKG_NAME
00033 from Products.Archetypes.Schema import Schemata
00034 from Products.Archetypes.Schema import getNames
00035 from Products.Archetypes.Field import StringField
00036 from Products.Archetypes.exceptions import SchemaException
00037 from Testing.ZopeTestCase import user_role
00038 from Products.CMFCore.permissions import ModifyPortalContent
00039 
00040 schema = BaseSchema
00041 
00042 class Dummy(BaseContent):
00043     schema = schema
00044 
00045 
00046 class SchemataTest( ATTestCase ):
00047 
00048     def afterSetUp(self):
00049         registerType(Dummy, 'Archetypes')
00050         content_types, constructors, ftis = process_types(listTypes(), PKG_NAME)
00051         self._dummy = Dummy(oid='dummy')
00052 
00053     def test_availschemata(self):
00054         dummy = self._dummy
00055         schemata = dummy.Schemata()
00056         keys = schemata.keys()
00057         keys.sort()
00058         self.assertEqual(keys, ['default', 'metadata'])
00059 
00060     def test_nameschemata(self):
00061         dummy = self._dummy
00062         schemata = dummy.Schemata()
00063         self.assertEqual(schemata['default'].getName(), 'default')
00064         self.assertEqual(schemata['metadata'].getName(), 'metadata')
00065 
00066     def test_baseschemata(self):
00067         dummy = self._dummy
00068         schemata = dummy.Schemata()
00069         base_names = getNames(schemata['default'])
00070         self.assertEqual(base_names, ['id', 'title'])
00071 
00072     def test_metaschemata(self):
00073         dummy = self._dummy
00074         schemata = dummy.Schemata()
00075         meta_names = getNames(schemata['metadata'])
00076         self.assertEqual(meta_names, ['allowDiscussion', 'subject',
00077                                       'description', 'location', 'contributors',
00078                                       'creators', 'effectiveDate',
00079                                       'expirationDate', 'language',
00080                                       'rights', 'creation_date',
00081                                       'modification_date'])
00082 
00083     def test_dupe_accessor_names_add(self):
00084         a = Schemata(fields=(StringField('foo',
00085                                          accessor='getSomething',
00086                                          edit_accessor='editSomething',
00087                                          mutator='setSomething',
00088                                          ),))
00089         b = Schemata(fields=(StringField('bar',
00090                                          accessor='getSomething',
00091                                          edit_accessor='editThat',
00092                                          mutator='setThat',
00093                                          ),))
00094         self.assertRaises(SchemaException, operator.add, a, b)
00095 
00096     def test_dupe_edit_accessor_names_add(self):
00097         a = Schemata(fields=(StringField('foo',
00098                                          accessor='getSomething',
00099                                          edit_accessor='editSomething',
00100                                          mutator='setSomething',
00101                                          ),))
00102         b = Schemata(fields=(StringField('bar',
00103                                          accessor='getThat',
00104                                          edit_accessor='editSomething',
00105                                          mutator='setThat',
00106                                          ),))
00107         self.assertRaises(SchemaException, operator.add, a, b)
00108 
00109     def test_dupe_mutator_names_add(self):
00110         a = Schemata(fields=(StringField('foo',
00111                                          accessor='getSomething',
00112                                          edit_accessor='editSomething',
00113                                          mutator='setSomething',
00114                                          ),))
00115         b = Schemata(fields=(StringField('bar',
00116                                          accessor='getThat',
00117                                          edit_accessor='editThat',
00118                                          mutator='setSomething',
00119                                          ),))
00120         self.assertRaises(SchemaException, operator.add, a, b)
00121 
00122     def test_dupe_primary_add(self):
00123         a = Schemata(fields=(StringField('foo', primary=True),))
00124         b = Schemata(fields=(StringField('bar', primary=True),))
00125         self.assertRaises(SchemaException, operator.add, a, b)
00126 
00127     def test_dupe_accessor_names_addField(self):
00128         a = Schemata(fields=(StringField('foo',
00129                                          accessor='getSomething',
00130                                          edit_accessor='editSomething',
00131                                          mutator='setSomething',
00132                                   ),))
00133         field = StringField('bar',
00134                             accessor='getSomething',
00135                             edit_accessor='editThat',
00136                             mutator='setThat',
00137                             )
00138         self.assertRaises(SchemaException, a.addField, field)
00139 
00140     def test_dupe_edit_accessor_names_addField(self):
00141         a = Schemata(fields=(StringField('foo',
00142                                          accessor='getSomething',
00143                                          edit_accessor='editSomething',
00144                                          mutator='setSomething',
00145                                          ),))
00146         field = StringField('bar',
00147                             accessor='getThat',
00148                             edit_accessor='editSomething',
00149                             mutator='setThat',
00150                             )
00151         self.assertRaises(SchemaException, a.addField, field)
00152 
00153     def test_dupe_mutator_names_addField(self):
00154         a = Schemata(fields=(StringField('foo',
00155                                          accessor='getSomething',
00156                                          edit_accessor='editSomething',
00157                                          mutator='setSomething',
00158                                          ),))
00159         field = StringField('bar',
00160                             accessor='getThat',
00161                             edit_accessor='editThat',
00162                             mutator='setSomething',
00163                             )
00164         self.assertRaises(SchemaException, a.addField, field)
00165 
00166     def test_dupe_primary_addField(self):
00167         a = Schemata(fields=(StringField('foo', primary=True),))
00168         field = StringField('bar', primary=True)
00169         self.assertRaises(SchemaException, a.addField, field)
00170 
00171     def test_editableFields(self):
00172         # Not a security test, but this is here because 'editableFields'
00173         # will return only fields the user is allowed to write.
00174         dummy = self._dummy.__of__(self.folder)
00175         dummy.manage_permission(ModifyPortalContent, (user_role,))
00176 
00177         # add test fields to schema
00178         fields = (
00179             StringField(
00180                 'f1',
00181                 mutator='setF1',
00182                 write_permission = ModifyPortalContent,
00183                 widget=StringWidget(visible={'edit': 'invisible'}),
00184             ),
00185             StringField('f2', 
00186                 mutator='setF2',
00187                 write_permission = ModifyPortalContent,
00188                 widget=StringWidget(visible={'edit': 'hidden'}),
00189             ),
00190         )
00191 
00192         for f in fields:
00193             dummy.schema.addField(f)
00194 
00195         # add dummy mutators to pass the test in 'editableFields'
00196         def dummy_mutator(instance, value):
00197             pass
00198 
00199         dummy.setF1 = dummy_mutator
00200         dummy.setF2 = dummy_mutator
00201 
00202         # get editable fields
00203         schemata = dummy.Schemata()['default']
00204         editable_field_ids = [f.getName() for f in \
00205             schemata.editableFields(dummy, visible_only=True)]
00206 
00207         self.failUnless('f1' not in editable_field_ids)
00208         self.failUnless('f2' in editable_field_ids)
00209 
00210 def test_suite():
00211     from unittest import TestSuite, makeSuite
00212     suite = TestSuite()
00213     suite.addTest(makeSuite(SchemataTest))
00214     return suite