|
| 1 | +""" |
| 2 | +Tests for VideoConfigService. |
| 3 | +""" |
| 4 | + |
| 5 | +import unittest |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from opaque_keys.edx.locator import CourseLocator |
| 10 | +from xblock.field_data import DictFieldData |
| 11 | +from xblock.fields import ScopeIds |
| 12 | + |
| 13 | +from openedx.core.djangoapps.video_config.services import VideoConfigService |
| 14 | +from xmodule.tests import get_test_descriptor_system |
| 15 | +from xmodule.video_block import VideoBlock |
| 16 | + |
| 17 | + |
| 18 | +class VideoConfigServiceTestCase(unittest.TestCase): |
| 19 | + """ |
| 20 | + Unit tests for the VideoConfigService class. |
| 21 | + """ |
| 22 | + |
| 23 | + def instantiate_video_block(self, **field_data): |
| 24 | + """Instantiate a video block with the given field data (e.g. data=xml_string).""" |
| 25 | + if field_data.get('data', None): |
| 26 | + field_data = VideoBlock.parse_video_xml(field_data['data']) |
| 27 | + system = get_test_descriptor_system() |
| 28 | + course_key = CourseLocator('org', 'course', 'run') |
| 29 | + usage_key = course_key.make_usage_key('video', 'SampleProblem') |
| 30 | + return system.construct_xblock_from_class( |
| 31 | + VideoBlock, |
| 32 | + scope_ids=ScopeIds(None, None, usage_key, usage_key), |
| 33 | + field_data=DictFieldData(field_data), |
| 34 | + ) |
| 35 | + |
| 36 | + def test_video_with_multiple_transcripts_translation_retrieval(self): |
| 37 | + """ |
| 38 | + Test translation retrieval of a video block with |
| 39 | + multiple transcripts uploaded by a user. |
| 40 | + """ |
| 41 | + xml_data_transcripts = ''' |
| 42 | + <video display_name="Test Video" |
| 43 | + youtube="1.0:p2Q6BrNhdh8,0.75:izygArpw-Qo,1.25:1EeWXzPdhSA,1.5:rABDYkeK0x8" |
| 44 | + show_captions="false" |
| 45 | + download_track="false" |
| 46 | + start_time="00:00:01" |
| 47 | + download_video="false" |
| 48 | + end_time="00:01:00"> |
| 49 | + <source src="http://www.example.com/source.mp4"/> |
| 50 | + <track src="http://www.example.com/track"/> |
| 51 | + <handout src="http://www.example.com/handout"/> |
| 52 | + <transcript language="ge" src="subs_grmtran1.srt" /> |
| 53 | + <transcript language="hr" src="subs_croatian1.srt" /> |
| 54 | + </video> |
| 55 | + ''' |
| 56 | + |
| 57 | + block = self.instantiate_video_block(data=xml_data_transcripts) |
| 58 | + video_config_service = block.runtime.service(block, 'video_config') |
| 59 | + assert isinstance(video_config_service, VideoConfigService) |
| 60 | + translations = video_config_service.available_translations( |
| 61 | + block, block.get_transcripts_info() |
| 62 | + ) |
| 63 | + assert sorted(translations) == sorted(['hr', 'ge']) |
| 64 | + |
| 65 | + def test_video_with_no_transcripts_translation_retrieval(self): |
| 66 | + """ |
| 67 | + Test translation retrieval of a video block with |
| 68 | + no transcripts uploaded by a user- ie, that retrieval |
| 69 | + does not throw an exception. |
| 70 | + """ |
| 71 | + block = self.instantiate_video_block(data=None) |
| 72 | + video_config_service = block.runtime.service(block, 'video_config') |
| 73 | + assert isinstance(video_config_service, VideoConfigService) |
| 74 | + translations_with_fallback = video_config_service.available_translations( |
| 75 | + block, block.get_transcripts_info() |
| 76 | + ) |
| 77 | + assert translations_with_fallback == ['en'] |
| 78 | + |
| 79 | + with patch.dict(settings.FEATURES, FALLBACK_TO_ENGLISH_TRANSCRIPTS=False): |
| 80 | + # Some organizations don't have English transcripts for all videos |
| 81 | + # This feature makes it configurable |
| 82 | + translations_no_fallback = video_config_service.available_translations( |
| 83 | + block, block.get_transcripts_info() |
| 84 | + ) |
| 85 | + assert translations_no_fallback == [] |
0 commit comments