""" Tests for the Unit XBlock """ import re import unittest from xml.dom import minidom from unittest.mock import patch from web_fragments.fragment import Fragment from xblock.core import XBlock from xblock.completable import XBlockCompletionMode from xblock.test.test_parsing import XmlTest from xmodule.unit_block import UnitBlock class FakeHTMLBlock(XBlock): """ An HTML block for use in tests """ def student_view(self, context=None): # pylint: disable=unused-argument """Provide simple HTML student view.""" return Fragment("This is some HTML.") class FakeVideoBlock(XBlock): """ A video block for use in tests """ def student_view(self, context=None): # pylint: disable=unused-argument """Provide simple Video student view.""" return Fragment( '' ) class UnitBlockTests(XmlTest, unittest.TestCase): """ Tests of the Unit XBlock. There's not much to this block, so we keep it simple. """ maxDiff = None @XBlock.register_temp_plugin(FakeHTMLBlock, identifier='fake-html') @XBlock.register_temp_plugin(FakeVideoBlock, identifier='fake-video') def test_unit_html(self): block = self.parse_xml_to_block("""\ """) with patch.object(block.runtime, 'applicable_aside_types', return_value=[]): # Disable problematic Acid aside html = block.runtime.render(block, 'student_view').content self.assertXmlEqual(html, ( '
' '
' '
' 'This is some HTML.' '
' '
' '' '
' '
' '
' )) def test_is_aggregator(self): """ The unit XBlock is designed to hold other XBlocks, so check that its completion status is defined as the aggregation of its child blocks. """ assert XBlockCompletionMode.get_mode(UnitBlock) == XBlockCompletionMode.AGGREGATOR def assertXmlEqual(self, xml_str_a, xml_str_b): """ Assert that the given XML strings are equal, ignoring attribute order and some whitespace variations. """ def clean(xml_str): # Collapse repeated whitespace: xml_str = re.sub(r'(\s)\s+', r'\1', xml_str) xml_bytes = xml_str.encode('utf8') return minidom.parseString(xml_bytes).toprettyxml() assert clean(xml_str_a) == clean(xml_str_b)