def test_force_publish(self): """ Test 'force_publish' command """ # Add some changes to course chapter = ItemFactory.create(category='chapter', parent_location=self.course.location) self.store.create_child(self.test_user_id, chapter.location, 'html', block_id='html_component') # verify that course has changes. self.assertTrue( self.store.has_changes(self.store.get_item(self.course.location))) # get draft and publish branch versions versions = get_course_versions(six.text_type(self.course.id)) draft_version = versions['draft-branch'] published_version = versions['published-branch'] # verify that draft and publish point to different versions self.assertNotEqual(draft_version, published_version) with mock.patch( 'cms.djangoapps.contentstore.management.commands.force_publish.query_yes_no' ) as patched_yes_no: patched_yes_no.return_value = True # force publish course call_command('force_publish', six.text_type(self.course.id), '--commit') # verify that course has no changes self.assertFalse( self.store.has_changes( self.store.get_item(self.course.location))) # get new draft and publish branch versions versions = get_course_versions(six.text_type(self.course.id)) new_draft_version = versions['draft-branch'] new_published_version = versions['published-branch'] # verify that the draft branch didn't change while the published branch did self.assertEqual(draft_version, new_draft_version) self.assertNotEqual(published_version, new_published_version) # verify that draft and publish point to same versions now self.assertEqual(new_draft_version, new_published_version)
def verify_versions_are_different(self, course): """ Verify draft and published versions point to different locations. Arguments: course (object): a course object. """ # get draft and publish branch versions versions = get_course_versions(six.text_type(course.id)) # verify that draft and publish point to different versions self.assertNotEqual(versions['draft-branch'], versions['published-branch'])
def post(self, request): """ This method force publishes a course if dry-run argument is not selected. If dry-run is selected, this view shows possible outcome if the `force_publish_course` modulestore method is executed. Arguments: course_id (string): a request parameter containing course id is_dry_run (string): a request parameter containing dry run value. It is obtained from checkbox so it has either values 'on' or ''. """ course_id = request.POST.get('course-id') self.context.update({'form_data': {'course_id': course_id}}) try: course_usage_key = self.validate_course_key(course_id) except InvalidKeyError: self.context['error'] = True self.context['msg'] = COURSE_KEY_ERROR_MESSAGES[ 'invalid_course_key'] except ItemNotFoundError as exc: self.context['error'] = True self.context['msg'] = text_type(exc) except ValidationError as exc: self.context['error'] = True self.context['msg'] = text_type(exc) if self.context['error']: return self.render_response() source_store = modulestore()._get_modulestore_for_courselike( course_usage_key) # pylint: disable=protected-access if not hasattr(source_store, 'force_publish_course'): self.context['msg'] = _( 'Force publishing course is not supported with old mongo courses.' ) log.warning( u'Force publishing course is not supported with old mongo courses. \ %s attempted to force publish the course %s.', request.user, course_id, exc_info=True) return self.render_response() current_versions = self.get_course_branch_versions( get_course_versions(course_id)) # if publish and draft are NOT different if current_versions['published-branch'] == current_versions[ 'draft-branch']: self.context['msg'] = _('Course is already in published state.') log.warning( u'Course is already in published state. %s attempted to force publish the course %s.', request.user, course_id, exc_info=True) return self.render_response() self.context['current_versions'] = current_versions log.info(u'%s dry ran force publish the course %s.', request.user, course_id, exc_info=True) return self.render_response()