From d1394922aefffd7817c64c3d2dfd1ca8148fefe8 Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Mon, 13 Jun 2022 11:13:08 -0700 Subject: [PATCH] feat: change text xblock title to 'Text' v2 (#30578) --- .../contentstore/tests/test_utils.py | 40 +++++++++++++++++++ cms/djangoapps/contentstore/utils.py | 14 +++++++ cms/djangoapps/contentstore/views/item.py | 5 ++- cms/templates/studio_xblock_wrapper.html | 4 +- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_utils.py b/cms/djangoapps/contentstore/tests/test_utils.py index 2ed40b6942..b166de0ef9 100644 --- a/cms/djangoapps/contentstore/tests/test_utils.py +++ b/cms/djangoapps/contentstore/tests/test_utils.py @@ -736,3 +736,43 @@ class ValidateCourseOlxTests(CourseTestCase): ignore=ignore, allowed_xblocks=allowed_xblocks ) + + +class DetermineLabelTests(TestCase): + """Tests for xblock Title""" + + def test_html_replaced_with_text_for_none(self): + """ + Tests that display names for "html" xblocks are replaced with "Text" when the display name is otherwise unset. + """ + display_name = None + block_type = "html" + result = utils.determine_label(display_name, block_type) + self.assertEqual(result, "Text") + + def test_html_replaced_with_text_for_empty(self): + """ + Tests that display names for "html" xblocks are replaced with "Text" when the display name is empty. + """ + display_name = "" + block_type = "html" + result = utils.determine_label(display_name, block_type) + self.assertEqual(result, "Text") + + def test_set_titles_not_replaced(self): + """ + Tests that display names for "html" xblocks are not replaced with "Text" when the display name is set. + """ + display_name = "Something" + block_type = "html" + result = utils.determine_label(display_name, block_type) + self.assertEqual(result, "Something") + + def test_non_html_blocks_titles_not_replaced(self): + """ + Tests that display names for non-"html" xblocks are not replaced with "Text" when the display name is set. + """ + display_name = None + block_type = "something else" + result = utils.determine_label(display_name, block_type) + self.assertEqual(result, "something else") diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 1e0d3e60c6..3aa171c922 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -703,6 +703,20 @@ def get_sibling_urls(subsection, unit_location): # pylint: disable=too-many-s return prev_url, next_url +def determine_label(display_name, block_type): + """ + Returns the name of the xblock to display in studio. + Please see TNL-9838. + """ + if display_name in {"", None}: + if block_type == 'html': + return _("Text") + else: + return block_type + else: + return display_name + + @contextmanager def translation_language(language): """Context manager to override the translation language for the scope diff --git a/cms/djangoapps/contentstore/views/item.py b/cms/djangoapps/contentstore/views/item.py index 69c3117417..2ef9b80d38 100644 --- a/cms/djangoapps/contentstore/views/item.py +++ b/cms/djangoapps/contentstore/views/item.py @@ -427,13 +427,16 @@ def xblock_view_handler(request, usage_key_string, view_name): # Note that the container view recursively adds headers into the preview fragment, # so only the "Pages" view requires that this extra wrapper be included. + display_label = xblock.display_name or xblock.scope_ids.block_type + if not xblock.display_name and xblock.scope_ids.block_type == 'html': + display_label = _("Text") if is_pages_view: fragment.content = render_to_string('component.html', { 'xblock_context': context, 'xblock': xblock, 'locator': usage_key, 'preview': fragment.content, - 'label': xblock.display_name or xblock.scope_ids.block_type, + 'label': display_label, }) else: raise Http404 diff --git a/cms/templates/studio_xblock_wrapper.html b/cms/templates/studio_xblock_wrapper.html index 2194a5e2f0..1bd2c3070e 100644 --- a/cms/templates/studio_xblock_wrapper.html +++ b/cms/templates/studio_xblock_wrapper.html @@ -2,7 +2,7 @@ <%! from django.utils.translation import gettext as _ from cms.djangoapps.contentstore.views.helpers import xblock_studio_url -from cms.djangoapps.contentstore.utils import is_visible_to_specific_partition_groups, get_editor_page_base_url +from cms.djangoapps.contentstore.utils import is_visible_to_specific_partition_groups, get_editor_page_base_url, determine_label from lms.lib.utils import is_unit from openedx.core.djangolib.js_utils import ( dump_js_escaped_json, js_escaped_string @@ -17,7 +17,7 @@ xblock_url = xblock_studio_url(xblock) show_inline = xblock.has_children and not xblock_url section_class = "level-nesting" if show_inline else "level-element" collapsible_class = "is-collapsible" if xblock.has_children else "" -label = xblock.display_name_with_default or xblock.scope_ids.block_type +label = determine_label(xblock.display_name_with_default, xblock.scope_ids.block_type) messages = xblock.validate().to_json() block_is_unit = is_unit(xblock) %>