Commit 6d567013 by Oksana Slusarenro

Add ui in studio for conditional_module.

parent bcf260ce
......@@ -11,7 +11,8 @@ class @Conditional
return
@url = @el.data('url')
@render(element)
if @url
@render(element)
render: (element) ->
$.postWithPrefix "#{@url}/conditional_get", (response) =>
......
......@@ -2,6 +2,7 @@ import json
import unittest
from fs.memoryfs import MemoryFS
from lxml import etree
from mock import Mock, patch
from xblock.field_data import DictFieldData
......@@ -11,8 +12,9 @@ from opaque_keys.edx.locations import SlashSeparatedCourseKey, Location
from xmodule.modulestore.xml import ImportSystem, XMLModuleStore, CourseLocationManager
from xmodule.conditional_module import ConditionalDescriptor
from xmodule.tests import DATA_DIR, get_test_system, get_test_descriptor_system
from xmodule.x_module import STUDENT_VIEW
from xmodule.tests.xml import factories as xml, XModuleXmlImportTest
from xmodule.validation import StudioValidationMessage
from xmodule.x_module import STUDENT_VIEW, AUTHOR_VIEW
ORG = 'test_org'
COURSE = 'conditional' # name of directory with course data
......@@ -37,6 +39,13 @@ class DummySystem(ImportSystem):
raise Exception("Shouldn't be called")
class ConditionalModuleFactory(xml.XmlImportFactory):
"""
Factory for generating ConditionalModule for testing purposes
"""
tag = 'conditional'
class ConditionalFactory(object):
"""
A helper class to create a conditional module and associated source and child modules
......@@ -94,6 +103,8 @@ class ConditionalFactory(object):
cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None)
field_data = DictFieldData({
'data': '<conditional/>',
'condional_attr': 'attempted',
'conditional_value': 'true',
'xml_attributes': {'attempted': 'true'},
'children': [child_descriptor.location],
})
......@@ -146,9 +157,9 @@ class ConditionalModuleBasicTest(unittest.TestCase):
def test_handle_ajax(self):
modules = ConditionalFactory.create(self.test_system)
modules['cond_module'].save()
modules['source_module'].is_attempted = "false"
ajax = json.loads(modules['cond_module'].handle_ajax('', ''))
modules['cond_module'].save()
print "ajax: ", ajax
html = ajax['html']
self.assertFalse(any(['This is a secret' in item for item in html]))
......@@ -167,8 +178,8 @@ class ConditionalModuleBasicTest(unittest.TestCase):
and that the condition is not satisfied.
'''
modules = ConditionalFactory.create(self.test_system, source_is_error_module=True)
ajax = json.loads(modules['cond_module'].handle_ajax('', ''))
modules['cond_module'].save()
ajax = json.loads(modules['cond_module'].handle_ajax('', ''))
html = ajax['html']
self.assertFalse(any(['This is a secret' in item for item in html]))
......@@ -304,3 +315,105 @@ class ConditionalModuleXmlTest(unittest.TestCase):
conditional.parse_sources(conditional.xml_attributes),
['i4x://HarvardX/ER22x/poll_question/T15_poll', 'i4x://HarvardX/ER22x/poll_question/T16_poll']
)
def test_conditional_module_parse_attr_values(self):
root = '<conditional attempted="false"></conditional>'
xml_object = etree.XML(root)
definition = ConditionalDescriptor.definition_from_xml(xml_object, Mock())[0]
expected_definition = {
'show_tag_list': [],
'condional_attr': 'attempted',
'conditional_value': 'false',
'conditional_message': ''
}
self.assertEqual(definition, expected_definition)
def test_presence_attributes_in_xml_attributes(self):
modules = ConditionalFactory.create(self.test_system)
modules['cond_module'].save()
modules['cond_module'].definition_to_xml(Mock())
expected_xml_attributes = {
'attempted': 'true',
'message': '{link} must be attempted before this will become visible.',
'sources': ''
}
self.assertDictEqual(modules['cond_module'].xml_attributes, expected_xml_attributes)
class ConditionalModuleStudioTest(XModuleXmlImportTest):
"""
Unit tests for how conditional test interacts with Studio.
"""
def setUp(self):
super(ConditionalModuleStudioTest, self).setUp()
course = xml.CourseFactory.build()
sequence = xml.SequenceFactory.build(parent=course)
conditional = ConditionalModuleFactory(
parent=sequence,
attribs={
'group_id_to_child': '{"0": "i4x://edX/xml_test_course/html/conditional_0"}'
}
)
xml.HtmlFactory(parent=conditional, url_name='conditional_0', text='This is a secret HTML')
self.course = self.process_xml(course)
self.sequence = self.course.get_children()[0]
self.conditional = self.sequence.get_children()[0]
self.module_system = get_test_system()
self.module_system.descriptor_runtime = self.course._runtime # pylint: disable=protected-access
user = Mock(username='ma', email='ma@edx.org', is_staff=False, is_active=True)
self.conditional.bind_for_student(
self.module_system,
user.id
)
def test_render_author_view(self,):
"""
Test the rendering of the Studio author view.
"""
def create_studio_context(root_xblock, is_unit_page):
"""
Context for rendering the studio "author_view".
"""
return {
'reorderable_items': set(),
'root_xblock': root_xblock,
'is_unit_page': is_unit_page
}
context = create_studio_context(self.conditional, False)
html = self.module_system.render(self.conditional, AUTHOR_VIEW, context).content
self.assertIn('This is a secret HTML', html)
context = create_studio_context(self.sequence, True)
html = self.module_system.render(self.conditional, AUTHOR_VIEW, context).content
self.assertNotIn('This is a secret HTML', html)
def test_non_editable_settings(self):
"""
Test the settings that are marked as "non-editable".
"""
non_editable_metadata_fields = self.conditional.non_editable_metadata_fields
self.assertIn(ConditionalDescriptor.due, non_editable_metadata_fields)
self.assertIn(ConditionalDescriptor.is_practice_exam, non_editable_metadata_fields)
self.assertIn(ConditionalDescriptor.is_time_limited, non_editable_metadata_fields)
self.assertIn(ConditionalDescriptor.default_time_limit_minutes, non_editable_metadata_fields)
self.assertIn(ConditionalDescriptor.show_tag_list, non_editable_metadata_fields)
def test_validation_messages(self):
"""
Test the validation message for a correctly configured conditional.
"""
self.conditional.sources_list = None
validation = self.conditional.validate()
self.assertEqual(
validation.summary.text,
u"This component has no source components configured yet."
)
self.assertEqual(validation.summary.type, StudioValidationMessage.NOT_CONFIGURED)
self.assertEqual(validation.summary.action_class, 'edit-button')
self.assertEqual(validation.summary.action_label, u"Configure list of sources")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment