diff --git a/common/lib/xmodule/xmodule/modulestore/search.py b/common/lib/xmodule/xmodule/modulestore/search.py index 47fd799d79..f04974042f 100644 --- a/common/lib/xmodule/xmodule/modulestore/search.py +++ b/common/lib/xmodule/xmodule/modulestore/search.py @@ -60,14 +60,14 @@ def path_to_location(modulestore, usage_key): # Found it! path = (next_usage, path) return flatten(path) + elif parent is None: + # Orphaned item. + return None # otherwise, add parent locations at the end newpath = (next_usage, path) queue.append((parent, newpath)) - # If we're here, there is no path - return None - if not modulestore.has_item(usage_key): raise ItemNotFoundError(usage_key) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py index f4c574d23f..62aa123b72 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py @@ -22,7 +22,8 @@ from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator from django.conf import settings from xmodule.modulestore.tests.factories import check_mongo_calls from xmodule.modulestore.search import path_to_location -from xmodule.modulestore.exceptions import DuplicateCourseError +from xmodule.modulestore.exceptions import DuplicateCourseError, NoPathToItem + if not settings.configured: settings.configure() from xmodule.modulestore.mixed import MixedModuleStore @@ -723,6 +724,18 @@ class TestMixedModuleStore(unittest.TestCase): with self.assertRaises(ItemNotFoundError): path_to_location(self.store, location) + # Orphaned items should not be found. + orphan = course_key.make_usage_key('chapter', 'OrphanChapter') + self.store.create_item( + self.user_id, + orphan.course_key, + orphan.block_type, + block_id=orphan.block_id + ) + + with self.assertRaises(NoPathToItem): + path_to_location(self.store, orphan) + def test_xml_path_to_location(self): """ Make sure that path_to_location works: should be passed a modulestore