diff --git a/cms/djangoapps/contentstore/views/tests/test_import_export.py b/cms/djangoapps/contentstore/views/tests/test_import_export.py index 38e0388b7c..8cbd11c40c 100644 --- a/cms/djangoapps/contentstore/views/tests/test_import_export.py +++ b/cms/djangoapps/contentstore/views/tests/test_import_export.py @@ -2,6 +2,7 @@ Unit tests for course import and export """ import copy +import itertools import json import logging import os @@ -15,6 +16,7 @@ from uuid import uuid4 import ddt import lxml +from bson import ObjectId from django.conf import settings from django.contrib.auth import get_user_model from django.core.exceptions import SuspiciousOperation @@ -1076,7 +1078,7 @@ class TestCourseExportImport(LibraryTestCase): self.source_course = CourseFactory.create(default_store=ModuleStoreEnum.Type.split) self.addCleanup(shutil.rmtree, self.export_dir, ignore_errors=True) - def _setup_source_course_with_library_content(self, publish=False): + def _setup_source_course_with_library_content(self, publish=False, version=None): """ Sets up course with library content. """ @@ -1095,7 +1097,9 @@ class TestCourseExportImport(LibraryTestCase): parent_location=sequential.location, display_name='Test Unit' ) - lc_block = self._add_library_content_block(vertical, self.lib_key, publish_item=publish) + lc_block = self._add_library_content_block( + vertical, self.lib_key, publish_item=publish, other_settings=dict(source_library_version=version) + ) self._refresh_children(lc_block) def get_lib_content_block_children(self, block_location): @@ -1134,13 +1138,18 @@ class TestCourseExportImport(LibraryTestCase): dest_child = self.store.get_item(dest_child_location) self.assertEqual(source_child.display_name, dest_child.display_name) - @ddt.data(True, False) - def test_library_content_on_course_export_import(self, publish_item): + @ddt.data(*itertools.product([False, True], repeat=2)) + @ddt.unpack + def test_library_content_on_course_export_import(self, publish_item, generate_version): """ Verify that library contents in destination and source courses are same after importing the source course into destination course. + + If a library with the specified version does not exist in the modulestore, the import should not fail. """ - self._setup_source_course_with_library_content(publish=publish_item) + self._setup_source_course_with_library_content( + publish=publish_item, version=str(ObjectId()) if generate_version else None + ) # Create a course to import source course. dest_course = CourseFactory.create(default_store=ModuleStoreEnum.Type.split) diff --git a/common/lib/xmodule/xmodule/modulestore/xml_importer.py b/common/lib/xmodule/xmodule/modulestore/xml_importer.py index 6e7e612f52..a45cb7df5f 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_importer.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_importer.py @@ -897,16 +897,20 @@ def _update_and_import_module( if lib_content_block_already_published: return block - # Update library content block's children on draft branch - with store.branch_setting(branch_setting=ModuleStoreEnum.Branch.draft_preferred): - LibraryToolsService(store, user_id).update_children( - block, - version=block.source_library_version, - ) - - # Publish it if importing the course for branch setting published_only. - if store.get_branch_setting() == ModuleStoreEnum.Branch.published_only: - store.publish(block.location, user_id) + try: + # Update library content block's children on draft branch + with store.branch_setting(branch_setting=ModuleStoreEnum.Branch.draft_preferred): + LibraryToolsService(store, user_id).update_children( + block, + version=block.source_library_version, + ) + except ValueError as err: + # The specified library version does not exist. + log.error(err) + else: + # Publish it if importing the course for branch setting published_only. + if store.get_branch_setting() == ModuleStoreEnum.Branch.published_only: + store.publish(block.location, user_id) return block