diff --git a/common/lib/xmodule/tests/test_import.py b/common/lib/xmodule/tests/test_import.py index 6643b93a67..dd55f8ff38 100644 --- a/common/lib/xmodule/tests/test_import.py +++ b/common/lib/xmodule/tests/test_import.py @@ -1,7 +1,8 @@ from path import path - import unittest +from lxml import etree + from xmodule.x_module import XMLParsingSystem, XModuleDescriptor from xmodule.errorhandlers import ignore_errors_handler from xmodule.modulestore import Location @@ -61,3 +62,26 @@ class ImportTestCase(unittest.TestCase): self.assertEqual(descriptor.definition['data'], re_import_descriptor.definition['data']) + + def test_fixed_xml_tag(self): + """Make sure a tag that's been fixed exports as the original tag type""" + + # create a malformed tag with valid xml contents + root = etree.Element('malformed') + good_xml = '''''' + root.text = good_xml + + xml_str_in = etree.tostring(root) + + # load it + system = self.get_system() + descriptor = XModuleDescriptor.load_from_xml(xml_str_in, system, 'org', 'course', + None) + # export it + resource_fs = None + xml_str_out = descriptor.export_to_xml(resource_fs) + + # Now make sure the exported xml is a sequential + xml_out = etree.fromstring(xml_str_out) + self.assertEqual(xml_out.tag, 'sequential') + diff --git a/common/lib/xmodule/xmodule/malformed_module.py b/common/lib/xmodule/xmodule/malformed_module.py index 54032cf7d2..ac419fff26 100644 --- a/common/lib/xmodule/xmodule/malformed_module.py +++ b/common/lib/xmodule/xmodule/malformed_module.py @@ -1,5 +1,6 @@ from pkg_resources import resource_string from lxml import etree +from xmodule.x_module import XModule from xmodule.mako_module import MakoModuleDescriptor from xmodule.xml_module import XmlDescriptor from xmodule.editing_module import EditingDescriptor @@ -8,10 +9,18 @@ import logging log = logging.getLogger(__name__) +class MalformedModule(XModule): + def get_html(self): + '''Show an error. + TODO (vshnayder): proper style, divs, etc. + ''' + return "Malformed content--not showing through get_html()" + class MalformedDescriptor(EditingDescriptor): """ Module that provides a raw editing view of broken xml. """ + module_class = MalformedModule @classmethod def from_xml(cls, xml_data, system, org=None, course=None): @@ -20,8 +29,8 @@ class MalformedDescriptor(EditingDescriptor): Does not try to parse the data--just stores it. ''' - #log.debug("processing '{0}'".format(xml_data)) try: + # If this is already a malformed tag, don't want to re-wrap it. xml_obj = etree.fromstring(xml_data) if xml_obj.tag == 'malformed': xml_data = xml_obj.text @@ -40,9 +49,18 @@ class MalformedDescriptor(EditingDescriptor): def export_to_xml(self, resource_fs): ''' - Export as a string wrapped in xml - ''' - root = etree.Element('malformed') - root.text = self.definition['data'] - return etree.tostring(root) + If the definition data is invalid xml, export it wrapped in a malformed + tag. If it is valid, export without the wrapper. + NOTE: There may still be problems with the valid xml--it could be + missing required attributes, could have the wrong tags, refer to missing + files, etc. + ''' + try: + xml = etree.fromstring(self.definition['data']) + return etree.tostring(xml) + except etree.XMLSyntaxError: + # still not valid. + root = etree.Element('malformed') + root.text = self.definition['data'] + return etree.tostring(root)