def afterSetUp(self):
     from plone.app.z3cform.interfaces import IPloneFormLayer
     from plone.z3cform.z2 import switch_on
     
     self.form = self.folder[self.folder.invokeFactory('FormFolder', 'form')]
     self.request = makerequest(self.app).REQUEST
     self.request.SESSION = Session()
     self.wizard = DummyWizard(self.form, self.request)
     switch_on(self.wizard, request_layer=IPloneFormLayer)
     self.session = self.request.SESSION[self.wizard.sessionKey] = {}
     self.step = SignersStep(self.form, self.request, self.wizard)
class TestSignersStep(MegaphoneTestCase):

    def afterSetUp(self):
        from plone.app.z3cform.interfaces import IPloneFormLayer
        from plone.z3cform.z2 import switch_on
        
        self.form = self.folder[self.folder.invokeFactory('FormFolder', 'form')]
        self.request = makerequest(self.app).REQUEST
        self.request.SESSION = Session()
        self.wizard = DummyWizard(self.form, self.request)
        switch_on(self.wizard, request_layer=IPloneFormLayer)
        self.session = self.request.SESSION[self.wizard.sessionKey] = {}
        self.step = SignersStep(self.form, self.request, self.wizard)
    
    def test_load(self):
        self.form.__annotations__['collective.megaphone'] = {
            'signers': {
                'show_sig_portlet': False,
                'batch_size': 50,
                'sig_portlet_template': u'bar',
                },
            }
        self.step.load(self.form)
        self.assertEqual(self.session['signers'], self.form.__annotations__['collective.megaphone']['signers'])
    
    _form_data = {
        'signers.widgets.show_sig_portlet': u'selected',
        'signers.widgets.batch_size': u'20',
        'signers.widgets.sig_portlet_template': u'baz',
        'signers.widgets.sig_portlet_title': u'Title',
        'signers.widgets.sig_portlet_text': u'Text',
        'signers.widgets.sig_portlet_min_count': u'20',
        'signers.widgets.goose_factor': u'0',
        'signers.widgets.sig_portlet_batch_size': u'3',
        'signers.widgets.show_full_listing': u'selected',
        'signers.widgets.full_template': u'baz',
        'form.buttons.continue': 1,
        }
    
    def test_continue(self):
        self.request.form.update(self._form_data)
        self.wizard.update()
        self.assertEqual({'show_sig_portlet': True, 'batch_size': 20, 'sig_portlet_template': u'baz',
                          'sig_portlet_title': u'Title', 'sig_portlet_text': u'Text', 'sig_portlet_min_count': 20,
                          'goose_factor': 0, 'sig_portlet_batch_size': 3, 'sig_portlet_button': None,
                          'sig_portlet_link': None, 'show_full_listing': True, 'full_template': u'baz'}, self.session['signers'])

    def test_continue_accepts_non_ascii_template(self):
        data = self._form_data.copy()
        data['signers.widgets.sig_portlet_template'] = u'\u9731'
        self.request.form.update(data)
        self.wizard.update()
        self.assertEqual({'show_sig_portlet': True, 'batch_size': 20, 'sig_portlet_template': u'\u9731',
                          'sig_portlet_title': u'Title', 'sig_portlet_text': u'Text', 'sig_portlet_min_count': 20,
                          'goose_factor': 0, 'sig_portlet_batch_size': 3, 'sig_portlet_button': None,
                          'sig_portlet_link': None, 'show_full_listing': True, 'full_template': u'baz'}, self.session['signers'])

    def test_continue_accepts_valid_template_variable(self):
        self.session['formfields'] = {'fields': {'foobar': ()}}
        data = self._form_data.copy()
        data['signers.widgets.sig_portlet_template'] = u'${sender_foobar}'
        self.request.form.update(data)
        self.wizard.update()
        self.failIf(self.wizard.currentStep.widgets.errors)
    
    def test_continue_blocks_invalid_template_variable(self):
        data = self._form_data.copy()
        data['signers.widgets.sig_portlet_template'] = u'${foobar}'
        self.request.form.update(data)
        self.wizard.update()
        self.failUnless(self.wizard.currentStep.widgets.errors)
    
    def test_apply(self):
        self.session['signers'] = {
            'show_sig_portlet': True,
            'batch_size': 30,
            'sig_portlet_template': u'foo',
            }
        self.step.apply(self.form)
        self.assertEqual(self.session['signers'], self.form.__annotations__['collective.megaphone']['signers'])

    def test_apply_forces_savedata_on_when_necessary(self):
        self._create_megaphone()
        self.session['signers'] = {
            'show_sig_portlet': True,
            'batch_size': 30,
            'sig_portlet_template': u'foo',
            }
        self.step.apply(self.portal.megaphone)
        self.assertEqual('python:True', self.portal.megaphone['saved-letters'].getRawExecCondition())