Skip to content

Commit

Permalink
Merge pull request #6209 from samvera/multiple-membership-checker
Browse files Browse the repository at this point in the history
refactor MulitpleMembershipChecker specs to always use valkyrie setup
  • Loading branch information
tpendragon authored Aug 25, 2023
2 parents c7603ea + a589560 commit 168c3da
Showing 1 changed file with 89 additions and 53 deletions.
142 changes: 89 additions & 53 deletions spec/services/hyrax/multiple_membership_checker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# frozen_string_literal: true
RSpec.describe Hyrax::MultipleMembershipChecker, :clean_repo do
let(:item) { create(:work, id: 'work-1', user: user) }
let(:user) { create(:user) }
let(:item) { FactoryBot.valkyrie_create(:hyrax_work, depositor: user.user_key) }
let(:user) { FactoryBot.create(:user) }

describe '#initialize' do
subject { described_class.new(item: item) }

it 'exposes an attr_reader' do
expect(subject.item).to eq item
expect(described_class.new(item: item)).to have_attributes(item: item)
end
end

Expand All @@ -17,7 +15,7 @@
let(:checker) { described_class.new(item: item) }
let(:collection_ids) { ['foobar'] }
let(:included) { false }
let!(:collection_type) { create(:collection_type, title: 'Greedy', allow_multiple_membership: false) }
let!(:collection_type) { FactoryBot.create(:collection_type, title: 'Greedy', allow_multiple_membership: false) }
let(:collection_types) { [collection_type] }
let(:collection_type_gids) { [collection_type.to_global_id] }

Expand Down Expand Up @@ -65,23 +63,24 @@

context 'and multiple single-membership collections of the same type exist' do
let(:collection1) do
create(:collection_lw, id: 'collection1', title: ['Foo'],
collection_type: collection_type,
with_solr_document: true)
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Foo'],
collection_type_gid: collection_type.to_global_id.to_s)
end

let(:collection2) do
create(:collection_lw, id: 'collection2', title: ['Bar'],
collection_type: collection_type,
with_solr_document: true)
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Bar'],
collection_type_gid: collection_type.to_global_id.to_s)
end

before do
Hyrax.publisher.publish('object.metadata.updated',
object: item.valkyrie_resource, user: user)
object: item, user: user)
Hyrax.publisher.publish('object.metadata.updated',
object: collection1.valkyrie_resource, user: user)
object: collection1, user: user)
Hyrax.publisher.publish('object.metadata.updated',
object: collection2.valkyrie_resource, user: user)
object: collection2, user: user)
end

context 'and only one is in the list' do
Expand All @@ -105,15 +104,29 @@
end

context 'and multiple single-membership collection instances of different types exist' do
let!(:collection_type_2) { create(:collection_type, title: 'Doc', allow_multiple_membership: false) }
let(:collection1) { create(:collection_lw, title: ['Foo'], collection_type: collection_type, with_solr_document: true) }
let(:collection2) { create(:collection_lw, title: ['Bar'], collection_type: collection_type, with_solr_document: true) }
let(:collection3) { create(:collection_lw, title: ['Baz'], collection_type: collection_type_2, with_solr_document: true) }
let!(:collection_type_2) { FactoryBot.create(:collection_type, title: 'Doc', allow_multiple_membership: false) }
let(:collection1) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Foo'],
collection_type_gid: collection_type.to_global_id.to_s)
end

let(:collection2) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Bar'],
collection_type_gid: collection_type.to_global_id.to_s)
end

let(:collection3) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Baz'],
collection_type_gid: collection_type_2.to_global_id.to_s)
end

before do
Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection3.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection1, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection3, user: user)
end

context 'and collections of both types are passed in' do
Expand Down Expand Up @@ -143,9 +156,18 @@
let(:included) { true }

context 'and multiple single-membership collections of the same type exist' do
let(:collection1) { create(:collection_lw, id: 'collection1', title: ['Foo'], collection_type: collection_type, with_solr_document: true) }
let(:collection2) { create(:collection_lw, id: 'collection2', title: ['Bar'], collection_type: collection_type, with_solr_document: true) }
let(:item_2) { create(:work, id: 'work-2', user: user) }
let(:collection1) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Foo'],
collection_type_gid: collection_type.to_global_id.to_s)
end

let(:collection2) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Bar'],
collection_type_gid: collection_type.to_global_id.to_s)
end
let(:item_2) { FactoryBot.valkyrie_create(:hyrax_work, depositor: user.user_key) }

