diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 22f8f24..012e250 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -67,14 +67,13 @@ customisation.published = false customisation.dirty = false customisation.customization_of = @template.dmptemplate_id - customisation.visibility = :private customisation.dmptemplate_id = loop do random = rand 2147483647 # max int field in psql break random unless Template.exists?(dmptemplate_id: random) end - customisation.save! + customisation.save # need to mark all of the original funder's phases, questions, # sections as not-modifiable @@ -104,7 +103,7 @@ # Only allow the current version to be updated if current != @template - redirect_to admin_template_template_path(@template), notice: _('You can publish a historical version of this template.') + redirect_to admin_template_template_path(@template), notice: _('You can not publish a historical version of this template.') else # Unpublish the older published version if there is one @@ -139,6 +138,20 @@ p.save end + # Make all of the new version's templates phase/section/questions modifiable + new_version.phases.each do |p| + p.sections.each do |s| + s.questions.each do |q| + q.modifiable = true + q.save + end + s.modifiable = true + s.save + end + p.modifiable = true + p.save + end + redirect_to admin_index_template_path(current_user.org) end end @@ -151,8 +164,14 @@ # Unpublish the live version @template = Template.live(current_user.org, template.dmptemplate_id) - @template.published = false - @template.save + + if @template.nil? + flash[:notice] = _('That template is not currently published.') + else + @template.published = false + @template.save + flash[:notice] = _('Your template is no longer published.') + end redirect_to admin_index_template_path(current_user.org) end @@ -188,8 +207,8 @@ redirect_to admin_template_template_path(@template), notice: _('You can not edit a historical version of this template.') else - if @template.description == params["template-desc"] || - @template.title == params[:template][:title] + if @template.description != params["template-desc"] || + @template.title != params[:template][:title] @template.dirty = true end @@ -251,7 +270,7 @@ current = Template.current(current_user.org, @template.dmptemplate_id) # Only allow the current version to be updated - if current != @template + if current == @template if @template.destroy redirect_to admin_index_template_path else @@ -261,6 +280,7 @@ end else flash[:notice] = _('You cannot delete historical versions of this template.') + redirect_to admin_index_template_path end end diff --git a/test/functional/templates_controller_test.rb b/test/functional/templates_controller_test.rb index 84745ba..97e837c 100644 --- a/test/functional/templates_controller_test.rb +++ b/test/functional/templates_controller_test.rb @@ -121,16 +121,17 @@ # Try to delete a historical version should fail delete admin_destroy_template_path(prior) - assert_response :success - assert flash[:notice].start_with?(_('Could not delete')) - assert_not Template.find(prior).nil? + assert_equal _('You cannot delete historical versions of this template.'), flash[:notice] + assert_response :redirect + assert_redirected_to admin_index_template_path + assert_not Template.find(prior.id).nil? # Try to delete the current version should work delete admin_destroy_template_path(current) assert_response :redirect assert_redirected_to admin_index_template_path assert_raise ActiveRecord::RecordNotFound do - Template.find(current).nil? + Template.find(current.id).nil? end assert_equal prior, Template.current(@template.org, family), "expected the old version to now be the current version" end @@ -186,13 +187,11 @@ assert_response :redirect assert_redirected_to admin_template_template_url(prior) assert assigns(:template) - assert assigns(:hash) # Make sure we get the right response when editing an unpublished template put admin_update_template_path(current), {template: params} assert_equal _('Information was successfully updated.'), flash[:notice] - assert_response :redirect - assert_redirected_to admin_template_template_url(Template.last.id) + assert_response :success assert assigns(:template) assert assigns(:hash) assert_equal 'ABCD', current.reload.title, "expected the record to have been updated" @@ -215,24 +214,22 @@ template = Template.live(funder, ids.last) # Make sure we are redirected if we're not logged in - put admin_customize_template(template) + put admin_customize_template_path(template) assert_unauthorized_redirect_to_root_path sign_in @user - put admin_customize_template(template) + put admin_customize_template_path(template) - customization = Template.find_by(org: @user.org, customization_of: template.dmptemplate_id) - + customization = Template.where(org: @user.org, customization_of: template.dmptemplate_id).last + assert_response :redirect - assert_redirected_to admin_template_template_url(customization) + assert_redirected_to admin_template_template_url(Template.last) assert assigns(:template) - assert assigns(:hash) - assert_equal 0, customisation.version - assert_equal 0, customisation.visibility - assert_not customisation.published - assert_not customisation.dirty + assert_equal 0, customization.version + assert_not customization.published? + assert_not customization.dirty? # Make sure the funder templates data is not modifiable! customization.phases.each do |p| @@ -268,17 +265,14 @@ assert_response :redirect assert_redirected_to admin_template_template_url(prior) assert assigns(:template) - assert assigns(:hash) # Publish the current template put admin_publish_template_path(current) assert_response :redirect assert_redirected_to admin_index_template_path(@user.org) - assert assigns(:funder_templates) - assert assigns(:org_templates) # Make sure it versioned properly - current.reload + current = Template.includes(:phases, :sections, :questions).find(current.id) new_version = Template.current(@user.org, family) assert_not_equal current.id = new_version.id, "expected it to create a new version" assert_equal (current.version + 1), new_version.version, "expected the version to have incremented" @@ -290,11 +284,11 @@ # The old version's phases/sections/questions should NOT be modifiable current.phases.each do |p| - assert_not p.modifiable + assert_not p.modifiable? p.sections.each do |s| - assert_not s.modifiable + assert_not s.modifiable? s.questions.each do |q| - assert_not q.modifiable + assert_not q.modifiable? end end end @@ -327,12 +321,20 @@ current = Template.current(@user.org, family) - # Publish the current template + # Try to unpublish a template that is not published put admin_unpublish_template_path(current) + assert_equal _('That template is not currently published.'), flash[:notice] assert_response :redirect assert_redirected_to admin_index_template_path(@user.org) - assert assigns(:funder_templates) - assert assigns(:org_templates) + + # Publish it so we can unpublish + put admin_publish_template_path(current) + assert_not Template.live(@user.org, family).nil? + + put admin_unpublish_template_path(current) + assert_equal _('Your template is no longer published.'), flash[:notice] + assert_response :redirect + assert_redirected_to admin_index_template_path(@user.org) # Make sure there are no published versions assert Template.live(@user.org, family).nil? diff --git a/test/integration/template_versioning_test.rb b/test/integration/template_versioning_test.rb index 545691d..517ad26 100644 --- a/test/integration/template_versioning_test.rb +++ b/test/integration/template_versioning_test.rb @@ -22,7 +22,7 @@ # ---------------------------------------------------------- test 'template gets versioned when its details are updated but it is already published' do # Publish the template - put admin_update_template_path(@template), {template: {published: "1"}} + put admin_publish_template_path(@template) @template = Template.current(@user.org, @dmptemplate_id) assert_equal (@initial_version + 1), @template.version, "expected the version to have incremented" @@ -51,17 +51,32 @@ # ---------------------------------------------------------- test 'template gets versioned when its phases are modified and it is already published' do + @template.dirty = false + @template.save! + put admin_update_phase_path @template.phases.first, {phase: {title: 'UPDATED'}} + @template.reload + assert @template.dirty end # ---------------------------------------------------------- test 'template gets versioned when its sections are modified and it is already published' do + @template.dirty = false + @template.save! + put admin_update_section_path @template.phases.first.sections.first, {section: {title: 'UPDATED'}} + @template.reload + assert @template.dirty end # ---------------------------------------------------------- test 'template gets versioned when its questions are modified and it is already published' do + @template.dirty = false + @template.save! + put admin_update_question_path @template.phases.first.sections.first.questions.first, {question: {text: 'UPDATED'}} + @template.reload + assert @template.dirty end # ---------------------------------------------------------- @@ -76,11 +91,24 @@ assert_equal false, @template.published?, "expected the version to have remained unpublished" end + # ---------------------------------------------------------- + test 'publishing a plan unpublishes the old published plan' do + put admin_publish_template_path(@template) + assert_not Template.live(@user.org, @dmptemplate_id).nil? + assert_equal 1, Template.where(org: @user.org, dmptemplate_id: @dmptemplate_id, published: true).count + end + + # ---------------------------------------------------------- + test 'unpublishing a plan makes all historical versions unpublished' do + put admin_publish_template_path(@template) + put admin_unpublish_template_path(@template) + assert Template.live(@user.org, @dmptemplate_id).nil? + end # ---------------------------------------------------------- test 'plans get attached to the appropriate template version' do # Template is published - put admin_update_template_path(@template), {template: {published: "1"}} + put admin_publish_template_path(@template) @template = Template.current(@user.org, @dmptemplate_id) liveA = Template.live(@user.org, @dmptemplate_id) @@ -106,7 +134,7 @@ assert_equal liveA, planA.template, "expected PlanA to still be attached to the original published version" # Template v2 is published - put admin_update_template_path(@template), {template: {published: "1"}} + put admin_publish_template_path(@template) @template = Template.current(@user.org, @dmptemplate_id) liveB = Template.live(@user.org, @dmptemplate_id)