def test_complex_xblock_tree(self, _mock1, _mock2): """ Test a Pathway with a deep XBlock tree and inter-bundle-links """ # Create the following XBlock tree # Library 2 # unit "alpha" # -> link to library 1's bundle ("link1") # -> unit "beta" (in Library 1) # -> html "gamma" # Then make sure we can add that whole hierarchy into a pathway. beta_key = library_api.create_library_block(self.lib1.key, "unit", "beta").usage_key gamma_key = library_api.create_library_block_child(beta_key, "html", "gamma").usage_key library_api.set_library_block_olx(gamma_key, '<html>This is gamma.</html>') library_api.publish_changes(self.lib1.key) library_api.create_bundle_link(self.lib2.key, 'link1', self.lib1.key) alpha_key = library_api.create_library_block(self.lib2.key, "unit", "alpha").usage_key library_api.set_library_block_olx(alpha_key, ''' <unit display_name="alpha"> <xblock-include source="link1" definition="unit/beta" usage_hint="beta" /> </unit> ''') library_api.publish_changes(self.lib2.key) # Create a new pathway: response = self.client.post(URL_CREATE_PATHWAY, { "owner_user_id": self.user.id, "draft_data": { "title": "A Commplex Pathway", "description": "Complex pathway for testing", "data": {}, "items": [ {"original_usage_id": str(alpha_key)}, ], }, }, format='json') self.assertEqual(response.status_code, 200) pathway_id = response.data["id"] pathway_url = URL_GET_PATHWAY.format(pathway_id=pathway_id) # Now publish the pathway: response = self.client.post(pathway_url + 'publish/') self.assertEqual(response.status_code, 200) # Get the resulting pathway: response = self.client.get(pathway_url) self.assertEqual(response.status_code, 200) # Get the usage ID of the root 'alpha' XBlock: alpha_key_pathway = UsageKey.from_string(response.data["published_data"]["items"][0]["usage_id"]) self.assertNotEqual(alpha_key_pathway, alpha_key) block = xblock_api.load_block(alpha_key_pathway, user=self.other_user) # Render the block and all its children - make sure even the descendants are rendered: fragment = block.render('student_view', context={}) self.assertIn('This is gamma.', fragment.content)
def post(self, request, lib_key_str): """ Create a new link in this library. """ key = LibraryLocatorV2.from_string(lib_key_str) api.require_permission_for_library_key(key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY) serializer = LibraryBundleLinkSerializer(data=request.data) serializer.is_valid(raise_exception=True) target_key = LibraryLocatorV2.from_string(serializer.validated_data['opaque_key']) api.create_bundle_link( library_key=key, link_id=serializer.validated_data['id'], target_opaque_key=target_key, version=serializer.validated_data['version'], # a number, or None for "use latest version" ) return Response({})