Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exclude Drag and Drop blocks from the link-scanning step in the Course Optimizer #36321

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cms/djangoapps/contentstore/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,13 @@ def _scan_course_for_links(course_key):
blocks.extend(vertical.get_children())

for block in blocks:
# Excluding 'drag-and-drop-v2' as it contains data of object type instead of string, causing errors,
# and it doesn't contain user-facing links to scan.
if block.category == 'drag-and-drop-v2':
continue
block_id = str(block.usage_key)
block_info = get_block_info(block)
block_data = block_info['data']

url_list = _get_urls(block_data)
urls_to_validate += [[block_id, url] for url in url_list]

Expand Down
45 changes: 44 additions & 1 deletion cms/djangoapps/contentstore/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory # lint-amnesty, pylint: disable=wrong-import-order
from ..tasks import (
export_olx,
update_special_exams_and_publish,
Expand Down Expand Up @@ -365,6 +365,49 @@ def test_number_of_scanned_blocks_equals_blocks_in_course(self, mock_get_urls):
_scan_course_for_links(self.test_course.id)
self.assertEqual(len(expected_blocks), mock_get_urls.call_count)

@mock.patch('cms.djangoapps.contentstore.tasks.get_block_info', autospec=True)
@mock.patch('cms.djangoapps.contentstore.tasks.modulestore', autospec=True)
def test_scan_course_excludes_drag_and_drop(self, mock_modulestore, mock_get_block_info):
"""
Test that `_scan_course_for_links` excludes blocks of category 'drag-and-drop-v2'.
"""
vertical = BlockFactory.create(
category='vertical',
parent_location=self.test_course.location
)
drag_and_drop_block = BlockFactory.create(
category='drag-and-drop-v2',
parent_location=vertical.location,
)
text_block = BlockFactory.create(
category='html',
parent_location=vertical.location,
data='Test Link -> <a href="http://example.com">Example.com</a>'
)

mock_modulestore_instance = mock.Mock()
mock_modulestore.return_value = mock_modulestore_instance
mock_modulestore_instance.get_items.return_value = [vertical]
vertical.get_children = mock.Mock(return_value=[drag_and_drop_block, text_block])

def get_block_side_effect(block):
block_data = getattr(block, 'data', '')
if isinstance(block_data, str):
return {'data': block_data}
raise TypeError("expected string or bytes-like object, got 'dict'")
mock_get_block_info.side_effect = get_block_side_effect

urls = _scan_course_for_links(self.test_course.id)
# The drag-and-drop block should not appear in the results
self.assertFalse(
any(block_id == str(drag_and_drop_block.usage_key) for block_id, _ in urls),
"Drag and Drop blocks should be excluded"
)
self.assertTrue(
any(block_id == str(text_block.usage_key) for block_id, _ in urls),
"Text block should be included"
)

@pytest.mark.asyncio
async def test_every_detected_link_is_validated(self):
'''
Expand Down
Loading