diff --git a/app/models/guidance.rb b/app/models/guidance.rb index 7a22a28..0985c67 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -94,11 +94,8 @@ guidance.guidance_groups.each do |guidance_group| # guidances are viewable if they are owned by any of the user's organisations - user.organisations.each do |organisation| - - if guidance_group.organisation.id == organisation.id - viewable = true - end + if guidance_group.organisation.id == user.organisation.id + viewable = true end # guidance groups are viewable if they are owned by the Managing Curation Center @@ -133,10 +130,8 @@ funder_groups += funder.guidance_groups end # find all groups owned by any of the user's organisations - organisation_groups = [] - user.organisations.each do |organisation| - organisation_groups += organisation.guidance_groups - end + organisation_groups = user.organisation.guidance_groups + # find all guidances belonging to any of the viewable groups all_viewable_guidances = [] all_viewable_groups = managing_groups + funder_groups + organisation_groups diff --git a/app/models/phase.rb b/app/models/phase.rb index 2f3c247..a35c75b 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -37,14 +37,6 @@ end ## - # returns the most_recent version of this phase - # - # @return [Version] the most recent version of this phase - def latest_version - return versions.order("number DESC").last - end - - ## # returns either the latest published version of this phase # also serves to verify if this phase has any published versions as returns nil # if there are no published versions diff --git a/test/unit/guidance_group_test.rb b/test/unit/guidance_group_test.rb index 8363157..e3f5743 100644 --- a/test/unit/guidance_group_test.rb +++ b/test/unit/guidance_group_test.rb @@ -4,6 +4,9 @@ include GlobalHelpers setup do + Organisation.create(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + + @user = User.first @organisation = Organisation.first @guidance_group = GuidanceGroup.create(name: 'Test Guidance Group', @@ -41,47 +44,43 @@ # --------------------------------------------------- test "user can view guidance_group if it belongs to their organisation" do - usr = User.first - org = usr.organisation + org = @user.organisation gg = GuidanceGroup.create(name: 'User Test', organisation: org) - assert GuidanceGroup.can_view?(usr, gg.id) + assert GuidanceGroup.can_view?(@user, gg.id) end # --------------------------------------------------- test "user can view guidance_group if it belongs to a funder" do - usr = User.first org = Organisation.find_by(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))) gg = GuidanceGroup.create(name: 'Funder Test', organisation: org) - assert GuidanceGroup.can_view?(usr, gg.id) + assert GuidanceGroup.can_view?(@user, gg.id) end # --------------------------------------------------- test "user can view guidance_group if it belongs to the managing curation centre" do - usr = User.first org = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) gg = GuidanceGroup.create(name: 'Managing CC Test', organisation: org) - assert GuidanceGroup.can_view?(usr, gg.id) + assert GuidanceGroup.can_view?(@user, gg.id) end # --------------------------------------------------- test "user can view all oftheir organisations, funders, and the managing curation centre's guidance groups" do - usr = User.first - @organisation.users << usr + @organisation.users << @user @organisation.save @organisation.reload funding = Organisation.where(organisation_type: OrganisationType.find_by(name: GlobalHelpers.constant("organisation_types.funder"))).first - managing = Organisation.create(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + managing = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) ggs = [@guidance_group, GuidanceGroup.create(name: 'User Test', organisation: @organisation), GuidanceGroup.create(name: 'Funder Test', organisation: funding), GuidanceGroup.create(name: 'Managing CC Test', organisation: managing)] - v = GuidanceGroup.all_viewable(usr) + v = GuidanceGroup.all_viewable(@user) ggs.each do |gg| assert v.include?(gg), "expected Guidance Group: '#{gg.name}' to be viewable" diff --git a/test/unit/guidance_test.rb b/test/unit/guidance_test.rb index 2afd194..6cc488b 100644 --- a/test/unit/guidance_test.rb +++ b/test/unit/guidance_test.rb @@ -3,7 +3,15 @@ class GuidanceTest < ActiveSupport::TestCase setup do - @guidance = Guidance.new(text: 'Testing some new guidance') + Organisation.create(name: GlobalHelpers.constant("organisation_types.managing_organisation")) + + @user = User.first + + @guidance_group = GuidanceGroup.create(name: 'Tester', organisation: @user.organisation) + @guidance = Guidance.create(text: 'Testing some new guidance') + + @guidance_group.guidances << @guidance + @guidance_group.save! @question = Question.first end @@ -38,11 +46,11 @@ # --------------------------------------------------- test "correctly identifies whether the user can view the guidance" do - g = Guidance.create!(text: 'Unviewable guidance') + g = Guidance.create(text: 'Unviewable guidance') - assert_not Guidance.can_view?(@user, g), "expected guidance that is not attached to a GuidanceGroup to be unviewable" + assert_not Guidance.can_view?(@user, g.id), "expected guidance that is not attached to a GuidanceGroup to be unviewable" - managing = Organisation.find_by( name: GlobalHelpers.constant("organisation_types.managing_organisation")) + managing = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) funder = Organisation.find_by(organisation_type: OrganisationType.find_by( name: GlobalHelpers.constant("organisation_types.funder"))) assert Guidance.can_view?(@user, @guidance.id), "expected the user to be able to view guidance belonging to their organisation" @@ -62,7 +70,7 @@ assert viewable.include?(@guidance), "expected the user to be able to view guidance belonging to their organisation" - managing = Organisation.find_by( name: GlobalHelpers.constant("organisation_types.managing_organisation")) + managing = Organisation.find_by(name: GlobalHelpers.constant("organisation_types.managing_organisation")) funder = Organisation.find_by(organisation_type: OrganisationType.find_by( name: GlobalHelpers.constant("organisation_types.funder"))) GuidanceGroup.create(name: 'managing guidance group test', organisation: managing) diff --git a/test/unit/phase_test.rb b/test/unit/phase_test.rb index c55189c..2c98714 100644 --- a/test/unit/phase_test.rb +++ b/test/unit/phase_test.rb @@ -32,31 +32,17 @@ end # --------------------------------------------------- - test "latest_version returns the correct version" do - assert_equal @phase.versions.first, @phase.latest_version, "expected the version if there is only one version" - - [2..4].each do |i| - @phase.versions << Version.new(title: "Version #{i}", number: i) - end - - @phase.save! - assert_equal 4, @phase.latest_version.number, "expected the last version if there there were multiple versions" - end - - # --------------------------------------------------- test "latest_published_version returns the correct version" do assert_equal nil, @phase.latest_published_version, "expected nil if there is only one version and it was not specifically designated as published" - - @phase.published = true - @phase.save! - assert_equal @phase.versions.first, @phase.latest_published_version, "expected the version if there is only one published version" - - [2..4].each do |i| + + 4.times do |i| @phase.versions << Version.new(title: "Version #{i}", number: i, published: (i == 3 ? true : false)) end @phase.save! + @phase.reload + assert_equal 3, @phase.latest_published_version.number, "expected the last published version if there there were multiple published versions" end