Skip to content

Commit bbfdce0

Browse files
authored
fix: Allow tests for Extracted & Builtin HTML XBlock (#36399)
Test HTML XBlock in both built-in and extracted modes to keep them in sync. Related to: #36283
1 parent 55a3757 commit bbfdce0

File tree

2 files changed

+96
-35
lines changed

2 files changed

+96
-35
lines changed

xmodule/html_block.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,17 @@ def safe_parse_date(date):
494494
return datetime.today()
495495

496496

497-
HtmlBlock = (
498-
_ExtractedHtmlBlock if settings.USE_EXTRACTED_HTML_BLOCK
499-
else _BuiltInHtmlBlock
500-
)
497+
HtmlBlock = None
498+
499+
500+
def reset_class():
501+
"""Reset class as per django settings flag"""
502+
global HtmlBlock
503+
HtmlBlock = (
504+
_ExtractedHtmlBlock if settings.USE_EXTRACTED_HTML_BLOCK
505+
else _BuiltInHtmlBlock
506+
)
507+
return HtmlBlock
508+
509+
reset_class()
501510
HtmlBlock.__name__ = "HtmlBlock"

xmodule/tests/test_html_block.py

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,32 @@
55

66
import ddt
77
from django.contrib.auth.models import AnonymousUser
8+
from django.test import TestCase
89
from django.test.utils import override_settings
910
from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator
1011
from xblock.field_data import DictFieldData
1112
from xblock.fields import ScopeIds
1213

13-
from xmodule.html_block import CourseInfoBlock, HtmlBlock
14+
from xmodule import html_block
15+
from xmodule.html_block import CourseInfoBlock
1416

1517
from ..x_module import PUBLIC_VIEW, STUDENT_VIEW
1618
from . import get_test_descriptor_system, get_test_system
1719

1820

19-
def instantiate_block(**field_data):
20-
"""
21-
Instantiate block with most properties.
22-
"""
23-
system = get_test_descriptor_system()
24-
course_key = CourseLocator('org', 'course', 'run')
25-
usage_key = course_key.make_usage_key('html', 'SampleHtml')
26-
return system.construct_xblock_from_class(
27-
HtmlBlock,
28-
scope_ids=ScopeIds(None, None, usage_key, usage_key),
29-
field_data=DictFieldData(field_data),
30-
)
31-
32-
3321
@ddt.ddt
34-
class HtmlBlockCourseApiTestCase(unittest.TestCase):
22+
class _HtmlBlockCourseApiTestCaseBase(TestCase):
3523
"""
3624
Test the HTML XModule's student_view_data method.
3725
"""
3826

27+
__test__ = False
28+
29+
@classmethod
30+
def setUpClass(cls):
31+
super().setUpClass()
32+
cls.html_class = html_block.reset_class()
33+
3934
@ddt.data(
4035
{},
4136
dict(FEATURES={}),
@@ -48,7 +43,7 @@ def test_disabled(self, settings):
4843
"""
4944
field_data = DictFieldData({'data': '<h1>Some HTML</h1>'})
5045
module_system = get_test_system()
51-
block = HtmlBlock(module_system, field_data, Mock())
46+
block = self.html_class(module_system, field_data, Mock())
5247

5348
with override_settings(**settings):
5449
assert block.student_view_data() ==\
@@ -76,7 +71,7 @@ def test_common_values(self, html):
7671
"""
7772
field_data = DictFieldData({'data': html})
7873
module_system = get_test_system()
79-
block = HtmlBlock(module_system, field_data, Mock())
74+
block = self.html_class(module_system, field_data, Mock())
8075
assert block.student_view_data() == dict(enabled=True, html=html)
8176

8277
@ddt.data(
@@ -90,25 +85,32 @@ def test_student_preview_view(self, view):
9085
html = '<p>This is a test</p>'
9186
field_data = DictFieldData({'data': html})
9287
module_system = get_test_system()
93-
block = HtmlBlock(module_system, field_data, Mock())
88+
block = self.html_class(module_system, field_data, Mock())
9489
rendered = module_system.render(block, view, {}).content
9590
assert html in rendered
9691

9792

98-
class HtmlBlockSubstitutionTestCase(unittest.TestCase): # lint-amnesty, pylint: disable=missing-class-docstring
93+
class _HtmlBlockSubstitutionTestCaseBase(TestCase): # lint-amnesty, pylint: disable=missing-class-docstring
94+
95+
__test__ = False
96+
97+
@classmethod
98+
def setUpClass(cls):
99+
super().setUpClass()
100+
cls.html_class = html_block.reset_class()
99101

100102
def test_substitution_user_id(self):
101103
sample_xml = '''%%USER_ID%%'''
102104
field_data = DictFieldData({'data': sample_xml})
103105
module_system = get_test_system()
104-
block = HtmlBlock(module_system, field_data, Mock())
106+
block = self.html_class(module_system, field_data, Mock())
105107
assert block.get_html() == str(module_system.anonymous_student_id)
106108

107109
def test_substitution_course_id(self):
108110
sample_xml = '''%%COURSE_ID%%'''
109111
field_data = DictFieldData({'data': sample_xml})
110112
module_system = get_test_system()
111-
block = HtmlBlock(module_system, field_data, Mock())
113+
block = self.html_class(module_system, field_data, Mock())
112114
course_key = CourseLocator(
113115
org='some_org',
114116
course='some_course',
@@ -130,30 +132,50 @@ def test_substitution_without_magic_string(self):
130132
'''
131133
field_data = DictFieldData({'data': sample_xml})
132134
module_system = get_test_system()
133-
block = HtmlBlock(module_system, field_data, Mock())
135+
block = self.html_class(module_system, field_data, Mock())
134136
assert block.get_html() == sample_xml
135137

136138
def test_substitution_without_anonymous_student_id(self):
137139
sample_xml = '''%%USER_ID%%'''
138140
field_data = DictFieldData({'data': sample_xml})
139141
module_system = get_test_system(user=AnonymousUser())
140-
block = HtmlBlock(module_system, field_data, Mock())
142+
block = self.html_class(module_system, field_data, Mock())
141143
block.runtime.service(block, 'user')._deprecated_anonymous_user_id = '' # pylint: disable=protected-access
142144
assert block.get_html() == sample_xml
143145

144146

145-
class HtmlBlockIndexingTestCase(unittest.TestCase):
147+
class _HtmlBlockIndexingTestCaseBase(TestCase):
146148
"""
147149
Make sure that HtmlBlock can format data for indexing as expected.
148150
"""
149151

152+
__test__ = False
153+
154+
@classmethod
155+
def setUpClass(cls):
156+
super().setUpClass()
157+
cls.html_class = html_block.reset_class()
158+
159+
def instantiate_block(self, **field_data):
160+
"""
161+
Instantiate HtmlBlock with field data.
162+
"""
163+
system = get_test_descriptor_system()
164+
course_key = CourseLocator('org', 'course', 'run')
165+
usage_key = course_key.make_usage_key('html', 'SampleHtml')
166+
return system.construct_xblock_from_class(
167+
self.html_class,
168+
scope_ids=ScopeIds(None, None, usage_key, usage_key),
169+
field_data=DictFieldData(field_data),
170+
)
171+
150172
def test_index_dictionary_simple_html_block(self):
151173
sample_xml = '''
152174
<html>
153175
<p>Hello World!</p>
154176
</html>
155177
'''
156-
block = instantiate_block(data=sample_xml)
178+
block = self.instantiate_block(data=sample_xml)
157179
assert block.index_dictionary() ==\
158180
{'content': {'html_content': ' Hello World! ', 'display_name': 'Text'}, 'content_type': 'Text'}
159181

@@ -164,7 +186,7 @@ def test_index_dictionary_cdata_html_block(self):
164186
<![CDATA[This is just a CDATA!]]>
165187
</html>
166188
'''
167-
block = instantiate_block(data=sample_xml_cdata)
189+
block = self.instantiate_block(data=sample_xml_cdata)
168190
assert block.index_dictionary() ==\
169191
{'content': {'html_content': ' This has CDATA in it. ', 'display_name': 'Text'}, 'content_type': 'Text'}
170192

@@ -174,7 +196,7 @@ def test_index_dictionary_multiple_spaces_html_block(self):
174196
<p> Text has spaces :) </p>
175197
</html>
176198
'''
177-
block = instantiate_block(data=sample_xml_tab_spaces)
199+
block = self.instantiate_block(data=sample_xml_tab_spaces)
178200
assert block.index_dictionary() ==\
179201
{'content': {'html_content': ' Text has spaces :) ', 'display_name': 'Text'}, 'content_type': 'Text'}
180202

@@ -185,7 +207,7 @@ def test_index_dictionary_html_block_with_comment(self):
185207
<!-- Html Comment -->
186208
</html>
187209
'''
188-
block = instantiate_block(data=sample_xml_comment)
210+
block = self.instantiate_block(data=sample_xml_comment)
189211
assert block.index_dictionary() == {'content': {'html_content': ' This has HTML comment in it. ', 'display_name': 'Text'}, 'content_type': 'Text'} # pylint: disable=line-too-long
190212

191213
def test_index_dictionary_html_block_with_both_comments_and_cdata(self):
@@ -198,7 +220,7 @@ def test_index_dictionary_html_block_with_both_comments_and_cdata(self):
198220
<p>HTML end.</p>
199221
</html>
200222
'''
201-
block = instantiate_block(data=sample_xml_mix_comment_cdata)
223+
block = self.instantiate_block(data=sample_xml_mix_comment_cdata)
202224
assert block.index_dictionary() ==\
203225
{'content': {'html_content': ' This has HTML comment in it. HTML end. ',
204226
'display_name': 'Text'}, 'content_type': 'Text'}
@@ -217,7 +239,7 @@ def test_index_dictionary_html_block_with_script_and_style_tags(self):
217239
</script>
218240
</html>
219241
'''
220-
block = instantiate_block(data=sample_xml_style_script_tags)
242+
block = self.instantiate_block(data=sample_xml_style_script_tags)
221243
assert block.index_dictionary() ==\
222244
{'content': {'html_content': ' This has HTML comment in it. HTML end. ',
223245
'display_name': 'Text'}, 'content_type': 'Text'}
@@ -320,3 +342,33 @@ def test_updates_order(self):
320342
template_name,
321343
expected_context
322344
)
345+
346+
347+
@override_settings(USE_EXTRACTED_HTML_BLOCK=True)
348+
class ExtractedHtmlBlockCourseApiTestCase(_HtmlBlockCourseApiTestCaseBase):
349+
__test__ = True
350+
351+
352+
@override_settings(USE_EXTRACTED_HTML_BLOCK=False)
353+
class BuiltInHtmlBlockCourseApiTestCase(_HtmlBlockCourseApiTestCaseBase):
354+
__test__ = True
355+
356+
357+
@override_settings(USE_EXTRACTED_HTML_BLOCK=True)
358+
class ExtractedHtmlBlockSubstitutionTestCase(_HtmlBlockSubstitutionTestCaseBase):
359+
__test__ = True
360+
361+
362+
@override_settings(USE_EXTRACTED_HTML_BLOCK=False)
363+
class BuiltInHtmlBlockSubstitutionTestCase(_HtmlBlockSubstitutionTestCaseBase):
364+
__test__ = True
365+
366+
367+
@override_settings(USE_EXTRACTED_HTML_BLOCK=True)
368+
class ExtractedHtmlBlockIndexingTestCase(_HtmlBlockIndexingTestCaseBase):
369+
__test__ = True
370+
371+
372+
@override_settings(USE_EXTRACTED_HTML_BLOCK=False)
373+
class BuiltInHtmlBlockIndexingTestCase(_HtmlBlockIndexingTestCaseBase):
374+
__test__ = True

0 commit comments

Comments
 (0)