diff --git a/common/lib/xmodule/xmodule/capa_base.py b/common/lib/xmodule/xmodule/capa_base.py index 00b9e77b21..5a74a8aaa2 100644 --- a/common/lib/xmodule/xmodule/capa_base.py +++ b/common/lib/xmodule/xmodule/capa_base.py @@ -659,7 +659,7 @@ class CapaMixin(CapaFields): check_button_checking = False content = { - 'name': self.display_name_with_default_escaped, + 'name': self.display_name_with_default, 'html': html, 'weight': self.weight, } diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index 75ced83ea1..774b39a39a 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -128,6 +128,19 @@ class CapaModule(CapaMixin, XModule): return json.dumps(result, cls=ComplexEncoder) + @property + def display_name_with_default(self): + """ + Constructs the display name for a CAPA problem. + + Default to the display_name if it isn't None or not an empty string, + else fall back to problem category. + """ + if self.display_name is None or not self.display_name.strip(): + return self.location.block_type + + return self.display_name + class CapaDescriptor(CapaFields, RawDescriptor): """ diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index 3fad0fdc9d..afa2454af3 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -1719,6 +1719,34 @@ class CapaModuleTest(unittest.TestCase): ('answerpool', ['choice_1', 'choice_3', 'choice_2', 'choice_0'])) self.assertEquals(event_info['success'], 'incorrect') + @ddt.unpack + @ddt.data( + {'display_name': None, 'expected_display_name': 'problem'}, + {'display_name': '', 'expected_display_name': 'problem'}, + {'display_name': ' ', 'expected_display_name': 'problem'}, + {'display_name': 'CAPA 101', 'expected_display_name': 'CAPA 101'} + ) + def test_problem_display_name_with_default(self, display_name, expected_display_name): + """ + Verify that display_name_with_default works as expected. + """ + module = CapaFactory.create(display_name=display_name) + self.assertEqual(module.display_name_with_default, expected_display_name) + + @ddt.data( + '', + ' ', + ) + def test_problem_no_display_name(self, display_name): + """ + Verify that if problem display name is not provided then a default name is used. + """ + module = CapaFactory.create(display_name=display_name) + module.get_problem_html() + render_args, _ = module.system.render_template.call_args + context = render_args[1] + self.assertEqual(context['problem']['name'], module.location.block_type) + @ddt.ddt class CapaDescriptorTest(unittest.TestCase): diff --git a/lms/templates/problem.html b/lms/templates/problem.html index 582d5fcfd4..55e1e77c78 100644 --- a/lms/templates/problem.html +++ b/lms/templates/problem.html @@ -1,4 +1,8 @@ -<%! from django.utils.translation import ugettext as _ %> +<%page expression_filter="h"/> +<%! +from django.utils.translation import ugettext as _ +from openedx.core.djangolib.markup import HTML +%> <%namespace name='static' file='static_content.html'/>