661 self.assertEqual(len(self.open_sessions), nbsessions) |
661 self.assertEqual(len(self.open_sessions), nbsessions) |
662 clear_cache(req, 'get_authorization') |
662 clear_cache(req, 'get_authorization') |
663 |
663 |
664 # content validation ####################################################### |
664 # content validation ####################################################### |
665 |
665 |
666 def assertDocTestFile(self, testfile): |
|
667 # doctest returns tuple (failure_count, test_count) |
|
668 result = self.shell().process_script(testfile) |
|
669 if result[0] and result[1]: |
|
670 raise self.failureException("doctest file '%s' failed" |
|
671 % testfile) |
|
672 |
|
673 # validators are used to validate (XML, DTD, whatever) view's content |
666 # validators are used to validate (XML, DTD, whatever) view's content |
674 # validators availables are : |
667 # validators availables are : |
675 # DTDValidator : validates XML + declared DTD |
668 # DTDValidator : validates XML + declared DTD |
676 # SaxOnlyValidator : guarantees XML is well formed |
669 # SaxOnlyValidator : guarantees XML is well formed |
677 # None : do not try to validate anything |
670 # None : do not try to validate anything |
752 except: |
745 except: |
753 msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__) |
746 msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__) |
754 raise AssertionError, msg, tcbk |
747 raise AssertionError, msg, tcbk |
755 return self._check_html(output, view, template) |
748 return self._check_html(output, view, template) |
756 |
749 |
|
750 def get_validator(self, view=None, content_type=None, output=None): |
|
751 if view is not None: |
|
752 try: |
|
753 return self.vid_validators[view.__regid__]() |
|
754 except KeyError: |
|
755 if content_type is None: |
|
756 content_type = view.content_type |
|
757 if content_type is None: |
|
758 content_type = 'text/html' |
|
759 if content_type in ('text/html', 'application/xhtml+xml'): |
|
760 if output and output.startswith('<?xml'): |
|
761 default_validator = htmlparser.DTDValidator |
|
762 else: |
|
763 default_validator = htmlparser.HTMLValidator |
|
764 else: |
|
765 default_validator = None |
|
766 validatorclass = self.content_type_validators.get(content_type, |
|
767 default_validator) |
|
768 if validatorclass is None: |
|
769 return |
|
770 return validatorclass() |
|
771 |
757 @nocoverage |
772 @nocoverage |
758 def _check_html(self, output, view, template='main-template'): |
773 def _check_html(self, output, view, template='main-template'): |
759 """raises an exception if the HTML is invalid""" |
774 """raises an exception if the HTML is invalid""" |
760 output = output.strip() |
775 output = output.strip() |
761 try: |
776 validator = self.get_validator(view, output=output) |
762 validatorclass = self.vid_validators[view.__regid__] |
|
763 except KeyError: |
|
764 if view.content_type in ('text/html', 'application/xhtml+xml'): |
|
765 if output.startswith('<?xml'): |
|
766 default_validator = htmlparser.DTDValidator |
|
767 else: |
|
768 default_validator = htmlparser.HTMLValidator |
|
769 else: |
|
770 default_validator = None |
|
771 validatorclass = self.content_type_validators.get(view.content_type, |
|
772 default_validator) |
|
773 if validatorclass is None: |
|
774 return |
|
775 validator = validatorclass() |
|
776 if isinstance(validator, htmlparser.DTDValidator): |
777 if isinstance(validator, htmlparser.DTDValidator): |
777 # XXX remove <canvas> used in progress widget, unknown in html dtd |
778 # XXX remove <canvas> used in progress widget, unknown in html dtd |
778 output = re.sub('<canvas.*?></canvas>', '', output) |
779 output = re.sub('<canvas.*?></canvas>', '', output) |
779 return self.assertWellFormed(validator, output.strip(), context= view.__regid__) |
780 return self.assertWellFormed(validator, output.strip(), context= view.__regid__) |
780 |
781 |
812 content = u'\n'.join(line_template % (idx + 1, line) |
813 content = u'\n'.join(line_template % (idx + 1, line) |
813 for idx, line in enumerate(content) |
814 for idx, line in enumerate(content) |
814 if line_context_filter(idx+1, position)) |
815 if line_context_filter(idx+1, position)) |
815 msg += u'\nfor content:\n%s' % content |
816 msg += u'\nfor content:\n%s' % content |
816 raise AssertionError, msg, tcbk |
817 raise AssertionError, msg, tcbk |
|
818 |
|
819 def assertDocTestFile(self, testfile): |
|
820 # doctest returns tuple (failure_count, test_count) |
|
821 result = self.shell().process_script(testfile) |
|
822 if result[0] and result[1]: |
|
823 raise self.failureException("doctest file '%s' failed" |
|
824 % testfile) |
817 |
825 |
818 # deprecated ############################################################### |
826 # deprecated ############################################################### |
819 |
827 |
820 @deprecated('[3.8] use self.execute(...).get_entity(0, 0)') |
828 @deprecated('[3.8] use self.execute(...).get_entity(0, 0)') |
821 def entity(self, rql, args=None, eidkey=None, req=None): |
829 def entity(self, rql, args=None, eidkey=None, req=None): |