[facet js] fix reordering of facet check boxes. Closes #2732947
Before this patch, when one select an element, it's moved to the top of
the select content. Fine. But when it's later deselected, it stays there
instead of moving back to its original location.
This patch fixes that by introducing a facetCheckBoxReorder function which
properly reorder the whole facet, instead of buggy attempt to locally reorder.
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.fromxml.etree.ElementTreeimportfromstringfromlogilab.common.testlibimportunittest_main,mock_objectfromlogilab.common.compatimportanyfromcubicwebimportBinary,ValidationErrorfromcubicweb.devtools.testlibimportCubicWebTCfromcubicweb.web.formfieldsimport(IntField,StringField,RichTextField,PasswordField,DateTimeField,FileField,EditableFileField)fromcubicweb.web.formwidgetsimportPasswordInput,Input,DateTimePickerfromcubicweb.web.views.formsimportEntityFieldsForm,FieldsFormfromcubicweb.web.views.workflowimportChangeStateFormfromcubicweb.web.views.formrenderersimportFormRendererclassFieldsFormTC(CubicWebTC):deftest_form_field_format(self):form=FieldsForm(self.request(),None)self.assertEqual(StringField().format(form),'text/html')self.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"')self.commit()self.assertEqual(StringField().format(form),'text/rest')deftest_process_posted(self):classAForm(FieldsForm):anint=IntField()astring=StringField()form=AForm(self.request(anint='1',astring='2',_cw_fields='anint,astring'))self.assertEqual(form.process_posted(),{'anint':1,'astring':'2'})form=AForm(self.request(anint='1a',astring='2b',_cw_fields='anint,astring'))self.assertRaises(ValidationError,form.process_posted)classEntityFieldsFormTC(CubicWebTC):defsetUp(self):super(EntityFieldsFormTC,self).setUp()self.req=self.request()self.entity=self.user(self.req)deftest_form_field_choices(self):b=self.req.create_entity('BlogEntry',title=u'di mascii code',content=u'a best-seller')t=self.req.create_entity('Tag',name=u'x')form1=self.vreg['forms'].select('edition',self.req,entity=t)choices=[reidforrview,reidinform1.field_by_name('tags','subject',t.e_schema).choices(form1)]self.assertIn(unicode(b.eid),choices)form2=self.vreg['forms'].select('edition',self.req,entity=b)choices=[reidforrview,reidinform2.field_by_name('tags','object',t.e_schema).choices(form2)]self.assertIn(unicode(t.eid),choices)b.cw_clear_all_caches()t.cw_clear_all_caches()self.execute('SET X tags Y WHERE X is Tag, Y is BlogEntry')choices=[reidforrview,reidinform1.field_by_name('tags','subject',t.e_schema).choices(form1)]self.assertIn(unicode(b.eid),choices)choices=[reidforrview,reidinform2.field_by_name('tags','object',t.e_schema).choices(form2)]self.assertIn(unicode(t.eid),choices)deftest_form_field_choices_new_entity(self):e=self.vreg['etypes'].etype_class('CWUser')(self.request())form=self.vreg['forms'].select('edition',self.req,entity=e)unrelated=[rviewforrview,reidinform.field_by_name('in_group','subject').choices(form)]# should be default groups but owners, i.e. managers, users, guestsself.assertEqual(unrelated,[u'guests',u'managers',u'users'])deftest_consider_req_form_params(self):e=self.vreg['etypes'].etype_class('CWUser')(self.request())e.eid='A'form=EntityFieldsForm(self.request(login=u'toto'),None,entity=e)field=StringField(name='login',role='subject',eidparam=True)form.append_field(field)form.build_context({})self.assertEqual(field.widget.values(form,field),(u'toto',))deftest_linkto_field_duplication(self):e=self.vreg['etypes'].etype_class('CWUser')(self.request())e.eid='A'e._cw=self.reqgeid=self.execute('CWGroup X WHERE X name "users"')[0][0]self.req.form['__linkto']='in_group:%s:subject'%geidform=self.vreg['forms'].select('edition',self.req,entity=e)form.content_type='text/html'pageinfo=self._check_html(form.render(),form,template=None)inputs=pageinfo.find_tag('select',False)ok=Falseforselectnodeinpageinfo.matching_nodes('select',name='from_in_group-subject:A'):foroptionnodeinselectnode:self.assertEqual(optionnode.get('value'),str(geid))self.assertEqual(ok,False)ok=Trueself.assertEqual(ok,True,'expected option not found')inputs=pageinfo.find_tag('input',False)self.assertFalse(list(pageinfo.matching_nodes('input',name='__linkto')))deftest_reledit_composite_field(self):rset=self.execute('INSERT BlogEntry X: X title "cubicweb.org", X content "hop"')form=self.vreg['views'].select('reledit',self.request(),rset=rset,row=0,rtype='content')data=form.render(row=0,rtype='content',formid='base',action='edit_rtype')self.assertTrue('content_format'indata)# form view tests #########################################################deftest_massmailing_formview(self):self.execute('INSERT EmailAddress X: X address L + "@cubicweb.org", ''U use_email X WHERE U is CWUser, U login L')rset=self.execute('CWUser X')self.view('massmailing',rset,template=None)# form tests ##############################################################deftest_form_inheritance(self):classCustomChangeStateForm(ChangeStateForm):hello=IntField(name='youlou')creation_date=DateTimeField(widget=DateTimePicker)form=CustomChangeStateForm(self.req,redirect_path='perdu.com',entity=self.entity)form.render(formvalues=dict(state=123,trcomment=u'',trcomment_format=u'text/plain'))deftest_change_state_form(self):form=ChangeStateForm(self.req,redirect_path='perdu.com',entity=self.entity)form.render(formvalues=dict(state=123,trcomment=u'',trcomment_format=u'text/plain'))# fields tests ############################################################def_render_entity_field(self,name,form):form.build_context({})renderer=FormRenderer(self.req)returnform.field_by_name(name,'subject').render(form,renderer)def_test_richtextfield(self,expected):classRTFForm(EntityFieldsForm):description=RichTextField(eidparam=True,role='subject')state=self.vreg['etypes'].etype_class('State')(self.req)state.eid='S'form=RTFForm(self.req,redirect_path='perdu.com',entity=state)# make it think it can use fck editor anywayform.field_by_name('description','subject').format=lambdaform,field=None:'text/html'self.assertMultiLineEqual(self._render_entity_field('description',form),expected%{'eid':state.eid})deftest_richtextfield_1(self):self.req.use_fckeditor=lambda:Falseself._test_richtextfield('''<select id="description_format-subject:%(eid)s" name="description_format-subject:%(eid)s" size="1" style="display: block" tabindex="1"><option value="text/cubicweb-page-template">text/cubicweb-page-template</option><option selected="selected" value="text/html">text/html</option><option value="text/plain">text/plain</option><option value="text/rest">text/rest</option></select><textarea cols="80" id="description-subject:%(eid)s" name="description-subject:%(eid)s" onkeyup="autogrow(this)" rows="2" tabindex="2"></textarea>''')deftest_richtextfield_2(self):self.req.use_fckeditor=lambda:Trueself._test_richtextfield('<input name="description_format-subject:%(eid)s" type="hidden" value="text/html" /><textarea cols="80" cubicweb:type="wysiwyg" id="description-subject:%(eid)s" name="description-subject:%(eid)s" onkeyup="autogrow(this)" rows="2" tabindex="1"></textarea>')deftest_filefield(self):classFFForm(EntityFieldsForm):data=FileField(format_field=StringField(name='data_format',max_length=50,eidparam=True,role='subject'),encoding_field=StringField(name='data_encoding',max_length=20,eidparam=True,role='subject'),eidparam=True,role='subject')file=self.req.create_entity('File',data_name=u"pouet.txt",data_encoding=u'UTF-8',data=Binary('new widgets system'))form=FFForm(self.req,redirect_path='perdu.com',entity=file)self.assertMultiLineEqual(self._render_entity_field('data',form),'''<input id="data-subject:%(eid)s" name="data-subject:%(eid)s" tabindex="1" type="file" value="" /><a href="javascript: toggleVisibility('data-subject:%(eid)s-advanced')" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a><div id="data-subject:%(eid)s-advanced" class="hidden"><label for="data_format-subject:%(eid)s">data_format</label><input id="data_format-subject:%(eid)s" maxlength="50" name="data_format-subject:%(eid)s" size="45" tabindex="2" type="text" value="text/plain" /><br/><label for="data_encoding-subject:%(eid)s">data_encoding</label><input id="data_encoding-subject:%(eid)s" maxlength="20" name="data_encoding-subject:%(eid)s" size="20" tabindex="3" type="text" value="UTF-8" /><br/></div><br/><input name="data-subject__detach:%(eid)s" type="checkbox" />detach attached file'''%{'eid':file.eid})deftest_editablefilefield(self):classEFFForm(EntityFieldsForm):data=EditableFileField(format_field=StringField('data_format',max_length=50,eidparam=True,role='subject'),encoding_field=StringField('data_encoding',max_length=20,eidparam=True,role='subject'),eidparam=True,role='subject')file=self.req.create_entity('File',data_name=u"pouet.txt",data_encoding=u'UTF-8',data=Binary('new widgets system'))form=EFFForm(self.req,redirect_path='perdu.com',entity=file)self.assertMultiLineEqual(self._render_entity_field('data',form),'''<input id="data-subject:%(eid)s" name="data-subject:%(eid)s" tabindex="1" type="file" value="" /><a href="javascript: toggleVisibility('data-subject:%(eid)s-advanced')" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a><div id="data-subject:%(eid)s-advanced" class="hidden"><label for="data_format-subject:%(eid)s">data_format</label><input id="data_format-subject:%(eid)s" maxlength="50" name="data_format-subject:%(eid)s" size="45" tabindex="2" type="text" value="text/plain" /><br/><label for="data_encoding-subject:%(eid)s">data_encoding</label><input id="data_encoding-subject:%(eid)s" maxlength="20" name="data_encoding-subject:%(eid)s" size="20" tabindex="3" type="text" value="UTF-8" /><br/></div><br/><input name="data-subject__detach:%(eid)s" type="checkbox" />detach attached file<p><b>You can either submit a new file using the browse button above, or choose to remove already uploaded file by checking the "detach attached file" check-box, or edit file content online with the widget below.</b></p><textarea cols="80" name="data-subject:%(eid)s" onkeyup="autogrow(this)" rows="3" tabindex="4">new widgets system</textarea>'''%{'eid':file.eid})deftest_passwordfield(self):classPFForm(EntityFieldsForm):upassword=PasswordField(eidparam=True,role='subject')form=PFForm(self.req,redirect_path='perdu.com',entity=self.entity)self.assertMultiLineEqual(self._render_entity_field('upassword',form),'''<input id="upassword-subject:%(eid)s" name="upassword-subject:%(eid)s" tabindex="1" type="password" value="" /><br/><input name="upassword-subject-confirm:%(eid)s" tabindex="1" type="password" value="" /> <span class="emphasis">confirm password</span>'''%{'eid':self.entity.eid})# def test_datefield(self):# class DFForm(EntityFieldsForm):# creation_date = DateTimeField(widget=Input)# form = DFForm(self.req, entity=self.entity)# init, cur = (fromstring(self._render_entity_field(attr, form)).get('value')# for attr in ('edits-creation_date', 'creation_date'))# self.assertEqual(init, cur)if__name__=='__main__':unittest_main()