Skip to content

Commit

Permalink
Merge branch 'pfg_migration_tests' of https://github.com/collective/c…
Browse files Browse the repository at this point in the history
…ollective.easyform into pfg_migration_tests

Conflicts:
	collective/easyform/migrations.py
	collective/easyform/tests/testPFGmigration.py
  • Loading branch information
prakharjoshi committed Aug 11, 2016
2 parents 6d85742 + 36c13d2 commit 6766f1a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 10 additions & 0 deletions collective/easyform/migrations.py
Expand Up @@ -2,11 +2,13 @@
from zope import schema
from plone.supermodel.exportimport import BaseHandler
from xml.etree.ElementTree import tostring
from Products.CMFPlone.utils import safe_unicode


# Get all the PFG forms present in the site.
# PFG_Forms = api.content.find(context=api.portal.get(), portal_type='FormFolder')


def migrate_pfg_content(pfg_form):
"""
This will contain all the logic for migrating plone PFG forms content
Expand All @@ -22,3 +24,11 @@ def migrate_pfg_content(pfg_form):
el = model_Schema_field.write(schema.Field, field.id, 'zope.schema.Field')
final_content += tostring(el)
return final_content


def migrate_pfg_string_field(pfg_string_field):
tl = schema.TextLine()
tl.title = safe_unicode(pfg_string_field.title)
tl.description = safe_unicode(pfg_string_field.Description())
tl.required = bool(pfg_string_field.getRequired())
return tl
38 changes: 37 additions & 1 deletion collective/easyform/tests/testPFGmigration.py
Expand Up @@ -5,9 +5,16 @@
# Run this test only: bin/test -s collective.easyform -t testPFGmigration
#

# from collective.easyform.migrations import migrate_pfg_content
from collective.easyform.migrations import migrate_pfg_string_field, migrate_pfg_content
from collective.easyform.tests import base
from plone.supermodel import serializeSchema
# from plone.supermodel.model import SchemaClass
from plone.schemaeditor.interfaces import IEditableSchema
from plone.testing import z2
from collective.easyform.migrations import migrate_pfg_content
from zope.interface import Interface

import re

try:
import Products.PloneFormGen
Expand All @@ -16,6 +23,9 @@
HAVE_PFG = False


SUBJECT_FIELD_XML = u'''<field name="topic" type="zope.schema.TextLine"> <title>Subject</title> </field>'''


class MyFixture(base.Fixture):

defaultBases = (base.PLONE_FIXTURE,)
Expand Down Expand Up @@ -45,6 +55,21 @@ class MigrationFormTestCase(base.EasyFormTestCase):
layer = INTEGRATION_TESTING


def serializeField(afield, name):
# return an XML serialization of an individual field,
# normalize for spacing, eols

class TestClass(Interface):
pass
schema = TestClass
IEditableSchema(TestClass).addField(afield, name=name)
s = serializeSchema(schema)
found = re.findall('<field.+?</field>', s.replace('\n', ' '))
if len(found) == 1:
return re.sub(u' +', ' ', found[0])
return u''


class TestPFGmigration(MigrationFormTestCase):

""" test migration from PloneFormGen """
Expand All @@ -69,6 +94,17 @@ def testStringField(self):
expected = '<field name="replyto" type="zope.schema.Field" /><field name="topic" type="zope.schema.Field" />'
self.assertEquals(transformed_string, expected)

def testStringFieldConversion(self):
sample_pfg_string_field = self.pfgff1.topic
tsf = migrate_pfg_string_field(sample_pfg_string_field)
self.assertEqual(SUBJECT_FIELD_XML, serializeField(tsf, name=u'topic'))

def testNonRequiredFieldConversion(self):
sample_pfg_string_field = self.pfgff1.topic
sample_pfg_string_field.setRequired(False)
tsf = migrate_pfg_string_field(sample_pfg_string_field)
self.assertTrue(u'<required>False</required>' in serializeField(tsf, name=u'topic'))


def test_suite():
from unittest import TestSuite, makeSuite
Expand Down

0 comments on commit 6766f1a

Please sign in to comment.