From 152ae4b1c29bd6dd2e7bd891f28690639d286f70 Mon Sep 17 00:00:00 2001 From: muhammad-ammar Date: Tue, 26 Jul 2016 16:32:45 +0500 Subject: [PATCH] fix xblock urls for verticals TNL-5003 --- common/lib/xmodule/xmodule/tests/helpers.py | 15 +++++++++++++++ .../xmodule/xmodule/tests/test_sequence.py | 15 +-------------- .../xmodule/xmodule/tests/test_vertical.py | 19 ++++++++++++++++--- common/lib/xmodule/xmodule/vertical_block.py | 10 +++++++++- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/helpers.py b/common/lib/xmodule/xmodule/tests/helpers.py index 28e593dd85..9cb4decab1 100644 --- a/common/lib/xmodule/xmodule/tests/helpers.py +++ b/common/lib/xmodule/xmodule/tests/helpers.py @@ -5,6 +5,8 @@ Utility methods for unit tests. import filecmp from path import Path as path +from xblock.reference.user_service import XBlockUser, UserService + def directories_equal(directory1, directory2): """ @@ -24,3 +26,16 @@ def directories_equal(directory1, directory2): return True return compare_dirs(path(directory1), path(directory2)) + + +class StubUserService(UserService): + """ + Stub UserService for testing the sequence module. + """ + def get_current_user(self): + """ + Implements abstract method for getting the current user. + """ + user = XBlockUser() + user.opt_attrs['edx-platform.username'] = 'bilbo' + return user diff --git a/common/lib/xmodule/xmodule/tests/test_sequence.py b/common/lib/xmodule/xmodule/tests/test_sequence.py index f98319bee9..5643ada5ca 100644 --- a/common/lib/xmodule/xmodule/tests/test_sequence.py +++ b/common/lib/xmodule/xmodule/tests/test_sequence.py @@ -7,27 +7,14 @@ import ddt from django.utils.timezone import now from freezegun import freeze_time from mock import Mock -from xblock.reference.user_service import XBlockUser, UserService from xmodule.tests import get_test_system +from xmodule.tests.helpers import StubUserService from xmodule.tests.xml import XModuleXmlImportTest from xmodule.tests.xml import factories as xml from xmodule.x_module import STUDENT_VIEW from xmodule.seq_module import SequenceModule -class StubUserService(UserService): - """ - Stub UserService for testing the sequence module. - """ - def get_current_user(self): - """ - Implements abstract method for getting the current user. - """ - user = XBlockUser() - user.opt_attrs['edx-platform.username'] = 'test user' - return user - - @ddt.ddt class SequenceBlockTestCase(XModuleXmlImportTest): """ diff --git a/common/lib/xmodule/xmodule/tests/test_vertical.py b/common/lib/xmodule/xmodule/tests/test_vertical.py index f381f7dda7..54d4d40ddb 100644 --- a/common/lib/xmodule/xmodule/tests/test_vertical.py +++ b/common/lib/xmodule/xmodule/tests/test_vertical.py @@ -1,9 +1,11 @@ """ Tests for vertical module. """ - +import ddt +from mock import Mock from fs.memoryfs import MemoryFS from xmodule.tests import get_test_system +from xmodule.tests.helpers import StubUserService from xmodule.tests.xml import XModuleXmlImportTest from xmodule.tests.xml import factories as xml from xmodule.x_module import STUDENT_VIEW, AUTHOR_VIEW @@ -41,6 +43,7 @@ class BaseVerticalBlockTest(XModuleXmlImportTest): self.default_context = {"bookmarked": False, "username": self.username} +@ddt.ddt class VerticalBlockTestCase(BaseVerticalBlockTest): """ Tests for the VerticalBlock. @@ -54,11 +57,21 @@ class VerticalBlockTestCase(BaseVerticalBlockTest): self.assertIn('bookmarked', content) self.assertIn('show_bookmark_button', content) - def test_render_student_view(self): + @ddt.unpack + @ddt.data( + {'context': None}, + {'context': {}} + ) + def test_render_student_view(self, context): """ Test the rendering of the student view. """ - html = self.module_system.render(self.vertical, STUDENT_VIEW, self.default_context).content + self.module_system._services['bookmarks'] = Mock() # pylint: disable=protected-access + self.module_system._services['user'] = StubUserService() # pylint: disable=protected-access + + html = self.module_system.render( + self.vertical, STUDENT_VIEW, self.default_context if context is None else context + ).content self.assertIn(self.test_html_1, html) self.assertIn(self.test_html_2, html) self.assert_bookmark_info_in(html) diff --git a/common/lib/xmodule/xmodule/vertical_block.py b/common/lib/xmodule/xmodule/vertical_block.py index 8edfd42b56..91a03863de 100644 --- a/common/lib/xmodule/xmodule/vertical_block.py +++ b/common/lib/xmodule/xmodule/vertical_block.py @@ -20,6 +20,7 @@ log = logging.getLogger(__name__) CLASS_PRIORITY = ['video', 'problem'] +@XBlock.needs('user', 'bookmarks') class VerticalBlock(SequenceFields, XModuleFields, StudioEditableBlock, XmlParserMixin, MakoTemplateBlockBase, XBlock): """ Layout XBlock for rendering subblocks vertically. @@ -41,7 +42,14 @@ class VerticalBlock(SequenceFields, XModuleFields, StudioEditableBlock, XmlParse fragment = Fragment() contents = [] - child_context = {} if not context else copy(context) + if context: + child_context = copy(context) + else: + child_context = { + 'bookmarked': self.runtime.service(self, 'bookmarks').is_bookmarked(usage_key=self.location), # pylint: disable=no-member + 'username': self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.username'] + } + child_context['child_of_vertical'] = True # pylint: disable=no-member