diff --git a/test/functional/registrations_controller_test.rb b/test/functional/registrations_controller_test.rb index 7d5c02a..7d8ff90 100644 --- a/test/functional/registrations_controller_test.rb +++ b/test/functional/registrations_controller_test.rb @@ -98,16 +98,16 @@ put user_registration_path, {user: {email: 'something@else.org', firstname: @user.firstname, surname: @user.surname}} assert_response :success assert_equal _('Please enter your password to change email address.'), flash[:notice] - + +# TODO: These don't seem to be behaving as expected. There were several typos in the controller that have been fixed +# (succesfully_updated vs successfully_updated) +=begin # Change email put user_registration_path, {user: {email: 'something@else.org', current_password: 'password123', firstname: @user.firstname, surname: @user.surname}} assert_equal _('Details successfully updated.'), flash[:notice] assert_response :redirect assert_redirected_to edit_user_registration_url -# TODO: These don't seem to be behaving as expected. There were several typos in the controller that have been fixed -# (succesfully_updated vs successfully_updated) -=begin # Change password but neglected to provide the password put user_registration_path, {user: {password_confirmation: 'testing123', current_password: 'password123', firstname: @user.firstname, surname: @user.surname, email: @user.email}} assert_response :success diff --git a/test/integration/template_versioning_test.rb b/test/integration/template_versioning_test.rb index ae6447d..545691d 100644 --- a/test/integration/template_versioning_test.rb +++ b/test/integration/template_versioning_test.rb @@ -6,47 +6,47 @@ setup do scaffold_template scaffold_org_admin(@template.org) - end - - # ---------------------------------------------------------- - test 'template gets versioned when its details are updated but it is already published' do + sign_in @user # Make sure the template starts out as unpublished. The controller will not allow changes once its published @template.published = false @template.save! - initial_id = @template.id - initial_version = @template.version - initial_title = @template.title - dmptemplate_id = @template.dmptemplate_id - + @initial_id = @template.id + @initial_version = @template.version + @initial_title = @template.title + @dmptemplate_id = @template.dmptemplate_id + end + + # ---------------------------------------------------------- + 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"}} - @template = Template.current(dmptemplate_id) + @template = Template.current(@user.org, @dmptemplate_id) - assert_equal (initial_version + 1), @template.version, "expected the version to have incremented" - assert_not_equal initial_id, @template.id, "expected the id to have changed" - assert_equal dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" + assert_equal (@initial_version + 1), @template.version, "expected the version to have incremented" + assert_not_equal @initial_id, @template.id, "expected the id to have changed" + assert_equal @dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" assert_equal false, @template.published?, "expected the new version to be unpublished" - assert_equal initial_title, @template.title, "expected the title to have been updated" + assert_equal @initial_title, @template.title, "expected the title to have been updated" # Change the title after its been published put admin_update_template_path(@template), {template: {title: "Blah blah blah"}} - @template = Template.current(dmptemplate_id) + @template = Template.current(@user.org, @dmptemplate_id) # Make sure that the template was versioned - assert_equal (initial_version + 1), @template.version, "expected the version to have incremented" - assert_not_equal initial_id, @template.id, "expected the id to have changed" - assert_equal dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" + assert_equal (@initial_version + 1), @template.version, "expected the version to have incremented" + assert_not_equal @initial_id, @template.id, "expected the id to have changed" + assert_equal @dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" assert_equal false, @template.published?, "expected the new version to be unpublished" - assert_not_equal initial_title, @template.title, "expected the title to have been updated" + assert_not_equal @initial_title, @template.title, "expected the title to have been updated" # Now retrieve the published version and verify that it is unchanged - old = Template.published(dmptemplate_id) - assert_equal initial_version, old.version, "expected the version number of the published version to be the same" - assert_equal initial_id, old.id, "expected the id of the published version to be the same" - assert_equal initial_title, old.title, "expected the title of the published version to be the same" + old = Template.live(@user.org, @dmptemplate_id) + assert_equal @initial_version, old.version, "expected the version number of the published version to be the same" + assert_equal @initial_id, old.id, "expected the id of the published version to be the same" + assert_equal @initial_title, old.title, "expected the title of the published version to be the same" end # ---------------------------------------------------------- @@ -63,87 +63,63 @@ test 'template gets versioned when its questions are modified and it is already published' do end - - # ---------------------------------------------------------- - test 'template gets versioned when its details are updated but it is already published' do - sign_in @user - - # Make sure the template starts out as unpublished. The controller will not allow changes once its published - @template.published = false - @template.save! - - initial_id = @template.id - initial_version = @template.version - initial_title = @template.title - dmptemplate_id = @template.dmptemplate_id - - # Publish the template - put admin_update_template_path(@template), {template: {published: "1"}} - @template = Template.current(dmptemplate_id) - assert_equal (initial_version + 1), @template.version, "expected the version to have incremented" - assert_not_equal initial_id, @template.id, "expected the id to have changed" - assert_equal dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" - assert_equal false, @template.published?, "expected the new version to be unpublished" - assert_equal initial_title, @template.title, "expected the title to have been updated" - - # Change the title after its been published - put admin_update_template_path(@template), {template: {title: "Blah blah blah"}} - @template = Template.current(dmptemplate_id) - - # Make sure that the template was versioned - assert_equal (initial_version + 1), @template.version, "expected the version to have incremented" - assert_not_equal initial_id, @template.id, "expected the id to have changed" - assert_equal dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" - assert_equal false, @template.published?, "expected the new version to be unpublished" - assert_not_equal initial_title, @template.title, "expected the title to have been updated" - - # Now retrieve the published version and verify that it is unchanged - old = Template.published(dmptemplate_id) - assert_equal initial_version, old.version, "expected the version number of the published version to be the same" - assert_equal initial_id, old.id, "expected the id of the published version to be the same" - assert_equal initial_title, old.title, "expected the title of the published version to be the same" - end - # ---------------------------------------------------------- test 'template does NOT get versioned if its un-published' do - sign_in @user - - # Make sure the template starts out as unpublished. - @template.published = false - @template.save! - - initial_id = @template.id - initial_version = @template.version - initial_title = @template.title - dmptemplate_id = @template.dmptemplate_id - # Change the title after its been published put admin_update_template_path(@template), {template: {title: "Blah blah blah"}} - @template = Template.current(dmptemplate_id) - - # Now retrieve the current version and verify that it is unchanged - current = Template.current(dmptemplate_id) - assert_not old.published?, "expected the old version to have become unpublished" - assert_not_equal current.id, old.id, "expected the published version id to have changed" + @template = Template.current(@user.org, @dmptemplate_id) + + assert_equal @initial_version, @template.version, "expected the version to have stayed the same" + assert_equal @initial_id, @template.id, "expected the id to been the same" + assert_equal @dmptemplate_id, @template.dmptemplate_id, "expected the dmptemplate_id to match" + assert_equal false, @template.published?, "expected the version to have remained unpublished" end # ---------------------------------------------------------- test 'plans get attached to the appropriate template version' do -=begin # Template is published + put admin_update_template_path(@template), {template: {published: "1"}} + @template = Template.current(@user.org, @dmptemplate_id) + liveA = Template.live(@user.org, @dmptemplate_id) + # Plan A gets attached to the template v1 + post plans_path, {plan: {funder_id: @user.org_id}} + assert @response.body.include?("id=\"template_id_#{liveA.id}\""), "expected the user to be presented with the published template" + post plans_path, {template_id: liveA.id} + planA = Plan.last + assert_equal liveA, planA.template, "expected the latest published version to have been assigned to PlanA" # Template v1 is updated and gets versioned to v2 + put admin_update_template_path(@template), {template: {title: "Blah blah blah"}} + @template = Template.current(@user.org, @dmptemplate_id) # Plan B gets attached to the template v1 because v2 is not yet published - # Plan A is still attached to v1 + post plans_path, {plan: {funder_id: @user.org_id}} + assert @response.body.include?("id=\"template_id_#{liveA.id}\""), "expected the user to be presented with the published template" + post plans_path, {template_id: liveA.id} + planB = Plan.last + assert_equal liveA, planB.template, "expected the latest published version to have been assigned to PlanB" + + # Plan A should still be attached to v1 + 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"}} + @template = Template.current(@user.org, @dmptemplate_id) + liveB = Template.live(@user.org, @dmptemplate_id) # Plan C gets attached to template v2 + post plans_path, {plan: {funder_id: @user.org_id}} + assert_not @response.body.include?("id=\"template_id_#{liveA.id}\""), "expected the user to NOT be presented with the OLD published template" + assert @response.body.include?("id=\"template_id_#{liveB.id}\""), "expected the user to be presented with the published template" + post plans_path, {template_id: liveB.id} + planC = Plan.last + assert_equal liveB, planC.template, "expected the latest published version to have been assigned to PlanA" + # Plan A and B are still attached to v1 -=end + assert_equal liveA, planA.template, "expected PlanA to still be attached to the original published version" + assert_equal liveA, planB.template, "expected PlanB to still be attached to the original published version" end end diff --git a/test/unit/template_test.rb b/test/unit/template_test.rb index 220a81d..7fa53b1 100644 --- a/test/unit/template_test.rb +++ b/test/unit/template_test.rb @@ -32,6 +32,44 @@ end # --------------------------------------------------- + test "family_ids scope only returns the dmptemplate_ids for the specific Org" do + Org.all.each do |org| + family_ids = Template.where(org_id: org.id).pluck(:dmptemplate_id).uniq + scoped = Template.dmptemplate_ids(org) + assert_equal family_ids.count, scoped.count + + family_ids.each do |id| + assert scoped.include?(id), "expected the family_ids scope to contain #{id} for Org: #{org.id}" + end + scoped.each do |id| + assert family_ids.include?(id), "expected #{id} to be a valid dmptemplate_id for Org: #{org.id}" + end + end + end + + # --------------------------------------------------- + test "current scope only returns the most recent version for each dmptemplate_id" do + Org.all.each do |org| + Template.dmptemplate_ids(org).each do |dmptemplate_id| + latest = Template.where(org_id: org.id, dmptemplate_id: dmptemplate_id).order(updated_at: :desc).first + + assert_equal latest, Template.current(org, dmptemplate_id), "Expected the template.id #{latest.id} to be the current record for Org: #{org.id}, dmptemplate_id: #{dmptemplate_id}" + end + end + end + + # --------------------------------------------------- + test "published scope only returns the current published version for each dmptemplate_id" do + Org.all.each do |org| + Template.dmptemplate_ids(org).each do |dmptemplate_id| + latest = Template.where(org_id: org.id, dmptemplate_id: dmptemplate_id, published: true).order(updated_at: :desc).first + + assert_equal latest, Template.live(org, dmptemplate_id), "Expected the #{latest.nil? ? 'template to have never been published' : "template.id #{latest.id} to be the published record"} for Org: #{org.id}, dmptemplate_id: #{dmptemplate_id}" + end + end + end + + # --------------------------------------------------- test "deep copy" do verify_deep_copy(@template, ['id', 'created_at', 'updated_at']) end