def test_auto_migration(self): from BTrees.OOBTree import OOBTree from plone.registry.registry import Registry, Records, _Records from plone.registry.record import Record from plone.registry import field # Create an "old-looking registry" registry = Registry() registry._records = Records(registry) registry._records.data = OOBTree() f = field.TextLine(title=u"Foo") record = Record(f, u"Bar") record.__dict__['field'] = f record.__dict__['value'] = u"Bar" registry._records.data['foo.bar'] = record # Attempt to access it value = registry['foo.bar'] # Migration should have happened self.assertEqual(value, u"Bar") self.assertEqual(registry.records['foo.bar'].field.title, u"Foo") self.assertEqual(registry.records['foo.bar'].value, u"Bar") self.assertFalse(isinstance(registry._records, Records)) self.assertTrue(isinstance(registry._records, _Records))
def test_import_collection_tuple_append(self): xml = """\ <registry> <record name="test.registry.field"> <value purge="false"> <element>b</element> <element>c</element> </value> </record> </registry> """ self.registry.records['test.registry.field'] = \ Record(field.Tuple(title=u"Simple record", value_type=field.TextLine(title=u"Val")), value=(u"a", u"b", )) context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} importRegistry(context) self.assertEquals(1, len(self.registry.records)) self.assertEquals(( u"a", u"b", u"c", ), self.registry['test.registry.field'])
def test_set_valid_registry_record(self): """Test that setting a valid registry record succeeds.""" registry = getUtility(IRegistry) registry.records['plone.api.plone_power'] = Record( field.TextLine(title=u"Plone's Power")) portal.set_registry_record('plone.api.plone_power', u'awesome') self.assertEqual(registry['plone.api.plone_power'], u'awesome')
def test_character_counter_registry_records_removed(self): title = u'Miscellaneous' step = self.get_upgrade_step(title) self.assertIsNotNone(step) registry = getUtility(IRegistry) BASE_REGISTRY = 'collective.nitf.controlpanel.INITFCharCountSettings.' records = [ BASE_REGISTRY + 'description_max_chars', BASE_REGISTRY + 'description_optimal_chars', BASE_REGISTRY + 'show_description_counter', BASE_REGISTRY + 'show_title_counter', BASE_REGISTRY + 'title_max_chars', BASE_REGISTRY + 'title_optimal_chars', ] # simulate state on previous version for r in records: registry.records[r] = Record(field.TextLine(title=u'Test')) self.assertIn(r, registry) # execute upgrade step and verify changes were applied self.execute_upgrade_step(step) for r in records: self.assertNotIn(r, registry)
def test_import_field_ref(self): xml = """\ <registry> <record name="test.registry.field.override"> <field ref="test.registry.field" /> <value>Another value</value> </record> </registry> """ context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} self.registry.records['test.registry.field'] = Record( field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Sample value") importRegistry(context) self.assertEquals(2, len(self.registry.records)) self.failUnless( IFieldRef.providedBy( self.registry.records['test.registry.field.override'].field)) self.assertEquals( u"Simple record", self.registry.records['test.registry.field.override'].field.title) self.assertEquals( u"value", self.registry.records['test.registry.field.override']. field.__name__) self.assertEquals(u"Another value", self.registry['test.registry.field.override'])
def test_import_overwrite_field_with_field(self): xml = """\ <registry> <record name="test.registry.field"> <field type="plone.registry.field.ASCIILine"> <default>Nada</default> <title>Simple record</title> </field> </record> </registry> """ self.registry.records['test.registry.field'] = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Old value") context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} importRegistry(context) self.assertEquals(1, len(self.registry.records)) self.failUnless( isinstance(self.registry.records['test.registry.field'].field, field.ASCIILine)) self.assertEquals( u"Simple record", self.registry.records['test.registry.field'].field.title) self.assertEquals("Nada", self.registry['test.registry.field'])
def test_import_overwrite_field_with_interface(self): xml = """\ <registry> <record name="test.registry.field" interface="plone.app.registry.tests.data.ITestSettingsDisallowed" field="age" /> </registry> """ self.registry.records['test.registry.field'] = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Old value") context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} importRegistry(context) self.assertEquals(1, len(self.registry.records)) self.failUnless( isinstance( self.registry.records['test.registry.field'].field, field.Int ) ) self.assertEquals( u"Age", self.registry.records['test.registry.field'].field.title ) self.assertEquals( None, self.registry['test.registry.field'] )
def test_import_value_only_condition_not_have(self): xml = """\ <registry> <record name="test.export.simple" condition="not-have plone"> <value>Imported value</value> </record> </registry> """ context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} self.registry.records['test.export.simple'] = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Sample value") importRegistry(context) self.assertEquals(1, len(self.registry.records)) self.assertEquals( u"Simple record", self.registry.records['test.export.simple'].field.title ) self.assertEquals( u"Sample value", self.registry['test.export.simple'] )
def test_import_records_omit(self): xml = """\ <registry> <records interface="plone.app.registry.tests.data.ITestSettingsDisallowed"> <omit>blob</omit> </records> </registry> """ context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} self.registry.records['test.export.simple'] = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Sample value") importRegistry(context) self.assertEquals(3, len(self.registry.records)) self.failUnless( 'plone.app.registry.tests.data.ITestSettingsDisallowed.name' in self.registry ) self.failUnless( 'plone.app.registry.tests.data.ITestSettingsDisallowed.age' in self.registry )
def test_export_with_missing_schema_does_not_error(self): xml = """\ <registry> <record name="test.export.simple" interface="non.existant.ISchema" field="blah"> <field type="plone.registry.field.TextLine"> <default>N/A</default> <title>Simple record</title> </field> <value>Sample value</value> </record> </registry>""" self.registry.records['test.export.simple'] = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Sample value") # Note: These are nominally read-only! self.registry.records[ 'test.export.simple'].field.interfaceName = 'non.existant.ISchema' self.registry.records['test.export.simple'].field.fieldName = 'blah' alsoProvides(self.registry.records['test.export.simple'], IInterfaceAwareRecord) context = DummyExportContext(self.site) exportRegistry(context) self.assertEquals('registry.xml', context._wrote[0][0]) self.assertXmlEquals(xml, context._wrote[0][1])
def test_export_field_ref(self): xml = """\ <registry> <record name="test.export.simple"> <field type="plone.registry.field.TextLine"> <default>N/A</default> <title>Simple record</title> </field> <value>Sample value</value> </record> <record name="test.export.simple.override"> <field ref="test.export.simple" /> <value>Another value</value> </record> </registry>""" self.registry.records['test.export.simple'] = refRecord = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Sample value") self.registry.records['test.export.simple.override'] = \ Record(FieldRef(refRecord.__name__, refRecord.field), value=u"Another value") context = DummyExportContext(self.site) exportRegistry(context) self.assertEquals('registry.xml', context._wrote[0][0]) self.assertXmlEquals(xml, context._wrote[0][1])
def activateSearchable(self): registry = getUtility(IRegistry) prefix = 'plone.app.querystring.field.' + self.field_name def add(name, value): registry.records[prefix + '.' + name] = value add('title', Record(field.TextLine(), unicode(self.field_title))) add('enabled', Record(field.Bool(), True)) add('group', Record(field.TextLine(), unicode('Taxonomy'))) add( 'operations', Record(field.List(value_type=field.TextLine()), [u'plone.app.querystring.operation.selection.is'])) add('vocabulary', Record(field.TextLine(), unicode(self.name))) add('sortable', Record(field.Bool(), False)) add('description', Record(field.Text(), unicode('')))
class ISponsorshipSettings(Interface): levels = schema.Tuple( title=u'Sponsorship Levels', value_type=reg_field.TextLine( # TODO schema=ISponsorshipLevel, title=u'Sponsorship Level', description=u'Name,amount,max'))
def set_password(self, value): """Setter.""" registry = getUtility(IRegistry) if PASSWORD_REGISTRY_KEY not in registry: password_field = field.TextLine(title=u"Password for passwordwall") password_record = Record(password_field, value=value) registry.records[PASSWORD_REGISTRY_KEY] = password_record else: registry[PASSWORD_REGISTRY_KEY] = value
def test_new_record_modified(self): registry = getUtility(IRegistry) registry.records['collective.fingerpointing.new_field'] = Record( field.TextLine(title=u'New field')) with LogCapture('collective.fingerpointing', level=INFO) as log: api.portal.set_registry_record('collective.fingerpointing.new_field', u'Descrição') # noqa: E501 log.check( ('collective.fingerpointing', 'INFO', 'user=test_user_1_ ip=None action=modify object=<Record collective.fingerpointing.new_field> value=Descrição'), # noqa: E501 )
def test_get_existing_registry_record(self): """Test that existing registry records are returned correctly.""" registry = getUtility(IRegistry) registry.records['plone.api.norris_power'] = Record( field.TextLine(title=u"Chuck Norris' Power")) registry.records['plone.api.unset'] = Record( field.TextLine(title=u"An unset field")) registry['plone.api.norris_power'] = u'infinite' self.assertEqual( portal.get_registry_record('plone.api.norris_power'), u'infinite', ) self.assertEqual( portal.get_registry_record('plone.api.unset'), None, )
def setUp(self): self.app = self.layer['app'] self.portal = self.layer['portal'] self.portal_url = self.portal.absolute_url() setRoles(self.portal, TEST_USER_ID, ['Manager']) self.api_session = RelativeSession(self.portal_url) self.api_session.headers.update({'Accept': 'application/json'}) self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) registry = getUtility(IRegistry) record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar'] = record for counter in range(1, 100): record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar' + str(counter)] = record transaction.commit()
def setUp(self): self.app = self.layer["app"] self.portal = self.layer["portal"] self.portal_url = self.portal.absolute_url() setRoles(self.portal, TEST_USER_ID, ["Manager"]) self.api_session = RelativeSession(self.portal_url, test=self) self.api_session.headers.update({"Accept": "application/json"}) self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) registry = getUtility(IRegistry) record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar"] = record for counter in range(1, 100): record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar" + str(counter)] = record transaction.commit()
def test_batched(self): registry = Registry() for counter in range(1, 100): record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar' + str(counter)] = record obj = self.serialize(registry) expected = ['@id', 'items_total', 'items', 'batching'] self.assertEqual(set(expected), set(obj)) self.assertEqual(obj['items_total'], len(list(range(1, 100))))
def test_batched(self): registry = Registry() for counter in range(1, 100): record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar" + str(counter)] = record obj = self.serialize(registry) expected = ["@id", "items_total", "items", "batching"] self.assertEqual(set(expected), set(obj)) self.assertEqual(obj["items_total"], len(list(range(1, 100))))
def test_structure(self): registry = Registry() record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar'] = record obj = self.serialize(registry) item = obj['items'][0] self.assertEqual(set(item.keys()), set(['name', 'value'])) self.assertEqual(item['name'], 'foo.bar') self.assertEqual(item['value'], u"Lorem Ipsum")
def __call__(self): registry = queryUtility(IRegistry) if not registry: # XXX must not happen, be gentle anyway return NullRecord() records = registry.records if REGKEY not in records: # init if not exist value = field.TextLine(title=u"servers, delimited by space") records[REGKEY] = Record(value) return records[REGKEY]
def test_empty_import_no_purge(self): xml = "<registry/>" context = DummyImportContext(self.site, purge=False) context._files = {'registry.xml': xml} self.registry.records['test.export.simple'] = \ Record(field.TextLine(title=u"Simple record", default=u"N/A"), value=u"Sample value") importRegistry(context) self.assertEqual(1, len(self.registry.records))
def test_lookupOption_default(self): provideUtility(Registry(), IRegistry) registry = getUtility(IRegistry) registry.records['plone.caching.tests.test'] = Record( field.TextLine(), u'default') result = lookupOption('plone.caching.tests', 'testrule', 'test', default=_marker) self.assertEqual(u'default', result)
def test_set_no_value_param_for_existing_record(self): """Test that calling portal.set_registry_record with a name parameter for an existing record, but without a value, raises an Exception. """ registry = getUtility(IRegistry) registry.records['plone.api.plone_power'] = Record( field.TextLine(title=u"Plone's Power")) from plone.api.exc import MissingParameterError with self.assertRaises(MissingParameterError): portal.set_registry_record(name='plone.api.plone_power')
def test_update_several_registry_records(self): registry = getUtility(IRegistry) record = Record(field.TextLine(title="Foo Bar Baz"), "Lorem Ipsum Dolor") registry.records["foo.bar.baz"] = record transaction.commit() payload = {"foo.bar": "lorem ipsum", "foo.bar.baz": "lorem ipsum dolor"} response = self.api_session.patch("/@registry", json=payload) transaction.commit() self.assertEqual(response.status_code, 204) self.assertEqual(registry["foo.bar"], "lorem ipsum") self.assertEqual(registry["foo.bar.baz"], "lorem ipsum dolor")
def test_use_default_layout_in_registry(self): transform = theming.getTransform(self.portal, self.request) layout_name = transform.get_layout_name(self.portal) self.assertEqual(layout_name, 'index.html') registry = getUtility(IRegistry) field = registry_field.TextLine(title=u'Default layout', required=False) new_record = Record(field) registry.records['castle.cms.default_layout'] = new_record registry['castle.cms.default_layout'] = u'foobar.html' layout_name = transform.get_layout_name(self.portal) self.assertEqual(layout_name, 'foobar.html')
def test_structure(self): registry = Registry() record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar"] = record obj = self.serialize(registry) item = obj["items"][0] self.assertEqual(set(item), {"name", "value", "schema"}) self.assertEqual(set(item["schema"]), {"properties"}) self.assertEqual(item["name"], "foo.bar") self.assertEqual(item["value"], "Lorem Ipsum")
class IFilterSchema(Interface): column_name = field.Choice( title=_(u'Column Name'), description=_(u'Enter the name of a catalog metadata column'), vocabulary='collective.isotope.vocabularies.friendly_columns', required=False, missing_value=u'') label = field.TextLine( title=_(u'Label'), description=_( u'If desired, enter the human-readable label for this column'), required=False, missing_value=u'')
def activateSearchable(self): registry = getUtility(IRegistry) prefix = "plone.app.querystring.field." + self.field_name def add(name, value): registry.records[prefix + "." + name] = value add("title", Record(field.TextLine(), safe_unicode(self.field_title))) add("enabled", Record(field.Bool(), True)) add("group", Record(field.TextLine(), safe_unicode("Taxonomy"))) add( "operations", Record( field.List(value_type=field.TextLine()), [u"plone.app.querystring.operation.selection.is"], ), ) add( "vocabulary", Record(field.TextLine(), safe_unicode(self.vocabulary_name)) ) # noqa: E501 add("sortable", Record(field.Bool(), False)) add("description", Record(field.Text(), safe_unicode("")))