context 'and only one is in the list' do
let(:collections) { [collection1] }
Expand All @@ -154,12 +176,12 @@
context 'and the member is already in the other single membership collection' do
before do
[item, item_2].each do |work|
work.member_of_collections << collection2
work.save!
Hyrax.publisher.publish('object.metadata.updated', object: work.valkyrie_resource, user: user)
work.member_of_collection_ids << collection2.id
Hyrax.persister.save(resource: work)
Hyrax.publisher.publish('object.metadata.updated', object: work, user: user)
end
Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection1, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2, user: user)
end

it 'returns an error' do
Expand All @@ -170,14 +192,13 @@

context 'and the member is not already in the other single membership collection' do
before do
[item_2].each do |work|
work.member_of_collections << collection2
work.save!
Hyrax.publisher.publish('object.metadata.updated', object: work.valkyrie_resource, user: user)
end
Hyrax.publisher.publish('object.metadata.updated', object: item.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user)
item_2.member_of_collection_ids << collection2.id
Hyrax.persister.save(resource: item_2)
Hyrax.publisher.publish('object.metadata.updated', object: item_2, user: user)

Hyrax.publisher.publish('object.metadata.updated', object: item, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection1, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2, user: user)
end

it 'returns nil' do
Expand All @@ -189,14 +210,28 @@

context 'and multiple single-membership collection instances of different types exist' do
let!(:collection_type_2) { create(:collection_type, title: 'Doc', allow_multiple_membership: false) }
let(:collection1) { create(:collection_lw, title: ['Foo'], collection_type: collection_type, with_solr_document: true) }
let(:collection2) { create(:collection_lw, title: ['Bar'], collection_type: collection_type, with_solr_document: true) }
let(:collection3) { create(:collection_lw, title: ['Baz'], collection_type: collection_type_2, with_solr_document: true) }
let(:collection1) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Foo'],
collection_type_gid: collection_type.to_global_id.to_s)
end

let(:collection2) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Bar'],
collection_type_gid: collection_type.to_global_id.to_s)
end

let(:collection3) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: ['Baz'],
collection_type_gid: collection_type_2.to_global_id.to_s)
end

before do
Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection3.valkyrie_resource, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection1, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection2, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: collection3, user: user)
end

context 'and collections of both types are passed in' do
Expand Down Expand Up @@ -230,34 +265,35 @@
let(:collection_types) { [collection_type] }
let(:collection_type_gids) { [collection_type.to_global_id] }

subject(:validation) { checker.validate }

context 'when the item has 0 collections' do
let(:item) { FactoryBot.build(:hyrax_work) }

it 'returns true' do
expect(validation).to be true
expect(checker.validate).to be true
end
end

context 'when the item has 1 collection' do
let(:item) { FactoryBot.build(:hyrax_work, :as_collection_member) }

it 'returns true' do
expect(validation).to be true
expect(checker.validate).to be true
end
end

context 'when there are no single-membership collection types' do
before { allow(Hyrax::CollectionType).to receive(:gids_that_do_not_allow_multiple_membership).and_return([]) }

it 'returns true' do
expect(validation).to be true
expect(checker.validate).to be true
end
end

context 'when there are no single-membership collection instances' do
let(:item) { FactoryBot.build(:hyrax_work, :as_member_of_multiple_collections) }
let(:collection_ids) { item.member_of_collection_ids }
it 'returns true' do
expect(validation).to be true
expect(checker.validate).to be true
end
end

Expand All @@ -282,7 +318,7 @@
let(:collection_ids) { [col1.id, col2.id] }

it 'returns true' do
expect(validation).to be true
expect(checker.validate).to be true
end
end

Expand All @@ -304,7 +340,7 @@

it 'returns an error' do
regexp = /#{base_errmsg} \(type: Single Membership 1, collections: (Foo and Bar|Bar and Foo)\)/
expect(validation).to match regexp
expect(checker.validate).to match regexp
end
end

Expand All @@ -317,7 +353,7 @@

it 'returns an error' do
regexp = /#{base_errmsg} \(type: Single Membership 1, collections: (Foo and Bar|Bar and Foo)\)/
expect(validation).to match regexp
expect(checker.validate).to match regexp
end
end
end
Expand Down

0 comments on commit 168c3da

Please sign in to comment.