From d0935d467c46008f3723f8e1ceaa96c2f7f692f4 Mon Sep 17 00:00:00 2001 From: Jillian Vogel Date: Mon, 21 Feb 2022 14:09:35 +1030 Subject: [PATCH] fix: fixes bug with HTML blocks in studio_view Rendering asset URLs requires HTML blocks to be associated with a course key. This change allows HTML blocks to be associated with libraries and still render without error. --- .../xmodule/xmodule/contentstore/content.py | 4 +- .../tests/test_content_libraries.py | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/common/lib/xmodule/xmodule/contentstore/content.py b/common/lib/xmodule/xmodule/contentstore/content.py index 45884adb4f..76f18d115b 100644 --- a/common/lib/xmodule/xmodule/contentstore/content.py +++ b/common/lib/xmodule/xmodule/contentstore/content.py @@ -9,7 +9,7 @@ from urllib.parse import parse_qsl, quote_plus, urlencode, urlparse, urlunparse from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import AssetKey, CourseKey -from opaque_keys.edx.locator import AssetLocator +from opaque_keys.edx.locator import AssetLocator, LibraryLocatorV2 from PIL import Image from xmodule.assetstore.assetmgr import AssetManager @@ -123,7 +123,7 @@ class StaticContent: # lint-amnesty, pylint: disable=missing-class-docstring @staticmethod def get_base_url_path_for_course_assets(course_key): # lint-amnesty, pylint: disable=missing-function-docstring - if course_key is None: + if (course_key is None) or isinstance(course_key, LibraryLocatorV2): return None assert isinstance(course_key, CourseKey) diff --git a/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py b/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py index 9bc3405d98..a39c5371e8 100644 --- a/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py +++ b/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py @@ -369,6 +369,60 @@ class ContentLibrariesTestMixin: # fin + def test_library_blocks_studio_view(self): + """ + Test the happy path of working with an HTML XBlock in a the studio_view of a content library. + """ + lib = self._create_library(slug="testlib2", title="A Test Library", description="Testing XBlocks") + lib_id = lib["id"] + assert lib['has_unpublished_changes'] is False + + # A library starts out empty: + assert self._get_library_blocks(lib_id) == [] + + # Add a 'html' XBlock to the library: + block_data = self._add_block_to_library(lib_id, "html", "html1") + self.assertDictContainsEntries(block_data, { + "id": "lb:CL-TEST:testlib2:html:html1", + "display_name": "Text", + "block_type": "html", + "has_unpublished_changes": True, + }) + block_id = block_data["id"] + # Confirm that the result contains a definition key, but don't check its value, + # which for the purposes of these tests is an implementation detail. + assert 'def_key' in block_data + + # now the library should contain one block and have unpublished changes: + assert self._get_library_blocks(lib_id) == [block_data] + assert self._get_library(lib_id)['has_unpublished_changes'] is True + + # Publish the changes: + self._commit_library_changes(lib_id) + assert self._get_library(lib_id)['has_unpublished_changes'] is False + # And now the block information should also show that block has no unpublished changes: + block_data["has_unpublished_changes"] = False + self.assertDictContainsEntries(self._get_library_block(block_id), block_data) + assert self._get_library_blocks(lib_id) == [block_data] + + # Now update the block's OLX: + orig_olx = self._get_library_block_olx(block_id) + assert '